From 0a9730e005db84ad1cd2108d62b20674b968e51c Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Thu, 11 Apr 2024 13:03:39 -0700 Subject: [PATCH] Align to latest prometheus version There are known bugs in 7.0, compatability issues across target fxs, as well as some strange/dangerous default behaviors This release aligns to 8.x for prometheus-net as well as .net 8 and chooses defaults that make sense for web apps --- .github/workflows/ci.yml | 6 +-- Spiffy.Monitoring.sln | 7 ++++ .../PrometheusConfigurationApi.cs | 24 ++++++++++-- .../PrometheusProvider.cs | 39 +++++++++++++------ .../Spiffy.Monitoring.Prometheus.csproj | 12 ++---- tests/TestConsoleApp/Program.cs | 8 +--- .../Properties/launchSettings.json | 2 +- tests/TestConsoleApp/TestConsoleApp.csproj | 2 +- tests/TestWebApp/Program.cs | 24 ++++++++++++ .../TestWebApp/Properties/launchSettings.json | 14 +++++++ tests/TestWebApp/TestWebApp.csproj | 15 +++++++ tests/UnitTests/UnitTests.csproj | 2 +- 12 files changed, 119 insertions(+), 36 deletions(-) create mode 100644 tests/TestWebApp/Program.cs create mode 100644 tests/TestWebApp/Properties/launchSettings.json create mode 100644 tests/TestWebApp/TestWebApp.csproj diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4f7a9c..ef5e0cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,11 +15,11 @@ jobs: Configuration: Release steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Build run: dotnet build diff --git a/Spiffy.Monitoring.sln b/Spiffy.Monitoring.sln index 2ffa916..71bc55b 100644 --- a/Spiffy.Monitoring.sln +++ b/Spiffy.Monitoring.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spiffy.Monitoring.Splunk", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spiffy.Monitoring.Aws", "src\Spiffy.Monitoring.Aws\Spiffy.Monitoring.Aws.csproj", "{E7E90469-1E8B-4204-9990-48CD0C42E481}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWebApp", "tests\TestWebApp\TestWebApp.csproj", "{4DF62688-C556-4439-93C1-2E4562E07F42}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -59,6 +61,10 @@ Global {E7E90469-1E8B-4204-9990-48CD0C42E481}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7E90469-1E8B-4204-9990-48CD0C42E481}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7E90469-1E8B-4204-9990-48CD0C42E481}.Release|Any CPU.Build.0 = Release|Any CPU + {4DF62688-C556-4439-93C1-2E4562E07F42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DF62688-C556-4439-93C1-2E4562E07F42}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DF62688-C556-4439-93C1-2E4562E07F42}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DF62688-C556-4439-93C1-2E4562E07F42}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -70,5 +76,6 @@ Global {1FE31255-E41A-4A76-B819-FA1E1AA4451F} = {8DEB13B2-A275-40CB-8D0F-AA672DB5717E} {9AD4DAE4-10DB-4933-9A28-6D0F994ADC17} = {8DEB13B2-A275-40CB-8D0F-AA672DB5717E} {E7E90469-1E8B-4204-9990-48CD0C42E481} = {8DEB13B2-A275-40CB-8D0F-AA672DB5717E} + {4DF62688-C556-4439-93C1-2E4562E07F42} = {2DF31750-1635-4C57-980C-23EA82B2DE53} EndGlobalSection EndGlobal diff --git a/src/Spiffy.Monitoring.Prometheus/PrometheusConfigurationApi.cs b/src/Spiffy.Monitoring.Prometheus/PrometheusConfigurationApi.cs index 03f0903..c860016 100644 --- a/src/Spiffy.Monitoring.Prometheus/PrometheusConfigurationApi.cs +++ b/src/Spiffy.Monitoring.Prometheus/PrometheusConfigurationApi.cs @@ -1,14 +1,30 @@ +using System; + namespace Spiffy.Monitoring.Prometheus { public class PrometheusConfigurationApi { -#if NET6_0_OR_GREATER + internal bool SuppressMeters { get; private set; } = true; + internal bool SuppressDebugMetrics { get; private set; } = true; + public PrometheusConfigurationApi() { - RuntimeStats = global::Prometheus.DotNetRuntime.DotNetRuntimeStatsBuilder.Customize(); } - public global::Prometheus.DotNetRuntime.DotNetRuntimeStatsBuilder.Builder RuntimeStats { get; private set; } -#endif + + [Obsolete("Prometheus.DotNetRuntime is poorly maintained and has been removed from options for this library. Consider removing, or otherwise move configuration to a different location", true)] + public object RuntimeStats { get ; } = null; + + public PrometheusConfigurationApi EnableMeters() + { + SuppressMeters = false; + return this; + } + + public PrometheusConfigurationApi EnableDebugMetrics() + { + SuppressDebugMetrics = false; + return this; + } } } diff --git a/src/Spiffy.Monitoring.Prometheus/PrometheusProvider.cs b/src/Spiffy.Monitoring.Prometheus/PrometheusProvider.cs index 250ea8d..982e386 100644 --- a/src/Spiffy.Monitoring.Prometheus/PrometheusProvider.cs +++ b/src/Spiffy.Monitoring.Prometheus/PrometheusProvider.cs @@ -1,13 +1,11 @@ using System; +using Prometheus; using Spiffy.Monitoring.Config; namespace Spiffy.Monitoring.Prometheus { public static class PrometheusProvider { - static IDisposable _previousCollector = null; - static readonly object _serializeCollectionAccess = new object(); - public static InitializationApi.ProvidersApi Prometheus(this InitializationApi.ProvidersApi api) { return Prometheus(api, null); @@ -15,18 +13,37 @@ public static InitializationApi.ProvidersApi Prometheus(this InitializationApi.P public static InitializationApi.ProvidersApi Prometheus(this InitializationApi.ProvidersApi api, Action customize) { + var config = new PrometheusConfigurationApi(); if (customize != null) { - var config = new PrometheusConfigurationApi(); customize(config); -#if NET6_0_OR_GREATER - lock (_serializeCollectionAccess) - { - _previousCollector?.Dispose(); - _previousCollector = config.RuntimeStats.StartCollecting(); - } -#endif } + var metricOptions = new SuppressDefaultMetricOptions + { + // these are small and useful; always include + // dotnet_* + // process_* + SuppressProcessMetrics = false, + + // these are small and useful; always include + // microsoft_aspnetcore_* + // system_runtime_* + // system_net_sockets_* + SuppressEventCounters = false, + + // these are not useful in most (all?) cases; omit by default + // prometheus_net_exemplars_* + // prometheus_net_metric_* + SuppressDebugMetrics = config.SuppressDebugMetrics, + + // these are insanely large (upwards of 10s of GBs); omit by default + // microsoft_aspnetcore_hosting_* + // microsoft_aspnetcore_routing_aspnetcore_routing_match_attempts + // microsoft_aspnetcore_server_kestrel_kestrel_connection_duration + // system_net_http_http_client_* + SuppressMeters = config.SuppressMeters + }; + Metrics.SuppressDefaultMetrics(metricOptions); api.Add("prometheus", PrometheusRules.Process); return api; } diff --git a/src/Spiffy.Monitoring.Prometheus/Spiffy.Monitoring.Prometheus.csproj b/src/Spiffy.Monitoring.Prometheus/Spiffy.Monitoring.Prometheus.csproj index 0690dfd..101e392 100644 --- a/src/Spiffy.Monitoring.Prometheus/Spiffy.Monitoring.Prometheus.csproj +++ b/src/Spiffy.Monitoring.Prometheus/Spiffy.Monitoring.Prometheus.csproj @@ -1,10 +1,10 @@  - 2020-2023 + 2020-2024 Chris Peterson The Prometheus provider for Spiffy.Monitoring - net6.0;netstandard2.0 + net8.0 Spiffy.Monitoring.Prometheus Spiffy.Monitoring.Prometheus monitoring;eventcontext;structured logging;metrics;prometheus;splunk @@ -12,17 +12,13 @@ README.md MIT True - 2.0.6 + 3.0.0 Spiffy.Monitoring.Prometheus - - - - - + diff --git a/tests/TestConsoleApp/Program.cs b/tests/TestConsoleApp/Program.cs index 522f6a0..5fccc1f 100755 --- a/tests/TestConsoleApp/Program.cs +++ b/tests/TestConsoleApp/Program.cs @@ -92,13 +92,7 @@ static void Main(string[] args) spiffy.Providers .Trace() .Console() - .Prometheus(cfg => - { - cfg.RuntimeStats - .WithGcStats() - //.WithXXX() - ; - }) + .Prometheus() .Splunk(cfg => {}) .NLog(cfg => {}); }); diff --git a/tests/TestConsoleApp/Properties/launchSettings.json b/tests/TestConsoleApp/Properties/launchSettings.json index 3998ae6..2dc97d4 100644 --- a/tests/TestConsoleApp/Properties/launchSettings.json +++ b/tests/TestConsoleApp/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "TestConsoleApp": { "commandName": "Project", - "commandLineArgs": "aws", + "commandLineArgs": "console", "environmentVariables": { "AWS_PROFILE": "xxx" } diff --git a/tests/TestConsoleApp/TestConsoleApp.csproj b/tests/TestConsoleApp/TestConsoleApp.csproj index 15cee16..600bf8a 100644 --- a/tests/TestConsoleApp/TestConsoleApp.csproj +++ b/tests/TestConsoleApp/TestConsoleApp.csproj @@ -5,7 +5,7 @@ - net7.0 + net8.0 portable exe diff --git a/tests/TestWebApp/Program.cs b/tests/TestWebApp/Program.cs new file mode 100644 index 0000000..90f225a --- /dev/null +++ b/tests/TestWebApp/Program.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Builder; +using Prometheus; +using Spiffy.Monitoring; +using Spiffy.Monitoring.Console; +using Spiffy.Monitoring.Prometheus; + +Configuration.Initialize(spiffy => +{ + spiffy.Providers + .Console() + .Prometheus(); +}); + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); +app.UseRouting(); + +app.MapGet("/", () => "Hello World!"); + +app.UseEndpoints(endpoints => { + endpoints.MapMetrics(); +}); + +app.Run(); diff --git a/tests/TestWebApp/Properties/launchSettings.json b/tests/TestWebApp/Properties/launchSettings.json new file mode 100644 index 0000000..08ba516 --- /dev/null +++ b/tests/TestWebApp/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5124", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/tests/TestWebApp/TestWebApp.csproj b/tests/TestWebApp/TestWebApp.csproj new file mode 100644 index 0000000..bcddc42 --- /dev/null +++ b/tests/TestWebApp/TestWebApp.csproj @@ -0,0 +1,15 @@ + + + + + + + + + + + + net8.0 + + + diff --git a/tests/UnitTests/UnitTests.csproj b/tests/UnitTests/UnitTests.csproj index d4efba3..dcfabcc 100644 --- a/tests/UnitTests/UnitTests.csproj +++ b/tests/UnitTests/UnitTests.csproj @@ -5,7 +5,7 @@ - net7.0 + net8.0 portable library