Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Added grpc request duration metric #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 Prometheus.AspNetCore.Grpc/GrpcMetricsMiddlewareExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static IApplicationBuilder UseGrpcMetrics(this IApplicationBuilder app,
app.UseMiddleware<GrpcRequestCountMiddleware>(options.RequestCount);
}

if (options.RequestDuration.Enabled)
{
app.UseMiddleware<GrpcRequestDurationMiddleware>(options.RequestDuration);
}

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public sealed class GrpcMiddlewareExporterOptions
{
public GrpcRequestCountOptions RequestCount { get; set; } = new GrpcRequestCountOptions();
public GrpcRequestDurationOptions RequestDuration { get; set; } = new GrpcRequestDurationOptions();
}
47 changes: 47 additions & 0 deletions 47 Prometheus.AspNetCore.Grpc/GrpcRequestDurationMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Prometheus
{
internal sealed class GrpcRequestDurationMiddleware: GrpcRequestMiddlewareBase<ICollector<IHistogram>, IHistogram>
{
private readonly RequestDelegate _next;

protected override string[] DefaultLabels => GrpcRequestLabelNames.All;

public GrpcRequestDurationMiddleware(RequestDelegate next, GrpcRequestDurationOptions? options) : base(options, options?.Histogram)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
var stopWatch = ValueStopwatch.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
{
CreateChild(context)?.Observe(stopWatch.GetElapsedTime().TotalSeconds);
}
}

protected override ICollector<IHistogram> CreateMetricInstance(string[] labelNames)
{
return 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
});
}
}
}
11 changes: 11 additions & 0 deletions 11 Prometheus.AspNetCore.Grpc/GrpcRequestDurationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Prometheus
{
public sealed class GrpcRequestDurationOptions: GrpcMetricsOptionsBase
{
public ICollector<IHistogram>? Histogram { get; set; }
}
}
11 changes: 4 additions & 7 deletions 11 Prometheus.AspNetCore.Grpc/Prometheus.AspNetCore.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<!-- NuGet packaging config -->
<PackageId>prometheus-net.AspNetCore.Grpc</PackageId>
<Authors>sandersaares</Authors>
<Company>prometheus-net</Company>
<Product>prometheus-net</Product>
<Description>ASP.NET Core gRPC integration with Prometheus</Description>
<PackageId>prometheus-net.Contrib.Plarium.AspNetCore.Grpc</PackageId>
<Authors>sandersaares, Deimos74</Authors>
<Description>ASP.NET Core gRPC integration with Prometheus(Fork)</Description>
<Copyright>Copyright © prometheus-net developers</Copyright>
<PackageProjectUrl>https://github.com/prometheus-net/prometheus-net</PackageProjectUrl>
<PackageIcon>prometheus-net-logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/Deimos74/prometheus-net</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>metrics prometheus aspnetcore</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<!-- NuGet packaging config -->
<PackageId>prometheus-net.AspNetCore.HealthChecks</PackageId>
<PackageId>prometheus-net.Contrib.Plarium.AspNetCore.HealthChecks</PackageId>
<Authors>sandersaares</Authors>
<Company>prometheus-net</Company>
<Product>prometheus-net</Product>
<Company>prometheus-net.Contrib.Plarium</Company>
<Product>prometheus-net.Contrib.Plarium</Product>
<Description>ASP.NET Core Health Checks integration with Prometheus</Description>
<Copyright>Copyright © prometheus-net developers</Copyright>
<PackageProjectUrl>https://github.com/prometheus-net/prometheus-net</PackageProjectUrl>
<PackageIcon>prometheus-net-logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/Deimos74/prometheus-net</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>metrics prometheus aspnetcore</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
9 changes: 4 additions & 5 deletions 9 Prometheus.AspNetCore/Prometheus.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<!-- NuGet packaging config -->
<PackageId>prometheus-net.AspNetCore</PackageId>
<PackageId>prometheus-net.Contrib.Plarium.AspNetCore</PackageId>
<Authors>andrasm,qed-,lakario,sandersaares</Authors>
<Company>prometheus-net</Company>
<Product>prometheus-net</Product>
<Company>prometheus-net.Contrib.Plarium</Company>
<Product>prometheus-net.Contrib.Plarium</Product>
<Description>ASP.NET Core middleware and stand-alone Kestrel server for exporting metrics to Prometheus</Description>
<Copyright>Copyright © prometheus-net developers</Copyright>
<PackageProjectUrl>https://github.com/prometheus-net/prometheus-net</PackageProjectUrl>
<PackageIcon>prometheus-net-logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/Deimos74/prometheus-net</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>metrics prometheus aspnetcore</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
9 changes: 4 additions & 5 deletions 9 Prometheus/Prometheus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>

