-
Notifications
You must be signed in to change notification settings - Fork 348
Document event logging #1260
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
Closed
Closed
Document event logging #1260
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
445addd
Begin adding documentation about server event logging
Zsailer 59b618e
add warning about separate instances of the event logger
Zsailer f13af30
Update docs/source/developers/emitting-events.md
Zsailer 019e23c
Update docs/source/developers/emitting-events.md
Zsailer 2ff1f9a
Update docs/source/developers/emitting-events.md
Zsailer 489a9e6
Update docs/source/developers/emitting-events.md
Zsailer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Emitting events | ||
|
||
Jupyter Server comes equipped with an `event_logger` for capturing structured events from a running server. | ||
|
||
## Emit a custom event from an extension | ||
|
||
The core `EventLogger` in Jupyter Server's `ServerApp` can be used to emit events from extensions. | ||
|
||
First, define an event schema. We recommend that you make the `$id` field | ||
a valid URI and incorporate the version as part of the URI, e.g.: | ||
|
||
```yaml | ||
$id: https://my.extension.org/myextension/myevent/v1.yaml | ||
version: 1 | ||
title: My Event | ||
description: | | ||
Some information about my event | ||
type: object | ||
properties: | ||
thing: | ||
title: Thing | ||
description: A random thing. | ||
type: string | ||
required: | ||
- thing | ||
``` | ||
|
||
We also recommend that you write this schema to a file in your extension repository in a logical location, e.g. `myextension/events/myevent/v1.yaml`. | ||
|
||
Then, you can easily register your event on the core serverapp's event logger from your extension: | ||
|
||
```python | ||
import pathlib | ||
|
||
def _load_jupyter_server_extension(serverapp: ServerApp): | ||
... | ||
# Register the event schema from its local filepath. | ||
schema = pathlib.Path("myextension/events/myevent/v1.yaml") | ||
serverapp.event_logger.register_event_schema(schema) | ||
... | ||
``` | ||
|
||
Once the event schema is registered, it is ready to be emitted. Note that the core `event_logger` is included in Jupyter Server's tornado settings and listed as a property of the `JupyterHandler` API. | ||
|
||
:::{attention} | ||
While you can (technically) create your own instance of an `EventLogger` from your extension, users/operators will likely only collect events from the instance created by Jupyter Server's `ServerApp`. If you would like to provide your own instance, be sure to clearly document how to configure your extension to collect your events. | ||
::: | ||
|
||
As an example, you can emit an event from a custom request handler by calling `self.event_logger.emit(...)`: | ||
|
||
```python | ||
from jupyter_server.base.handlers import JupyterHandler | ||
|
||
class MyExtensionHandler(JupyterHandler): | ||
|
||
def get(self): | ||
... | ||
self.event_logger.emit( | ||
schema_id="https://my.extension.org/myextension/myevent/v1.yaml", | ||
data={ | ||
"thing": "`GET` method was called." | ||
} | ||
) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Event logging | ||
|
||
Jupyter Server emits the following list of events (via [`jupyter_events`](https://jupyter-events.readthedocs.io/en/latest/)) | ||
|
||
{{ jupyter_server_events }} | ||
|
||
## Collecting emitted events | ||
|
||
Configure the `EventLogger` to begin emitting events from Jupyter Server | ||
|
||
```python | ||
from logging import FileHandler | ||
|
||
event_handler = FileHandler("events.log") | ||
|
||
c.EventLogger.handlers = [event_handler] | ||
``` | ||
|
||
## Filtering events | ||
|
||
By default, all events are emitted to all handlers. If you would like to filter a specific set of | ||
events to a handler, use Python's `logging.Filter`. | ||
|
||
See the following example: | ||
|
||
```python | ||
from logging import Filter | ||
from logging import FileHandler | ||
|
||
|
||
class ContentsFilter(Filter): | ||
|
||
contents_events = [ | ||
"https://events.jupyter.org/jupyter_server/contents_service/v1" | ||
] | ||
|
||
def filter(self, record): | ||
if record.msg['__schema__'] in self.contents_events: | ||
return True | ||
return False | ||
|
||
|
||
contents_filter = ContentsFilter() | ||
event_handler = FileHandler("events.log") | ||
event_handler.addFilter(contents_filter) | ||
|
||
c.EventLogger.handlers = [event_handler] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I believe when I tried this, I found I had to use
register_handler()
, otherwise the_logger
attribute doesn't get set.Ah, is this because this is a config entry and the
EventLogger
instance does not yet exist, in which case its initializer will do the right thing? I had been trying to set the handlers list afterEventLogger
's instantiation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's exactly right. This is a config syntax. If you do this after instantiation, the handlers trait does not update.