Skip to content

Commit

Permalink
fix grpc method being bytes in metrics (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
vangheem authored Apr 6, 2023
1 parent f97684a commit f11b06c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nucliadb_telemetry/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
1.7.1
24 changes: 19 additions & 5 deletions nucliadb_telemetry/nucliadb_telemetry/grpc_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import functools
from typing import Any, Awaitable, Callable
from typing import Any, Awaitable, Callable, Union

import grpc
from grpc import ClientCallDetails, aio # type: ignore
Expand Down Expand Up @@ -91,11 +91,19 @@ def finish_metric_grpc(metric: metrics.ObserverRecorder, result):
metric.end()


def _to_str(v: Union[str, bytes]) -> str:
if isinstance(v, str):
return v
return v.decode("utf-8")


class UnaryUnaryClientInterceptor(aio.UnaryUnaryClientInterceptor):
async def intercept_unary_unary(
self, continuation, client_call_details: ClientCallDetails, request
):
metric = grpc_client_observer(labels={"method": client_call_details.method})
metric = grpc_client_observer(
labels={"method": _to_str(client_call_details.method)}
)
metric.start()

call = await continuation(client_call_details, request)
Expand All @@ -108,7 +116,9 @@ class UnaryStreamClientInterceptor(aio.UnaryStreamClientInterceptor):
async def intercept_unary_stream(
self, continuation, client_call_details: ClientCallDetails, request
):
metric = grpc_client_observer(labels={"method": client_call_details.method})
metric = grpc_client_observer(
labels={"method": _to_str(client_call_details.method)}
)
metric.start()

call = await continuation(client_call_details, request)
Expand All @@ -121,7 +131,9 @@ class StreamStreamClientInterceptor(aio.StreamStreamClientInterceptor):
async def intercept_stream_stream(
self, continuation, client_call_details: ClientCallDetails, request_iterator
):
metric = grpc_client_observer(labels={"method": client_call_details.method})
metric = grpc_client_observer(
labels={"method": _to_str(client_call_details.method)}
)
metric.start()

call = await continuation(client_call_details, request_iterator)
Expand All @@ -134,7 +146,9 @@ class StreamUnaryClientInterceptor(aio.StreamUnaryClientInterceptor):
async def intercept_stream_unary(
self, continuation, client_call_details: ClientCallDetails, request_iterator
):
metric = grpc_client_observer(labels={"method": client_call_details.method})
metric = grpc_client_observer(
labels={"method": _to_str(client_call_details.method)}
)
metric.start()

call = await continuation(client_call_details, request_iterator)
Expand Down

0 comments on commit f11b06c

Please sign in to comment.