Pignone.OpenTelemetry.DistributedCache
8.0.0
See the version list below for details.
dotnet add package Pignone.OpenTelemetry.DistributedCache --version 8.0.0
NuGet\Install-Package Pignone.OpenTelemetry.DistributedCache -Version 8.0.0
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.0" />
<PackageVersion Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.0" />
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" />
paket add Pignone.OpenTelemetry.DistributedCache --version 8.0.0
#r "nuget: Pignone.OpenTelemetry.DistributedCache, 8.0.0"
#:package Pignone.OpenTelemetry.DistributedCache@8.0.0
#addin nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.0
#tool nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.0
OpenTelemetry.Instrumentation.DistributedCache
| Status | |
|---|---|
| Stability | Beta |
| Code owners | ricardo-pignone |
NuGet package for OpenTelemetry instrumentation of implementations that use the
IDistributedCacheinterface (for example,MemoryDistributedCache,Redis). The package decoratesIDistributedCacheto 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
IDistributedCachevia 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
IDistributedCachein theIServiceCollectionbefore adding the instrumentation (e.g.:AddDistributedMemoryCache(),AddStackExchangeRedisCache()).
Installation
Via CLI:
dotnet add package OpenTelemetry.Instrumentation.DistributedCache
Or via PackageReference (e.g. csproj):
<PackageReference Include="OpenTelemetry.Instrumentation.DistributedCache" Version="*" />
Quick start
Minimal example using MemoryDistributedCache and OpenTelemetry (Traces + Metrics):
Program.cs (minimal API example)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
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
Meterwith 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
IDistributedCachebefore callingAddDistributedCacheInstrumentation. Otherwise, the method will throwInvalidOperationException. - Avoid adding sensitive keys as span attributes in production. Use
MaskCacheKeysor 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.nameattributes for better context.
Troubleshooting
Error: "The IDistributedCache service is not registered."
- Cause: you did not register an implementation of
IDistributedCache. - Solution: call
services.AddDistributedMemoryCache()orservices.AddStackExchangeRedisCache()before adding the instrumentation.
- Cause: you did not register an implementation of
No spans appearing:
- Check if the
TracerProvideris configured correctly and if there's an enabled exporter. - Ensure that the instrumentation was added to the
TracerProviderBuilderand that the application is using the injectedIDistributedCachefrom DI (not direct instantiations).
- Check if the
Metrics not visible:
- Confirm that
MeterProviderincludes the instrumentation (AddDistributedCacheInstrumentation) and an exporter or scraping endpoint is configured.
- Confirm that
References
- OpenTelemetry: https://opentelemetry.io/
- OpenTelemetry semantic conventions for databases/Redis (may be relevant): https://github.com/open-telemetry/semantic-conventions
IDistributedCachedocumentation: 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.
| 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.