-
Notifications
You must be signed in to change notification settings - Fork 839
Implementation Plan for HybridCache ReportTagMetrics #6862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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.
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/HybridCacheEventSource.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultHybridCache.StampedeStateT.cs
Show resolved
Hide resolved
test/Libraries/Microsoft.Extensions.Caching.Hybrid.Tests/ReportTagMetricsIntegrationTests.cs
Outdated
Show resolved
Hide resolved
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.
@eerhardt Could you review this, please? |
@DeagleGross @jeffhandley @MihaZupan Could you review this, please? |
Overview
Fix #5989
The
ReportTagMetrics
feature in HybridCache enables emission of cache metrics with tag dimensions usingSystem.Diagnostics.Metrics
. This provides richer telemetry for cache performance analysis and debugging.Configuration
Enable tag metrics reporting by setting the
ReportTagMetrics
option:Metrics Emitted
When
ReportTagMetrics
is enabled, the following metrics are emitted with tag dimensions:hybrid_cache.local.hits
hybrid_cache.local.misses
hybrid_cache.distributed.hits
hybrid_cache.distributed.misses
hybrid_cache.local.writes
hybrid_cache.distributed.writes
hybrid_cache.tag.invalidations
Usage Example
Integration with Observability Systems
OpenTelemetry
The metrics integrate seamlessly with OpenTelemetry:
Application Insights
Metrics are automatically collected when using Application Insights:
Prometheus
Export metrics to Prometheus:
Backwards Compatibility
EventSource
-based metrics continue to work unchangedReportTagMetrics
defaults tofalse
, so no behavior changes unless explicitly enabledMicrosoft Reviewers: Open in CodeFlow