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

allow a 'message' field in an event schema #72

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions jupyter_events/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,21 @@ def register_handler(self, handler: logging.Handler) -> None:
All outgoing messages will be formatted as a JSON string.
"""

def _skip_message(record, **kwargs):
def _handle_message_field(record, **kwargs):
"""Python's logger always emits the "message" field with
the value as "null" unless it's present in the schema/data.
Message happens to be a common field for event logs,
so special case it here and only emit it if "message"
is found the in the schema's property list.
"""
Remove 'message' from log record.
It is always emitted with 'null', and we do not want it,
since we are always emitting events only
"""
del record["message"]
schema = self.schemas.get(record["__schema__"])
if "message" not in schema.properties:
del record["message"]
return json.dumps(record, **kwargs)

formatter = jsonlogger.JsonFormatter(json_serializer=_skip_message)
formatter = jsonlogger.JsonFormatter(
json_serializer=_handle_message_field,
)
handler.setFormatter(formatter)
self._logger.addHandler(handler)
if handler not in self.handlers:
Expand Down
4 changes: 4 additions & 0 deletions jupyter_events/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def version(self) -> int:
"""Schema's version."""
return self._schema["version"]

@property
def properties(self) -> dict:
return self._schema["properties"]
kevin-bates marked this conversation as resolved.
Show resolved Hide resolved

def validate(self, data: dict) -> None:
"""Validate an incoming instance of this event schema."""
self._validator.validate(data)
93 changes: 92 additions & 1 deletion tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_emit():
"something": {
"type": "string",
"title": "test",
},
}
},
}

Expand Down Expand Up @@ -166,6 +166,97 @@ def test_emit():
}


def test_message_field():
"""
Simple test for emitting an event with
the literal property "message".
"""
schema = {
"$id": "http://test/test",
"version": 1,
"properties": {
"something": {
"type": "string",
"title": "test",
},
"message": {
"type": "string",
"title": "test",
},
},
}

output = io.StringIO()
handler = logging.StreamHandler(output)
el = EventLogger(handlers=[handler])
el.register_event_schema(schema)

el.emit(
schema_id="http://test/test",
data={"something": "blah", "message": "a message was seen"},
)
handler.flush()

event_capsule = json.loads(output.getvalue())

assert "__timestamp__" in event_capsule
# Remove timestamp from capsule when checking equality, since it is gonna vary
del event_capsule["__timestamp__"]
assert event_capsule == {
"__schema__": "http://test/test",
"__schema_version__": 1,
"__metadata_version__": 1,
"something": "blah",
"message": "a message was seen",
}


def test_nested_message_field():
"""
Simple test for emitting an event with
the literal property "message".
"""
schema = {
"$id": "http://test/test",
"version": 1,
"properties": {
"thing": {
"type": "object",
"title": "thing",
"properties": {
"message": {
"type": "string",
"title": "message",
},
},
},
},
}

output = io.StringIO()
handler = logging.StreamHandler(output)
el = EventLogger(handlers=[handler])
el.register_event_schema(schema)

el.emit(
schema_id="http://test/test",
data={"thing": {"message": "a nested message was seen"}},
)
handler.flush()

event_capsule = json.loads(output.getvalue())

assert "__timestamp__" in event_capsule
# Remove timestamp from capsule when checking equality, since it is gonna vary
del event_capsule["__timestamp__"]
assert event_capsule == {
"__schema__": "http://test/test",
"__schema_version__": 1,
"__metadata_version__": 1,
"thing": {"message": "a nested message was seen"},
}


def test_register_event_schema(tmp_path):
"""
Register schema from a file
Expand Down