Skip to content

Commit

Permalink
allow a 'message' field in an event schema (#72)
Browse files Browse the repository at this point in the history
* allow a 'message' field in an event schema
* add unit test for nested message field; update private docstring
  • Loading branch information
Zsailer authored Apr 11, 2023
1 parent 00d2062 commit ebbebf8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
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"]

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

0 comments on commit ebbebf8

Please # to comment.