Skip to content

Commit

Permalink
Allow ?no_track_activity=1 to opt-out of activity tracking (#4235)
Browse files Browse the repository at this point in the history
* Don't track API requests with `?no_track_activity=1` in the activity counter

allows external idle-culling scripts to avoid updating the activity counter

* Don't track kernel shutdown as kernel activity

this causes idle-kernel shutdowns to restart the idle-shutdown timer

user-requested shutdowns will still be tracked as api activity

* test ?no_track_activity=1 tracking

* Changelog for activity
  • Loading branch information
minrk authored and takluyver committed Jun 25, 2019
1 parent f68e34f commit efc0f00
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
10 changes: 10 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ We strongly recommend that you upgrade pip to version 9+ of pip before upgrading
Use ``pip install pip --upgrade`` to upgrade pip. Check pip version with
``pip --version``.

.. _release-6.0.0:

6.0.0
-----

- add ``?no_track_activity=1`` argument to allow API requests
to not be registered as activity (e.g. API calls by external activity monitors).
- Kernels shutting down due to an idle timeout is no longer considered
an activity-updating event.

.. _release-5.7.8:

5.7.8
Expand Down
6 changes: 5 additions & 1 deletion notebook/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,11 @@ def content_security_policy(self):
def update_api_activity(self):
"""Update last_activity of API requests"""
# record activity of authenticated requests
if self._track_activity and getattr(self, '_user_cache', None):
if (
self._track_activity
and getattr(self, '_user_cache', None)
and self.get_argument('no_track_activity', None) is None
):
self.settings['api_last_activity'] = utcnow()

def finish(self, *args, **kwargs):
Expand Down
29 changes: 22 additions & 7 deletions notebook/services/api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
"""Test the basic /api endpoints"""

import requests
from datetime import timedelta

from notebook._tz import isoformat
from notebook._tz import isoformat, utcnow
from notebook.utils import url_path_join
from notebook.tests.launchnotebook import NotebookTestBase


class KernelAPITest(NotebookTestBase):
class APITest(NotebookTestBase):
"""Test the kernels web service API"""

def _req(self, verb, path, **kwargs):
r = self.request(verb, url_path_join('api', path))
r.raise_for_status()
return r

def get(self, path, **kwargs):
return self._req('GET', path)

def test_get_spec(self):
r = self.get('spec.yaml')
assert r.text

def test_get_status(self):
r = self.get('status')
data = r.json()
Expand All @@ -30,3 +30,18 @@ def test_get_status(self):
assert data['last_activity'].endswith('Z')
assert data['started'].endswith('Z')
assert data['started'] == isoformat(self.notebook.web_app.settings['started'])

def test_no_track_activity(self):
# initialize with old last api activity
old = utcnow() - timedelta(days=1)
settings = self.notebook.web_app.settings
settings['api_last_activity'] = old
# accessing status doesn't update activity
self.get('status')
assert settings['api_last_activity'] == old
# accessing with ?no_track_activity doesn't update activity
self.get('contents?no_track_activity=1')
assert settings['api_last_activity'] == old
# accessing without ?no_track_activity does update activity
self.get('contents')
assert settings['api_last_activity'] > old
1 change: 0 additions & 1 deletion notebook/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ def shutdown_kernel(self, kernel_id, now=False):
kernel._activity_stream = None
self.stop_buffering(kernel_id)
self._kernel_connections.pop(kernel_id, None)
self.last_kernel_activity = utcnow()

# Decrease the metric of number of kernels
# running for the relevant kernel type by 1
Expand Down

0 comments on commit efc0f00

Please sign in to comment.