Skip to content

Conversation

mokarchi
Copy link

@mokarchi mokarchi commented Sep 28, 2025

Overview

Fix #5989
The ReportTagMetrics feature in HybridCache enables emission of cache metrics with tag dimensions using System.Diagnostics.Metrics. This provides richer telemetry for cache performance analysis and debugging.

Configuration

Enable tag metrics reporting by setting the ReportTagMetrics option:

services.AddHybridCache(options =>
{
    options.ReportTagMetrics = true;
});

Metrics Emitted

When ReportTagMetrics is enabled, the following metrics are emitted with tag dimensions:

Metric Name Description Type
hybrid_cache.local.hits Local cache hits Counter
hybrid_cache.local.misses Local cache misses Counter
hybrid_cache.distributed.hits Distributed cache hits Counter
hybrid_cache.distributed.misses Distributed cache misses Counter
hybrid_cache.local.writes Local cache writes Counter
hybrid_cache.distributed.writes Distributed cache writes Counter
hybrid_cache.tag.invalidations Tag invalidations Counter

Usage Example

// Cache operations with tags
await cache.GetOrCreateAsync("user-profile", userId, 
    async (userId, token) => await GetUserProfileAsync(userId, token),
    tags: new[] { "region:us-west", "service:user-api", "environment:prod" });

// Tags will appear as metric dimensions when ReportTagMetrics = true

Integration with Observability Systems

OpenTelemetry

The metrics integrate seamlessly with OpenTelemetry:

builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics =>
    {
        metrics.AddMeter("Microsoft.Extensions.Caching.Hybrid");
    });

Application Insights

Metrics are automatically collected when using Application Insights:

builder.Services.AddApplicationInsightsTelemetry();

Prometheus

Export metrics to Prometheus:

builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics =>
    {
        metrics.AddMeter("Microsoft.Extensions.Caching.Hybrid")
               .AddPrometheusExporter();
    });

Backwards Compatibility

  • The existing EventSource-based metrics continue to work unchanged
  • ReportTagMetrics defaults to false, so no behavior changes unless explicitly enabled
  • All existing telemetry consumers will continue to receive the same events
Microsoft Reviewers: Open in CodeFlow

Enhanced the HybridCache system with support for tag-aware
metrics, controlled via the `ReportTagMetrics` option. This
enables emitting detailed telemetry for cache operations,
including tag dimensions, using `System.Diagnostics.Metrics`.

- Added counters for cache hits, misses, writes, and tag
  invalidations with optional tag dimensions.
- Updated `HybridCacheOptions` with detailed documentation
  on `ReportTagMetrics`, including security and PII guidance.
- Introduced methods in `HybridCacheEventSource` for emitting
  tag-aware metrics conditionally.
- Ensured high-cardinality tags are avoided to prevent
  performance issues in metrics systems.
- Added integration and unit tests to validate behavior with
  and without tags.
- Maintained backward compatibility by ensuring metrics are
  emitted without tag dimensions when `ReportTagMetrics` is
  disabled.
- Improved observability by allowing metrics categorization
  by tags like "region", "service", and "environment".

This update provides developers with a powerful tool for
analyzing cache performance by tag categories, enabling
better insights and decision-making.
@mokarchi mokarchi requested a review from a team as a code owner September 28, 2025 15:46
@mokarchi mokarchi requested review from Copilot and removed request for a team September 28, 2025 15:46
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the ReportTagMetrics feature for HybridCache that enables emission of cache metrics with tag dimensions using System.Diagnostics.Metrics. The implementation allows for richer telemetry for cache performance analysis and debugging while maintaining backward compatibility.

