Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory?

Meter meter = meterFactory?.Create(new MeterOptions("Microsoft.Extensions.Caching.Memory.MemoryCache")
{
Tags = [new("cache.name", _options.Name)]
Tags = [new("dotnet.cache.name", _options.Name)]
}) ?? SharedMeter.Instance;
InitializeMetrics(meter);
}
Expand Down Expand Up @@ -883,9 +883,9 @@ private void InitializeMetrics(Meter meter)
// Use a weak reference for `this` to avoid keeping it alive indefinitely
// due to it being captured in the instrument's lambda and hence kept alive by the Meter.
WeakReference<MemoryCache> weakThis = new(this);
KeyValuePair<string, object?> cacheNameTag = new("cache.name", _options.Name);
KeyValuePair<string, object?> cacheNameTag = new("dotnet.cache.name", _options.Name);

meter.CreateObservableCounter("cache.requests",
meter.CreateObservableCounter("dotnet.cache.requests",
() =>
{
if (!weakThis.TryGetTarget(out MemoryCache? cache) || cache._disposed)
Expand All @@ -898,14 +898,14 @@ private void InitializeMetrics(Meter meter)
? []
: new Measurement<long>[]
{
new(stats.TotalHits, cacheNameTag, new("cache.request.type", "hit")),
new(stats.TotalMisses, cacheNameTag, new("cache.request.type", "miss")),
new(stats.TotalHits, cacheNameTag, new("dotnet.cache.request.type", "hit")),
new(stats.TotalMisses, cacheNameTag, new("dotnet.cache.request.type", "miss")),
};
},
unit: "{requests}",
unit: "{request}",
description: "Total cache requests.");

meter.CreateObservableCounter<long>("cache.evictions",
meter.CreateObservableCounter<long>("dotnet.cache.evictions",
() =>
{
if (!weakThis.TryGetTarget(out MemoryCache? cache) || cache._disposed)
Expand All @@ -918,10 +918,10 @@ private void InitializeMetrics(Meter meter)
? []
: [new Measurement<long>(stats.TotalEvictions, cacheNameTag)];
},
unit: "{evictions}",
unit: "{eviction}",
description: "Total cache evictions.");

meter.CreateObservableUpDownCounter<long>("cache.entries",
meter.CreateObservableUpDownCounter<long>("dotnet.cache.entries",
() =>
{
if (!weakThis.TryGetTarget(out MemoryCache? cache) || cache._disposed)
Expand All @@ -934,10 +934,10 @@ private void InitializeMetrics(Meter meter)
? []
: [new Measurement<long>(stats.CurrentEntryCount, cacheNameTag)];
},
unit: "{entries}",
unit: "{entry}",
description: "Current number of cache entries.");

meter.CreateObservableGauge<long>("cache.estimated_size",
meter.CreateObservableGauge<long>("dotnet.cache.estimated_size",
() =>
{
if (!weakThis.TryGetTarget(out MemoryCache? cache) || cache._disposed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public void Constructor_WithMeterFactory_CreatesInstruments()
meterListener.RecordObservableInstruments();

Assert.Contains(measurements, m =>
m.name == "cache.requests" &&
m.tags.Any(t => t.Key == "cache.request.type" && (string?)t.Value == "hit") &&
m.tags.Any(t => t.Key == "cache.name" && (string?)t.Value == "test-cache"));
m.name == "dotnet.cache.requests" &&
m.tags.Any(t => t.Key == "dotnet.cache.request.type" && (string?)t.Value == "hit") &&
m.tags.Any(t => t.Key == "dotnet.cache.name" && (string?)t.Value == "test-cache"));

Assert.Contains(measurements, m =>
m.name == "cache.requests" &&
m.tags.Any(t => t.Key == "cache.request.type" && (string?)t.Value == "miss") &&
m.tags.Any(t => t.Key == "cache.name" && (string?)t.Value == "test-cache"));
m.name == "dotnet.cache.requests" &&
m.tags.Any(t => t.Key == "dotnet.cache.request.type" && (string?)t.Value == "miss") &&
m.tags.Any(t => t.Key == "dotnet.cache.name" && (string?)t.Value == "test-cache"));

Assert.Contains(measurements, m => m.name == "cache.entries");
Assert.Contains(measurements, m => m.name == "dotnet.cache.entries");
}

[Fact]
Expand Down Expand Up @@ -102,9 +102,9 @@ public void Constructor_WithNullMeterFactory_UsesSharedMeter()

meterListener.RecordObservableInstruments();

Assert.Contains(measurements, m => m.name == "cache.requests");
Assert.Contains(measurements, m => m.name == "cache.evictions");
Assert.Contains(measurements, m => m.name == "cache.entries");
Assert.Contains(measurements, m => m.name == "dotnet.cache.requests");
Assert.Contains(measurements, m => m.name == "dotnet.cache.evictions");
Assert.Contains(measurements, m => m.name == "dotnet.cache.entries");
}

[Fact]
Expand Down Expand Up @@ -155,7 +155,7 @@ public void EvictionInstrument_ReflectsCompaction()
meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == "Microsoft.Extensions.Caching.Memory.MemoryCache" &&
instrument.Name == "cache.evictions")
instrument.Name == "dotnet.cache.evictions")
{
listener.EnableMeasurementEvents(instrument);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public void MeterOptions_IncludesCacheNameTag()
Assert.Single(meterFactory.Meters);
Meter meter = meterFactory.Meters[0];
Assert.Equal("Microsoft.Extensions.Caching.Memory.MemoryCache", meter.Name);
Assert.Contains(meter.Tags, t => t.Key == "cache.name" && (string?)t.Value == "my-cache");
Assert.Contains(meter.Tags, t => t.Key == "dotnet.cache.name" && (string?)t.Value == "my-cache");
}

[Fact]
Expand All @@ -251,7 +251,7 @@ public void EvictionCount_NotOvercounted_WhenEntryAlreadyRemoved()
meterListener.InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == "Microsoft.Extensions.Caching.Memory.MemoryCache" &&
instrument.Name == "cache.evictions")
instrument.Name == "dotnet.cache.evictions")
{
listener.EnableMeasurementEvents(instrument);
}
Expand Down Expand Up @@ -322,4 +322,4 @@ public void Dispose()
}
}
}
#endif
#endif
Loading