Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 1.33 KB

01.Metrics.md

File metadata and controls

46 lines (38 loc) · 1.33 KB

Metrics

We want metrics collected via Histogram<double> and we don't want to touch MyService.

How about

internal static class Telemetry {
    private static readonly _meter = new Meter("myApp", "0.0.0");
    public static Histogram<double> MyServiceMethod { get; } = _meter.CreateHistogram<double>("MyService.Method");
}

public partial class MyClass {

    [RegisterDelegate]
    [UseHistogram(nameof(Telemetry.MyServiceMethod)]
    internal static ServiceMethod RegisterMyServiceMethod(MyService myService) {
        //...
    }
}

and we generate

partial class MyClass {

    internal class Module : Autofac.Module {
        protected override void Load(ContainerBuilder builder) {
            builder.Register<MyClass.ServiceMethod>(ctx => {
                var service = ctx.Resolve<IService>();

                var method = RegisterMyServiceMethod(service);

                [DebuggerStepThrough]
                void WrappedMethod() {
                    using var _ = HistogramEntry.Record(_histogram);
                    method();
                }
                return WrappedMethod;
            });
        }
    }
}

* see /src/Bonus.SourceGen.Attributes/HistogramEntry.cs for HistogramEntry details

further readings