From 9b6cac194f2a03227d392056aa439deb748e87e1 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 19 Nov 2021 20:59:55 -0800 Subject: [PATCH] Improve PrometheusExporter example --- examples/Console/TestPrometheusExporter.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/Console/TestPrometheusExporter.cs b/examples/Console/TestPrometheusExporter.cs index 3b6d2a24d9c..0d3a917b6e1 100644 --- a/examples/Console/TestPrometheusExporter.cs +++ b/examples/Console/TestPrometheusExporter.cs @@ -29,6 +29,7 @@ namespace Examples.Console; internal class TestPrometheusExporter { private static readonly Meter MyMeter = new Meter("MyMeter"); + private static readonly Meter MyMeter2 = new Meter("MyMeter2"); private static readonly Counter Counter = MyMeter.CreateCounter("myCounter", description: "A counter for demonstration purpose."); private static readonly Histogram MyHistogram = MyMeter.CreateHistogram("myHistogram"); private static readonly ThreadLocal ThreadLocalRandom = new ThreadLocal(() => new Random()); @@ -49,16 +50,26 @@ internal static object Run(int port) using var meterProvider = Sdk.CreateMeterProviderBuilder() .AddMeter(MyMeter.Name) - .AddPrometheusExporter(opt => + .AddMeter(MyMeter2.Name) + .AddPrometheusExporter(options => { - opt.StartHttpListener = true; - opt.HttpListenerPrefixes = new string[] { $"http://localhost:{port}/" }; + options.StartHttpListener = true; + options.HttpListenerPrefixes = new string[] { $"http://localhost:{port}/" }; + options.ScrapeResponseCacheDurationMilliseconds = 0; }) .Build(); var process = Process.GetCurrentProcess(); MyMeter.CreateObservableCounter("thread.cpu_time", () => GetThreadCpuTime(process), "ms"); + // If the same Instrument name+unit combination happened under different Meters, PrometheusExporter + // exporter will output duplicated metric names. Related issues and PRs: + // * https://github.com/open-telemetry/opentelemetry-specification/pull/2017 + // * https://github.com/open-telemetry/opentelemetry-specification/pull/2035 + // * https://github.com/open-telemetry/opentelemetry-dotnet/pull/2593 + // + // MyMeter2.CreateObservableCounter("thread.cpu_time", () => GetThreadCpuTime(process), "ms"); + using var token = new CancellationTokenSource(); Task.Run(() =>