Skip to content

Commit

Permalink
Align to latest prometheus version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chris-peterson committed Apr 24, 2024
1 parent 9fade3c commit 88c19b9
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 36 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions Spiffy.Monitoring.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
24 changes: 20 additions & 4 deletions src/Spiffy.Monitoring.Prometheus/PrometheusConfigurationApi.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
39 changes: 28 additions & 11 deletions src/Spiffy.Monitoring.Prometheus/PrometheusProvider.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
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);
}

public static InitializationApi.ProvidersApi Prometheus(this InitializationApi.ProvidersApi api, Action<PrometheusConfigurationApi> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2020-2023</Copyright>
<Copyright>2020-2024</Copyright>
<Authors>Chris Peterson</Authors>
<Description>The Prometheus provider for Spiffy.Monitoring</Description>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Spiffy.Monitoring.Prometheus</AssemblyName>
<PackageId>Spiffy.Monitoring.Prometheus</PackageId>
<PackageTags>monitoring;eventcontext;structured logging;metrics;prometheus;splunk</PackageTags>
<PackageProjectUrl>http://github.com/chris-peterson/spiffy#overview</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.0.6</Version>
<Version>3.0.0</Version>
<RootNamespace>Spiffy.Monitoring.Prometheus</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Spiffy.Monitoring\Spiffy.Monitoring.csproj" />
<PackageReference Include="prometheus-net" Version="6.0.*" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' " >
<PackageReference Include="prometheus-net.DotNetRuntime" Version="4.4.*" />
<PackageReference Include="prometheus-net" Version="8.2.*" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 1 addition & 7 deletions tests/TestConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ static void Main(string[] args)
spiffy.Providers
.Trace()
.Console()
.Prometheus(cfg =>
{
cfg.RuntimeStats
.WithGcStats()
//.WithXXX()
;
})
.Prometheus()
.Splunk(cfg => {})
.NLog(cfg => {});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/TestConsoleApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"TestConsoleApp": {
"commandName": "Project",
"commandLineArgs": "aws",
"commandLineArgs": "console",
"environmentVariables": {
"AWS_PROFILE": "xxx"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestConsoleApp/TestConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DebugType>portable</DebugType>
<OutputTypeEx>exe</OutputTypeEx>
</PropertyGroup>
Expand Down
24 changes: 24 additions & 0 deletions tests/TestWebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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 => {

Check warning on line 20 in tests/TestWebApp/Program.cs

View workflow job for this annotation

GitHub Actions / build

Suggest using top level route registrations instead of UseEndpoints (https://aka.ms/aspnet/analyzers)

Check warning on line 20 in tests/TestWebApp/Program.cs

View workflow job for this annotation

GitHub Actions / build

Suggest using top level route registrations instead of UseEndpoints (https://aka.ms/aspnet/analyzers)
endpoints.MapMetrics();
});

app.Run();
14 changes: 14 additions & 0 deletions tests/TestWebApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
15 changes: 15 additions & 0 deletions tests/TestWebApp/TestWebApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<ProjectReference Include="..\..\src\Spiffy.Monitoring.Prometheus\Spiffy.Monitoring.Prometheus.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.*" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DebugType>portable</DebugType>
<ApplicationIcon />
<OutputTypeEx>library</OutputTypeEx>
Expand Down

0 comments on commit 88c19b9

Please sign in to comment.