diff --git a/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryModule.cs b/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryModule.cs index cc6b9bd28..c8f162ca2 100644 --- a/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryModule.cs +++ b/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryModule.cs @@ -5,24 +5,46 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using System; +using System.Collections.Generic; +using Volo.Abp; using Volo.Abp.Modularity; namespace LINGYUN.Abp.Telemetry.OpenTelemetry; public class AbpTelemetryOpenTelemetryModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + + PreConfigure(options => + { + var ignureRequestLocalUrlPrefixs = configuration.GetSection("OpenTelemetry:IgnoreUrls:Local").Get>(); + if (ignureRequestLocalUrlPrefixs != null) + { + options.IgnoreLocalRequestUrls = ignureRequestLocalUrlPrefixs; + } + var ignureRequestRemoteUrlPrefixs = configuration.GetSection("OpenTelemetry:IgnoreUrls:Remote").Get>(); + if (ignureRequestRemoteUrlPrefixs != null) + { + options.IgnoreRemoteRequestUrls = ignureRequestRemoteUrlPrefixs; + } + }); + } + public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); var openTelmetrySetup = context.Services.GetPreConfigureActions(); - var openTelemetryEnabled = configuration["OpenTelemetry:IsEnabled"]; - if (openTelemetryEnabled.IsNullOrWhiteSpace() || "false".Equals(openTelemetryEnabled.ToLower())) + if (!configuration.GetValue("OpenTelemetry:IsEnabled", false)) { return; } + var openTelemetyOptions = context.Services.ExecutePreConfiguredActions(new AbpTelemetryOpenTelemetryOptions()); + var applicationName = configuration["OpenTelemetry:ServiceName"]; if (applicationName.IsNullOrWhiteSpace()) { @@ -37,7 +59,7 @@ public class AbpTelemetryOpenTelemetryModule : AbpModule .WithTracing(tracing => { tracing.AddSource(applicationName); - ConfigureTracing(tracing, configuration); + ConfigureTracing(tracing, configuration, openTelemetyOptions); }) .WithMetrics(metrics => { @@ -47,15 +69,38 @@ public class AbpTelemetryOpenTelemetryModule : AbpModule openTelmetrySetup.Configure(openTelmetryBuilder); } - private static void ConfigureTracing(TracerProviderBuilder tracing, IConfiguration configuration) + private static void ConfigureTracing(TracerProviderBuilder tracing, IConfiguration configuration, AbpTelemetryOpenTelemetryOptions openTelemetyOptions) { - tracing.AddHttpClientInstrumentation(); - tracing.AddAspNetCoreInstrumentation(); + tracing.AddHttpClientInstrumentation(options => + { + options.FilterHttpRequestMessage += (request) => + { + if (request.RequestUri != null && + openTelemetyOptions.IsIgnureRemoteRequestUrl(request.RequestUri.AbsolutePath)) + { + return false; + } + + return true; + }; + }); + tracing.AddAspNetCoreInstrumentation(options => + { + options.Filter += (ctx) => + { + if (openTelemetyOptions.IsIgnureLocalRequestUrl(ctx.Request.Path)) + { + return false; + } + + return true; + }; + }); tracing.AddCapInstrumentation(); tracing.AddEntityFrameworkCoreInstrumentation(efcore => { efcore.SetDbStatementForText = configuration.GetValue( - "OpenTelemetry:EntityFrameworkCore:SetDbStatementForText", + "OpenTelemetry:EntityFrameworkCore:SetDbStatementForText", efcore.SetDbStatementForText); efcore.SetDbStatementForStoredProcedure = configuration.GetValue( @@ -68,24 +113,28 @@ public class AbpTelemetryOpenTelemetryModule : AbpModule tracing.AddConsoleExporter(); } - var tracingOtlpEndpoint = configuration["OpenTelemetry:Otlp:Endpoint"]; - if (!tracingOtlpEndpoint.IsNullOrWhiteSpace()) + if (configuration.GetValue("OpenTelemetry:Otlp:IsEnabled", false)) { tracing.AddOtlpExporter(otlpOptions => { - otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); + var otlpEndPoint = configuration["OpenTelemetry:Otlp:Endpoint"]; + Check.NotNullOrWhiteSpace(otlpEndPoint, nameof(otlpEndPoint)); + + otlpOptions.Headers = configuration["OpenTelemetry:Otlp:Headers"]; + otlpOptions.Endpoint = new Uri(otlpEndPoint.EnsureEndsWith('/') + "v1/traces"); + otlpOptions.Protocol = configuration.GetValue("OpenTelemetry:Otlp:Protocol", otlpOptions.Protocol); }); - return; } - var zipkinEndpoint = configuration["OpenTelemetry:ZipKin:Endpoint"]; - if (!zipkinEndpoint.IsNullOrWhiteSpace()) + if (configuration.GetValue("OpenTelemetry:ZipKin:IsEnabled", false)) { tracing.AddZipkinExporter(zipKinOptions => { - zipKinOptions.Endpoint = new Uri(zipkinEndpoint); + var zipkinEndPoint = configuration["OpenTelemetry:ZipKin:Endpoint"]; + Check.NotNullOrWhiteSpace(zipkinEndPoint, nameof(zipkinEndPoint)); + + zipKinOptions.Endpoint = new Uri(zipkinEndPoint); }); - return; } } @@ -95,19 +144,17 @@ public class AbpTelemetryOpenTelemetryModule : AbpModule metrics.AddHttpClientInstrumentation(); metrics.AddAspNetCoreInstrumentation(); - if (configuration.GetValue("OpenTelemetry:Console:IsEnabled", false)) - { - metrics.AddConsoleExporter(); - } - - var tracingOtlpEndpoint = configuration["OpenTelemetry:Otlp:Endpoint"]; - if (!tracingOtlpEndpoint.IsNullOrWhiteSpace()) + if (configuration.GetValue("OpenTelemetry:Otlp:IsEnabled", false)) { metrics.AddOtlpExporter(otlpOptions => { - otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); + var otlpEndPoint = configuration["OpenTelemetry:Otlp:Endpoint"]; + Check.NotNullOrWhiteSpace(otlpEndPoint, nameof(otlpEndPoint)); + + otlpOptions.Headers = configuration["OpenTelemetry:Otlp:Headers"]; + otlpOptions.Endpoint = new Uri(otlpEndPoint.EnsureEndsWith('/') + "v1/metrics"); + otlpOptions.Protocol = configuration.GetValue("OpenTelemetry:Otlp:Protocol", otlpOptions.Protocol); }); - return; } } } diff --git a/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryOptions.cs b/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryOptions.cs new file mode 100644 index 000000000..6734baffd --- /dev/null +++ b/aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryOptions.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LINGYUN.Abp.Telemetry.OpenTelemetry; +public class AbpTelemetryOpenTelemetryOptions +{ + /// + /// 是否忽略CAP仪表板请求, 默认: true + /// + public bool IgnoreCapDashboardUrls { get; set; } + /// + /// 是否忽略ES请求, 默认: true + /// + public bool IgnoreElasticsearchUrls { get; set; } + /// + /// 忽略本地请求路径 + /// + public List IgnoreLocalRequestUrls { get; set; } + /// + /// 忽略远程请求路径 + /// + public List IgnoreRemoteRequestUrls { get; set; } + + public AbpTelemetryOpenTelemetryOptions() + { + IgnoreLocalRequestUrls = new List + { + "/healthz", // 服务健康状态请求不记录 + }; + IgnoreRemoteRequestUrls = new List(); + IgnoreCapDashboardUrls = true; + IgnoreElasticsearchUrls = true; + } + + public bool IsIgnureLocalRequestUrl(string url) + { + // 忽略CAP仪表板请求 + if (IgnoreCapDashboardUrls && url.StartsWith("/cap")) + { + return true; + } + + return IgnoreLocalRequestUrls.Any(localUrl => url.Equals(localUrl, StringComparison.InvariantCultureIgnoreCase)); + } + + public bool IsIgnureRemoteRequestUrl(string url) + { + // 忽略向es推送数据请求 + if (IgnoreElasticsearchUrls && url.EndsWith("/_bulk")) + { + return true; + } + + return IgnoreRemoteRequestUrls.Any(remoteUrl => url.Equals(remoteUrl, StringComparison.InvariantCultureIgnoreCase)); + } +} +