Key changes include:

  • Addition of System.Diagnostics.Metrics instruments with tag-aware capabilities to HybridCacheEventSource
  • Integration of the ReportTagMetrics option throughout the cache implementation to conditionally emit tagged metrics
  • Comprehensive test coverage for both the new functionality and backward compatibility

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
HybridCacheEventSource.cs Adds System.Diagnostics.Metrics instruments and methods for emitting tagged metrics
HybridCacheOptions.cs Enhances documentation for ReportTagMetrics with security and PII guidance
DefaultHybridCache.cs Integrates tagged metric reporting in cache hit/miss scenarios
DefaultHybridCache.TagInvalidation.cs Adds tagged metrics for tag invalidation operations
DefaultHybridCache.StampedeStateT.cs Integrates tagged metrics for distributed cache operations
DefaultHybridCache.L2.cs Adds tagged metrics for local cache write operations
ReportTagMetricsTests.cs Unit tests for the new tagged metrics functionality
ReportTagMetricsIntegrationTests.cs Integration tests covering end-to-end tagged metrics scenarios
HybridCacheEventSourceTests.cs Tests for EventSource methods with tag support
Microsoft.Extensions.Caching.Hybrid.Tests.csproj Adds reference to diagnostics testing library

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Updated the `ReportTagMetricsIntegrationTests` class to implement the `IDisposable` interface for proper resource cleanup. Introduced a private `_serviceProvider` field to store the `ServiceProvider` instance, enabling its disposal in the new `Dispose` method. Adjusted the constructor to use `_serviceProvider` for creating and retrieving the `HybridCache` instance.
Updated XML documentation in `HybridCacheOptions.cs` for clarity and consistency. Enhanced `LocalCacheHitWithTags` in `HybridCacheEventSource.cs` by adding braces for readability and refining metrics emission logic to handle empty tag sets. Fixed a missing `return` statement in `CreateTagList` to ensure proper functionality. These changes improve code readability, maintainability, and correctness.
Refactored methods to add braces around single-line `if`
statements for improved readability and maintainability.
Enhanced metric emission logic in methods like
`LocalCacheHitWithTags`, `DistributedCacheMissWithTags`,
and others to handle `reportTagMetrics` more robustly.

Introduced static helper methods for emitting metrics
(`EmitLocalCacheHitMetrics`, `EmitDistributedCacheMissMetrics`,
etc.) to centralize logic, reduce duplication, and improve
maintainability. Ensured consistent structure across all
metric-emitting methods.
Updated `CreateTagList` to initialize `tagList` with `default`
instead of `new`, potentially improving performance. Added
curly braces to the `for` loop in the `default` case for better
readability and maintainability.
The `ReportTagMetricsIntegrationTests` class was made `sealed` to prevent inheritance, enhancing clarity and potential performance.

A `_disposed` field was added to implement the `Dispose` pattern more robustly, ensuring idempotency and avoiding multiple disposals of `_serviceProvider`. The `Dispose` method was updated to check `_disposed` before executing disposal logic.

Minor formatting adjustments were made to comments in `CacheOperations_WithoutTags_EmitsMetricsWithoutDimensions` for consistency.

In `EventSource_EmptyTags_ReportTagMetrics`, the assertion for empty tags was updated to use `Assert.Empty` for improved readability.
@mokarchi mokarchi changed the title Implementation Plan for HybridCache ReportTagMetrics (Fix #5989) Implementation Plan for HybridCache ReportTagMetrics (Fix https://github.com/dotnet/extensions/issues/5989) Sep 29, 2025
@mokarchi mokarchi changed the title Implementation Plan for HybridCache ReportTagMetrics (Fix https://github.com/dotnet/extensions/issues/5989) Implementation Plan for HybridCache ReportTagMetrics Sep 29, 2025
@mokarchi
Copy link
Author

mokarchi commented Oct 1, 2025

@eerhardt Could you review this, please?

@mokarchi
Copy link
Author

mokarchi commented Oct 6, 2025

@eerhardt Could you review this, please?

@DeagleGross @jeffhandley @MihaZupan Could you review this, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HybridCache: implement ReportTagMetrics

1 participant