Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strawman Demo of EventLogging #1

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Use EventLog class for receiving & emitting events
yuvipanda committed May 21, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c480e21ffe87a133b63edaab3095aca6079a3aa7
46 changes: 34 additions & 12 deletions jupyterlab_telemetry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
""" JupyterLab LaTex : live Telemetry editing for JupyterLab """

import json
import os
from glob import glob

from tornado import gen, web
from tornado import web

from notebook.utils import url_path_join
from notebook.base.handlers import APIHandler, json_errors
from jupyterhub.events import EventLog

from ._version import __version__
here = os.path.dirname(__file__)


class TelemetryHandler(APIHandler):
class EventLoggingHandler(APIHandler):
"""
A handler that receives and stores telemetry data from the client.
"""
@property
def eventlog(self) -> EventLog:
return self.settings['eventlog']

@json_errors
@gen.coroutine
@web.authenticated
def put(self, *args, **kwargs):
# Parse the data from the request body
raw = self.request.body.strip().decode(u'utf-8')
async def put(self, *args, **kwargs):
try:
decoder = json.JSONDecoder()
session_log = decoder.decode(raw)
# Parse the data from the request body
raw_event = json.loads(self.request.body.strip().decode())
except Exception as e:
raise web.HTTPError(400, str(e))

required_fields = {'schema', 'version', 'event'}
for rf in required_fields:
if rf not in raw_event:
raise web.HTTPError(400, f'{rf} is a required field')

schema_name = raw_event['schema']
version = raw_event['version']
event = raw_event['event']
self.eventlog.emit(schema_name, version, event)

self.log.info(session_log)
self.set_status(204)
self.finish()

@@ -45,8 +59,16 @@ def load_jupyter_server_extension(nb_server_app):
nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
"""
web_app = nb_server_app.web_app

eventlog = EventLog(parent=nb_server_app)
web_app.settings['eventlog'] = eventlog
for schema_file in glob(os.path.join(here, 'event-schemas','*.json')):
with open(schema_file) as f:
eventlog.register_schema(json.load(f))

# Prepend the base_url so that it works in a jupyterhub setting
base_url = web_app.settings['base_url']
endpoint = url_path_join(base_url, 'telemetry')
handlers = [(endpoint + '(.*)', TelemetryHandler)]
web_app.add_handlers('.*$', handlers)
endpoint = url_path_join(base_url, 'eventlog')

handlers = [(endpoint + '(.*)', EventLoggingHandler)]
web_app.add_handlers('.*$', handlers)