Snowberry.Mediator.OpenTelemetry.Shared
3.0.0
dotnet add package Snowberry.Mediator.OpenTelemetry.Shared --version 3.0.0
NuGet\Install-Package Snowberry.Mediator.OpenTelemetry.Shared -Version 3.0.0
<PackageReference Include="Snowberry.Mediator.OpenTelemetry.Shared" Version="3.0.0" />
<PackageVersion Include="Snowberry.Mediator.OpenTelemetry.Shared" Version="3.0.0" />
<PackageReference Include="Snowberry.Mediator.OpenTelemetry.Shared" />
paket add Snowberry.Mediator.OpenTelemetry.Shared --version 3.0.0
#r "nuget: Snowberry.Mediator.OpenTelemetry.Shared, 3.0.0"
#:package Snowberry.Mediator.OpenTelemetry.Shared@3.0.0
#addin nuget:?package=Snowberry.Mediator.OpenTelemetry.Shared&version=3.0.0
#tool nuget:?package=Snowberry.Mediator.OpenTelemetry.Shared&version=3.0.0
Snowberry.Mediator.OpenTelemetry.Shared
Container-agnostic OpenTelemetry instrumentation for Snowberry.Mediator. InstrumentedMediator decorates IMediator and emits an Activity plus count and duration metrics for every SendAsync, CreateStreamAsync and PublishAsync, with opt-in spans per pipeline behavior and per notification handler. It targets net11.0, net10.0 and netstandard2.0 and depends on OpenTelemetry.Api.
Applications normally reference the integration for their container, which brings this package in:
| Container | Package |
|---|---|
Microsoft.Extensions.DependencyInjection |
Snowberry.Mediator.Extensions.OpenTelemetry |
Snowberry.DependencyInjection |
Snowberry.Mediator.OpenTelemetry |
Table of Contents
- Installation
- Quick Start
- Options
- How It Works
- Emitted Telemetry
- Per-Step Spans
- Enrichment and Filtering
- Other Containers
- Pitfalls
- Related Packages
Installation
dotnet add package Snowberry.Mediator.OpenTelemetry.Shared
Exporters and the SDK (OpenTelemetry, OpenTelemetry.Exporter.*, OpenTelemetry.Extensions.Hosting) are not included; add the ones your app uses.
Quick Start
Decorate the mediator through your container's integration, then subscribe the OpenTelemetry SDK to the mediator sources with the builder extensions from this package:
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Snowberry.Mediator.OpenTelemetry; // AddSnowberryMediatorInstrumentation
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSnowberryMediatorInstrumentation() // Snowberry.Mediator + .Pipeline + .Notification sources
.AddConsoleExporter() // OpenTelemetry.Exporter.Console
.Build();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddSnowberryMediatorInstrumentation() // Snowberry.Mediator meter
.AddConsoleExporter()
.Build();
Options
MediatorTelemetryOptions (namespace Snowberry.Mediator.OpenTelemetry):
| Property | Type | Default | Purpose |
|---|---|---|---|
EnableTracing |
bool |
true |
Emit the dispatch Activity. |
EnableMetrics |
bool |
true |
Record the count and duration instruments. |
EnablePipelineBehaviorSpans |
bool |
false |
Emit one span per pipeline behavior (process-wide, see Per-Step Spans). |
EnableNotificationHandlerSpans |
bool |
false |
Emit one span per notification handler (process-wide). |
SourceName |
string |
"Snowberry.Mediator" |
Name of the dispatch ActivitySource and Meter. |
Filter |
Func<object, bool>? |
null |
Return false to skip instrumentation for a request or notification. |
EnrichWithRequest |
Action<Activity, object>? |
null |
Add tags from the request (Send, Stream). |
EnrichWithResponse |
Action<Activity, object, object>? |
null |
Add tags from the response (Send only). |
EnrichWithNotification |
Action<Activity, object>? |
null |
Add tags from the notification (Publish). |
EnrichWithException |
Action<Activity, object, Exception>? |
null |
Add tags or events for a failed dispatch. |
The options are read when the decorator is constructed.
How It Works
caller -> InstrumentedMediator ----------------------------> inner IMediator
| no tracer listening and no meter listener: (forwarded as-is)
| Filter(message) == false: (forwarded as-is)
|
+-> start Activity "Mediator.Send {RequestType}"
+-> inner.SendAsync(...)
+-> status Ok / Error, count + duration metrics, stop Activity
The instrumented path is taken when tracing is enabled and the ActivitySource has a listener, or when metrics are enabled and an instrument is being listened to. Otherwise the call is forwarded to the inner mediator directly.
Emitted Telemetry
Activities
| Source | Activity name | Tags |
|---|---|---|
Snowberry.Mediator |
Mediator.Send {RequestType} |
snowberry.mediator.request.type, snowberry.mediator.response.type, snowberry.mediator.operation = send |
Snowberry.Mediator |
Mediator.Stream {RequestType} |
snowberry.mediator.request.type, snowberry.mediator.response.type, snowberry.mediator.operation = stream |
Snowberry.Mediator |
Mediator.Publish {NotificationType} |
snowberry.mediator.notification.type, snowberry.mediator.operation = publish |
Snowberry.Mediator.Pipeline (opt-in) |
Mediator.Behavior {BehaviorType} |
snowberry.mediator.behavior.type, snowberry.mediator.request.type |
Snowberry.Mediator.Notification (opt-in) |
Mediator.Handler {HandlerType} |
snowberry.mediator.handler.type, snowberry.mediator.notification.type |
- All activities use
ActivityKind.Internal. The status isOkon success andError(with the exception message) on failure; no exception event is recorded, so add one inEnrichWithExceptionif you need it. - Type names are
Type.FullName(for exampleMyApp.Orders.PlaceOrder), so types that share a simple name across namespaces are not conflated. For closed generic types, such as open-generic behaviors closed at runtime, the full name includes assembly-qualified type arguments. - Stream activities start on the first
MoveNextAsyncand cover the whole enumeration, including time spent in the consumer.
Metrics
Meter name: SourceName (default Snowberry.Mediator).
| Instrument | Type | Unit | Tags |
|---|---|---|---|
snowberry.mediator.send.count |
Counter<long> |
type, status |
|
snowberry.mediator.send.duration |
Histogram<double> |
ms |
type, status |
snowberry.mediator.stream.count |
Counter<long> |
type, status |
|
snowberry.mediator.stream.duration |
Histogram<double> |
ms |
type, status |
snowberry.mediator.publish.count |
Counter<long> |
type, status |
|
snowberry.mediator.publish.duration |
Histogram<double> |
ms |
type, status |
status is success or failure. A stream the consumer stops enumerating early counts as failure.
Instrumentation scope
Every ActivitySource and the Meter carry the package version as their version, exported as the OpenTelemetry instrumentation scope version. It is available as MediatorTelemetryConventions.c_InstrumentationVersion. All names and tag keys are public constants on MediatorTelemetryConventions (ActivityNames, Tags, Operations, Status, Instruments, HookNames).
Per-Step Spans
options.EnablePipelineBehaviorSpans = true; // Mediator.Behavior spans, request and stream pipelines
options.EnableNotificationHandlerSpans = true; // Mediator.Handler spans
- The flags are process-wide and cannot be turned off again: enabling them calls
MediatorDiagnostics.EnablePipelineSpans()/EnableNotificationSpans()in the core package when the decorator is registered. You can call those methods directly to get per-step spans without the decorator. - Per-step spans come from the fixed
Snowberry.Mediator.PipelineandSnowberry.Mediator.Notificationsources and ignoreEnableTracingandFilter. AddSnowberryMediatorInstrumentation()on the tracer builder subscribes both sources.
Enrichment and Filtering
- Enrichment callbacks never break a dispatch. An exception thrown from an
EnrichWith*callback is caught and recorded as anActivityevent namedsnowberry.mediator.enrichment.failedwith the tagsexception.type,exception.messageandsnowberry.mediator.hook.name. - Callbacks run only when an
Activityexists, so not in metrics-only mode and not when the span is sampled out. EnrichWithExceptionruns for failedSendAsync,PublishAsyncandCreateStreamAsynccalls, including a stream whose inner mediator fails before enumeration starts (for exampleHandlerNotFoundException). It does not run when a consumer merely stops enumerating early; the span is still markedError.Filteris not guarded. It runs before anyActivityexists, so an exception fromFilterpropagates out of the dispatch call. Keep it total (non-throwing).
Other Containers
The building blocks are public, so any container can be wired by hand:
var options = new MediatorTelemetryOptions();
var instrumentation = new MediatorInstrumentation(options); // owns ActivitySource, Meter, instruments; IDisposable
IMediator instrumented = new InstrumentedMediator(innerMediator, instrumentation, options);
MediatorDiagnostics.EnablePipelineSpans(); // per-step spans are switched on separately
Register MediatorInstrumentation as a singleton and dispose it with the container. The EnablePipelineBehaviorSpans / EnableNotificationHandlerSpans options only take effect through the DI integrations; call MediatorDiagnostics directly when wiring by hand.
Pitfalls
- A custom
SourceNamemust be passed to the builders. UseAddSnowberryMediatorInstrumentation(sourceName)on the tracer builder andAddSnowberryMediatorInstrumentation(meterName)on the meter builder; the parameterless overloads subscribe toSnowberry.Mediatorand would miss everything. - A meter listener alone activates the instrumented path. Subscribing a
MeterProvidermakes every dispatch go through the decorator even without tracing; setEnableMetrics = falseif you only want spans. - Per-step span flags are one-way. Once enabled they stay on for the process, including other mediator instances.
- Keep
Filternon-throwing. Its exceptions reach the caller.
Related Packages
| Package | Purpose |
|---|---|
Snowberry.Mediator |
The mediator implementation and MediatorDiagnostics. |
Snowberry.Mediator.Extensions.OpenTelemetry |
Decorator wiring for Microsoft.Extensions.DependencyInjection. |
Snowberry.Mediator.OpenTelemetry |
Decorator wiring for Snowberry.DependencyInjection. |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- OpenTelemetry.Api (>= 1.15.3)
- Snowberry.Mediator (>= 3.0.0)
- System.Diagnostics.DiagnosticSource (>= 10.0.6)
-
net10.0
- OpenTelemetry.Api (>= 1.15.3)
- Snowberry.Mediator (>= 3.0.0)
-
net11.0
- OpenTelemetry.Api (>= 1.15.3)
- Snowberry.Mediator (>= 3.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Snowberry.Mediator.OpenTelemetry.Shared:
| Package | Downloads |
|---|---|
|
Snowberry.Mediator.Extensions.OpenTelemetry
Microsoft.Extensions.DependencyInjection integration for Snowberry.Mediator OpenTelemetry. |
|
|
Snowberry.Mediator.OpenTelemetry
Snowberry.DependencyInjection integration for Snowberry.Mediator OpenTelemetry. |
GitHub repositories
This package is not used by any popular GitHub repositories.