Skip to content

Commit

Permalink
Support otlp traces and metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 17, 2024
1 parent 41f7a2d commit 6887dc7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 26 deletions.
9 changes: 9 additions & 0 deletions src/AgileConfig.Server.Apisite/Appsettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,14 @@ public class Appsettings
public static string OtlpLogsEndpoint => Global.Config["otlp:logs:endpoint"];

public static string OtlpLogsProtocol => Global.Config["otlp:logs:protocol"];

public static string OtlpTracesEndpoint => Global.Config["otlp:traces:endpoint"];

public static string OtlpTracesProtocol => Global.Config["otlp:traces:protocol"];

public static string OtlpMetricsEndpoint => Global.Config["otlp:metrics:endpoint"];

public static string OtlpMetricsProtocol => Global.Config["otlp:metrics:protocol"];

}
}
10 changes: 1 addition & 9 deletions src/AgileConfig.Server.Apisite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ private static void AddOtlpLogging(ILoggingBuilder builder)
options
.AddOtlpExporter(expOp =>
{
if (Appsettings.OtlpLogsProtocol == "grpc")
{
expOp.Protocol = OtlpExportProtocol.Grpc;
}
else
{
expOp.Protocol = OtlpExportProtocol.HttpProtobuf;
}

expOp.Protocol = Appsettings.OtlpLogsProtocol == "http" ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc;
expOp.Endpoint = new Uri(Appsettings.OtlpLogsEndpoint);
})
;
Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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.AddOtlp();
services.AddOtlpTraces();
services.AddDefaultHttpClient(IsTrustSSL(Configuration));
services.AddRestClient();

Expand Down
50 changes: 38 additions & 12 deletions src/AgileConfig.Server.Apisite/StartupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ public static void AddDefaultHttpClient(this IServiceCollection services, bool i
;
}

public static void AddOtlp(this IServiceCollection services)
public static void AddOtlpTraces(this IServiceCollection services)
{
//services.AddOpenTelemetry()
// .ConfigureResource(resource => resource.AddService(Program.AppName))
// .WithTracing(tracing => tracing
// .AddAspNetCoreInstrumentation()
// .AddHttpClientInstrumentation()
// .AddOtlpExporter(op =>
// {
// op.Protocol = OtlpExportProtocol.HttpProtobuf;
// op.Endpoint = new System.Uri(Global.Config["otlp:traces:endpoint"]);
// })
// )
if (string.IsNullOrEmpty(Appsettings.OtlpTracesEndpoint))
{
return;
}

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()
Expand All @@ -46,6 +51,27 @@ public static void AddOtlp(this IServiceCollection services)
;
}

public static void AddOtlpMetrics(this IServiceCollection services)
{
if (string.IsNullOrEmpty(Appsettings.OtlpMetricsEndpoint))
{
return;
}

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);
})
)
;
}

static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl)
{
var handler = new HttpClientHandler();
Expand Down
6 changes: 4 additions & 2 deletions src/AgileConfig.Server.Apisite/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/logs"
},
"traces": {
"endpoint": "http://" // 暂时不支持
"protocol": "http", // http grpc
"endpoint": "http://192.168.0.201:5341/ingest/otlp/v1/traces"
},
"metrics": {
"endpoint": "http://" // 暂时不支持
"protocol": "http", // http grpc
"endpoint": "" // not supported yet
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
Expand Down
6 changes: 4 additions & 2 deletions src/AgileConfig.Server.Apisite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"endpoint": ""
},
"traces": {
"endpoint": "" // 暂时不支持
"protocol": "http", // http grpc
"endpoint": ""
},
"metrics": {
"endpoint": "" // 暂时不支持
"protocol": "http", // http grpc
"endpoint": "" // not supported yet
}
},
"alwaysTrustSsl": true, // If true, the server will ignore SSL errors.
Expand Down

0 comments on commit 6887dc7

Please sign in to comment.