diff --git a/WebPullApp/MyInstruments.cs b/WebPullApp/MyInstruments.cs index 946d986..bd7543d 100644 --- a/WebPullApp/MyInstruments.cs +++ b/WebPullApp/MyInstruments.cs @@ -1,4 +1,5 @@ -using System.Diagnostics.Metrics; +using OpenTelemetry.Metrics; +using System.Diagnostics.Metrics; namespace WebPullApp; @@ -33,4 +34,12 @@ public void Dispose() _meter.Dispose(); GC.SuppressFinalize(this); } +} + +public static class MeterProviderBuilderExtensions +{ + public static MeterProviderBuilder AddMyInstruments(this MeterProviderBuilder builder) => + builder + .AddMeter(MyInstruments.MeterName) + .AddInstrumentation(); } \ No newline at end of file diff --git a/WebPullApp/Program.cs b/WebPullApp/Program.cs index 29a5bc6..986065a 100644 --- a/WebPullApp/Program.cs +++ b/WebPullApp/Program.cs @@ -10,13 +10,12 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddOpenTelemetry() //Add OpenTelemetry.Extensions.Hosting nuget package .ConfigureResource(r => r.AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName)) .WithMetrics(b => b - .AddMeter(MyInstruments.MeterName) + .AddMyInstruments() .AddAspNetCoreInstrumentation() // Add OpenTelemetry.Instrumentation.AspNetCore nuget package .AddHttpClientInstrumentation() // Add OpenTelemetry.Instrumentation.Http nuget package .AddRuntimeInstrumentation() // Add OpenTelemetry.Instrumentation.Runtime nuget package diff --git a/WebPushApp/MyInstruments.cs b/WebPushApp/MyInstruments.cs index fdc81ea..a11ed4f 100644 --- a/WebPushApp/MyInstruments.cs +++ b/WebPushApp/MyInstruments.cs @@ -1,9 +1,11 @@ using System.Diagnostics.Metrics; +using OpenTelemetry.Metrics; namespace WebPushApp; public class MyInstruments : IDisposable { + internal const string GlobalSystemName = "MeterUsing.Demo"; internal const string MeterName = "MeterUsing.AspNetCore"; private readonly Meter _meter; private int _todayTemperature; @@ -33,4 +35,12 @@ public void Dispose() _meter.Dispose(); GC.SuppressFinalize(this); } +} + +public static class MeterProviderBuilderExtensions +{ + public static MeterProviderBuilder AddMyInstruments(this MeterProviderBuilder builder) => + builder + .AddMeter(MyInstruments.MeterName) + .AddInstrumentation(); } \ No newline at end of file diff --git a/WebPushApp/Program.cs b/WebPushApp/Program.cs index 7fb826f..c9becf2 100644 --- a/WebPushApp/Program.cs +++ b/WebPushApp/Program.cs @@ -10,13 +10,14 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddOpenTelemetry() //Add OpenTelemetry.Extensions.Hosting nuget package - .ConfigureResource(r => r.AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName)) + .ConfigureResource(r => r + .AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName) + .AddAttributes(new Dictionary { ["SystemName"] = MyInstruments.GlobalSystemName })) // That is visible in the target_info separate metric. .WithMetrics(meterBuilder => meterBuilder - .AddMeter(MyInstruments.MeterName) + .AddMyInstruments() .AddAspNetCoreInstrumentation() // Add OpenTelemetry.Instrumentation.AspNetCore nuget package .AddHttpClientInstrumentation() // Add OpenTelemetry.Instrumentation.Http nuget package .AddRuntimeInstrumentation() // Add OpenTelemetry.Instrumentation.Runtime nuget package