|
| 1 | +import grpc |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +from python_grpc_prometheus.client_metrics import (CLIENT_HANDLED_LATENCY_SECONDS, |
| 6 | + CLIENT_HANDLED_COUNTER, |
| 7 | + CLIENT_STARTED_COUNTER, |
| 8 | + CLIENT_MSG_RECEIVED_TOTAL, |
| 9 | + CLIENT_MSG_SENT_TOTAL) |
| 10 | + |
| 11 | +from python_grpc_prometheus import util |
| 12 | +from python_grpc_prometheus.util import split_call_details |
| 13 | +from python_grpc_prometheus.util import code_to_string |
| 14 | + |
| 15 | +_ = CLIENT_MSG_RECEIVED_TOTAL |
| 16 | + |
| 17 | + |
| 18 | +class PromClientInterceptor(grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor, |
| 19 | + grpc.StreamUnaryClientInterceptor, |
| 20 | + grpc.StreamStreamClientInterceptor): |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def _callback(grpc_type, grpc_service, grpc_method, start): |
| 24 | + def callback(future_response): |
| 25 | + exception = future_response.exception() |
| 26 | + code = code_to_string(grpc.StatusCode.OK) |
| 27 | + if exception is not None: |
| 28 | + if isinstance(exception, grpc.Call): |
| 29 | + code = code_to_string(exception.code()) |
| 30 | + else: |
| 31 | + code = code_to_string(grpc.StatusCode.UNKNOWN) |
| 32 | + |
| 33 | + CLIENT_HANDLED_COUNTER.labels( |
| 34 | + grpc_type=grpc_type, |
| 35 | + grpc_service=grpc_service, |
| 36 | + grpc_method=grpc_method, |
| 37 | + grpc_code=code |
| 38 | + ).inc() |
| 39 | + |
| 40 | + CLIENT_HANDLED_LATENCY_SECONDS.labels( |
| 41 | + grpc_type=grpc_type, |
| 42 | + grpc_service=grpc_service, |
| 43 | + grpc_method=grpc_method).observe(max(time.time() - start, 0)) |
| 44 | + |
| 45 | + return callback |
| 46 | + |
| 47 | + def intercept_unary_unary(self, continuation, client_call_details, request): |
| 48 | + grpc_service, grpc_method, ok = split_call_details(client_call_details, 3) |
| 49 | + if not ok: |
| 50 | + return continuation(client_call_details, request) |
| 51 | + |
| 52 | + start = time.time() |
| 53 | + grpc_type = util.Unary |
| 54 | + CLIENT_STARTED_COUNTER.labels( |
| 55 | + grpc_type=grpc_type, |
| 56 | + grpc_service=grpc_service, |
| 57 | + grpc_method=grpc_method).inc() |
| 58 | + CLIENT_MSG_SENT_TOTAL.labels( |
| 59 | + grpc_type=grpc_type, |
| 60 | + grpc_service=grpc_service, |
| 61 | + grpc_method=grpc_method).inc() |
| 62 | + |
| 63 | + try: |
| 64 | + response = continuation(client_call_details, request) |
| 65 | + response.add_done_callback(self._callback(util.Unary, grpc_service, grpc_method, start)) |
| 66 | + except grpc.RpcError as e: |
| 67 | + code = code_to_string(grpc.StatusCode.UNKNOWN) |
| 68 | + if isinstance(e, grpc.Call): |
| 69 | + code = code_to_string(e.code()) |
| 70 | + CLIENT_HANDLED_COUNTER.labels( |
| 71 | + grpc_type=grpc_type, |
| 72 | + grpc_service=grpc_service, |
| 73 | + grpc_method=grpc_method, |
| 74 | + grpc_code=code |
| 75 | + ).inc() |
| 76 | + |
| 77 | + CLIENT_HANDLED_LATENCY_SECONDS.labels( |
| 78 | + grpc_type=grpc_type, |
| 79 | + grpc_service=grpc_service, |
| 80 | + grpc_method=grpc_method).observe(max(time.time() - start, 0)) |
| 81 | + raise |
| 82 | + return response |
| 83 | + |
| 84 | + def intercept_unary_stream(self, continuation, client_call_details, request): |
| 85 | + response = continuation(client_call_details, request) |
| 86 | + return response |
| 87 | + |
| 88 | + def intercept_stream_unary(self, continuation, client_call_details, request_iterator): |
| 89 | + response = continuation(client_call_details, request_iterator) |
| 90 | + return response |
| 91 | + |
| 92 | + def intercept_stream_stream(self, continuation, client_call_details, request_iterator): |
| 93 | + response = continuation(client_call_details, request_iterator) |
| 94 | + return response |
0 commit comments