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

Add test meter to stress app with meter and instrument tags #5046

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion playground/Stress/Stress.ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@
builder.AddServiceDefaults();

builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing.AddSource(TraceCreator.ActivitySourceName, ProducerConsumer.ActivitySourceName));
.WithTracing(tracing => tracing.AddSource(TraceCreator.ActivitySourceName, ProducerConsumer.ActivitySourceName))
.WithMetrics(metrics => metrics.AddMeter(TestMetrics.MeterName));
builder.Services.AddSingleton<TestMetrics>();

var app = builder.Build();

app.Lifetime.ApplicationStarted.Register(ConsoleStresser.Stress);

app.MapGet("/", () => "Hello world");

app.MapGet("/increment-counter", (TestMetrics metrics) =>
{
metrics.IncrementCounter(1, new TagList([new KeyValuePair<string, object?>("add-tag", "1")]));

return "Big trace created";
});

app.MapGet("/big-trace", async () =>
{
var bigTraceCreator = new TraceCreator();
Expand Down
38 changes: 38 additions & 0 deletions playground/Stress/Stress.ApiService/TestMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.Metrics;

namespace Stress.ApiService;

public class TestMetrics : IDisposable
{
public const string MeterName = "TestMeter";

private readonly Meter _meter;
private readonly Counter<int> _counter;

public TestMetrics()
{
_meter = new Meter(MeterName, "1.0.0", new[]
{
new KeyValuePair<string, object?>("meter-tag", Guid.NewGuid().ToString())
});

_counter = _meter.CreateCounter<int>("test-counter", unit: null, description: null, tags: new[]
{
new KeyValuePair<string, object?>("instrument-tag", Guid.NewGuid().ToString())
});
}

public void IncrementCounter(int value, in TagList tags)
{
_counter.Add(value, in tags);
}

public void Dispose()
{
_meter.Dispose();
}
}