Add MetricDescriptor and descriptor-based factory APIs (PoC for #236) #239
Hi @ktoso @FranzBusch @tomerd .
The "PR label" workflow expired before anyone approved it, so it never ran. Could you approve the workflows and add the appropriate semver label (I think minor, as this adds new API)? I'm happy to rebase or push an update if that helps. Thanks!
@ManjunathAnawal thank you for working on this! This would be a great addition to a proposal. I would recommend converting this PR to a draft while you're working on the proposal similar to one in #237.
Hi @kukushechkin Proposal is up: #245 — reusing the SMT-0003 slot since #237 was
closed without merging. This PR is now in draft; the proposal links it as the
implementation reference.
This is more of an implementation review, rather that design, but still worth discussing. The current implementation compares dimensions positionally, which is order-sensitive. That's inconsistent with for example TestMetrics.FullKey, where we deliberately made dimension comparison order-insensitive and duplicate-tolerant in #244.
Could you:
1. Make MetricDescriptor.== operator sort dimensions before comparing.
2. Extract the sorting/comparison logic into one shared package helper in CoreMetrics so that both MetricDescriptor and TestMetrics use.
3. Add a test mirroring countersRepeatedDimensionNames from #244, but for MetricDescriptor.== operator.
kukushechkin
ManjunathAnawal
Motivation
Relates to #236. Swift Metrics currently passes only a label and dimensions to backends, so there is no portable way to express a Prometheus/OpenMetrics
HELPline or an OTLP instrumentdescription. Todayswift-prometheusexposes a separate registry API takinghelp:, andswift-otelworks around it via dimensions.Status: proof of concept, not a merge request
Per the review feedback on #237 — "I strongly recommend to provide a PoC with the proposed solution" — this is the PoC implementation to accompany an SMT proposal, rather than something to land ahead of that process. Opening it as a PR so the diff is reviewable and CI runs against it; happy to convert it to a draft, or close it and keep it as a branch link under the proposal's Implementation heading, whichever fits the Proposals workflow better.
Design
MetricDescriptor(Sendable,Equatable) inCoreMetrics:label,dimensions, and an optionaldescription, with an evolvable memberwise init — the "hypothetical struct" from the issue, so future fields (unit, etc.) don't require new factory methods.MetricsFactoryrequirements —makeCounter(descriptor:),makeFloatingPointCounter(descriptor:),makeMeter(descriptor:),makeRecorder(descriptor:aggregate:),makeTimer(descriptor:)— each with a default implementation that forwards label and dimensions to the existingmakeXXX(label:dimensions:)methods. Existing backends compile and behave unchanged; descriptor-aware backends override to receive the description.Counter,FloatingPointCounter,Meter,Gauge,Recorder, andTimer(including thepreferredDisplayUnitvariant). No existing signature was changed or removed.MultiplexMetricsHandlerforwards descriptors to every sub-factory;MappingMetricsFactorytransforms label/dimensions while preserving the description;TestMetricsstores the full descriptor (withlabel/dimensionsbecoming computed properties, so existing reads still work) so tests can assert on descriptions.Validation
swift test— 132 tests pass (22 new), including coverage for legacy-only factories, descriptor-aware dispatch, multiplex and mapping propagation, dimension dedupe, and the floating-point-counter fallback.swift package diagnose-api-breaking-changes— no breaking changes inCoreMetrics,Metrics, orMetricsTestKit.-warnings-as-errors -require-explicit-sendable.swift format lintclean apart from two findings that are pre-existing onmain.Sources/CoreMetrics/CMakeLists.txtupdated for thecmake-listscheck added in #238.Open questions for review
MetricDescriptorhere, versusMetricDescriptionas floated in the issue.aggregatebelongs inside the descriptor forRecorder, rather than staying a separate parameter.