From 31a2d673dad40ef31586252be06aebf3015d1e1f Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Sat, 20 Nov 2021 09:10:03 -0800 Subject: [PATCH] Improve PrometheusExporter example (#2649) Co-authored-by: Cijo Thomas --- 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 3b6d2a24d..0d3a917b6 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 class TestPrometheusExporter 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(() =>