Browse Source

Refactor telemetry configuration: rename `ConfigureTelemetry` to `SetupTelemetryTracking`, improve method structure, enhance telemetry initialization logic, and streamline exception handling.

pull/23059/head
Emre KARA 1 year ago
parent
commit
6a49166b17
  1. 83
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
  2. 4
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs
  3. 4
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs

83
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs

@ -143,7 +143,8 @@ public abstract class AbpApplicationBase : IAbpApplication
initLogger.Entries.Clear(); initLogger.Entries.Clear();
} }
protected virtual IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollection services, AbpApplicationCreationOptions options) protected virtual IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollection services,
AbpApplicationCreationOptions options)
{ {
return services return services
.GetSingletonInstance<IModuleLoader>() .GetSingletonInstance<IModuleLoader>()
@ -179,7 +180,9 @@ public abstract class AbpApplicationBase : IAbpApplication
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new AbpInitializationException($"An error occurred during {nameof(IPreConfigureServices.PreConfigureServicesAsync)} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.", ex); throw new AbpInitializationException(
$"An error occurred during {nameof(IPreConfigureServices.PreConfigureServicesAsync)} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.",
ex);
} }
} }
@ -366,49 +369,61 @@ public abstract class AbpApplicationBase : IAbpApplication
abpHostEnvironment.EnvironmentName = Environments.Production; abpHostEnvironment.EnvironmentName = Environments.Production;
} }
} }
protected void ConfigureTelemetry(IServiceProvider serviceProvider) protected void SetupTelemetryTracking()
{ {
var abpHostEnvironment = serviceProvider.GetRequiredService<IAbpHostEnvironment>(); if (ShouldSendTelemetryData())
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
if (!abpHostEnvironment.IsDevelopment() || configuration.GetValue<bool?>("Abp:Telemetry:IsEnabled") != true)
{ {
return; return;
} }
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
try await InitializeTelemetryTracking();
{ });
using var scope = serviceProvider.CreateScope(); }
var assembly = Assembly.GetEntryAssembly()!;
var projectMetaData = AbpProjectMetadataReader.ReadProjectMetadata(assembly); private async Task InitializeTelemetryTracking()
{
try
{
var assembly = Assembly.GetEntryAssembly()!;
if (projectMetaData != null) var projectMetaData = AbpProjectMetadataReader.ReadProjectMetadata(assembly);
{
var telemetryService = scope.ServiceProvider.GetRequiredService<ITelemetryService>();
await using var _ = telemetryService.TrackActivity(ActivityNameConsts.ApplicationRun, activity => if (projectMetaData is { ProjectId: not null, AbpSlnPath: not null })
{ {
activity[ActivityPropertyNames.Assembly] = assembly.Location; using var scope = ServiceProvider.CreateScope();
activity[ActivityPropertyNames.ProjectId] = projectMetaData.ProjectId!; var telemetryService = scope.ServiceProvider.GetRequiredService<ITelemetryService>();
activity[ActivityPropertyNames.ProjectType] = projectMetaData.Role!;
if (!projectMetaData.AbpSlnPath.IsNullOrEmpty())
{
activity[ActivityPropertyNames.SolutionPath] = projectMetaData.AbpSlnPath;
}
});
} await telemetryService.AddActivityAsync(ActivityNameConsts.ApplicationRun, activity =>
{
activity[ActivityPropertyNames.Assembly] = assembly.Location;
activity[ActivityPropertyNames.ProjectId] = projectMetaData.ProjectId!;
activity[ActivityPropertyNames.ProjectType] = projectMetaData.Role!;
activity[ActivityPropertyNames.SolutionPath] = projectMetaData.AbpSlnPath;
});
} }
catch (Exception e) }
catch (Exception ex)
{
try
{ {
var logger = serviceProvider.GetRequiredService<ILogger<AbpApplicationWithExternalServiceProvider>>(); var logger = ServiceProvider.GetRequiredService<ILogger<AbpApplicationBase>>();
logger.LogError(e, "An error occurred while configuring telemetry."); logger.LogException(ex, LogLevel.Trace);
} }
}); catch
{
/* ignored */
}
}
}
private bool ShouldSendTelemetryData()
{
using var scope = ServiceProvider.CreateScope();
var abpHostEnvironment = scope.ServiceProvider.GetRequiredService<IAbpHostEnvironment>();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
return abpHostEnvironment.IsDevelopment() && configuration.GetValue<bool?>("Abp:Telemetry:IsEnabled") == true;
} }
} }

4
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs

@ -51,7 +51,7 @@ internal class AbpApplicationWithExternalServiceProvider : AbpApplicationBase, I
await InitializeModulesAsync(); await InitializeModulesAsync();
ConfigureTelemetry(serviceProvider); SetupTelemetryTracking();
} }
public void Initialize([NotNull] IServiceProvider serviceProvider) public void Initialize([NotNull] IServiceProvider serviceProvider)
@ -62,7 +62,7 @@ internal class AbpApplicationWithExternalServiceProvider : AbpApplicationBase, I
InitializeModules(); InitializeModules();
ConfigureTelemetry(serviceProvider); SetupTelemetryTracking();
} }

4
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs

@ -51,7 +51,7 @@ internal class AbpApplicationWithInternalServiceProvider : AbpApplicationBase, I
CreateServiceProvider(); CreateServiceProvider();
await InitializeModulesAsync(); await InitializeModulesAsync();
ConfigureTelemetry(ServiceProvider); SetupTelemetryTracking();
} }
public void Initialize() public void Initialize()
@ -59,7 +59,7 @@ internal class AbpApplicationWithInternalServiceProvider : AbpApplicationBase, I
CreateServiceProvider(); CreateServiceProvider();
InitializeModules(); InitializeModules();
ConfigureTelemetry(ServiceProvider); SetupTelemetryTracking();
} }
public override void Dispose() public override void Dispose()

Loading…
Cancel
Save