diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index a2625c5..d6c94a1 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -13,6 +13,7 @@ jobs: strategy: matrix: project: + - src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj - src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore.csproj - src/Madev.Utils.Infrastructure.Http/Madev.Utils.Infrastructure.Http.csproj - src/Madev.Utils.Infrastructure.Services.Mailing/Madev.Utils.Infrastructure.Services.Mailing.csproj diff --git a/Madev.Utils.sln b/Madev.Utils.sln index 6b9ad4e..991e29a 100644 --- a/Madev.Utils.sln +++ b/Madev.Utils.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Madev.Utils.Infrastructure. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore", "src\Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore\Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore.csproj", "{6E266322-CCCA-4774-892B-30F1690FD734}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Madev.Utils.Infrastructure.ApplicationInsights.WorkerService", "src\Madev.Utils.Infrastructure.ApplicationInsights.WorkerService\Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj", "{9C8C911F-6BCF-4027-A088-FBA9104B7F7E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/DependencyExtensions.cs b/src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/DependencyExtensions.cs index d747911..086bee6 100644 --- a/src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/DependencyExtensions.cs +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/DependencyExtensions.cs @@ -6,11 +6,12 @@ namespace Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore; public static class DependencyExtensions { - public static IServiceCollection AddMadevApplicationInsightsTelemetry(this IServiceCollection services, + public static IServiceCollection AddMadevApplicationInsightsTelemetry(this IServiceCollection services, + string connectionString, string roleName, - string[] excludedOperations = null) + string[]? excludedOperations = null) { - services.AddApplicationInsightsTelemetry(); + services.AddApplicationInsightsTelemetry(conf => conf.ConnectionString = connectionString); services.AddSingleton(new RoleNameTelemetryInitializer(roleName)); excludedOperations ??= []; diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/DependencyExtensions.cs b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/DependencyExtensions.cs new file mode 100644 index 0000000..7522192 --- /dev/null +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/DependencyExtensions.cs @@ -0,0 +1,25 @@ +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.WorkerService; +using Microsoft.Extensions.DependencyInjection; + +namespace Madev.Utils.Infrastructure.ApplicationInsights.WorkerService; + +public static class DependencyExtensions +{ + public static IServiceCollection AddMadevApplicationInsightsWorkerServiceTelemetry(this IServiceCollection services, + string connectionString, + string roleName, + string[]? excludedOperations = null) + { + services.AddApplicationInsightsTelemetryWorkerService(config => config.ConnectionString = connectionString); + services.AddSingleton(new RoleNameTelemetryInitializer(roleName)); + + excludedOperations ??= []; + if (excludedOperations.Any()) + { + services.AddSingleton(new TelemetryProcessorFactory(excludedOperations)); + } + + return services; + } +} \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/ExcludeOperationsTelemetryProcessor.cs b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/ExcludeOperationsTelemetryProcessor.cs new file mode 100644 index 0000000..1c83e83 --- /dev/null +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/ExcludeOperationsTelemetryProcessor.cs @@ -0,0 +1,18 @@ +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; + +namespace Madev.Utils.Infrastructure.ApplicationInsights.WorkerService; + +internal class ExcludeOperationsTelemetryProcessor(ITelemetryProcessor nextProcessor, string[] excludedOperations) + : ITelemetryProcessor +{ + public void Process(ITelemetry item) + { + if (excludedOperations.Contains(item.Context.Operation.Name)) + { + return; + } + + nextProcessor.Process(item); + } +} \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj new file mode 100644 index 0000000..8b6da31 --- /dev/null +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj @@ -0,0 +1,19 @@ + + + + net9.0 + enable + enable + Miroslav Adamec + Madev + Application insghts telemetry setup + https://github.com/madev/Madev.Utils + https://github.com/madev/Madev.Utils + mirecad madev telemetry application insights worker service tools utils extensions + https://madevfiles.blob.core.windows.net/$web/Madev/madev-icon.png + + + + + + diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/RoleNameTelemetryInitializer.cs b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/RoleNameTelemetryInitializer.cs new file mode 100644 index 0000000..7d4726f --- /dev/null +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/RoleNameTelemetryInitializer.cs @@ -0,0 +1,13 @@ +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; + +namespace Madev.Utils.Infrastructure.ApplicationInsights.WorkerService; + +internal class RoleNameTelemetryInitializer(string roleName) + : ITelemetryInitializer +{ + public void Initialize(ITelemetry telemetry) + { + telemetry.Context.Cloud.RoleName = roleName; + } +} \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/TelemetryProcessorFactory.cs b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/TelemetryProcessorFactory.cs new file mode 100644 index 0000000..bd6982c --- /dev/null +++ b/src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/TelemetryProcessorFactory.cs @@ -0,0 +1,12 @@ +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.WorkerService; + +namespace Madev.Utils.Infrastructure.ApplicationInsights.WorkerService; + +internal class TelemetryProcessorFactory(string[] excludedOperations) : ITelemetryProcessorFactory +{ + public ITelemetryProcessor Create(ITelemetryProcessor nextProcessor) + { + return new ExcludeOperationsTelemetryProcessor(nextProcessor, excludedOperations); + } +} \ No newline at end of file