From ada0e2f665862d2010f738f14902e0cab08200a8 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Thu, 12 Jun 2025 19:12:38 +0200 Subject: [PATCH] Use custom activity source --- .../Common/DependencyInjectionExtensions.cs | 7 +++++ .../Persistence/ApplyMigrationsService.cs | 28 +++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs b/src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs index 041b3dd..539aef2 100644 --- a/src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs +++ b/src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs @@ -2,6 +2,8 @@ using FluentValidation; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Options; +using System.Diagnostics; +using System.Reflection; using Vegasco.Server.Api.Authentication; using Vegasco.Server.Api.Common; using Vegasco.Server.Api.Persistence; @@ -27,6 +29,11 @@ public static class DependencyInjectionExtensions private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services) { + var assemblyName = Assembly.GetExecutingAssembly() + .GetName() + .Name ?? "Vegasco.Server.Api"; + services.AddSingleton(new ActivitySource(assemblyName)); + services.AddResponseCompression(); services.AddValidatorsFromAssemblies( diff --git a/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs b/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs index 19b9d70..3b0ffc7 100644 --- a/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs +++ b/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs @@ -1,18 +1,24 @@ using Microsoft.EntityFrameworkCore; +using System.Diagnostics; namespace Vegasco.Server.Api.Persistence; -public class ApplyMigrationsService(ILogger logger, IServiceScopeFactory scopeFactory) - : IHostedService +public class ApplyMigrationsService( + ILogger logger, + IServiceScopeFactory scopeFactory, + ActivitySource activitySource) + : IHostedService { - public async Task StartAsync(CancellationToken cancellationToken) - { - logger.LogInformation("Starting migrations"); - - using IServiceScope scope = scopeFactory.CreateScope(); - await using var dbContext = scope.ServiceProvider.GetRequiredService(); - await dbContext.Database.MigrateAsync(cancellationToken); - } + public async Task StartAsync(CancellationToken cancellationToken) + { + using var activity = activitySource.StartActivity("ApplyMigrations"); - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + logger.LogInformation("Starting migrations"); + + using IServiceScope scope = scopeFactory.CreateScope(); + await using var dbContext = scope.ServiceProvider.GetRequiredService(); + await dbContext.Database.MigrateAsync(cancellationToken); + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; } \ No newline at end of file