Description
Goal
Eliminate ~7,800 LOC of mechanical boilerplate across ~90 typed-event ref structs in src/Typhon.Engine/Profiler/Events/ and src/Typhon.Engine/Profiler/TyphonEvent.cs (currently 5,214 LOC).
The duplication exists in four buckets: (a) struct field declarations, (b) Begin* factory bodies, (c) per-kind codec encode/size methods, (d) call sites. (d) has the right shape (using var scope = BeginX(...)) and is out of scope. (a)–(c) are mechanical and regular — well-suited to a header struct + Roslyn source generator.
Scope (three layered phases)
Phase 1 — TraceSpanHeader struct
Introduce a single TraceSpanHeader struct holding the seven prologue fields (ThreadSlot, StartTimestamp, SpanId, ParentSpanId, PreviousSpanId, TraceIdHi, TraceIdLo) and embed it as the first field in every event ref struct. Update Begin* factories and Dispose accordingly. Mechanical, no behavior change, no wire-format change. ~1,200 LOC saved.
Phase 2 — Roslyn source generator
Add a generator driven by a declarative kind list (or [TraceEvent] attribute on partial structs). Generator emits:
- The ref struct fields (header + payload).
- The
BeginXxx/EmitXxxmethods inTyphonEvent. - The
ITraceEventEncoder.Kind/ComputeSize/EncodeTo/Disposeplumbing.
Replaces ~6,000 LOC of hand-written boilerplate with ~500 LOC of declarations + a generator template.
Phase 3 — Codec consolidation
Consolidate the per-kind *EventCodec.Encode / ComputeSize methods into the generator, with a hand-written escape hatch for kinds with weird wire shapes (e.g. TransactionPersist's walLsn-in-componentTypeId-slot, optional-mask kinds with non-trivial encoding).
Constraints
- MUST preserve zero-cost-when-off semantics. The
if (!TelemetryConfig.ProfilerActive) return default;gate must still let the JIT dead-code-eliminate the entire factory body when the profiler is off. - MUST preserve
using var scope = BeginX(...)ergonomics at call sites. No call-site changes — the ref-struct + RAII shape is the correct abstraction for spans and is not the source of duplication. - MUST preserve wire format byte-for-byte. Consumers (exporters, viewer, profiler library) depend on the exact layout. Phase 1 is wire-neutral by construction; Phase 3 needs careful golden-file verification.
- All existing tests must continue to pass. Add round-trip tests if not already present for kinds touched in Phase 3.
Out of scope
- Call-site changes (
using var scope = ...stays). - Adding new event kinds.
- Changing the
TraceEventKindenum or wire-format byte layout.
Related Documents
- Design:
claude/design/Profiler/typhon-event-emission-refactor.md - Architecture overview — Observability
claude/design/Profiler/typhon-profiler.mdclaude/design/Profiler/07-tracing-instrumentation/— per-domain instrumentation specs
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
Solved #294
Refactor TyphonEvent emission: header + source generator + codec (#294)…