-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.AspNetCore.Http; | ||
using Prometheus; | ||
using System.Threading.Tasks; | ||
using Grpc.AspNetCore.Server; | ||
using Grpc.Core; | ||
|
||
namespace Benchmark.NetCore | ||
{ | ||
[MemoryDiagnoser] | ||
public class GrpcExporterBenchmarks | ||
{ | ||
private CollectorRegistry _registry; | ||
private MetricFactory _factory; | ||
private GrpcRequestCountMiddleware _countMiddleware; | ||
private GrpcRequestDurationMiddleware _durationMiddleware; | ||
private DefaultHttpContext _ctx; | ||
|
||
[Params(1000, 10000)] | ||
public int RequestCount { get; set; } | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_ctx = new DefaultHttpContext(); | ||
_ctx.SetEndpoint(new Endpoint( | ||
ctx => Task.CompletedTask, | ||
new EndpointMetadataCollection(new GrpcMethodMetadata(typeof(int), | ||
new Method<object, object>(MethodType.Unary, | ||
"test", | ||
"test", | ||
new Marshaller<object>(o => new byte[0], c => null), | ||
new Marshaller<object>(o => new byte[0], c => null)))), | ||
"test")); | ||
_registry = Metrics.NewCustomRegistry(); | ||
_factory = Metrics.WithCustomRegistry(_registry); | ||
|
||
_countMiddleware = new GrpcRequestCountMiddleware(next => Task.CompletedTask, new GrpcRequestCountOptions | ||
{ | ||
Counter = _factory.CreateCounter("count", "help") | ||
}); | ||
_durationMiddleware = new GrpcRequestDurationMiddleware(next => Task.CompletedTask, new GrpcRequestDurationOptions | ||
{ | ||
Histogram = _factory.CreateHistogram("duration", "help") | ||
}); | ||
} | ||
|
||
[Benchmark] | ||
public async Task GrpcRequestCount() | ||
{ | ||
for (var i = 0; i < RequestCount; i++) | ||
await _countMiddleware.Invoke(_ctx); | ||
} | ||
|
||
[Benchmark] | ||
public async Task GrpcRequestDuration() | ||
{ | ||
for (var i = 0; i < RequestCount; i++) | ||
await _durationMiddleware.Invoke(_ctx); | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
Prometheus.AspNetCore.Grpc/GrpcRequestDurationMiddleware.cs
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,50 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Prometheus.HttpMetrics; | ||
|
||
namespace Prometheus | ||
{ | ||
internal sealed class GrpcRequestDurationMiddleware : GrpcRequestMiddlewareBase<ICollector<IHistogram>, IHistogram> | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public GrpcRequestDurationMiddleware(RequestDelegate next, GrpcRequestDurationOptions? options) | ||
: base(options, options?.Histogram) | ||
{ | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
var stopWatch = Stopwatch.StartNew(); | ||
|
||
// We need to write this out in long form instead of using a timer because routing data in | ||
// ASP.NET Core 2 is only available *after* executing the next request delegate. | ||
// So we would not have the right labels if we tried to create the child early on. | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
finally | ||
{ | ||
stopWatch.Stop(); | ||
|
||
CreateChild(context)?.Observe(stopWatch.Elapsed.TotalSeconds); | ||
} | ||
} | ||
|
||
protected override string[] DefaultLabels => GrpcRequestLabelNames.NoStatusSpecific; | ||
|
||
protected override ICollector<IHistogram> CreateMetricInstance(string[] labelNames) => MetricFactory.CreateHistogram( | ||
"grpc_request_duration_seconds", | ||
"The duration of gRPC requests processed by an ASP.NET Core application.", | ||
new HistogramConfiguration | ||
{ | ||
// 1 ms to 32K ms buckets | ||
Buckets = Histogram.ExponentialBuckets(0.001, 2, 16), | ||
LabelNames = labelNames | ||
}); | ||
} | ||
} |
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,10 @@ | ||
namespace Prometheus | ||
{ | ||
public sealed class GrpcRequestDurationOptions : GrpcMetricsOptionsBase | ||
{ | ||
/// <summary> | ||
/// Set this to use a custom metric instead of the default. | ||
/// </summary> | ||
public ICollector<IHistogram>? Histogram { get; set; } | ||
} | ||
} |
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,28 @@ | ||
using System.Linq; | ||
using Grpc.Core; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Prometheus | ||
{ | ||
internal static class HttpResponseExtensions | ||
{ | ||
private const string _grpcStatus = "grpc-status"; | ||
|
||
public static StatusCode GetStatusCode(this HttpResponse response) | ||
{ | ||
var headerExists = response.Headers.TryGetValue(_grpcStatus, out var header); | ||
|
||
if (!headerExists && response.StatusCode == StatusCodes.Status200OK) | ||
{ | ||
return StatusCode.OK; | ||
} | ||
|
||
if (header.Any() && int.TryParse(header.FirstOrDefault(), out var status)) | ||
{ | ||
return (StatusCode)status; | ||
} | ||
|
||
return StatusCode.OK; | ||
} | ||
} | ||
} |