djluck / prometheus-net.DotNetRuntime

Exposes .NET core runtime metrics (GC, JIT, lock contention, thread pool) using the prometheus-net package
MIT License
357 stars 85 forks source link

The given key '8' was not present in the dictionary exception #69

Closed oionut closed 2 years ago

oionut commented 2 years ago

Hi I'm facing an exception thrown by the ThreadPoolMetricsProducer:

System.Collections.Generic.KeyNotFoundException: The given key '8' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Prometheus.DotNetRuntime.Metrics.Producers.ThreadPoolMetricsProducer.<RegisterMetrics>b__29_4(ThreadPoolAdjustedEvent e)
   at Prometheus.DotNetRuntime.EventListening.DotNetEventListener.OnEventWritten(EventWrittenEventArgs eventData)

This happens on a dotnet 6 web api application using the prometheus-net.DotNetRuntime version 4.2.3. The way the collector is initialized:

...
var builder = DotNetRuntimeStatsBuilder.Default();

if (!_options.UseDefaultMetrics)
{
    builder = DotNetRuntimeStatsBuilder.Customize()
        .WithContentionStats(CaptureLevel.Informational)
        .WithGcStats(CaptureLevel.Verbose)
        .WithThreadPoolStats(CaptureLevel.Informational)
        .WithExceptionStats(CaptureLevel.Errors)
        .WithJitStats();
}

builder
    .RecycleCollectorsEvery(_options.RecycleEvery)
    .WithErrorHandler(ex => _logger.LogError(ex, "Unexpected exception occurred in prometheus-net.DotNetRuntime"));

if (_options.UseDebuggingMetrics)
{
    _logger.LogInformation("Using debugging metrics.");
    builder.WithDebuggingMetrics(true);
}

_logger.LogInformation("Starting prometheus-net.DotNetRuntime...");

builder.StartCollecting();
...
djluck commented 2 years ago

Looks like .NET 6 added a new threadpool adjustment reason- https://github.com/dotnet/runtime/blob/8f7761af3763d0b84acefbb117ed6becd83061d1/src/libraries/System.Private.CoreLib/src/System/Threading/NativeRuntimeEventSource.PortableThreadPool.cs#L70.

I

djluck commented 2 years ago

This has been fixed in version 4.2.4: https://github.com/djluck/prometheus-net.DotNetRuntime/releases/tag/4.2.4.

oionut commented 2 years ago

Thanks for the quick fix @djluck!