diff --git a/src/AgileConfig.Server.Apisite/Startup.cs b/src/AgileConfig.Server.Apisite/Startup.cs index 313b2492..8e4bf18f 100644 --- a/src/AgileConfig.Server.Apisite/Startup.cs +++ b/src/AgileConfig.Server.Apisite/Startup.cs @@ -19,6 +19,7 @@ using AgileConfig.Server.Data.Repository.Selector; using AgileConfig.Server.Data.Abstraction; using AgileConfig.Server.Common.EventBus; +using OpenTelemetry.Resources; namespace AgileConfig.Server.Apisite { @@ -59,7 +60,6 @@ public IConfiguration Configuration // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddOtlpTraces(); services.AddDefaultHttpClient(IsTrustSSL(Configuration)); services.AddRestClient(); @@ -91,6 +91,10 @@ public void ConfigureServices(IServiceCollection services) services.AddOIDC(); + services.AddOpenTelemetry() + .ConfigureResource(resource => resource.AddService(Program.AppName)) + .AddOtlpTraces() + .AddOtlpMetrics(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/AgileConfig.Server.Apisite/StartupExtension.cs b/src/AgileConfig.Server.Apisite/StartupExtension.cs index b96958d4..cf10152b 100644 --- a/src/AgileConfig.Server.Apisite/StartupExtension.cs +++ b/src/AgileConfig.Server.Apisite/StartupExtension.cs @@ -6,6 +6,7 @@ using OpenTelemetry.Trace; using OpenTelemetry.Logs; using OpenTelemetry.Exporter; +using OpenTelemetry; namespace AgileConfig.Server.Apisite { @@ -21,55 +22,44 @@ public static void AddDefaultHttpClient(this IServiceCollection services, bool i ; } - public static void AddOtlpTraces(this IServiceCollection services) + public static IOpenTelemetryBuilder AddOtlpTraces(this IOpenTelemetryBuilder builder) { if (string.IsNullOrEmpty(Appsettings.OtlpTracesEndpoint)) { - return; + return builder; } - services.AddOpenTelemetry() - .ConfigureResource(resource => resource.AddService(Program.AppName)) - .WithTracing(tracing => tracing - .AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddOtlpExporter(op => - { - op.Protocol = Appsettings.OtlpTracesProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc; - op.Endpoint = new System.Uri(Appsettings.OtlpTracesEndpoint); - }) - ) - //.WithMetrics(metrics => metrics - // .AddRuntimeInstrumentation() - // .AddAspNetCoreInstrumentation() - // .AddOtlpExporter(op => - // { - // op.Protocol = OtlpExportProtocol.HttpProtobuf; - // op.Endpoint = new System.Uri(Global.Config["otlp:trace:endpoint"]); - // }) - // ) - ; + builder.WithTracing(tracing => tracing + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddOtlpExporter(op => + { + op.Protocol = Appsettings.OtlpTracesProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc; + op.Endpoint = new System.Uri(Appsettings.OtlpTracesEndpoint); + }) + ); + + return builder; } - public static void AddOtlpMetrics(this IServiceCollection services) + public static IOpenTelemetryBuilder AddOtlpMetrics(this IOpenTelemetryBuilder builder) { if (string.IsNullOrEmpty(Appsettings.OtlpMetricsEndpoint)) { - return; + return builder; } - services.AddOpenTelemetry() - .ConfigureResource(resource => resource.AddService(Program.AppName)) - .WithMetrics(metrics => metrics - .AddRuntimeInstrumentation() - .AddAspNetCoreInstrumentation() - .AddOtlpExporter(op => - { - op.Protocol = Appsettings.OtlpMetricsProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc; - op.Endpoint = new System.Uri(Appsettings.OtlpMetricsEndpoint); - }) - ) - ; + builder.WithMetrics(metrics => metrics + .AddAspNetCoreInstrumentation() + .AddRuntimeInstrumentation() + .AddOtlpExporter(op => + { + op.Protocol = Appsettings.OtlpMetricsProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc; + op.Endpoint = new System.Uri(Appsettings.OtlpMetricsEndpoint); + }) + ); + + return builder; } static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl) diff --git a/src/AgileConfig.Server.Apisite/appsettings.Development.json b/src/AgileConfig.Server.Apisite/appsettings.Development.json index 8ce21bfa..9615c585 100644 --- a/src/AgileConfig.Server.Apisite/appsettings.Development.json +++ b/src/AgileConfig.Server.Apisite/appsettings.Development.json @@ -18,7 +18,7 @@ }, "metrics": { "protocol": "http", // http grpc - "endpoint": "" // not supported yet + "endpoint": "http://192.168.0.201:14318" } }, "alwaysTrustSsl": true, // If true, the server will ignore SSL errors.