Skip to content

Commit dfb8d4d

Browse files
committed
调整 otel 集成资源信息
1 parent 1fa0bbd commit dfb8d4d

7 files changed

Lines changed: 46 additions & 43 deletions

File tree

.github/workflows/docker-image.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ jobs:
1717
- name: Login docker regsitry
1818
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_USER_PASSWORD }}
1919
- name: Build the Docker image
20-
run: docker build --target zserver-api -f API.Dockerfile -t zlzforever/zserver-api:202604-7 .
20+
run: docker build --target zserver-api -f API.Dockerfile -t zlzforever/zserver-api:202604-8 .
2121
- name: Publish the Docker image
22-
run: docker push zlzforever/zserver-api:202604-7
22+
run: docker push zlzforever/zserver-api:202604-8

API.Dockerfile

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ FROM mcr.microsoft.com/dotnet/sdk:10.0 AS api-builder
22
WORKDIR /app
33
COPY . .
44
RUN cd src/ZServer.API && dotnet publish -c Release -o out
5-
RUN rm -rf src/ZServer.API/out/nacos.json
6-
RUN rm -rf src/ZServer.API/out/zserver.json
7-
RUN rm -rf src/ZServer.API/out/runtimes/linux-arm
8-
RUN rm -rf src/ZServer.API/out/runtimes/linux-arm64
9-
RUN rm -rf src/ZServer.API/out/runtimes/linux-musl-x64
10-
RUN rm -rf src/ZServer.API/out/runtimes/osx
11-
RUN rm -rf src/ZServer.API/out/runtimes/osx-x64
12-
RUN rm -rf src/ZServer.API/out/runtimes/win-arm64
13-
RUN rm -rf src/ZServer.API/out/runtimes/win-x64
14-
RUN rm -rf src/ZServer.API/out/runtimes/win-x86
15-
RUN rm -rf src/ZServer.API/out/shapes
16-
RUN rm -rf src/ZServer.API/out/fonts
5+
RUN rm -rf src/ZServer.API/out/nacos.json && \
6+
rm -rf src/ZServer.API/out/zserver.json && \
7+
rm -rf src/ZServer.API/out/runtimes/linux-arm && \
8+
rm -rf src/ZServer.API/out/runtimes/linux-arm64 && \
9+
rm -rf src/ZServer.API/out/runtimes/linux-musl-x64 && \
10+
rm -rf src/ZServer.API/out/runtimes/osx && \
11+
rm -rf src/ZServer.API/out/runtimes/osx-x64 && \
12+
rm -rf src/ZServer.API/out/runtimes/win-arm64 && \
13+
rm -rf src/ZServer.API/out/runtimes/win-x64 && \
14+
rm -rf src/ZServer.API/out/runtimes/win-x86 && \
15+
rm -rf src/ZServer.API/out/shapes && \
16+
rm -rf src/ZServer.API/out/fonts
1717

