Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PrometheusExporter example #2649

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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