Skip to content

Commit

Permalink
Add test meter to stress app with meter and instrument tags
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jul 24, 2024
1 parent df935f4 commit e2dc231
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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();
}
}

0 comments on commit e2dc231

Please sign in to comment.