-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from prom-client-net/test/units
test: add units
- Loading branch information
Showing
9 changed files
with
412 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.