prometheus-net
44 строки · 1.2 Кб
1// This sample demonstrates how to integrate prometheus-net into a web app while instructing it
2// to export metrics on a dedicated port (e.g. so it can be firewalled off from the internet).
3//
4// NuGet packages required:
5// * prometheus-net.AspNetCore
6
7using Prometheus;8
9var builder = WebApplication.CreateBuilder(args);10
11builder.Services.AddRazorPages();12
13// Start the metrics exporter as a background service.
14// Open http://localhost:5167 to see the web app.
15// Open http://localhost:1234/metrics to see the metrics.
16//
17// Metrics published:
18// * built-in process metrics giving basic information about the .NET runtime (enabled by default)
19// * metrics from .NET Event Counters (enabled by default, updated every 10 seconds)
20// * metrics from .NET Meters (enabled by default)
21// * metrics about requests handled by the web app (configured below)
22builder.Services.AddMetricServer(options =>23{
24options.Port = 1234;25});26
27var app = builder.Build();28
29if (!app.Environment.IsDevelopment())30{
31app.UseExceptionHandler("/Error");32}
33app.UseStaticFiles();34
35app.UseRouting();36
37// Capture metrics about all received HTTP requests.
38app.UseHttpMetrics();39
40app.UseAuthorization();41
42app.MapRazorPages();43
44app.Run();45