|
18 | 18 | using GameFrameX.Utility;
|
19 | 19 | using GameFrameX.Utility.Extensions;
|
20 | 20 | using GameFrameX.Utility.Setting;
|
| 21 | +using Grafana.OpenTelemetry; |
21 | 22 | using Microsoft.AspNetCore.Builder;
|
22 | 23 | using Microsoft.AspNetCore.Diagnostics;
|
23 | 24 | using Microsoft.AspNetCore.Hosting;
|
|
27 | 28 | using Microsoft.Extensions.Hosting;
|
28 | 29 | using Microsoft.Extensions.Logging;
|
29 | 30 | using Microsoft.OpenApi.Models;
|
| 31 | +using OpenTelemetry.Metrics; |
30 | 32 | using OpenTelemetry.Resources;
|
31 | 33 | using OpenTelemetry.Trace;
|
32 | 34 | using Serilog;
|
33 | 35 | using CloseReason = GameFrameX.SuperSocket.WebSocket.CloseReason;
|
| 36 | +using Sdk = OpenTelemetry.Sdk; |
34 | 37 |
|
35 | 38 | namespace GameFrameX.StartUp;
|
36 | 39 |
|
@@ -257,28 +260,67 @@ private async Task StartServer<TMessageDecoderHandler>(List<BaseHttpHandler> bas
|
257 | 260 | // await StartHttpServerAsync(hostBuilder,baseHandler, httpFactory, aopHandlerTypes, minimumLevelLogLevel);
|
258 | 261 | await StartHttpServer(baseHandler, httpFactory, aopHandlerTypes, minimumLevelLogLevel);
|
259 | 262 |
|
260 |
| - // 配置日志 |
261 |
| - multipleServerHostBuilder.ConfigureLogging(logging => |
262 |
| - { |
263 |
| - logging.ClearProviders(); |
264 |
| - logging.AddSerilog(Log.Logger, true); |
265 |
| - logging.SetMinimumLevel(minimumLevelLogLevel); |
266 |
| - }); |
267 |
| - |
268 | 263 | // 配置监控和跟踪
|
269 | 264 | multipleServerHostBuilder.ConfigureServices(services =>
|
270 | 265 | {
|
271 | 266 | services.AddOpenTelemetry()
|
272 | 267 | .ConfigureResource(configure => { configure.AddService(Setting.ServerName + "-" + Setting.TagName, "GameFrameX").AddTelemetrySdk(); })
|
| 268 | + .WithMetrics(configure => |
| 269 | + { |
| 270 | + configure.AddAspNetCoreInstrumentation(); |
| 271 | + if (EnvironmentHelper.IsDevelopment()) |
| 272 | + { |
| 273 | + configure.AddConsoleExporter(); |
| 274 | + } |
| 275 | + |
| 276 | + // Metrics provides by ASP.NET Core in .NET 8 |
| 277 | + configure.AddMeter("Microsoft.AspNetCore.Hosting"); |
| 278 | + configure.AddMeter("Microsoft.AspNetCore.Server.Kestrel"); |
| 279 | + // Metrics provided by System.Net libraries |
| 280 | + configure.AddMeter("System.Net.Http"); |
| 281 | + configure.AddMeter("System.Net.NameResolution"); |
| 282 | + configure.AddPrometheusExporter(); |
| 283 | + }) |
273 | 284 | .WithTracing(configure =>
|
274 | 285 | {
|
275 | 286 | configure.AddAspNetCoreInstrumentation();
|
276 |
| - configure.AddConsoleExporter(); |
277 |
| - }); |
| 287 | + configure.AddHttpClientInstrumentation(); |
| 288 | + configure.AddSource("GameFrameX." + Setting.ServerName + "." + Setting.TagName); |
| 289 | + |
| 290 | + // if (tracingOtlpEndpoint != null) |
| 291 | + // { |
| 292 | + // tracing.AddOtlpExporter(otlpOptions => { otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); }); |
| 293 | + // } |
| 294 | + // else |
| 295 | + { |
| 296 | + if (EnvironmentHelper.IsDevelopment()) |
| 297 | + { |
| 298 | + configure.AddConsoleExporter(); |
| 299 | + } |
| 300 | + } |
| 301 | + }).UseGrafana(); |
| 302 | + }); |
| 303 | + // 配置日志 |
| 304 | + multipleServerHostBuilder.ConfigureLogging(logging => |
| 305 | + { |
| 306 | + logging.ClearProviders(); |
| 307 | + logging.AddSerilog(Log.Logger, true); |
| 308 | + logging.SetMinimumLevel(minimumLevelLogLevel); |
| 309 | + logging.AddOpenTelemetry(configure => { configure.UseGrafana(); }); |
278 | 310 | });
|
279 | 311 |
|
| 312 | + using var tracerProvider = Sdk.CreateTracerProviderBuilder() |
| 313 | + .UseGrafana(config => |
| 314 | + { |
| 315 | + config.ServiceName = Setting.ServerName + "-" + Setting.TagName; |
| 316 | + config.ServiceVersion = Assembly.GetCallingAssembly().ImageRuntimeVersion; |
| 317 | + config.ServiceInstanceId = Setting.ServerId.ToString(); |
| 318 | + config.DeploymentEnvironment = EnvironmentHelper.GetEnvironmentName().IsNullOrEmpty() ? Setting.IsDebug ? "Debug" : "Release" : EnvironmentHelper.GetEnvironmentName(); |
| 319 | + }) |
| 320 | + .Build(); |
280 | 321 | // 构建并启动服务器
|
281 | 322 | _gameServer = multipleServerHostBuilder.Build();
|
| 323 | + |
282 | 324 | await _gameServer.StartAsync();
|
283 | 325 | }
|
284 | 326 |
|
|
0 commit comments