C
C#15mo ago
nocgod

❔ Adding global tags with System.Diagnostics.Metrics

Hey, I might have missed it in the documentation. I'm looking for a way to globally set some tags/labels that will be added to all metrics in my service. Motivation: I have multiple DCs and multiple environments in a single DC. I want all metrics reported with DC and ENV as a dimension. I'd prefer not to have to add those dimensions manually on each measurement, especially since the developers will probably forget to add those. as you see in the example I have
new KeyValuePair<string, object>("dc", Environment.GetEnvironmentVariable("dc") ?? "il3"),
new KeyValuePair<string, object>("env", Environment.GetEnvironmentVariable("env") ?? "local"),
new KeyValuePair<string, object>("dc", Environment.GetEnvironmentVariable("dc") ?? "il3"),
new KeyValuePair<string, object>("env", Environment.GetEnvironmentVariable("env") ?? "local"),
they are defined on the resource. I would really like to have them reported on every metric I report without me having to add them manually please checkout my code on dotnet fiddle https://dotnetfiddle.net/aIRBu0 unfortunately, you wouldn't be able to run it in .net fiddle because they don't support readkey, but you'll get the gist and be able to run if you want
example metrics console .net otlp with prom | C# Online Compiler | ...
example metrics console .net otlp with prom | Test your C# code online with .NET Fiddle code editor.
1 Reply
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.