Pignone.OpenTelemetry.DistributedCache 8.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pignone.OpenTelemetry.DistributedCache --version 8.0.3
                    
NuGet\Install-Package Pignone.OpenTelemetry.DistributedCache -Version 8.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pignone.OpenTelemetry.DistributedCache" Version="8.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Pignone.OpenTelemetry.DistributedCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Pignone.OpenTelemetry.DistributedCache --version 8.0.3
                    
#r "nuget: Pignone.OpenTelemetry.DistributedCache, 8.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Pignone.OpenTelemetry.DistributedCache@8.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Pignone.OpenTelemetry.DistributedCache&version=8.0.3
                    
Install as a Cake Tool

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

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

License

MIT

Contact

For questions and issues, please open an issue in the package repository.

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 alt text

If you are Brazilian, you can donate through pix.

Key QR Code
alt text alt text

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.0.4 90 3/1/2026
8.0.3 86 3/1/2026
8.0.1 89 2/17/2026
8.0.0 90 2/17/2026

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.