Browse Source

Refactor telemetry configuration

pull/23059/head
Emre KARA 1 year ago
parent
commit
892deb7d49
  1. 47
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
  2. 40
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs
  3. 4
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs
  4. 9
      framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Helpers/AbpPackageMetadataReader.cs

47
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<IConfiguration>();
if (configuration.GetValue<bool?>("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<ITelemetryService>();
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<ILogger<AbpApplicationWithExternalServiceProvider>>();
logger.LogError(e, "An error occurred while configuring telemetry.");
}
});
}
}

40
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<IConfiguration>();
if (configuration.GetValue<bool>("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<ITelemetryService>();
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<ILogger<AbpApplicationWithExternalServiceProvider>>();
logger.LogError(e, "An error occurred while configuring telemetry.");
}
});
}
public override void Dispose()
{
base.Dispose();

4
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()

9
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;

Loading…
Cancel
Save