Skip to content

Commit

Permalink
feat: metrics and distributed tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Apr 7, 2024
1 parent 95535b7 commit 4d807c1
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 25 deletions.
33 changes: 33 additions & 0 deletions docs/pages/config/appsettings.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ Here is a complete example of configuration:
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Telemetry": {
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
"EndpointUri": "http://localhost:4317"
},
"AzureMonitor": {
"Enable": false,
"ConnectionString": "InstrumentationKey=12345678-abcd-abcd-abcd-12345678..."
},
"Console": {
"Enable": false
}
},
"EmailConfig": {
"SendMailAddress": "a@a.com",
"UserName": "",
Expand Down Expand Up @@ -126,6 +140,25 @@ GZCTF only supports PostgreSQL as the database, and does not support MySQL and o

</Callout>

### Telemetry

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

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

- **Enable**: Enable it or not.
- **Protocol**: `Grpc` or `HttpProtobuf`.
- **EndpointUri**: The endpoint to export.

- **AzureMonitor**: Exporting metrics and tracing data to ApplicationInsights.

- **Enable**: Enable it or not.
- **ConnectionString**: The connection string.

- **Console**: Exporting tracing data to console.

- **Enable**: Enable it or not.

### EmailConfig

Here we can configure the information of email sending server. If you use email registration and other email features, this is required.
Expand Down
33 changes: 33 additions & 0 deletions docs/pages/config/appsettings.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ import { Callout } from "nextra-theme-docs";
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Telemetry": {
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
"EndpointUri": "http://localhost:4317"
},
"AzureMonitor": {
"Enable": false,
"ConnectionString": "InstrumentationKey=12345678-abcd-abcd-abcd-12345678..."
},
"Console": {
"Enable": false
}
},
"EmailConfig": {
"SendMailAddress": "a@a.com",
"UserName": "",
Expand Down Expand Up @@ -126,6 +140,25 @@ GZCTFはデータベースとしてPostgreSQLのみをサポートしており

</Callout>

### Telemetry

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

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

- **Enable**: 有効にするかどうか。
- **Protocol**: `Grpc` または `HttpProtobuf`
- **EndpointUri**: エクスポートするエンドポイント。

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

- **Enable**: 有効にするかどうか。
- **ConnectionString**: 接続文字列。

- **Console**: トレーシングデータをコンソールにエクスポートします。

- **Enable**: 有効にするかどうか。

### EmailConfig

ここではメール送信に関する情報を設定します。メールでの登録やその他のメール機能を使用する場合、この項目は必須です。
Expand Down
34 changes: 34 additions & 0 deletions docs/pages/config/appsettings.zh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ import { Callout } from "nextra-theme-docs";
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Telemetry": {
"OpenTelemetry": {
"Enable": true,
"Protocol": "Grpc",
"EndpointUri": "http://localhost:4317"
},
"AzureMonitor": {
"Enable": false,
"ConnectionString": "InstrumentationKey=12345678-abcd-abcd-abcd-12345678..."
},
"Console": {
"Enable": false
}
},
"EmailConfig": {
"SendMailAddress": "a@a.com",
"UserName": "",
Expand Down Expand Up @@ -126,6 +140,26 @@ GZCTF 仅支持 PostgreSQL 作为数据库,不支持 MySQL 等其他数据库

</Callout>

### Telemetry

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

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

- **Enable**:是否启用。
- **Protocol**`Grpc``HttpProtobuf`
- **EndpointUri**:导出到的地址。

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

- **Enable**:是否启用。
- **ConnectionString**:连接字符串。

- **Console**:将追踪数据导出到控制台。

- **Enable**:是否启用。


### EmailConfig

此处配置邮件发送相关信息,若采用邮箱注册和其他相关邮件功能,此处为必填项。
Expand Down
38 changes: 14 additions & 24 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Aigamo.ResXGenerator" Version="4.2.0" />
<PackageVersion Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.1.0" />
<PackageVersion Include="Docker.DotNet" Version="3.125.15" />
<PackageVersion Include="KubernetesClient" Version="13.0.26" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Protocols.Json" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.3" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageVersion Include="Npgsql.OpenTelemetry" Version="8.0.2" />
<PackageVersion Include="NPOI" Version="2.7.0" />
<PackageVersion Include="NSwag.AspNetCore" Version="14.0.7" />
<PackageVersion Include="NSwag.MSBuild" Version="14.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="NSwag.MSBuild" Version="14.0.7" />
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="1.8.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.0" />
<PackageVersion Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.8.0-rc.1" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.8.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.8.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageVersion Include="Serilog.Expressions" Version="4.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="8.0.0" />
Expand All @@ -43,21 +45,9 @@
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="xunit.runner.console" Version="2.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7"/>
<PackageVersion Include="xunit.runner.console" Version="2.7.0"/>
<PackageVersion Include="coverlet.collector" Version="6.0.2"/>
<PackageVersion Include="coverlet.msbuild" Version="6.0.2"/>
</ItemGroup>
</Project>
10 changes: 9 additions & 1 deletion src/GZCTF/GZCTF.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down Expand Up @@ -28,6 +28,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" />
<PackageReference Include="Docker.DotNet" />
<PackageReference Include="KubernetesClient" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
Expand All @@ -39,12 +40,19 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Npgsql.OpenTelemetry" />
<PackageReference Include="NPOI" />
<PackageReference Include="NSwag.AspNetCore" />
<PackageReference Include="NSwag.MSBuild">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenTelemetry.Exporter.Console" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Expressions" />
<PackageReference Include="Serilog.Extensions.Logging" />
Expand Down
56 changes: 56 additions & 0 deletions src/GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.Reflection;
using System.Text;
using Azure.Monitor.OpenTelemetry.AspNetCore;
using GZCTF.Extensions;
using GZCTF.Hubs;
using GZCTF.Middlewares;
Expand All @@ -23,6 +24,12 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Npgsql;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
using StackExchange.Redis;

Expand Down Expand Up @@ -197,6 +204,53 @@

#endregion Identity

#region Telemetry

var telemetryOptions = builder.Configuration.GetSection("Telemetry");
var otel = builder.Services.AddOpenTelemetry();

otel.ConfigureResource(resource => resource.AddService("GZCTF"));

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.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation();
metrics.AddHttpClientInstrumentation();
metrics.AddPrometheusExporter();
});

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

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

#region Services and Repositories

builder.Services.AddTransient<IMailSender, MailSender>()
Expand Down Expand Up @@ -327,6 +381,8 @@
app.MapHub<MonitorHub>("/hub/monitor");
app.MapHub<AdminHub>("/hub/admin");

app.MapPrometheusScrapingEndpoint();

app.MapFallbackToFile("index.html");

#endregion Middlewares
Expand Down

0 comments on commit 4d807c1

Please sign in to comment.