From 589e17a97305c5ac15021be05b223e7931fa8b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Rubio?= Date: Wed, 16 Oct 2024 10:36:07 +0200 Subject: [PATCH] Update Telemetry to read MeterOptions and ActivitySourceOptions from derived classes. --- .../WeatherForecastExtensions.cs | 7 ++- .../ActivitySourceOptions.cs | 47 +++++++++++++++++++ .../Telemetry.cs | 18 +++---- 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs diff --git a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/WeatherForecastExtensions.cs b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/WeatherForecastExtensions.cs index 296a1ac..1c63f92 100644 --- a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/WeatherForecastExtensions.cs +++ b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/WeatherForecastExtensions.cs @@ -17,11 +17,10 @@ public static IEndpointRouteBuilder MapWeatherForecast(this IEndpointRouteBuilde "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; - endpoints.MapGet("/weatherforecast", (IWeatherForecastTelemetry telemetry) => + endpoints.MapGet("/weather-forecast", (IWeatherForecastTelemetry telemetry) => { - using var _ = telemetry.ActivitySource.StartActivity(name: "sample_activity", kind: ActivityKind.Internal); - telemetry.Logger.LogInformation("Requesting weather forecast"); - + using var activity = telemetry.ActivitySource.StartActivity(name: "weather_forecast.request", kind: ActivityKind.Internal); + var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast ( diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs new file mode 100644 index 0000000..f3e54c0 --- /dev/null +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/ActivitySourceOptions.cs @@ -0,0 +1,47 @@ +namespace System.Diagnostics; + +/// +/// Options for creating an . +/// +public class ActivitySourceOptions +{ + private string _name; + + /// + /// The ActivitySource name. + /// + public string Name + { + get => _name; + set + { + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + _name = value; + } + } + + /// + /// The optional ActivitySource version. + /// + public string? Version { get; set; } + + /// + /// The optional list of key-value pair tags associated with the ActivitySource. + /// + public IEnumerable>? Tags { get; set; } + + /// + /// Constructs a new instance of . + /// + /// The ActivitySource name. + public ActivitySourceOptions(string name) + { + Name = name; + + Debug.Assert(_name is not null); + } +} diff --git a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs index ea7f39d..aeb75b5 100644 --- a/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs +++ b/AspNetCore.Examples.OpenTelemetry.TelemetryExtensions/Telemetry.cs @@ -8,27 +8,21 @@ public class Telemetry : ITelemetry { private bool disposedValue; - internal static readonly string CategoryName = TypeNameHelper.GetTypeDisplayName(typeof(TCategoryName), includeGenericParameters: false, nestedTypeDelimiter: '.'); + public static readonly string CategoryName = TypeNameHelper.GetTypeDisplayName(typeof(TCategoryName), includeGenericParameters: false, nestedTypeDelimiter: '.'); public Telemetry(ILoggerFactory loggerFactory, IMeterFactory meterFactory) { Logger = loggerFactory.CreateLogger(); - ActivitySource = new ActivitySource(CategoryName, Version); - Meter = meterFactory.Create(new MeterOptions(CategoryName) - { - Version = Version, - Scope = Scope, - Tags = Tags, - }); + ActivitySource = new ActivitySource(ActivitySourceOptions.Name, ActivitySourceOptions.Version, ActivitySourceOptions.Tags); + Meter = meterFactory.Create(MeterOptions); } public ILogger Logger { get; } public ActivitySource ActivitySource { get; } public Meter Meter { get; } - protected virtual string? Version { get; } - protected virtual object? Scope { get; } - protected virtual IEnumerable>? Tags { get; } + protected virtual ActivitySourceOptions ActivitySourceOptions => new ActivitySourceOptions(CategoryName); + protected virtual MeterOptions MeterOptions => new MeterOptions(CategoryName); protected virtual void Dispose(bool disposing) { @@ -48,4 +42,4 @@ public void Dispose() Dispose(disposing: true); GC.SuppressFinalize(this); } -} \ No newline at end of file +}