Skip to content

Commit

Permalink
Merge pull request #41 from prom-client-net/test/units
Browse files Browse the repository at this point in the history
test: add units
  • Loading branch information
phnx47 authored Jun 5, 2023
2 parents 3b3de18 + 7c5914a commit 4c08f64
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 171 deletions.
36 changes: 6 additions & 30 deletions Prometheus.Client.AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D994EEE1-3786-4FDF-8A49-76D8B9819A29}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{FA0CD8D2-E9AE-4518-B989-17901C5928B0}"
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
.gitignore = .gitignore
LICENSE = LICENSE
README.md = README.md
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
.ruleset = .ruleset
stylecop.json = stylecop.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{255858DA-9CD1-49FD-BA65-010985E26F2A}"
ProjectSection(SolutionItems) = preProject
.github\CODEOWNERS = .github\CODEOWNERS
.github\FUNDING.yml = .github\FUNDING.yml
.github\renovate.json = .github\renovate.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{D3EB5CE6-053E-4CB3-A828-A987DA33BC3C}"
ProjectSection(SolutionItems) = preProject
.github\workflows\pr.yml = .github\workflows\pr.yml
.github\workflows\master.yml = .github\workflows\master.yml
.github\workflows\prod.yml = .github\workflows\prod.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prometheus.Client.AspNetCore", "src\Prometheus.Client.AspNetCore.csproj", "{9CEA986B-142E-4A8A-87B4-A6C2A5B7106B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prometheus.Client.AspNetCore.Tests", "tests\Prometheus.Client.AspNetCore.Tests.csproj", "{F0E6A705-D34D-4EF5-90BD-883DF236EAB6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -43,12 +17,14 @@ Global
{9CEA986B-142E-4A8A-87B4-A6C2A5B7106B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CEA986B-142E-4A8A-87B4-A6C2A5B7106B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CEA986B-142E-4A8A-87B4-A6C2A5B7106B}.Release|Any CPU.Build.0 = Release|Any CPU
{F0E6A705-D34D-4EF5-90BD-883DF236EAB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0E6A705-D34D-4EF5-90BD-883DF236EAB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0E6A705-D34D-4EF5-90BD-883DF236EAB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0E6A705-D34D-4EF5-90BD-883DF236EAB6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D3EB5CE6-053E-4CB3-A828-A987DA33BC3C} = {255858DA-9CD1-49FD-BA65-010985E26F2A}
{9CEA986B-142E-4A8A-87B4-A6C2A5B7106B} = {D994EEE1-3786-4FDF-8A49-76D8B9819A29}
EndGlobalSection
EndGlobal
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![ci](https://img.shields.io/github/actions/workflow/status/prom-client-net/prom-client-aspnetcore/ci.yml?branch=main&label=ci&logo=github&style=flat-square)](https://github.com/prom-client-net/prom-client-aspnetcore/actions/workflows/ci.yml)
[![nuget](https://img.shields.io/nuget/v/Prometheus.Client.AspNetCore?logo=nuget&style=flat-square)](https://www.nuget.org/packages/Prometheus.Client.AspNetCore)
[![nuget](https://img.shields.io/nuget/dt/Prometheus.Client.AspNetCore?logo=nuget&style=flat-square)](https://www.nuget.org/packages/Prometheus.Client.AspNetCore)
[![codecov](https://img.shields.io/codecov/c/github/prom-client-net/prom-client-aspnetcore?logo=codecov&style=flat-square)](https://app.codecov.io/gh/prom-client-net/prom-client-aspnetcore)
[![codefactor](https://img.shields.io/codefactor/grade/github/prom-client-net/prom-client-aspnetcore?logo=codefactor&style=flat-square)](https://www.codefactor.io/repository/github/prom-client-net/prom-client-aspnetcore)
[![license](https://img.shields.io/github/license/prom-client-net/prom-client-aspnetcore?style=flat-square)](https://github.com/prom-client-net/prom-client-aspnetcore/blob/main/LICENSE)

Expand All @@ -16,27 +17,24 @@ dotnet add package Prometheus.Client.AspNetCore

## Use

There are [Examples](https://github.com/prom-client-net/prom-examples)
[Examples](https://github.com/prom-client-net/prom-examples)

```c#

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
public void Configure(IApplicationBuilder app)
{
app.UsePrometheusServer();
}

```

```c#

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
public void Configure(IApplicationBuilder app)
{
app.UsePrometheusServer(q =>
{
q.MapPath = "/metrics1";
q.MapPath = "/prom";
q.MetricPrefixName = "my_app_";
});
}

```

## Contribute
Expand Down
72 changes: 72 additions & 0 deletions src/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Prometheus.Client.Collectors;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Prometheus.Client.AspNetCore;

/// <summary>
/// PrometheusExtensions
/// </summary>
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Add PrometheusServer request execution pipeline.
/// </summary>
public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder app)
{
return UsePrometheusServer(app, null);
}

/// <summary>
/// Add PrometheusServer request execution pipeline.
/// </summary>
public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder app, Action<PrometheusOptions> setupOptions)
{
if (app == null)
throw new ArgumentNullException(nameof(app));

var options = new PrometheusOptions
{
CollectorRegistryInstance = (ICollectorRegistry)app.ApplicationServices.GetService(typeof(ICollectorRegistry)) ?? Metrics.DefaultCollectorRegistry
};

setupOptions?.Invoke(options);

if (!options.MapPath.StartsWith("/"))
options.MapPath = "/" + options.MapPath;

if (options.UseDefaultCollectors)
{
#pragma warning disable CS0618
if (options.AddLegacyMetrics)
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName, options.AddLegacyMetrics);
else
options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName);
#pragma warning restore CS0618
}

var contentType = options.ResponseEncoding != null
? $"{Defaults.ContentType}; charset={options.ResponseEncoding.BodyName}"
: Defaults.ContentType;

void AddMetricsHandler(IApplicationBuilder coreapp)
{
coreapp.Run(async context =>
{
var response = context.Response;

response.ContentType = contentType;

using var outputStream = response.Body;
await ScrapeHandler.ProcessAsync(options.CollectorRegistryInstance, outputStream);
});
}

if (options.Port == null)
return app.Map(options.MapPath, AddMetricsHandler);

bool PortMatches(HttpContext context) => context.Connection.LocalPort == options.Port;
return app.Map(options.MapPath, cfg => cfg.MapWhen(PortMatches, AddMetricsHandler));
}
}
7 changes: 7 additions & 0 deletions src/Defaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Prometheus.Client.AspNetCore;

internal static class Defaults
{
internal const string MapPath = "/metrics";
internal const string ContentType = "text/plain; version=0.0.4";
}
11 changes: 4 additions & 7 deletions src/Prometheus.Client.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>ASP.NET Core middleware for the Prometheus.Client</Description>
<VersionPrefix>4.8.0</VersionPrefix>
<VersionPrefix>4.8.1</VersionPrefix>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<PackageId>Prometheus.Client.AspNetCore</PackageId>
<RepositoryUrl>https://github.com/prom-client-net/prom-client-aspnetcore</RepositoryUrl>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyOriginatorKeyFile>../Prometheus.Client.AspNetCore.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<None Include="../icon.png" Visible="false" Pack="true" PackagePath="" />
<None Include="../README.md" Visible="false" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Prometheus.Client" Version="[5.0.0,6.0.0)" />
</ItemGroup>
Expand All @@ -22,4 +16,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.5.1" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="$(MSBuildProjectName).Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001008be6f2b28dd255318050634aca9465e8d012e309e1d8165462810ca599355ecc0ec05f67fd8d0dac9dabe6df4636764262c742574fa17eb243862c3349521ecff57153ecd557059560a73b0c5ef3cf1587a763c30acdb9222decb309a9425e29b1ed402398d1312e87947e73e03282d2a66156d6ad2bbf7cf924e349a0e9d59d" />
</ItemGroup>
</Project>
77 changes: 0 additions & 77 deletions src/PrometheusExtensions.cs

This file was deleted.

97 changes: 48 additions & 49 deletions src/PrometheusOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,55 @@
using System.Text;
using Prometheus.Client.Collectors;

namespace Prometheus.Client.AspNetCore
namespace Prometheus.Client.AspNetCore;

/// <summary>
/// Options for Prometheus
/// </summary>
public class PrometheusOptions
{
/// <summary>
/// Options for Prometheus
/// Url, default = "/metrics"
/// </summary>
public string MapPath { get; set; } = Defaults.MapPath;

/// <summary>
/// When specified only allow access to metrics on this port, otherwise return 404
/// </summary>
public int? Port { get; set; }

/// <summary>
/// CollectorRegistry instance.
/// </summary>
public ICollectorRegistry CollectorRegistryInstance { get; set; }

/// <summary>
/// Use default collectors
/// </summary>
public bool UseDefaultCollectors { get; set; } = true;

/// <summary>
/// Charset of text response.
/// </summary>
public Encoding ResponseEncoding { get; set; }

/// <summary>
/// Metric prefix for Default collectors
/// </summary>
public string MetricPrefixName { get; set; } = string.Empty;

/// <summary>
/// Add legacy metrics to Default collectors
/// </summary>
public class PrometheusOptions
{
/// <summary>
/// Url, default = "/metrics"
/// </summary>
public string MapPath { get; set; } = "/metrics";

/// <summary>
/// When specified only allow access to metrics on this port, otherwise return 404
/// </summary>
public int? Port { get; set; }

/// <summary>
/// CollectorRegistry instance.
/// </summary>
public ICollectorRegistry CollectorRegistryInstance { get; set; }

/// <summary>
/// Use default collectors
/// </summary>
public bool UseDefaultCollectors { get; set; } = true;

/// <summary>
/// Charset of text response.
/// </summary>
public Encoding ResponseEncoding { get; set; }

/// <summary>
/// Metric prefix for Default collectors
/// </summary>
public string MetricPrefixName { get; set; } = "";

/// <summary>
/// Add legacy metrics to Default collectors
/// </summary>
/// <remarks>
/// Some metrics renamed since v5, <c>AddLegacyMetrics</c> will add old and new name<br />
/// <para>
/// process_virtual_bytes -> process_virtual_memory_bytes<br />
/// process_private_bytes -> process_private_memory_bytes<br />
/// process_working_set -> process_working_set_bytes<br />
/// dotnet_totalmemory -> dotnet_total_memory_bytes
/// </para>
/// </remarks>
[Obsolete("'AddLegacyMetrics' will be removed in future versions")]
public bool AddLegacyMetrics { get; set; }
}
/// <remarks>
/// Some metrics renamed since v5, <c>AddLegacyMetrics</c> will add old and new name<br />
/// <para>
/// process_virtual_bytes -> process_virtual_memory_bytes<br />
/// process_private_bytes -> process_private_memory_bytes<br />
/// process_working_set -> process_working_set_bytes<br />
/// dotnet_totalmemory -> dotnet_total_memory_bytes
/// </para>
/// </remarks>
[Obsolete("'AddLegacyMetrics' will be removed in future versions")]
public bool AddLegacyMetrics { get; set; }
}
Loading

0 comments on commit 4c08f64

Please sign in to comment.