Skip to content

Commit

Permalink
Add Metrics (OpenTelemetry with Prometheus Export endpoint) (#2010)
Browse files Browse the repository at this point in the history
* add metrics in opentelemetry standard, uses built-in aspnetcore metrics, built-in npgsql metrics, and exports in prometheus format

* make metrics enabled by default, but disableable in appsettings

* link to metrics on diagnostics page if enabled

* use uppercase convention /Metrics instead of /metrics

* use a service extension method to follow conventions
  • Loading branch information
Masterjun3 authored Oct 18, 2024
1 parent 47d7fe3 commit 0329372
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<PackageVersion Include="Namotion.Reflection" Version="3.1.1" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="RoslynCodeTaskFactory" Version="2.0.7" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageVersion Include="SharpCompress" Version="0.36.0" />
Expand Down
2 changes: 2 additions & 0 deletions TASVideos.Core/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class AppSettings

public ReCaptchaSettings ReCaptcha { get; set; } = new();

public bool EnableMetrics { get; set; }

// User is only allowed to submit X submissions in Y days
public class SubmissionRateLimit
{
Expand Down
8 changes: 8 additions & 0 deletions TASVideos/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public static IApplicationBuilder UseMvcWithOptions(this IApplicationBuilder app
return app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();

if (settings.EnableMetrics)
{
endpoints.MapPrometheusScrapingEndpoint().RequireAuthorization(builder =>
{
builder.RequireClaim(CustomClaimTypes.Permission, ((int)PermissionTo.SeeDiagnostics).ToString());
});
}
});
}
}
27 changes: 27 additions & 0 deletions TASVideos/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.ResponseCompression;
using OpenTelemetry.Metrics;
using TASVideos.Core.Settings;
using TASVideos.TagHelpers;

Expand Down Expand Up @@ -161,4 +162,30 @@ private static IServiceCollection AddHttpContext(this IServiceCollection service
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
.AddTransient(provider => provider.GetRequiredService<IHttpContextAccessor>().HttpContext!.User);
}

public static IServiceCollection AddMetrics(this IServiceCollection services, AppSettings settings)
{
if (settings.EnableMetrics)
{
services
.AddOpenTelemetry()
.WithMetrics(builder =>
{
builder.AddMeter(
"Microsoft.AspNetCore.Hosting",
"Microsoft.AspNetCore.Server.Kestrel",
"Microsoft.AspNetCore.Routing",
"Microsoft.AspNetCore.Diagnostics");

builder.AddMeter("Npgsql");

builder.AddPrometheusExporter(options =>
{
options.ScrapeEndpointPath = "/Metrics";
});
});
}

return services;
}
}
2 changes: 2 additions & 0 deletions TASVideos/Pages/Diagnostics/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<li><a asp-page="Logging">Logging</a></li>
<li><a asp-page="SendEmail">Send an Email</a></li>
<li><a asp-page="SystemPages">System Wiki Pages</a></li>
<li condition="@Settings.EnableMetrics"><a href="/Metrics">Metrics</a></li>
<li condition="[email protected]">Metrics (disabled)</li>
</ul>

<standard-table>
Expand Down
3 changes: 2 additions & 1 deletion TASVideos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
pipeline.AddScssBundle("/css/site.css", "/css/site.scss");
pipeline.AddScssBundle("/css/forum.css", "/css/forum.scss");
pipeline.AddFiles("text/javascript", "/js/*");
});
})
.AddMetrics(settings);

builder.Host.UseSerilog();

Expand Down
2 changes: 2 additions & 0 deletions TASVideos/TASVideos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" />
<PackageReference Include="Namotion.Reflection" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="RoslynCodeTaskFactory" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion TASVideos/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"webOptimizer": {
"enableCaching": true,
"enableTagHelperBundling": true
}
},
"EnableMetrics": true
}

0 comments on commit 0329372

Please sign in to comment.