Search in sources :

Example 1 with CallMetricRecorder

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);
}
Also used : Context(io.grpc.Context) Status(io.grpc.Status) Metadata(io.grpc.Metadata) OrcaLoadReport(com.github.xds.data.orca.v3.OrcaLoadReport) InternalCallMetricRecorder(io.grpc.services.InternalCallMetricRecorder) CallMetricRecorder(io.grpc.services.CallMetricRecorder) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall)

Example 2 with CallMetricRecorder

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);
}
Also used : Context(io.grpc.Context) StreamObserver(io.grpc.stub.StreamObserver) ServerStreamTracer(io.grpc.ServerStreamTracer) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleServiceGrpc(io.grpc.testing.protobuf.SimpleServiceGrpc) SimpleRequest(io.grpc.testing.protobuf.SimpleRequest) SimpleResponse(io.grpc.testing.protobuf.SimpleResponse) ServerInterceptor(io.grpc.ServerInterceptor) ManagedChannel(io.grpc.ManagedChannel) InternalCallMetricRecorder(io.grpc.services.InternalCallMetricRecorder) CallMetricRecorder(io.grpc.services.CallMetricRecorder) Test(org.junit.Test)

Aggregations

Context (io.grpc.Context)2 Metadata (io.grpc.Metadata)2 CallMetricRecorder (io.grpc.services.CallMetricRecorder)2 InternalCallMetricRecorder (io.grpc.services.InternalCallMetricRecorder)2 OrcaLoadReport (com.github.xds.data.orca.v3.OrcaLoadReport)1 SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)1 ManagedChannel (io.grpc.ManagedChannel)1 ServerInterceptor (io.grpc.ServerInterceptor)1 ServerStreamTracer (io.grpc.ServerStreamTracer)1 Status (io.grpc.Status)1 StreamObserver (io.grpc.stub.StreamObserver)1 SimpleRequest (io.grpc.testing.protobuf.SimpleRequest)1 SimpleResponse (io.grpc.testing.protobuf.SimpleResponse)1 SimpleServiceGrpc (io.grpc.testing.protobuf.SimpleServiceGrpc)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1