Skip to content

Commit 31a2d67

Browse files
reyangcijothomas
andauthored
Improve PrometheusExporter example (#2649)
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
1 parent 81cc49c commit 31a2d67

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

examples/Console/TestPrometheusExporter.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Examples.Console;
2929
internal class TestPrometheusExporter
3030
{
3131
private static readonly Meter MyMeter = new Meter("MyMeter");
32+
private static readonly Meter MyMeter2 = new Meter("MyMeter2");
3233
private static readonly Counter<double> Counter = MyMeter.CreateCounter<double>("myCounter", description: "A counter for demonstration purpose.");
3334
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram");
3435
private static readonly ThreadLocal<Random> ThreadLocalRandom = new ThreadLocal<Random>(() => new Random());
@@ -49,16 +50,26 @@ internal static object Run(int port)
4950

5051
using var meterProvider = Sdk.CreateMeterProviderBuilder()
5152
.AddMeter(MyMeter.Name)
52-
.AddPrometheusExporter(opt =>
53+
.AddMeter(MyMeter2.Name)
54+
.AddPrometheusExporter(options =>
5355
{
54-
opt.StartHttpListener = true;
55-
opt.HttpListenerPrefixes = new string[] { $"http://localhost:{port}/" };
56+
options.StartHttpListener = true;
57+
options.HttpListenerPrefixes = new string[] { $"http://localhost:{port}/" };
58+
options.ScrapeResponseCacheDurationMilliseconds = 0;
5659
})
5760
.Build();
5861

5962
var process = Process.GetCurrentProcess();
6063
MyMeter.CreateObservableCounter("thread.cpu_time", () => GetThreadCpuTime(process), "ms");
6164

65+
// If the same Instrument name+unit combination happened under different Meters, PrometheusExporter
66+
// exporter will output duplicated metric names. Related issues and PRs:
67+
// * https://github.com/open-telemetry/opentelemetry-specification/pull/2017
68+
// * https://github.com/open-telemetry/opentelemetry-specification/pull/2035
69+
// * https://github.com/open-telemetry/opentelemetry-dotnet/pull/2593
70+
//
71+
// MyMeter2.CreateObservableCounter("thread.cpu_time", () => GetThreadCpuTime(process), "ms");
72+
6273
using var token = new CancellationTokenSource();
6374

6475
Task.Run(() =>

0 commit comments

Comments
 (0)