From 0f924a0c84b5686e2c996c4aaa1266b98af510b9 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 25 Jul 2024 10:08:29 +0800 Subject: [PATCH] Add test meter to stress app with meter and instrument tags (#5046) --- .../Stress/Stress.ApiService/Program.cs | 11 +++++- .../Stress/Stress.ApiService/TestMetrics.cs | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 playground/Stress/Stress.ApiService/TestMetrics.cs diff --git a/playground/Stress/Stress.ApiService/Program.cs b/playground/Stress/Stress.ApiService/Program.cs index 875f66f74a..b71038fce8 100644 --- a/playground/Stress/Stress.ApiService/Program.cs +++ b/playground/Stress/Stress.ApiService/Program.cs @@ -12,7 +12,9 @@ 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(); var app = builder.Build(); @@ -20,6 +22,13 @@ app.MapGet("/", () => "Hello world"); +app.MapGet("/increment-counter", (TestMetrics metrics) => +{ + metrics.IncrementCounter(1, new TagList([new KeyValuePair("add-tag", "1")])); + + return "Big trace created"; +}); + app.MapGet("/big-trace", async () => { var bigTraceCreator = new TraceCreator(); diff --git a/playground/Stress/Stress.ApiService/TestMetrics.cs b/playground/Stress/Stress.ApiService/TestMetrics.cs new file mode 100644 index 0000000000..5ab0b3681c --- /dev/null +++ b/playground/Stress/Stress.ApiService/TestMetrics.cs @@ -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 _counter; + + public TestMetrics() + { + _meter = new Meter(MeterName, "1.0.0", new[] + { + new KeyValuePair("meter-tag", Guid.NewGuid().ToString()) + }); + + _counter = _meter.CreateCounter("test-counter", unit: null, description: null, tags: new[] + { + new KeyValuePair("instrument-tag", Guid.NewGuid().ToString()) + }); + } + + public void IncrementCounter(int value, in TagList tags) + { + _counter.Add(value, in tags); + } + + public void Dispose() + { + _meter.Dispose(); + } +}