From 5e0054d0907c5151655831543c043211eb26706b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Rubio?= Date: Wed, 6 Nov 2024 15:50:11 +0100 Subject: [PATCH] Cleanup. --- .../Endpoints/GetWeatherForecast.cs | 31 ++++++++----------- .../Services/WeatherForecastTelemetry.cs | 2 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Endpoints/GetWeatherForecast.cs b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Endpoints/GetWeatherForecast.cs index 91cccb4..30323f9 100644 --- a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Endpoints/GetWeatherForecast.cs +++ b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Endpoints/GetWeatherForecast.cs @@ -8,34 +8,29 @@ internal static class GetWeatherForecast public static IEndpointRouteBuilder MapGetWeatherForecast(this IEndpointRouteBuilder endpoints) { endpoints.MapGet("/weather-forecast", (IWeatherForecastTelemetry telemetry) => - { + { using var activity = telemetry.ActivitySource.StartActivity(name: "weather_forecast.request", kind: ActivityKind.Internal); - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast + return Enumerable.Range(1, 5).Select(index => + { + var item = new WeatherForecast ( Date: DateOnly.FromDateTime(DateTime.Now.AddDays(index)), TemperatureC: Random.Shared.Next(-20, 55) - )) - .ToArray(); - - - foreach (var item in forecast) - { - telemetry.Logger.LogInformation("Weather forecast calculated for {Date}: {Summary}, {TemperatureC}ºC ({TemperatureF}ºF)", + ); + telemetry.Logger.LogInformation("Weather forecast calculated for {Date}: {Summary}, {TemperatureC}ºC ({TemperatureF}ºF)", item.Date.ToString("yyyy-MM-dd"), item.Summary, - item.TemperatureC, + item.TemperatureC, item.TemperatureF ); - telemetry.TemperatureC.Record(item.TemperatureC, new TagList - { - { "Date", item.Date.ToString("yyyy-MM-dd") }, - { "Summary", item.Summary } + telemetry.TemperatureC.Record(item.TemperatureC, new TagList + { + { "Date", item.Date.ToString("yyyy-MM-dd") }, + { "Summary", item.Summary } }); - } - - return forecast; + return item; + }); }) .WithName("GetWeatherForecast"); diff --git a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Services/WeatherForecastTelemetry.cs b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Services/WeatherForecastTelemetry.cs index 4991b9c..e426377 100644 --- a/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Services/WeatherForecastTelemetry.cs +++ b/AspNetCore.Examples.OpenTelemetry.Api/WeatherForecast/Services/WeatherForecastTelemetry.cs @@ -21,7 +21,7 @@ public WeatherForecastTelemetry(ILoggerFactory loggerFactory, IMeterFactory mete public Histogram TemperatureC { get; } } -internal static class TelemetryExtensions +internal static class WeatherForecastTelemetryExtensions { public static IServiceCollection AddWeatherForecastTelemetry(this IServiceCollection services) {