Skip to content

Commit

Permalink
Merge pull request #67 from avast/grpc_call_count
Browse files Browse the repository at this point in the history
Add metric for amount of calls per method to GrpcServerMonitoringInterceptor
  • Loading branch information
Tasssadar authored Jan 24, 2023
2 parents 3b4b889 + 618f029 commit 2a393e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public GrpcServerMonitoringInterceptor(final Monitor monitor) {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) {
MethodDescriptor<ReqT, RespT> method = call.getMethodDescriptor();
final AtomicInteger currentCalls = cache.getGaugedValue(method, "Current");

cache.getMeter(method, "Calls", "count").mark();

final AtomicInteger currentCalls = cache.getGaugedValue(method, "Current");
final Instant start = clock.instant();
currentCalls.incrementAndGet();

Expand Down
6 changes: 6 additions & 0 deletions grpc/src/main/java/com/avast/metrics/grpc/MetricsCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.avast.metrics.api.Timer;
import io.grpc.MethodDescriptor;

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

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

public <ReqT, RespT> Meter getMeter(MethodDescriptor<ReqT, RespT> methodDescriptor, String name1, String name2) {
String methodMonitorName = MetricNaming.getMethodMonitorName(methodDescriptor);
return meters.computeIfAbsent(methodMonitorName + name1 + name2, (s) -> monitor.named(methodMonitorName, name1).newMeter(name2));
}

public <ReqT, RespT> AtomicInteger getGaugedValue(MethodDescriptor<ReqT, RespT> methodDescriptor, String name) {
String methodMonitorName = MetricNaming.getMethodMonitorName(methodDescriptor);
return gaugedValues.computeIfAbsent(methodMonitorName + name, n -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.avast.metrics.grpc;

import com.avast.metrics.api.Meter;
import com.avast.metrics.api.Monitor;
import com.avast.metrics.api.Timer;
import io.grpc.ManagedChannel;
Expand Down Expand Up @@ -44,6 +45,12 @@ public void testOk() throws IOException {
return null;
});

final Monitor callsMonitor = mock(Monitor.class);
when(monitor.named("TestApiService_Get", "Calls")).thenReturn(callsMonitor);

final Meter callsMeter = mock(Meter.class);
when(callsMonitor.newMeter(eq("count"))).thenReturn(callsMeter);

final Clock clock = mock(Clock.class);
when(clock.instant()).thenReturn(
Instant.ofEpochMilli(0),
Expand Down Expand Up @@ -73,6 +80,7 @@ public void get(final TestApiOuterClass.TestApi.GetRequest request, final Stream

verify(timer, times(1)).update(Matchers.eq(Duration.ofMillis(42)));
assertEquals(0, currentCallsSupplier.get().get().longValue());
verify(callsMeter, times(1)).mark();
}

@Test
Expand All @@ -93,6 +101,12 @@ public void testFailure() throws IOException {
return null;
});

final Monitor callsMonitor = mock(Monitor.class);
when(monitor.named("TestApiService_Get", "Calls")).thenReturn(callsMonitor);

final Meter callsMeter = mock(Meter.class);
when(callsMonitor.newMeter(eq("count"))).thenReturn(callsMeter);

final Clock clock = mock(Clock.class);
when(clock.instant()).thenReturn(
Instant.ofEpochMilli(0),
Expand Down Expand Up @@ -124,6 +138,7 @@ public void get(final TestApiOuterClass.TestApi.GetRequest request, final Stream

verify(timer, times(1)).update(Matchers.eq(Duration.ofMillis(42)));
assertEquals(0, currentCallsSupplier.get().get().longValue());
verify(callsMeter, times(1)).mark();
}
}

0 comments on commit 2a393e2

Please sign in to comment.