File: MetricsUtil.cs
Web Access
Project: src\bench\Libraries\Microsoft.Extensions.Resilience.PerformanceTests\Microsoft.Extensions.Resilience.PerformanceTests.csproj (Microsoft.Extensions.Resilience.PerformanceTests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
 
namespace Microsoft.Extensions.Resilience.Bench;
 
internal sealed class MetricsUtil
{
    public static MeterListener ListenPollyMetrics()
    {
        var meterListener = new MeterListener
        {
            InstrumentPublished = (instrument, listener) =>
            {
                if (instrument.Meter.Name is "Polly")
                {
                    listener.EnableMeasurementEvents(instrument);
                }
            }
        };
 
        meterListener.SetMeasurementEventCallback<int>(OnMeasurementRecorded);
        meterListener.Start();
 
        static void OnMeasurementRecorded<T>(
            Instrument instrument,
            T measurement,
            ReadOnlySpan<KeyValuePair<string, object?>> tags,
            object? state)
        {
            // do nothing
        }
 
        return meterListener;
    }
}