fix: Read event fields from a mapping as well as an object - #96
Merged
Conversation
`event_to_span`, `event_to_log` and `SpanMapper` read events with `getattr`
only, so a `dict` got the default for every field. It produced a span named
"unknown.0" with no provider and no PID -- no error, no warning, just wrong:
event_to_span({"provider_name": "Microsoft-Windows-Kernel-Process",
"event_id": 1, "process_id": 4104})
# name='unknown.0', etw.provider='unknown', process.pid=0
`pyetwkit.export` has accepted both shapes all along, via `_event_to_dict`, so
the same dict exported to JSON perfectly well while OTLP silently emptied it.
Read fields through one helper that handles a Mapping or an object, and reject
anything that is neither with a TypeError rather than returning a span of
defaults. A partial event is still fine: absent fields fall back, because that
is a different thing from not being an event.
Found while implementing #91: the tests in this repo passed dicts, so they had
been asserting against spans of default values without anyone noticing. Those
were switched to SimpleNamespace at the time; this fixes the library instead.
Closes #92
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HhPcm483PrDmuQEUSALBFn
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
event_to_span,event_to_logandSpanMapperread events withgetattronly, so adictgot the default for every field — a span namedunknown.0with no provider and no PID. No error, no warning, just wrong.pyetwkit.exporthas accepted both shapes all along via_event_to_dict, so the same dict exported to JSON perfectly well while OTLP silently emptied it.Changes
_event_field()helper handling aMappingor an object, used byevent_to_span,event_to_log,SpanMapper.get_span_nameandSpanMapper.extract_attributes_require_event()raisesTypeErrorfor something that is not an event at all, rather than returning a span of defaultsRelated Issue
Closes #92
Test Plan
Added/Modified Tests
Six tests: dict and object agree for
event_to_spanandevent_to_log,SpanMappermatches a dict, a dict survives the whole exporter throughOtlpFileExporter, junk raisesTypeError, and a partial event still gets defaults.Test Results
The reproduction from #92, before and after:
Checklist
Additional Notes
How this surfaced is worth recording: while implementing #91 I noticed the OTLP tests in this repo were passing dicts, which meant they had been asserting against spans of default values. I switched those tests to
SimpleNamespaceat the time so they exercised the real event shape; this fixes the library so the dict shape works too, rather than only avoiding it in tests.Goes with #95 (#93). Both are independent of each other.
🤖 Generated with Claude Code
https://claude.ai/code/session_01HhPcm483PrDmuQEUSALBFn