Skip to content

Commit 4301c98

Browse files
committed
feat(open-telemetry): Enhance OpenTelemetry module
1 parent 447dbc2 commit 4301c98

2 files changed

Lines changed: 129 additions & 24 deletions

File tree

aspnet-core/framework/telemetry/LINGYUN.Abp.Telemetry.OpenTelemetry/LINGYUN/Abp/Telemetry/OpenTelemetry/AbpTelemetryOpenTelemetryModule.cs

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,46 @@
55
using OpenTelemetry.Resources;
66
using OpenTelemetry.Trace;
77
using System;
8+
using System.Collections.Generic;
9+
using Volo.Abp;
810
using Volo.Abp.Modularity;
911

1012
namespace LINGYUN.Abp.Telemetry.OpenTelemetry;
1113

1214
public class AbpTelemetryOpenTelemetryModule : AbpModule
1315
{
16+
public override void PreConfigureServices(ServiceConfigurationContext context)
17+
{
18+
var configuration = context.Services.GetConfiguration();
19+
20+
PreConfigure<AbpTelemetryOpenTelemetryOptions>(options =>
21+
{
22+
var ignureRequestLocalUrlPrefixs = configuration.GetSection("OpenTelemetry:IgnoreUrls:Local").Get<List<string>>();
23+
if (ignureRequestLocalUrlPrefixs != null)
24+
{
25+
options.IgnoreLocalRequestUrls = ignureRequestLocalUrlPrefixs;
26+
}
27+
var ignureRequestRemoteUrlPrefixs = configuration.GetSection("OpenTelemetry:IgnoreUrls:Remote").Get<List<string>>();
28+
if (ignureRequestRemoteUrlPrefixs != null)
29+
{
30+
options.IgnoreRemoteRequestUrls = ignureRequestRemoteUrlPrefixs;
31+
}
32+
});
33+
}
34+
1435
public override void ConfigureServices(ServiceConfigurationContext context)
1536
{
1637
var configuration = context.Services.GetConfiguration();
1738

1839
var openTelmetrySetup = context.Services.GetPreConfigureActions<OpenTelemetryBuilder>();
1940

20-
var openTelemetryEnabled = configuration["OpenTelemetry:IsEnabled"];
21-
if (openTelemetryEnabled.IsNullOrWhiteSpace() || "false".Equals(openTelemetryEnabled.ToLower()))
41+
if (!configuration.GetValue("OpenTelemetry:IsEnabled", false))
2242
{
2343
return;
2444
}
2545

46+
var openTelemetyOptions = context.Services.ExecutePreConfiguredActions(new AbpTelemetryOpenTelemetryOptions());
47+
2648
var applicationName = configuration["OpenTelemetry:ServiceName"];
2749
if (applicationName.IsNullOrWhiteSpace())
2850
{
@@ -37,7 +59,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
3759
.WithTracing(tracing =>
3860
{
3961
tracing.AddSource(applicationName);
40-
ConfigureTracing(tracing, configuration);
62+
ConfigureTracing(tracing, configuration, openTelemetyOptions);
4163
})
4264
.WithMetrics(metrics =>
4365
{
@@ -47,15 +69,38 @@ public override void ConfigureServices(ServiceConfigurationContext context)
4769
openTelmetrySetup.Configure(openTelmetryBuilder);
4870
}
4971

50-
private static void ConfigureTracing(TracerProviderBuilder tracing, IConfiguration configuration)
72+
private static void ConfigureTracing(TracerProviderBuilder tracing, IConfiguration configuration, AbpTelemetryOpenTelemetryOptions openTelemetyOptions)
5173
{
52-
tracing.AddHttpClientInstrumentation();
53-
tracing.AddAspNetCoreInstrumentation();
74+
tracing.AddHttpClientInstrumentation(options =>
75+
{
76+
options.FilterHttpRequestMessage += (request) =>
77+
{
78+
if (request.RequestUri != null &&
79+
openTelemetyOptions.IsIgnureRemoteRequestUrl(request.RequestUri.AbsolutePath))
80+
{
81+
return false;
82+
}
83+
84+
return true;
85+
};
86+
});
87+
tracing.AddAspNetCoreInstrumentation(options =>
88+
{
89+
options.Filter += (ctx) =>
90+
{
91+
if (openTelemetyOptions.IsIgnureLocalRequestUrl(ctx.Request.Path))
92+
{
93+
return false;
94+
}
95+
96+
return true;
97+
};
98+
});
5499
tracing.AddCapInstrumentation();
55100
tracing.AddEntityFrameworkCoreInstrumentation(efcore =>
56101
{
57102
efcore.SetDbStatementForText = configuration.GetValue(
58-
"OpenTelemetry:EntityFrameworkCore:SetDbStatementForText",
103+
"OpenTelemetry:EntityFrameworkCore:SetDbStatementForText",
59104
efcore.SetDbStatementForText);
60105

61106
efcore.SetDbStatementForStoredProcedure = configuration.GetValue(
@@ -68,24 +113,28 @@ private static void ConfigureTracing(TracerProviderBuilder tracing, IConfigurati
68113
tracing.AddConsoleExporter();
69114
}
70115

71-
var tracingOtlpEndpoint = configuration["OpenTelemetry:Otlp:Endpoint"];
72-
if (!tracingOtlpEndpoint.IsNullOrWhiteSpace())
116+
if (configuration.GetValue("OpenTelemetry:Otlp:IsEnabled", false))
73117
{
74118
tracing.AddOtlpExporter(otlpOptions =>
75119
{
76-
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint);
120+
var otlpEndPoint = configuration["OpenTelemetry:Otlp:Endpoint"];
121+
Check.NotNullOrWhiteSpace(otlpEndPoint, nameof(otlpEndPoint));
122+
123+
otlpOptions.Headers = configuration["OpenTelemetry:Otlp:Headers"];
124+
otlpOptions.Endpoint = new Uri(otlpEndPoint.EnsureEndsWith('/') + "v1/traces");
125+
otlpOptions.Protocol = configuration.GetValue("OpenTelemetry:Otlp:Protocol", otlpOptions.Protocol);
77126
});
78-
return;
79127
}
80128

81-
var zipkinEndpoint = configuration["OpenTelemetry:ZipKin:Endpoint"];
82-
if (!zipkinEndpoint.IsNullOrWhiteSpace())
129+
if (configuration.GetValue("OpenTelemetry:ZipKin:IsEnabled", false))
83130
{
84131
tracing.AddZipkinExporter(zipKinOptions =>
85132
{
86-
zipKinOptions.Endpoint = new Uri(zipkinEndpoint);
133+
var zipkinEndPoint = configuration["OpenTelemetry:ZipKin:Endpoint"];
134+
Check.NotNullOrWhiteSpace(zipkinEndPoint, nameof(zipkinEndPoint));
135+
136+
zipKinOptions.Endpoint = new Uri(zipkinEndPoint);
87137
});
88-
return;
89138
}
90139
}
91140

@@ -95,19 +144,17 @@ private static void ConfigureMetrics(MeterProviderBuilder metrics, IConfiguratio
95144
metrics.AddHttpClientInstrumentation();
96145
metrics.AddAspNetCoreInstrumentation();
97146

98-
if (configuration.GetValue("OpenTelemetry:Console:IsEnabled", false))
99-
{
100-
metrics.AddConsoleExporter();
101-
}
102-
103-
var tracingOtlpEndpoint = configuration["OpenTelemetry:Otlp:Endpoint"];
104-
if (!tracingOtlpEndpoint.IsNullOrWhiteSpace())
147+
if (configuration.GetValue("OpenTelemetry:Otlp:IsEnabled", false))
105148
{
106149
metrics.AddOtlpExporter(otlpOptions =>
107150
{
108-
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint);
151+
var otlpEndPoint = configuration["OpenTelemetry:Otlp:Endpoint"];
152+
Check.NotNullOrWhiteSpace(otlpEndPoint, nameof(otlpEndPoint));
153+
154+
otlpOptions.Headers = configuration["OpenTelemetry:Otlp:Headers"];
155+
otlpOptions.Endpoint = new Uri(otlpEndPoint.EnsureEndsWith('/') + "v1/metrics");
156+
otlpOptions.Protocol = configuration.GetValue("OpenTelemetry:Otlp:Protocol", otlpOptions.Protocol);
109157
});
110-
return;
111158
}
112159
}
113160
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace LINGYUN.Abp.Telemetry.OpenTelemetry;
6+
public class AbpTelemetryOpenTelemetryOptions
7+
{
8+
/// <summary>
9+
/// 是否忽略CAP仪表板请求, 默认: true
10+
/// </summary>
11+
public bool IgnoreCapDashboardUrls { get; set; }
12+
/// <summary>
13+
/// 是否忽略ES请求, 默认: true
14+
/// </summary>
15+
public bool IgnoreElasticsearchUrls { get; set; }
16+
/// <summary>
17+
/// 忽略本地请求路径
18+
/// </summary>
19+
public List<string> IgnoreLocalRequestUrls { get; set; }
20+
/// <summary>
21+
/// 忽略远程请求路径
22+
/// </summary>
23+
public List<string> IgnoreRemoteRequestUrls { get; set; }
24+
25+
public AbpTelemetryOpenTelemetryOptions()
26+
{
27+
IgnoreLocalRequestUrls = new List<string>
28+
{
29+
"/healthz", // 服务健康状态请求不记录
30+
};
31+
IgnoreRemoteRequestUrls = new List<string>();
32+
IgnoreCapDashboardUrls = true;
33+
IgnoreElasticsearchUrls = true;
34+
}
35+
36+
public bool IsIgnureLocalRequestUrl(string url)
37+
{
38+
// 忽略CAP仪表板请求
39+
if (IgnoreCapDashboardUrls && url.StartsWith("/cap"))
40+
{
41+
return true;
42+
}
43+
44+
return IgnoreLocalRequestUrls.Any(localUrl => url.Equals(localUrl, StringComparison.InvariantCultureIgnoreCase));
45+
}
46+
47+
public bool IsIgnureRemoteRequestUrl(string url)
48+
{
49+
// 忽略向es推送数据请求
50+
if (IgnoreElasticsearchUrls && url.EndsWith("/_bulk"))
51+
{
52+
return true;
53+
}
54+
55+
return IgnoreRemoteRequestUrls.Any(remoteUrl => url.Equals(remoteUrl, StringComparison.InvariantCultureIgnoreCase));
56+
}
57+
}
58+

0 commit comments

Comments
 (0)