Pignone.OpenTelemetry.DistributedCache
8.0.4
dotnet add package Pignone.OpenTelemetry.DistributedCache --version 8.0.4
NuGet\Install-Package Pignone.OpenTelemetry.DistributedCache -Version 8.0.4
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.4" />
<PackageVersion Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.4" />
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" />
paket add Pignone.OpenTelemetry.DistributedCache --version 8.0.4
#r "nuget: Pignone.OpenTelemetry.DistributedCache, 8.0.4"
#:package Pignone.OpenTelemetry.DistributedCache@8.0.4
#addin nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.4
#tool nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.4
Pignone.OpenTelemetry.DistributedCache
NuGet package for OpenTelemetry instrumentation of implementations that use the IDistributedCache interface (for example, MemoryDistributedCache, Redis). The package decorates IDistributedCache to create spans enriched with tags and to expose metrics related to cache operations.
Index
- Overview
- Prerequisites
- Installation
- Quick start
- Advanced example — configuration and enrichment
- Best practices and recommendations
- Troubleshooting
- References
- License
- Contact
Overview
This package provides:
- Automatic instrumentation for IDistributedCache via decorating the service.
- Span generation for cache operations (Get, Set, Refresh, Remove).
- Enrichment of spans with attributes (for example, cache.key, cache.operation, cache.hit).
- Emission of metrics (counters and histogram/latency) per operation.
Compatible with .NET 8 and C# 12.
Prerequisites
- .NET 8 SDK
- Register an implementation of IDistributedCache in the IServiceCollection before adding the instrumentation (e.g.: AddDistributedMemoryCache(), AddStackExchangeRedisCache()).
Installation
Via CLI:
dotnet add package Pignone.OpenTelemetry.DistributedCache
Or via PackageReference (e.g. csproj):
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" Version="*" />
Quick start
Minimal example using MemoryDistributedCache and OpenTelemetry (Traces + Metrics):
Program.cs (minimal API example)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDistributedMemoryCache();
services.AddOpenTelemetry()
.WithTracing(tracing =>
{
tracing.AddSource("Serviço");
tracing.AddDistributedCacheInstrumentation();
tracing.AddConsoleExporter();
})
.WithMetrics(metrics =>
{
metrics.AddDistributedCacheInstrumentation();
metrics.AddConsoleExporter();
});
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
With this code:
- The tracing provider will add instrumentation for IDistributedCache.
- Spans will be created when calling GetAsync, SetAsync, RemoveAsync, etc.
- Metrics will be exposed via Meter with a name consistent with the instrumentation.
Advanced example — configuration and enrichment
Example of InstrumentationSettings (C# pseudocode showing common options):
...
services.AddOpenTelemetry()
.WithTracing(tracing =>
{
tracing.AddSource("Serviço");
tracing.AddDistributedCacheInstrumentation( option =>
{
option.Enrich = (activity, sp) =>
{
activity.SetTag("custom.tag", "custom");
};
});
tracing.AddConsoleExporter();
});
...
});
Best practices and recommendations
- Register IDistributedCache before calling AddDistributedCacheInstrumentation. Otherwise, the method will throw InvalidOperationException.
- Avoid adding sensitive keys as span attributes in production. Use MaskCacheKeys or sanitize first.
- Keep tag cardinality low (do not use user/key unique values in tags).
- Adjust trace/exporter sampling according to the cache operation volume.
- For Redis, it's recommended to add redis.index / net.peer.name attributes for better context.
Troubleshooting
Error: "The IDistributedCache service is not registered."
- Cause: you did not register an implementation of IDistributedCache.
- Solution: call services.AddDistributedMemoryCache() or services.AddStackExchangeRedisCache() before adding the instrumentation.
No spans appearing:
- Check if the TracerProvider is configured correctly and if there's an enabled exporter.
- Ensure that the instrumentation was added to the TracerProviderBuilder and that the application is using the injected IDistributedCache from DI (not direct instantiations).
Metrics not visible:
- Confirm that MeterProvider includes the instrumentation (AddDistributedCacheInstrumentation) and an exporter or scraping endpoint is configured.
References
- OpenTelemetry: https://opentelemetry.io/
- OpenTelemetry semantic conventions for databases/Redis (may be relevant): https://github.com/open-telemetry/semantic-conventions
- IDistributedCache documentation: https://learn.microsoft.com/dotnet/api/microsoft.extensions.caching.distributed.idistributedcache
License
Contact
For questions and issues, please open an issue in the package repository.
Donate
If you think has been helpful to your work, please consider making a small donation to support continued development.
It may be the equivalent of a coffee ☕, but it makes a huge difference on the other side of the screen.
| Provider | Link |
|---|---|
| Paypal | ![]() |
If you are Brazilian, you can donate through pix.
| Key | QR Code |
|---|---|
![]() |
![]() |
Your contribution helps ensure that I can continue to improve and support the extension, making it even more useful to the community.
Thank you in advance for considering supporting this project. Your generosity makes all the difference!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Options (>= 10.0.3)
- OpenTelemetry (>= 1.15.0)
- Scrutor (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v8.0.2:
- Package Changelog notes.
- Updated project license from MIT to Custom Proprietary License.
- Code cleanup and structure improvements.
- No behavioral changes.
- No API changes.
v8.0.1:
- Code cleanup and structure improvements.
- No behavioral changes.
- No API changes.
v8.0.0:
- Initial release of Pignone.OpenTelemetry.DistributedCache.