1818
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS zserver-api
1919
WORKDIR /app
@@ -26,9 +26,9 @@ RUN mkdir /app/shapes && mkdir /app/Fonts
2626
# echo "deb-src https://mirrors.aliyun.com/debian-security/ noble-security main" >> /etc/apt/sources.list
2727
RUN apt-get update &&\
2828
apt-get install -y fontconfig iputils-ping net-tools curl && apt-get clean
29+
ADD ./src/ZServer.API/fonts/* /usr/share/fonts/truetype/deng/
2930
COPY docker-entrypoint.sh /usr/local/bin/
3031
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
31-
ADD ./src/ZServer.API/fonts/* /usr/share/fonts/truetype/deng/
3232
COPY --from=api-builder /app/src/ZServer.API/out .
3333
ENTRYPOINT ["docker-entrypoint.sh"]
3434
CMD ["dotnet", "zapi.dll"]

docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fc-cache -f -v
77
# 输入文件名
88
input_file="${CONFIG_SOURCE}"
99
# 输出文件名
10-
output_file="/app/appsettings.json"
10+
output_file="/app/conf/appsettings.json"
1111

1212
# 检查输入文件是否存在
1313
if [ -f "${input_file}" ]; then

src/ZServer.API/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.IO.Compression;
5-
using System.Net.Http;
63
using System.Text;
74
using System.Threading.Tasks;
85
using Dapper;
9-
using Microsoft.AspNetCore.Authentication.JwtBearer;
10-
using Microsoft.AspNetCore.Authorization;
116
using Microsoft.AspNetCore.Builder;
127
using Microsoft.AspNetCore.Hosting;
138
using Microsoft.AspNetCore.Http;
@@ -22,21 +17,18 @@
2217
using NetTopologySuite.Geometries.Implementation;
2318
using NetTopologySuite.IO.Converters;
2419
using Orleans.Configuration;
25-
using Prometheus;
2620
using Serilog;
2721
using ZMap;
2822
using ZMap.DynamicCompiler;
2923
using ZMap.Infrastructure;
3024
using ZMap.Permission;
3125
// using ZMap.DynamicCompiler;
3226
using ZMap.Renderer.SkiaSharp.Utilities;
33-
using ZServer.API.Authentication;
3427
using ZServer.API.Features;
3528
using ZServer.API.Filters;
3629
using ZServer.API.Middlewares;
3730
using ZServer.API.Permission;
3831
using ZServer.Silo;
39-
using ILogger = Microsoft.Extensions.Logging.ILogger;
4032

4133
#if !DEBUG
4234
#endif
@@ -99,7 +91,6 @@ public static async Task Main(string[] args)
9991
app.MapSubscribeHandler();
10092
app.MapControllers()
10193
.RequireCors(CrosPolicy);
102-
app.MapMetrics();
10394
await app.RunAsync();
10495
}
10596

src/ZServer.API/WebApplicationBuilderExtensions.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,25 @@ public WebApplicationBuilder AddOtel(string serviceName)
8787
{
8888
var apiKey = builder.Configuration["OpenTelemetry:ApiKey"];
8989
var authorization = string.IsNullOrEmpty(apiKey) ? null : $"Authorization={apiKey}";
90-
// 1. 配置通用服务名称和资源属性(推荐方式)
91-
ResourceBuilder.CreateDefault()
92-
.AddService(
93-
serviceName: serviceName, // 替换为你的服务名
94-
serviceVersion: "1.0.0")
95-
.AddAttributes(new Dictionary<string, object>
96-
{
97-
["deployment.environment"] = builder.Environment.EnvironmentName
98-
});
99-
90+
var samplerProbability = builder.Configuration.GetSection("OpenTelemetry")
91+
.GetValue<double?>("SamplerProbability") ?? 0.5;
92+
var instanceId =
93+
$"{Environment.GetEnvironmentVariable("DAPR_HOST_IP") ?? Environment.GetEnvironmentVariable("HOST_IP")}:{Environment.GetEnvironmentVariable("DAPR_HTTP_PORT")}";
94+
instanceId = string.IsNullOrWhiteSpace(instanceId) ? null : instanceId;
95+
var @namespace = builder.Configuration["OpenTelemetry:Namespace"];
10096
// 2. 添加 OpenTelemetry 服务
10197
builder.Services.AddOpenTelemetry()
102-
// 配置资源,让所有信号(Trace/Metrics/Logs)共享
103-
.ConfigureResource(r => r.AddService(serviceName))
98+
.ConfigureResource(configure =>
99+
{
100+
configure.AddService(
101+
serviceName: serviceName, // 替换为你的服务名
102+
serviceVersion: "1.0.0", serviceInstanceId: instanceId, serviceNamespace: @namespace,
103+
autoGenerateServiceInstanceId: true)
104+
.AddAttributes(new Dictionary<string, object>
105+
{
106+
["deployment.environment"] = builder.Environment.EnvironmentName
107+
});
108+
})
104109
// 🔍 追踪(Traces)配置
105110
.WithTracing(tracing => tracing
106111
.AddAspNetCoreInstrumentation(options =>
@@ -110,13 +115,14 @@ public WebApplicationBuilder AddOtel(string serviceName)
110115
options.Filter = context => !context.Request.Path.StartsWithSegments("/health");
111116
})
112117
.AddHttpClientInstrumentation() // 追踪 HttpClient 调用
113-
.SetSampler(new ParentBasedSampler(new AlwaysOnSampler())) // 全量采样(生产可调整)
118+
.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(samplerProbability))) // 采样率
114119
.AddOtlpExporter(options =>
115120
{
116121
options.Endpoint = new Uri(otlpEndpoint);
117122
options.ExportProcessorType = ExportProcessorType.Batch; // 批量导出提升性能
118123
// 👇 关键:添加 Authorization Header
119124
options.Headers = authorization;
125+
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
120126
}))
121127
// 📊 指标(Metrics)配置
122128
.WithMetrics(metrics => metrics
@@ -129,6 +135,8 @@ public WebApplicationBuilder AddOtel(string serviceName)
129135
options.Endpoint = new Uri(otlpEndpoint);
130136
// 👇 关键:添加 Authorization Header
131137
options.Headers = authorization;
138+
options.ExportProcessorType = ExportProcessorType.Batch; // 批量导出提升性能
139+
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
132140
}))
133141
// 📝 日志(Logs)配置
134142
// 统一由 Serilog 管理

src/ZServer.API/ZServer.API.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0"/>
2121
<PackageReference Include="Serilog.Expressions" Version="5.0.0"/>
2222
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0"/>
23-
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.3.2" />
23+
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.3.2"/>
2424
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.2.0"/>
2525
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.119.2"/>
2626
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1"/>
27-
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.2" />
27+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.2"/>
2828
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.1"/>
29-
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.2" />
29+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.2"/>
3030
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.0"/>
3131
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.0"/>
3232
<PackageReference Include="Dapr.AspNetCore" Version="1.17.3"/>
33-
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1"/>
34-
<PackageReference Include="Dapr.AspNetCore" Version="1.17.3"/>
3533
</ItemGroup>
3634

3735
<ItemGroup>

src/ZServer.API/conf/appsettings.sample.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"appId": "",
1919
"appSecret": ""
2020
},
21+
"openTelemetry": {
22+
"endpoint": "",
23+
"apiKey": "",
24+
"samplerProbability": 0.5,
25+
"namespace": ""
26+
},
2127
"orleans": {
2228
"connectionString": "User ID=postgres;Password=;Host=192.168.100.254;Port=5432;Database=zserver_dev;Pooling=true;",
2329
"invariant": "Npgsql",

0 commit comments

Comments
 (0)