Skip to content

Commit

Permalink
Improve PrometheusExporter example (#2649)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
reyang and cijothomas authored Nov 20, 2021
1 parent 81cc49c commit 31a2d67
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions examples/Console/TestPrometheusExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> Counter = MyMeter.CreateCounter<double>("myCounter", description: "A counter for demonstration purpose.");
private static readonly Histogram<long> MyHistogram = MyMeter.CreateHistogram<long>("myHistogram");
private static readonly ThreadLocal<Random> ThreadLocalRandom = new ThreadLocal<Random>(() => new Random());
Expand All @@ -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(() =>
Expand Down

0 comments on commit 31a2d67

Please sign in to comment.