Ejemplo n.º 1
0
    def intercept_service(self, continuation, handler_call_details):

        handler = continuation(handler_call_details)
        if handler is None:
            return handler

        # unary
        if handler.request_streaming or handler.response_streaming:
            return handler

        mn, ok = parse_method_name(handler_call_details.method)
        grpc_service_name = mn.fully_qualified_service
        grpc_method_name = mn.method

        if not ok:
            return continuation(handler_call_details)

        # pylint: disable=W0613
        def latency_wrapper(behaviour, request_streaming, response_streaming):
            def new_behaviour(request_or_iterator, servicer_context):
                start = default_timer()
                try:
                    return behaviour(request_or_iterator, servicer_context)
                finally:
                    GRPC_SERVER_HANDLED_HISTOGRAM.labels(
                        grpc_type='UNARY',
                        grpc_service=grpc_service_name,
                        grpc_method=grpc_method_name,
                    ).observe(max(default_timer() - start, 0))

            return new_behaviour

        return _wrap_rpc_behaviour(continuation(handler_call_details),
                                   latency_wrapper)
Ejemplo n.º 2
0
    def intercept_service(self, continuation, handler_call_details):

        # handles if call_details are None
        handler = continuation(handler_call_details)
        if handler is None:
            return handler

        # unary
        if handler.request_streaming or handler.response_streaming:
            return handler

        mn, ok = parse_method_name(handler_call_details.method)
        grpc_service_name = mn.fully_qualified_service
        grpc_method_name = mn.method

        if not ok:
            return continuation(handler_call_details)

        def metrics_wrapper(behaviour, request_streaming, response_streaming):
            def new_behaviour(request_or_iterator, servicer_context):
                grpc_type = get_method_type(request_streaming,
                                            response_streaming)

                try:
                    if request_streaming:
                        request_or_iterator = wrap_interator_inc_counter(
                            request_or_iterator,
                            GRPC_SERVER_STREAM_MSG_RECEIVED,
                            grpc_type,
                            grpc_service_name,
                            grpc_method_name,
                        )
                        request_or_iterator = wrap_interator_inc_counter(
                            request_or_iterator,
                            GRPC_SERVER_STREAM_MSG_SENT,
                            grpc_type,
                            grpc_service_name,
                            grpc_method_name,
                        )
                    else:
                        GRPC_SERVER_STARTED_COUNTER.labels(
                            grpc_type=grpc_type,
                            grpc_service=grpc_service_name,
                            grpc_method=grpc_method_name,
                        ).inc()

                    # Invoke the original RPC behaviour
                    response_or_iterator = behaviour(request_or_iterator,
                                                     servicer_context)

                    if response_streaming:
                        sent_metrics = GRPC_SERVER_STREAM_MSG_SENT
                        response_or_iterator = wrap_interator_inc_counter(
                            response_or_iterator,
                            GRPC_SERVER_STREAM_MSG_RECEIVED,
                            grpc_type,
                            grpc_service_name,
                            grpc_method_name,
                        )
                        response_or_iterator = wrap_interator_inc_counter(
                            response_or_iterator,
                            sent_metrics,
                            grpc_type,
                            grpc_service_name,
                            grpc_method_name,
                        )
                    else:
                        GRPC_SERVER_HANDLED_TOTAL.labels(
                            grpc_type=grpc_type,
                            grpc_service=grpc_service_name,
                            grpc_method=grpc_method_name,
                            grpc_code=self._compute_status_code(
                                servicer_context).name,
                        ).inc()

                    return response_or_iterator

                except grpc.RpcError as e:
                    GRPC_SERVER_HANDLED_TOTAL.labels(
                        grpc_type=grpc_type,
                        grpc_service=grpc_service_name,
                        grpc_method=grpc_method_name,
                        grpc_code=self._compute_error_code(e),
                    ).inc()
                    raise e

            return new_behaviour

        optional_any = _wrap_rpc_behaviour(continuation(handler_call_details),
                                           metrics_wrapper)
        return optional_any
Ejemplo n.º 3
0
def test_parse_empty_package():
    # parse_method_name works with no package.
    mn, _ = parse_method_name("/SearchService/Search")
    assert mn.package == ""
    assert mn.service == "SearchService"
    assert mn.method == "Search"
Ejemplo n.º 4
0
def test_parse_method_name():
    mn, ok = parse_method_name("/foo.bar.SearchService/Search")
    assert mn.package == "foo.bar"
    assert mn.service == "SearchService"
    assert mn.method == "Search"
    assert ok