Repository navigation
feat(metrics): add Compono.Metrics testing support - #179
Conversation
Add Compono.Metrics, a reflection-free System.Diagnostics.Metrics testing package. UseMetrics() composes IMeterFactory as a CapturingMeterFactory; tests read typed measurements and assert on them with GetMeasurements<T>/Verify<T>, WithValue/WithTag/WithoutTag/Matching, and Once/Never/Exactly/AtLeast/AtMost. Capture is scope-isolated per factory via one lazy MeterListener with no static state, observable instruments are polled only on explicit RecordObservableInstruments(), and disposal stays test-owned. Ships RESEARCH-0039, ADR-0072 (with dated amendments), PLAN-0074, the package, tests across net8.0-net11.0, a Native AOT smoke test, documentation/API reference, the Compono skill entry and evals, a BasicUsage sample, and CI/packaging wiring. Zero third-party dependency beyond core Compono. Refs #165, #161.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b928b18a74
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A SUT that disposes (or 'using's) the Meter it obtained from CapturingMeterFactory.Create no longer disables capture for every other holder of the cached meter. Mirrors DefaultMeterFactory: meters handed to consumers ignore consumer Dispose; only the factory releases them (on factory Dispose). Adds FactoryMeter and a regression test.
|
@codex review Targeted re-review request for the single finding from the initial review (inline comment on
Please confirm this finding is resolved and that the change introduced no regressions. No other production behavior changed in this commit. |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Adds Compono.Metrics, a source-generated, reflection-free testing package for
System.Diagnostics.Metrics, delivering the full scope of RESEARCH-0039 / Accepted ADR-0072 / PLAN-0074 (phases B0–B6). It is the companion to Compono.Logging (#164): a SUT that depends onIMeterFactorycomposes cleanly, records measurements, and lets the test assert on them with a fluent, typed API — with the same "compose every contributor, not just the object graph" philosophy as the rest of Compono.This is one combined PR (D8, revised) carrying the planning artifacts, the implementation, tests, AOT smoke test, docs, skill, sample, and CI/packaging wiring.
Feature overview
builder.UseMetrics()registers a stage-6 provider that composesIMeterFactoryas a hand-writtenCapturingMeterFactory.MeterListenerthat enables only instruments whoseMeter.Scopeis that factory. Two factories never see each other's measurements, even for identical meter/instrument names. No static or shared capture state.byte, short, int, long, float, double, decimal) across every instrument kind (Counter<T>,UpDownCounter<T>,Histogram<T>,Gauge<T>where the runtime provides it, and the observable instruments).RecordObservableInstruments()— no background polling, no wait APIs.System.Diagnostics.Metrics. Native AOT-safe (no reflection / dynamic activation).Public API
MeasurementVerificationBuilder<T>exposesWithValue,WithTag,WithoutTag,Matching, and the terminalsOnce,Never,Exactly,AtLeast,AtMost(forwarding to coreCallVerifier).CapturedMeasurement<T>is a plain readonly struct exposingValue,Tags, andInstrument(a real reference that stays valid after disposal).Usage
Composition and Generic Host integration
UseMetrics()registers a stage-6 test-double provider; because providers resolve in registration order, register it beforeUseNSubstitute()forIMeterFactory.Register<IMeterFactory>(...)still wins, per core precedence.IMeterFactory; to use the capturing factory, register it last —services.AddSingleton<IMeterFactory>(meters)— so single-service resolution returns it.TryAddSingletonwould keep the existing default. This is a documented recipe (noCompono.Metricsdependency onMicrosoft.Extensions.*), covered by host integration tests.Measurement capture and verification
InvalidOperationExceptionwith actionable messages; unsupportedTthrowsArgumentExceptionlisting the seven supported types.WithValueusesEqualityComparer<T>.Default(floating-point equality is exact; useMatchingfor tolerance);WithTagusesobject.Equalswith no numeric coercion (200int does not match200L).with/and, numbers are invariant-culture, a string tag value is quoted, other values show their runtime type (status = 200 (int)), and an unknown instrument appends the existing instruments.Thread safety and disposal
objectlock — the project multi-targets net8.0). Concurrent recording, reading, clearing, andCreateare safe; concurrentCreatereturns a single meter instance.using var _ = meters;ortry/finally); composition never disposes the factory. Snapshots remain readable after disposal; measurements recorded after disposal are not captured.Supported TFMs and Native AOT
net8.0,net9.0,net10.0,net11.0with AOT/trim analyzers enabled.Compono.Metrics.AotSmokeTestpublishes and runs under Native AOT (CI:net10.0,linux-x64), exercising composition, all instrument kinds available on the target, observable polling, tags, the type-conflict diagnostic,Verify, and direct construction.Documentation and skill updates
docs/packages/compono-metrics.md), host how-to, troubleshooting entries, API reference,BasicUsagesample scenario, and navigation/README/index updates.references/metrics.md, and eval cases.Validation evidence
dotnet build Compono.slnx -c Debug→ 0 warnings, 0 errors.dotnet test Compono.slnx -c Debug→ 7467 total, 0 failed, 6966 succeeded, 501 skipped (pre-existing platform skips).Compono.Metrics.Tests: 94 (net9.0/10.0/11.0), 93 (net8.0)..github/scripts/inspect-packed-nupkgs.shpasses (Compono.Metrics title and exact-pin dependencyCompono [1.0.0]verified).net10.0osx-arm64publish-and-run → zero IL2xxx/IL3xxx, binary PASS (exit 0).ADR / plan
In Progressuntil this PR merges).Closes #165
Refs #161 (parent tracker — not closed by this PR).