diff --git a/src/ZB.MOM.WW.OtOpcUa.Admin/Program.cs b/src/ZB.MOM.WW.OtOpcUa.Admin/Program.cs index 0089e24..135f6b9 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Admin/Program.cs +++ b/src/ZB.MOM.WW.OtOpcUa.Admin/Program.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; +using OpenTelemetry.Metrics; using Serilog; using ZB.MOM.WW.OtOpcUa.Admin.Components; using ZB.MOM.WW.OtOpcUa.Admin.Hubs; @@ -70,6 +71,19 @@ builder.Services.AddScoped(); // SignalR real-time fleet status + alerts (admin-ui.md §"Real-Time Updates"). builder.Services.AddHostedService(); +// OpenTelemetry Prometheus exporter — Meter stream from RedundancyMetrics + any future +// Admin-side instrumentation lands on the /metrics endpoint Prometheus scrapes. Pull-based +// means no OTel Collector deployment required for the common deploy-in-a-K8s case; appsettings +// Metrics:Prometheus:Enabled=false disables the endpoint entirely for locked-down deployments. +var metricsEnabled = builder.Configuration.GetValue("Metrics:Prometheus:Enabled", true); +if (metricsEnabled) +{ + builder.Services.AddOpenTelemetry() + .WithMetrics(m => m + .AddMeter(RedundancyMetrics.MeterName) + .AddPrometheusExporter()); +} + var app = builder.Build(); app.UseSerilogRequestLogging(); @@ -87,6 +101,15 @@ app.MapPost("/auth/logout", async (HttpContext ctx) => app.MapHub("/hubs/fleet"); app.MapHub("/hubs/alerts"); +if (metricsEnabled) +{ + // Prometheus scrape endpoint — expose instrumentation registered in the OTel MeterProvider + // above. Emits text-format metrics at /metrics; auth is intentionally NOT required (Prometheus + // scrape jobs typically run on a trusted network). Operators who need auth put the endpoint + // behind a reverse-proxy basic-auth gate per fleet-ops convention. + app.MapPrometheusScrapingEndpoint(); +} + app.MapRazorComponents().AddInteractiveServerRenderMode(); await app.RunAsync(); diff --git a/src/ZB.MOM.WW.OtOpcUa.Admin/ZB.MOM.WW.OtOpcUa.Admin.csproj b/src/ZB.MOM.WW.OtOpcUa.Admin/ZB.MOM.WW.OtOpcUa.Admin.csproj index 1fa7aa4..3a322a8 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Admin/ZB.MOM.WW.OtOpcUa.Admin.csproj +++ b/src/ZB.MOM.WW.OtOpcUa.Admin/ZB.MOM.WW.OtOpcUa.Admin.csproj @@ -16,6 +16,8 @@ + + diff --git a/src/ZB.MOM.WW.OtOpcUa.Admin/appsettings.json b/src/ZB.MOM.WW.OtOpcUa.Admin/appsettings.json index 24d73fd..cbb842a 100644 --- a/src/ZB.MOM.WW.OtOpcUa.Admin/appsettings.json +++ b/src/ZB.MOM.WW.OtOpcUa.Admin/appsettings.json @@ -23,5 +23,10 @@ }, "Serilog": { "MinimumLevel": "Information" + }, + "Metrics": { + "Prometheus": { + "Enabled": true + } } }