Skip to content

Commit

Permalink
feat: control enabling of telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Apr 8, 2024
1 parent 3060ca3 commit 93b00d8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
3 changes: 3 additions & 0 deletions docs/pages/config/appsettings.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Here is a complete example of configuration:
}
},
"Telemetry": {
"Enable": true,
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
Expand Down Expand Up @@ -144,6 +145,8 @@ GZCTF only supports PostgreSQL as the database, and does not support MySQL and o

GZCTF supports metrics and distributed tracing. You can configure the providers you want to use.

- **Enable**: Enable telemetry support or not.

- **OpenTelemetry**: Exporting metrics and tracing data to OpenTelemetry.

- **Enable**: Enable it or not.
Expand Down
3 changes: 3 additions & 0 deletions docs/pages/config/appsettings.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Callout } from "nextra-theme-docs";
}
},
"Telemetry": {
"Enable": true,
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
Expand Down Expand Up @@ -144,6 +145,8 @@ GZCTFはデータベースとしてPostgreSQLのみをサポートしており

GZCTFはメトリクスと分散トレーシングをサポートしています。使用するプロバイダーを設定できます。

- **Enable**: テレメトリーを有効化するかどうか。

- **OpenTelemetry**: メトリクスとトレーシングデータを OpenTelemetry にエクスポートします。

- **Enable**: 有効にするかどうか。
Expand Down
3 changes: 3 additions & 0 deletions docs/pages/config/appsettings.zh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Callout } from "nextra-theme-docs";
}
},
"Telemetry": {
"Enable": true,
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
Expand Down Expand Up @@ -144,6 +145,8 @@ GZCTF 仅支持 PostgreSQL 作为数据库,不支持 MySQL 等其他数据库

GZCTF 支持测量和分布式追踪。您可以配置您想要使用的提供商。

- **Enable**: 是否启用测量和追踪支持。

- **OpenTelemetry**:将测量和追踪数据导出到 OpenTelemetry。

- **Enable**:是否启用。
Expand Down
76 changes: 42 additions & 34 deletions src/GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,46 +207,51 @@
#region Telemetry

var telemetryOptions = builder.Configuration.GetSection("Telemetry");
var otel = builder.Services.AddOpenTelemetry();
var telemetryEnabled = telemetryOptions.Exists() && telemetryOptions.GetSection("Enable").Get<bool>();

otel.ConfigureResource(resource => resource.AddService("GZCTF"));
if (telemetryEnabled)
{
var otel = builder.Services.AddOpenTelemetry();

var azmoOptions = telemetryOptions.GetSection("AzureMonitor");
var azureMonitorEnabled = azmoOptions.Exists() && azmoOptions.GetSection("Enable").Get<bool>();
var otelOptions = telemetryOptions.GetSection("OpenTelemetry");
var otelEnabled = otelOptions.Exists() && otelOptions.GetSection("Enable").Get<bool>();
var consoleOptions = telemetryOptions.GetSection("Console");
var consoleEnabled = consoleOptions.Exists() && consoleOptions.GetSection("Enable").Get<bool>();
otel.ConfigureResource(resource => resource.AddService("GZCTF"));

otel.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation();
metrics.AddHttpClientInstrumentation();
metrics.AddPrometheusExporter();
});
var azmoOptions = telemetryOptions.GetSection("AzureMonitor");
var azureMonitorEnabled = azmoOptions.Exists() && azmoOptions.GetSection("Enable").Get<bool>();
var otelOptions = telemetryOptions.GetSection("OpenTelemetry");
var otelEnabled = otelOptions.Exists() && otelOptions.GetSection("Enable").Get<bool>();
var consoleOptions = telemetryOptions.GetSection("Console");
var consoleEnabled = consoleOptions.Exists() && consoleOptions.GetSection("Enable").Get<bool>();

otel.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation();
tracing.AddHttpClientInstrumentation();
tracing.AddNpgsql();
if (consoleEnabled)
otel.WithMetrics(metrics =>
{
tracing.AddConsoleExporter();
}
});
metrics.AddAspNetCoreInstrumentation();
metrics.AddHttpClientInstrumentation();
metrics.AddPrometheusExporter();
});

if (azureMonitorEnabled)
{
otel.UseAzureMonitor(
options => options.ConnectionString = azmoOptions.GetSection("ConnectionString").Get<string>());
}
otel.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation();
tracing.AddHttpClientInstrumentation();
tracing.AddNpgsql();
if (consoleEnabled)
{
tracing.AddConsoleExporter();
}
});

if (otelEnabled)
{
otel.UseOtlpExporter(
otelOptions.GetRequiredSection("Protocol").Get<OtlpExportProtocol>(),
new(otelOptions.GetRequiredSection("EndpointUri").Get<string>()!));
if (azureMonitorEnabled)
{
otel.UseAzureMonitor(
options => options.ConnectionString = azmoOptions.GetSection("ConnectionString").Get<string>());
}

if (otelEnabled)
{
otel.UseOtlpExporter(
otelOptions.GetRequiredSection("Protocol").Get<OtlpExportProtocol>(),
new(otelOptions.GetRequiredSection("EndpointUri").Get<string>()!));
}
}

#endregion
Expand Down Expand Up @@ -381,7 +386,10 @@
app.MapHub<MonitorHub>("/hub/monitor");
app.MapHub<AdminHub>("/hub/admin");

app.MapPrometheusScrapingEndpoint();
if (telemetryEnabled)
{
app.MapPrometheusScrapingEndpoint();
}

app.MapFallbackToFile("index.html");

Expand Down

0 comments on commit 93b00d8

Please sign in to comment.