From 892deb7d49f5e72ce7578dae07e9da1fdb934789 Mon Sep 17 00:00:00 2001 From: Emre KARA Date: Wed, 28 May 2025 16:16:25 +0300 Subject: [PATCH] Refactor telemetry configuration --- .../Volo/Abp/AbpApplicationBase.cs | 47 +++++++++++++++++++ ...pApplicationWithExternalServiceProvider.cs | 40 +--------------- ...pApplicationWithInternalServiceProvider.cs | 4 ++ .../Helpers/AbpPackageMetadataReader.cs | 9 ++-- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index ec20467ac9..ee559a41af 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; using JetBrains.Annotations; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -11,6 +12,9 @@ using Volo.Abp.DependencyInjection; using Volo.Abp.Internal; using Volo.Abp.Logging; using Volo.Abp.Modularity; +using Volo.Abp.Telemetry; +using Volo.Abp.Telemetry.Constants; +using Volo.Abp.Telemetry.Helpers; namespace Volo.Abp; @@ -362,4 +366,47 @@ public abstract class AbpApplicationBase : IAbpApplication abpHostEnvironment.EnvironmentName = Environments.Production; } } + + protected void ConfigureTelemetry(IServiceProvider serviceProvider) + { + var configuration = serviceProvider.GetRequiredService(); + if (configuration.GetValue("Abp:Telemetry:IsEnabled") == false) + { + return; + } + + Task.Run(async () => + { + try + { + using var scope = serviceProvider.CreateScope(); + + var assembly = Assembly.GetEntryAssembly()!; + + var projectMetaData = AbpProjectMetadataReader.ReadProjectMetadata(assembly); + + if (projectMetaData != null) + { + var telemetryService = scope.ServiceProvider.GetRequiredService(); + + await using var _ = telemetryService.TrackActivity(ActivityNameConsts.ApplicationRun, activity => + { + activity[ActivityPropertyName.Assembly] = assembly.Location; + activity[ActivityPropertyName.ProjectId] = projectMetaData.ProjectId!; + activity[ActivityPropertyName.ProjectType] = projectMetaData.Role!; + if (!projectMetaData.AbpSlnPath.IsNullOrEmpty()) + { + activity[ActivityPropertyName.SolutionPath] = projectMetaData.AbpSlnPath; + } + }); + + } + } + catch (Exception e) + { + var logger = serviceProvider.GetRequiredService>(); + logger.LogError(e, "An error occurred while configuring telemetry."); + } + }); + } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs index 9bca03b5f9..f1a8e09bfc 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs @@ -65,45 +65,7 @@ internal class AbpApplicationWithExternalServiceProvider : AbpApplicationBase, I ConfigureTelemetry(serviceProvider); } - private void ConfigureTelemetry(IServiceProvider serviceProvider) - { - var configuration = serviceProvider.GetRequiredService(); - if (configuration.GetValue("Abp:Telemetry:Disable")) - { - return; - } - - Task.Run(async () => - { - try - { - using var scope = serviceProvider.CreateScope(); - - var assembly = Assembly.GetEntryAssembly()!; - - var packageMetadata = AbpPackageMetadataReader.GetMetaData(assembly); - - if (packageMetadata != null) - { - var telemetryService = scope.ServiceProvider.GetRequiredService(); - - await using var _ = telemetryService.TrackActivity(ActivityNameConsts.ApplicationRun, activity => - { - activity[ActivityPropertyName.Assembly] = assembly.Location; - activity[ActivityPropertyName.ProjectId] = packageMetadata.ProjectId!; - activity[ActivityPropertyName.ProjectType] = packageMetadata.Role!; - activity[ActivityPropertyName.SolutionPath] = packageMetadata.AbpSlnPath!; - }); - - } - } - catch (Exception e) - { - var logger = serviceProvider.GetRequiredService>(); - logger.LogError(e, "An error occurred while configuring telemetry."); - } - }); - } + public override void Dispose() { base.Dispose(); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs index c5e5eccc83..8a2f84feef 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs @@ -50,12 +50,16 @@ internal class AbpApplicationWithInternalServiceProvider : AbpApplicationBase, I { CreateServiceProvider(); await InitializeModulesAsync(); + + ConfigureTelemetry(ServiceProvider); } public void Initialize() { CreateServiceProvider(); InitializeModules(); + + ConfigureTelemetry(ServiceProvider); } public override void Dispose() diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Helpers/AbpPackageMetadataReader.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Helpers/AbpPackageMetadataReader.cs index 9df90c366e..aa550b05a7 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Helpers/AbpPackageMetadataReader.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Helpers/AbpPackageMetadataReader.cs @@ -7,9 +7,9 @@ using System.Text.Json; namespace Volo.Abp.Telemetry.Helpers; -public static class AbpPackageMetadataReader +public static class AbpProjectMetadataReader { - public static AbpPackageMetadata? GetMetaData(Assembly assembly) + public static AbpPackageMetadata? ReadProjectMetadata(Assembly assembly) { var assemblyPath = assembly.Location; try @@ -73,8 +73,10 @@ public static class AbpPackageMetadataReader private static string? FindFileUpwards(string startingDir, string searchPattern) { var currentDir = new DirectoryInfo(startingDir); + const int maxDepth = 10; + var currentDepth = 0; - while (currentDir != null) + while (currentDir != null && currentDepth < maxDepth) { var file = currentDir.GetFiles(searchPattern).FirstOrDefault(); if (file != null) @@ -83,6 +85,7 @@ public static class AbpPackageMetadataReader } currentDir = currentDir.Parent; + currentDepth++; } return null;