<!-- NuGet packaging config -->
<PackageId>prometheus-net</PackageId>
<PackageId>prometheus-net.Contrib.Plarium</PackageId>
<Authors>andrasm,qed-,lakario,sandersaares</Authors>
<Company>prometheus-net</Company>
<Product>prometheus-net</Product>
<Company>prometheus-net.Contrib.Plarium</Company>
<Product>prometheus-net.Contrib.Plarium</Product>
<Description>.NET client library for the Prometheus monitoring and alerting system</Description>
<Copyright>Copyright © prometheus-net developers</Copyright>
<PackageProjectUrl>https://github.com/prometheus-net/prometheus-net</PackageProjectUrl>
<PackageIcon>prometheus-net-logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/Deimos74/prometheus-net</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>metrics prometheus</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
3 changes: 2 additions & 1 deletion 3 Resources/SolutionAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
[assembly: InternalsVisibleTo("Benchmark.NetCore, PublicKey=002400000480000014010000060200000024000052534131000800000100010049b30b6bccc8311c8d5f9c006a5968b0592eca8b5a228e9e0a2ac0292e2a162ea3314b0f9941ffad9fe40a4071de2a0b6e4f50b70292d26081054f96df6a05e5a89a71538d50decaf8322f0cdd008e8e14d5e227b46c8c10a6cc850a5d7febf9ad5e0ffb8371e840744d3dd0cb88012ee61490a09d007fab29fc13fb0b4c2fb4d72692232546712b3e9e25a201e309bec907a9a241059d26f1826a337faf6e7a16902fc35e8dafeceff35a48622a9716af86138a1a064c879b7239a9495b8416abf63f8763a613e5be2e6b13403eb952c36008a281502bc2c89ca3367624b0791712f50674760fcbab2e7795fb6c53b0675f940d152ef449ad10463bce59a7d5")]
[assembly: InternalsVisibleTo("Prometheus.AspNetCore, PublicKey=002400000480000014010000060200000024000052534131000800000100010049b30b6bccc8311c8d5f9c006a5968b0592eca8b5a228e9e0a2ac0292e2a162ea3314b0f9941ffad9fe40a4071de2a0b6e4f50b70292d26081054f96df6a05e5a89a71538d50decaf8322f0cdd008e8e14d5e227b46c8c10a6cc850a5d7febf9ad5e0ffb8371e840744d3dd0cb88012ee61490a09d007fab29fc13fb0b4c2fb4d72692232546712b3e9e25a201e309bec907a9a241059d26f1826a337faf6e7a16902fc35e8dafeceff35a48622a9716af86138a1a064c879b7239a9495b8416abf63f8763a613e5be2e6b13403eb952c36008a281502bc2c89ca3367624b0791712f50674760fcbab2e7795fb6c53b0675f940d152ef449ad10463bce59a7d5")]
[assembly: InternalsVisibleTo("Prometheus.NetCore, PublicKey=002400000480000014010000060200000024000052534131000800000100010049b30b6bccc8311c8d5f9c006a5968b0592eca8b5a228e9e0a2ac0292e2a162ea3314b0f9941ffad9fe40a4071de2a0b6e4f50b70292d26081054f96df6a05e5a89a71538d50decaf8322f0cdd008e8e14d5e227b46c8c10a6cc850a5d7febf9ad5e0ffb8371e840744d3dd0cb88012ee61490a09d007fab29fc13fb0b4c2fb4d72692232546712b3e9e25a201e309bec907a9a241059d26f1826a337faf6e7a16902fc35e8dafeceff35a48622a9716af86138a1a064c879b7239a9495b8416abf63f8763a613e5be2e6b13403eb952c36008a281502bc2c89ca3367624b0791712f50674760fcbab2e7795fb6c53b0675f940d152ef449ad10463bce59a7d5")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo("Prometheus.AspNetCore.Grpc, PublicKey=002400000480000014010000060200000024000052534131000800000100010049b30b6bccc8311c8d5f9c006a5968b0592eca8b5a228e9e0a2ac0292e2a162ea3314b0f9941ffad9fe40a4071de2a0b6e4f50b70292d26081054f96df6a05e5a89a71538d50decaf8322f0cdd008e8e14d5e227b46c8c10a6cc850a5d7febf9ad5e0ffb8371e840744d3dd0cb88012ee61490a09d007fab29fc13fb0b4c2fb4d72692232546712b3e9e25a201e309bec907a9a241059d26f1826a337faf6e7a16902fc35e8dafeceff35a48622a9716af86138a1a064c879b7239a9495b8416abf63f8763a613e5be2e6b13403eb952c36008a281502bc2c89ca3367624b0791712f50674760fcbab2e7795fb6c53b0675f940d152ef449ad10463bce59a7d5")]
15 changes: 11 additions & 4 deletions 15 Tester.NetCore/GrpcMiddlewareTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void OnStart()
{
_webserverTask =
WebHost.CreateDefaultBuilder()
.UseUrls($"http://localhost:{TesterConstants.TesterPort}")
.UseUrls($"https://localhost:{TesterConstants.TesterPort}")
.ConfigureServices(services =>
{
services.AddGrpc();
Expand Down Expand Up @@ -69,10 +69,17 @@ private void StartDummyRequest()
{
Task.Run(async delegate
{
using var channel = GrpcChannel.ForAddress($"http://localhost:{TesterConstants.TesterPort}");
var client = new Greeter.GreeterClient(channel);
try
{
using var channel = GrpcChannel.ForAddress($"https://localhost:{TesterConstants.TesterPort}");
var client = new Greeter.GreeterClient(channel);

await client.SayHelloAsync(new HelloRequest { Name = "Anonymous" });
await client.SayHelloAsync(new HelloRequest { Name = "Anonymous" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.