Add support for instrument-specific metric tags in the incoming pipeline - #7912
Merged
Conversation
… and improve metric handling logic
…agConventionTests`.
…ency and refactoring metric tagging logic.
…eTime` to handle context-based tags.
ramonsmits
reviewed
Aug 21, 2026
…` with `MetricTags` extension property. Remove IncomingPipelineMetrics dependency from fakes
…y tag application and enforce consistent usage of `TagList`.
…ime` in metric tagging logic
…in `MessageContext` constructor. All calls are now GetOrCreate via extension property.
irinascurtu
approved these changes
Aug 27, 2026
…xtensionBag. Makes accessing tags requrie less nesting
ramonsmits
approved these changes
Aug 27, 2026
tmasternak
commented
Aug 27, 2026
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.
Based on https://discuss.particular.net/t/adding-custom-tags-values-to-nsb-metrics/4631
Requirements and Constraints
The PR lets a user customize the tags NServiceBus Core attaches to the meters it publishes: adding new tags, removing ones Core sets by default, and modifying values already present. Today the tag set is fixed by Core, so nothing application-specific can reach the metrics.
Constraints
Minimize public API surface — Every new public type or member is a permanent semver commitment, so the design should expose the smallest thing that delivers the capability. This is what disqualifies shipping internal state objects as API just because they happen to be reachable.
Reuse OpenTelemetry's API surface — Where OTel or
System.Diagnosticsalready defines a concept (tags, instruments,TagList), use it rather than inventing an NServiceBus-shaped equivalent. Users already know that surface, and parallel abstractions age badly.Per-instrument, from inside the pipeline — A user must be able to set different tags on different instruments, and to do it during pipeline execution where the message context is available. This is the hard requirement: a design that applies one uniform tag set everywhere, or that runs outside the pipeline with no access to the message, doesn't solve the problem.
Decisions
Public API invoked from inside the pipeline — Customization is done by calling a public API from a pipeline behavior, keyed by instrument name. Being inside the pipeline is what gives access to the message context that makes a custom tag worth setting; keying by name is what lets tags vary between instruments.
IncomingPipelineMetricTagsbecomes internal — It stays in Core but leaves the public API surface, because the concrete class carries per-instrument storage,ApplyTag/ApplyTagsthat only Core calls, andTagListplumbing. Removing it from the public API isn't a SemVer-breaking change, as it was never documented and used only inTesting.Fakes.IMetricsTagsis the public API — A single interface in theNServiceBusnamespace with one member,void AddOrOverride(string tagKey, object value, string instrumentName), implemented by the now-internal class, and returned byContextBag.MetricTags. The message-wideAdd(tagKey, value)overload is deliberately internal — only Core seeds message-wide tags (queue, discriminator, message type, handler types).internalaccessor for Core's own use — Core's recording code reaches the concrete collection through a second,internalextension propertyContextBag.IncomingMetricTagsin the same extension block. That keepsApplyTag/ApplyTagsout of the public API without forcing a cast at every call site.Rejected Solutions
Activity— ReadingActivity.Current's tags at record time and projecting them onto the meter scores well on both principles (almost no new API, pure OTel reuse), which is why it keeps resurfacing. It fails twice: the ambient activity isn't under our control, so a customer replacingActivity.Currentmakes the tags unpredictable, and it offers no way to vary tags by instrument name.