Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Added IDisposable support to enable release of metrics port.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giles Cope committed Oct 23, 2018
1 parent f4d638d commit 30de959
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 19 deletions.
19 changes: 13 additions & 6 deletions NetGrpcPrometheus/ClientInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ namespace NetGrpcPrometheus
/// <summary>
/// Interceptor for intercepting calls on client side
/// </summary>
public class ClientInterceptor : Interceptor
public class ClientInterceptor : Interceptor, IDisposable
{
private readonly MetricServer _metricServer;

private readonly MetricsBase _metrics;

/// <summary>
/// Enable recording of latency for responses. By default it's set to false
/// </summary>
Expand All @@ -23,9 +27,7 @@ public bool EnableLatencyMetrics
get => _metrics.EnableLatencyMetrics;
set => _metrics.EnableLatencyMetrics = value;
}

private readonly MetricsBase _metrics;


/// <summary>
/// Constructor for client side interceptor with metric server.
/// Metric server will be created and provide metrics on /metrics endpoint.
Expand All @@ -37,8 +39,8 @@ public bool EnableLatencyMetrics
public ClientInterceptor(string hostname, int port, bool defaultMetrics = true,
bool enableLatencyMetrics = false)
{
MetricServer metricServer = new MetricServer(hostname, port);
metricServer.Start();
_metricServer = new MetricServer(hostname, port);
_metricServer.Start();

if (!defaultMetrics)
{
Expand Down Expand Up @@ -261,5 +263,10 @@ public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreami

return result;
}

public void Dispose()
{
((IDisposable) _metricServer)?.Dispose();
}
}
}
20 changes: 14 additions & 6 deletions NetGrpcPrometheus/ServerInterceptor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
Expand All @@ -12,8 +13,12 @@ namespace NetGrpcPrometheus
/// <summary>
/// Interceptor for intercepting calls on server side
/// </summary>
public class ServerInterceptor : Interceptor
public class ServerInterceptor : Interceptor, IDisposable
{
private readonly MetricServer _metricServer;

private readonly MetricsBase _metrics;

/// <summary>
/// Enable recording of latency for responses. By default it's set to false
/// </summary>
Expand All @@ -23,8 +28,6 @@ public bool EnableLatencyMetrics
set => _metrics.EnableLatencyMetrics = value;
}

private readonly MetricsBase _metrics;

/// <summary>
/// Constructor for server side interceptor
/// </summary>
Expand All @@ -34,8 +37,8 @@ public bool EnableLatencyMetrics
/// <param name="enableLatencyMetrics">Enable recording of latency for responses. By default it's set to false</param>
public ServerInterceptor(string hostname, int port, bool defaultMetrics = true, bool enableLatencyMetrics = false)
{
MetricServer metricServer = new MetricServer(hostname, port);
metricServer.Start();
_metricServer = new MetricServer(hostname, port);
_metricServer.Start();

if (!defaultMetrics)
{
Expand Down Expand Up @@ -183,5 +186,10 @@ public override Task DuplexStreamingServerHandler<TRequest, TResponse>(

return result;
}

public void Dispose()
{
_metricServer.Stop();
}
}
}
12 changes: 9 additions & 3 deletions NetGrpcPrometheusTest/Helpers/TestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace NetGrpcPrometheusTest.Helpers
{
public class TestClient
public class TestClient : IDisposable
{
public static readonly MetricsBase Metrics = new ClientMetrics();
public static readonly string MetricsHostname = "127.0.0.1";
Expand All @@ -24,17 +24,18 @@ public class TestClient
public int MetricsPort;

private readonly TestService.TestServiceClient _client;
private ClientInterceptor _interceptor;

public TestClient(string grpcHostName, int grpcPort, int metricsPort)
{
MetricsPort = metricsPort;

ClientInterceptor interceptor =
_interceptor =
new ClientInterceptor(MetricsHostname, metricsPort) {EnableLatencyMetrics = true};

Channel channel = new Channel(grpcHostName, grpcPort, ChannelCredentials.Insecure);
_client = new TestService.TestServiceClient(
channel.Intercept(interceptor));
channel.Intercept(_interceptor));
}

public void UnaryCall(Status status = Status.Ok)
Expand Down Expand Up @@ -127,5 +128,10 @@ public void Wait()
{
Task.Run(() => { Thread.Sleep(2000); }).Wait();
}

public void Dispose()
{
_interceptor.Dispose();
}
}
}
16 changes: 12 additions & 4 deletions NetGrpcPrometheusTest/Helpers/TestServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grpc.Core;
using System;
using Grpc.Core;
using Grpc.Core.Interceptors;
using NetGrpcPrometheus;
using NetGrpcPrometheus.Helpers;
Expand All @@ -7,7 +8,7 @@

namespace NetGrpcPrometheusTest.Helpers
{
public class TestServer
public class TestServer : IDisposable
{
public static readonly string GrpcHostname = "127.0.0.1";
public static readonly int GrpcPort = 50051;
Expand All @@ -17,17 +18,18 @@ public class TestServer
public static readonly MetricsBase Metrics = new ServerMetrics();

private readonly Server _server;
private readonly ServerInterceptor _interceptor;

public TestServer()
{
ServerInterceptor interceptor =
_interceptor =
new ServerInterceptor(MetricsHostname, MetricsPort) {EnableLatencyMetrics = true};

_server = new Server()
{
Services =
{
TestService.BindService(new TestServiceImp()).Intercept(interceptor)
TestService.BindService(new TestServiceImp()).Intercept(_interceptor)
},
Ports = {new ServerPort(GrpcHostname, GrpcPort, ServerCredentials.Insecure)}
};
Expand All @@ -39,5 +41,11 @@ public void Shutdown()
{
_server.ShutdownAsync().Wait();
}

public void Dispose()
{
Shutdown();
_interceptor?.Dispose();
}
}
}
50 changes: 50 additions & 0 deletions NetGrpcPrometheusTest/InterceptorTeardownTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Threading.Tasks;
using NetGrpcPrometheusTest.Grpc;
using NetGrpcPrometheusTest.Helpers;
using NUnit.Framework;

namespace NetGrpcPrometheusTest
{
/// <summary>
/// When Interceptor disposed should release all ports.
/// </summary>
[TestFixture]
public class InterceptorTeardownTest
{
private TestServer _server;
private TestClient _client;

[SetUp]
public async Task SetUp()
{
_server = new TestServer();
_client = new TestClient(TestServer.GrpcHostname, TestServer.GrpcPort, 9001);

_client.UnaryCall();
await _client.UnaryCallAsync();
await _client.ClientStreamingCall();
await _client.ServerStreamingCall();
await _client.DuplexStreamingCall();

_client.UnaryCall(Status.Bad);
await _client.UnaryCallAsync(Status.Bad);
await _client.ClientStreamingCall(Status.Bad);
await _client.ServerStreamingCall(Status.Bad);
await _client.DuplexStreamingCall(Status.Bad);
}

[Test]
public void TestFirstInvocationTeardown() {}

[Test]
public void TestSecondInvocationTeardown() {}

[TearDown]
public void TearDown()
{
// These disposes should free the metric port. If not, the second SetUp call will fail.
_server.Dispose();
_client.Dispose();
}
}
}

0 comments on commit 30de959

Please sign in to comment.