Skip to content

Commit

Permalink
Update otlp metric
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 17, 2024
1 parent 0fd026f commit 0428d6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
6 changes: 5 additions & 1 deletion src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.
Expand Down
64 changes: 27 additions & 37 deletions src/AgileConfig.Server.Apisite/StartupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using OpenTelemetry.Trace;
using OpenTelemetry.Logs;
using OpenTelemetry.Exporter;
using OpenTelemetry;

namespace AgileConfig.Server.Apisite
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0428d6a

Please sign in to comment.