Skip to content

Instantly share code, notes, and snippets.

@guilhem
Created August 14, 2020 17:03
Show Gist options
  • Save guilhem/da9c38d74ad1f9f28705b3df2e7b047d to your computer and use it in GitHub Desktop.
Save guilhem/da9c38d74ad1f9f28705b3df2e7b047d to your computer and use it in GitHub Desktop.
Jupyter notebook log kernel execution and user based on http proxy header
diff --git a/notebook/base/handlers.py b/notebook/base/handlers.py
index 743f7bac7..6d8f478cf 100755
--- a/notebook/base/handlers.py
+++ b/notebook/base/handlers.py
@@ -126,6 +126,8 @@ class AuthenticatedHandler(web.RequestHandler):
self.force_clear_cookie(self.cookie_name)
def get_current_user(self):
+ if "X-AUTH-USER" in self.request.headers:
+ return self.request.headers["X-AUTH-USER"]
if self.login_handler is None:
return 'anonymous'
return self.login_handler.get_user(self)
diff --git a/notebook/services/kernels/handlers.py b/notebook/services/kernels/handlers.py
index bca99ce1b..887044d59 100644
--- a/notebook/services/kernels/handlers.py
+++ b/notebook/services/kernels/handlers.py
@@ -327,6 +327,9 @@ class ZMQChannelsHandler(AuthenticatedZMQStreamHandler):
channel = getattr(stream, 'channel', None)
msg_type = msg['header']['msg_type']
+ if msg_type == 'execute_input':
+ self.log.critical("TRAK: %s", json.dumps({'who': self.current_user, 'what': msg['content']['code']}))
+
if channel == 'iopub' and msg_type == 'status' and msg['content'].get('execution_state') == 'idle':
# reset rate limit counter on status=idle,
# to avoid 'Run All' hitting limits prematurely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment