prometheus net - How to custom to set a metrics lifetime instead of global setting - Stack Overflow

admin2025-04-26  7

We have customed some metrics in .NET6, from MeterAdapterOptions.MetricsExpireAfter we can see the metrics default expire time is 5 minutes. But the metrics update interval is 1 hour, so the metrics will always be expired and not update correctly.

So I want to set this metrics lifetime to forever, here is the custom metrics:

public ServiceMetrics(IMeterFactory factory)
{
    var meter = factory.Create("custom");
    Success_Count = meter.CreateCounter<int>(SuccessJobName, null, SuccessJobDescription);
    Failed_Count = meter.CreateCounter<int>(FailedJobName, null, FailedJobDescription);

    InBlockedCount = meter.CreateObservableGauge(InBlockedJobName, () => new Measurement<int>(JobName, new KeyValuePair<string, object?>(JobName, jobName)));
}

Now the exsiting solution is setting following in Program.cs. but it is global setting and will affect all metrics, we just want to set the above metrics be never expired.

Metrics.ConfigureMeterAdapter(o => o.MetricsExpireAfter = TimeSpan.FromDays(1));
转载请注明原文地址:http://anycun.com/QandA/1745600337a91013.html