Skip to content

Commit 2a393e2

Browse files
authored
Merge pull request #67 from avast/grpc_call_count
Add metric for amount of calls per method to GrpcServerMonitoringInterceptor
2 parents 3b4b889 + 618f029 commit 2a393e2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

grpc/src/main/java/com/avast/metrics/grpc/GrpcServerMonitoringInterceptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public GrpcServerMonitoringInterceptor(final Monitor monitor) {
2424
@Override
2525
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) {
2626
MethodDescriptor<ReqT, RespT> method = call.getMethodDescriptor();
27-
final AtomicInteger currentCalls = cache.getGaugedValue(method, "Current");
2827

28+
cache.getMeter(method, "Calls", "count").mark();
29+
30+
final AtomicInteger currentCalls = cache.getGaugedValue(method, "Current");
2931
final Instant start = clock.instant();
3032
currentCalls.incrementAndGet();
3133

grpc/src/main/java/com/avast/metrics/grpc/MetricsCache.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.avast.metrics.api.Timer;
66
import io.grpc.MethodDescriptor;
77

8+
import java.util.Arrays;
89
import java.util.concurrent.ConcurrentHashMap;
910
import java.util.concurrent.atomic.AtomicInteger;
1011

@@ -28,6 +29,11 @@ public <ReqT, RespT> Meter getMeter(MethodDescriptor<ReqT, RespT> methodDescript
2829
return meters.computeIfAbsent(methodMonitorName + name, (s) -> monitor.named(methodMonitorName).newMeter(name));
2930
}
3031

32+
public <ReqT, RespT> Meter getMeter(MethodDescriptor<ReqT, RespT> methodDescriptor, String name1, String name2) {
33+
String methodMonitorName = MetricNaming.getMethodMonitorName(methodDescriptor);
34+
return meters.computeIfAbsent(methodMonitorName + name1 + name2, (s) -> monitor.named(methodMonitorName, name1).newMeter(name2));
35+
}
36+
3137
public <ReqT, RespT> AtomicInteger getGaugedValue(MethodDescriptor<ReqT, RespT> methodDescriptor, String name) {
3238
String methodMonitorName = MetricNaming.getMethodMonitorName(methodDescriptor);
3339
return gaugedValues.computeIfAbsent(methodMonitorName + name, n -> {

grpc/src/test/java/com/avast/metrics/grpc/GrpcServerMonitoringInterceptorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.avast.metrics.grpc;
22

3+
import com.avast.metrics.api.Meter;
34
import com.avast.metrics.api.Monitor;
45
import com.avast.metrics.api.Timer;
56
import io.grpc.ManagedChannel;
@@ -44,6 +45,12 @@ public void testOk() throws IOException {
4445
return null;
4546
});
4647

48+
final Monitor callsMonitor = mock(Monitor.class);
49+
when(monitor.named("TestApiService_Get", "Calls")).thenReturn(callsMonitor);
50+
51+
final Meter callsMeter = mock(Meter.class);
52+
when(callsMonitor.newMeter(eq("count"))).thenReturn(callsMeter);
53+
4754
final Clock clock = mock(Clock.class);
4855
when(clock.instant()).thenReturn(
4956
Instant.ofEpochMilli(0),
@@ -73,6 +80,7 @@ public void get(final TestApiOuterClass.TestApi.GetRequest request, final Stream
7380

7481
verify(timer, times(1)).update(Matchers.eq(Duration.ofMillis(42)));
7582
assertEquals(0, currentCallsSupplier.get().get().longValue());
83+
verify(callsMeter, times(1)).mark();
7684
}
7785

7886
@Test
@@ -93,6 +101,12 @@ public void testFailure() throws IOException {
93101
return null;
94102
});
95103

104+
final Monitor callsMonitor = mock(Monitor.class);
105+
when(monitor.named("TestApiService_Get", "Calls")).thenReturn(callsMonitor);
106+
107+
final Meter callsMeter = mock(Meter.class);
108+
when(callsMonitor.newMeter(eq("count"))).thenReturn(callsMeter);
109+
96110
final Clock clock = mock(Clock.class);
97111
when(clock.instant()).thenReturn(
98112
Instant.ofEpochMilli(0),
@@ -124,6 +138,7 @@ public void get(final TestApiOuterClass.TestApi.GetRequest request, final Stream
124138

125139
verify(timer, times(1)).update(Matchers.eq(Duration.ofMillis(42)));
126140
assertEquals(0, currentCallsSupplier.get().get().longValue());
141+
verify(callsMeter, times(1)).mark();
127142
}
128143
}
129144

0 commit comments

Comments
 (0)