Search code examples
c#.netperformance.net-8.0system.diagnostics

.Net - how to parse histogram metrics using TraceEvent?


I am collecting metrics from one .net app in another using DiagnosticsClient:

            var providers = new List<EventPipeProvider>()
            {
                new EventPipeProvider("System.Diagnostics.Metrics", EventLevel.Verbose, keywords: -1, arguments: new Dictionary<string, string>
                {
                    ["Metrics"] = "SomeApi",
                    ["Metrics"] = "MyApp",
                }),
            };

            client = new DiagnosticsClient(pid);
            session = client.StartEventPipeSession(providers, requestRundown: false);

            source = new EventPipeEventSource(session.EventStream);

            source.Dynamic.All += (TraceEvent obj) =>
            {
                if (obj.PayloadNames.Length < 7) return;

                var n = obj.PayloadValue(3) as string; 
                var v = obj.PayloadValue(6) as string; 

                //[0]: "sessionId"
                //[1]: "meterName"
                //[2]: "meterVersion"
                //[3]: "instrumentName"
                //[4]: "unit"
                //[5]: "tags"
                //[6]: "lastValue"
            };

So I figured that payload index 6 is value for a "plane" metric like a counter. But if it is a histogram or observable gauge then there is no value.

I want to print values same as dotnet-counters does: enter image description here

EDIT: my code is based on 2 sources:

https://learn.microsoft.com/en-us/dotnet/core/diagnostics/diagnostics-client-library

https://www.meziantou.net/getting-telemetry-data-from-inside-or-outside-a-dotnet-application.htm#diagnostic-event-pip


Solution

  • I had to "hack" it, for histograms the values are in "quantiles" :

    if (obj.PayloadNames.Length == 9)
        v = obj.PayloadByName("quantiles") as string;