Skip to content

fix: Read event fields from a mapping as well as an object - #96

Merged
m96-chan merged 1 commit into
mainfrom
fix/92-event-field-lookup
Sep 6, 2026
Merged

fix: Read event fields from a mapping as well as an object#96
m96-chan merged 1 commit into
mainfrom
fix/92-event-field-lookup

Conversation

@m96-chan

@m96-chan m96-chan commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Summary

event_to_span, event_to_log and SpanMapper read events with getattr only, so a dict got the default for every field — 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', etw.event_id=0, 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.

Changes

  • One _event_field() helper handling a Mapping or an object, used by event_to_span, event_to_log, SpanMapper.get_span_name and SpanMapper.extract_attributes
  • _require_event() raises TypeError for something that is not an event at all, rather than returning a span of defaults
  • A partial event still falls back per-field — not being complete is different from not being an event

Related Issue

Closes #92

Test Plan

Added/Modified Tests

Six tests: dict and object agree for event_to_span and event_to_log, SpanMapper matches a dict, a dict survives the whole exporter through OtlpFileExporter, junk raises TypeError, and a partial event still gets defaults.

Test Results

The reproduction from #92, before and after:

# before
dict   name='unknown.0'          pid=[{'intValue': 0}]
object name='Microsoft-Windows-Kernel-Process.1'  pid=[{'intValue': 4104}]

# after
dict   name='Microsoft-Windows-Kernel-Process.1'  pid=[{'intValue': 4104}]
object name='Microsoft-Windows-Kernel-Process.1'  pid=[{'intValue': 4104}]
junk -> TypeError expected an ETW event or a mapping of event fields, got str
355 passed
ruff / black  clean

Checklist

  • Tests have been added/updated
  • All tests pass
  • Ready for code review
  • Documentation updated (if applicable)
  • CHANGELOG.md updated (if applicable)

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 SimpleNamespace at 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

`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
@m96-chan
m96-chan merged commit 4685082 into main Sep 6, 2026
15 checks passed
@m96-chan
m96-chan deleted the fix/92-event-field-lookup branch September 6, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] event_to_span が dict を受け取ると既定値だけの span を黙って返す

1 participant