use of io.grpc.services.CallMetricRecorder in project grpc-java by grpc.
the class OrcaMetricReportingServerInterceptor method interceptCall.
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
Context ctx = Context.current();
CallMetricRecorder callMetricRecorder = InternalCallMetricRecorder.CONTEXT_KEY.get(ctx);
if (callMetricRecorder == null) {
callMetricRecorder = InternalCallMetricRecorder.newCallMetricRecorder();
ctx = ctx.withValue(InternalCallMetricRecorder.CONTEXT_KEY, callMetricRecorder);
}
final CallMetricRecorder finalCallMetricRecorder = callMetricRecorder;
ServerCall<ReqT, RespT> trailerAttachingCall = new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void close(Status status, Metadata trailers) {
Map<String, Double> metricValues = InternalCallMetricRecorder.finalizeAndDump(finalCallMetricRecorder);
// Only attach a metric report if there are some metric values to be reported.
if (!metricValues.isEmpty()) {
OrcaLoadReport report = OrcaLoadReport.newBuilder().putAllRequestCost(metricValues).build();
trailers.put(ORCA_ENDPOINT_LOAD_METRICS_KEY, report);
}
super.close(status, trailers);
}
};
return Contexts.interceptCall(ctx, trailerAttachingCall, headers, next);
}
use of io.grpc.services.CallMetricRecorder in project grpc-java by grpc.
the class OrcaMetricReportingServerInterceptorTest method shareCallMetricRecorderInContext.
@Test
public void shareCallMetricRecorderInContext() throws IOException {
final CallMetricRecorder callMetricRecorder = InternalCallMetricRecorder.newCallMetricRecorder();
ServerStreamTracer.Factory callMetricRecorderSharingStreamTracerFactory = new ServerStreamTracer.Factory() {
@Override
public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) {
return new ServerStreamTracer() {
@Override
public Context filterContext(Context context) {
return context.withValue(InternalCallMetricRecorder.CONTEXT_KEY, callMetricRecorder);
}
};
}
};
final AtomicReference<CallMetricRecorder> callMetricRecorderCapture = new AtomicReference<>();
SimpleServiceGrpc.SimpleServiceImplBase simpleServiceImpl = new SimpleServiceGrpc.SimpleServiceImplBase() {
@Override
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
callMetricRecorderCapture.set(CallMetricRecorder.getCurrent());
SimpleResponse response = SimpleResponse.newBuilder().setResponseMessage("Simple response").build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
};
ServerInterceptor metricReportingServerInterceptor = new OrcaMetricReportingServerInterceptor();
String serverName = InProcessServerBuilder.generateName();
grpcCleanupRule.register(InProcessServerBuilder.forName(serverName).directExecutor().addStreamTracerFactory(callMetricRecorderSharingStreamTracerFactory).addService(ServerInterceptors.intercept(simpleServiceImpl, metricReportingServerInterceptor)).build().start());
ManagedChannel channel = grpcCleanupRule.register(InProcessChannelBuilder.forName(serverName).build());
ClientCalls.blockingUnaryCall(channel, SIMPLE_METHOD, CallOptions.DEFAULT, REQUEST);
assertThat(callMetricRecorderCapture.get()).isSameInstanceAs(callMetricRecorder);
}
Aggregations