Skip to content

Commit 03ab549

Browse files
mergify[bot]majorgreysKyle-Verhoog
authored
fix(grpc): no package in method service (backport #2384) (#2432)
* fix(grpc): no package in method service (#2384) * fix(grpc): add snapshot test for method service metadata * handle empty method package * revert change to host parsing * add release note * use async_mode Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit 5c197a4) # Conflicts: # ddtrace/contrib/grpc/utils.py # tests/contrib/grpc/test_grpc.py * Resolve conflict * fix snapshot Co-authored-by: Tahir H. Butt <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 9a548a8 commit 03ab549

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

.circleci/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,12 @@ jobs:
722722
pattern: '^httplib_contrib'
723723

724724
grpc:
725-
<<: *contrib_job
725+
<<: *machine_executor
726726
parallelism: 6
727727
steps:
728728
- run_test:
729729
pattern: "grpc"
730+
snapshot: true
730731

731732
molten:
732733
<<: *contrib_job

ddtrace/contrib/grpc/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def set_grpc_method_meta(span, method, method_kind):
1919
method_path = method
2020
method_package, method_service, method_name = parse_method_path(method_path)
2121
span._set_str_tag(constants.GRPC_METHOD_PATH_KEY, method_path)
22-
span._set_str_tag(constants.GRPC_METHOD_PACKAGE_KEY, method_package)
22+
if method_package is not None:
23+
span._set_str_tag(constants.GRPC_METHOD_PACKAGE_KEY, method_package)
2324
span._set_str_tag(constants.GRPC_METHOD_SERVICE_KEY, method_service)
2425
span._set_str_tag(constants.GRPC_METHOD_NAME_KEY, method_name)
2526
span._set_str_tag(constants.GRPC_METHOD_KIND_KEY, method_kind)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
grpc: handle no package in fully qualified method

tests/contrib/grpc/test_grpc.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import grpc
44
from grpc._grpcio_metadata import __version__ as _GRPC_VERSION
55
from grpc.framework.foundation import logging_pool
6+
import pytest
67

78
from ddtrace import Pin
89
from ddtrace.constants import ANALYTICS_SAMPLE_RATE_KEY
@@ -12,6 +13,7 @@
1213
from ddtrace.contrib.grpc.patch import _unpatch_server
1314
from ddtrace.ext import errors
1415
from tests.utils import TracerTestCase
16+
from tests.utils import snapshot
1517

1618
from .hello_pb2 import HelloReply
1719
from .hello_pb2 import HelloRequest
@@ -572,3 +574,40 @@ class NotFutureLike(object):
572574
assert span.duration is None
573575
_handle_response(span, FutureLike())
574576
assert span.duration is not None
577+
578+
579+
@pytest.fixture()
580+
def patch_grpc():
581+
patch()
582+
try:
583+
yield
584+
finally:
585+
unpatch()
586+
587+
588+
class _UnaryUnaryRpcHandler(grpc.GenericRpcHandler):
589+
def __init__(self, handler):
590+
self._handler = handler
591+
592+
def service(self, handler_call_details):
593+
return grpc.unary_unary_rpc_method_handler(self._handler)
594+
595+
596+
@snapshot(ignores=["meta.grpc.port"])
597+
def test_method_service(patch_grpc):
598+
def handler(request, context):
599+
return b""
600+
601+
server = grpc.server(
602+
logging_pool.pool(1),
603+
options=(("grpc.so_reuseport", 0),),
604+
)
605+
port = server.add_insecure_port("[::]:0")
606+
channel = grpc.insecure_channel("localhost:{}".format(port))
607+
server.add_generic_rpc_handlers((_UnaryUnaryRpcHandler(handler),))
608+
try:
609+
server.start()
610+
channel.unary_unary("/Servicer/Handler")(b"request")
611+
channel.unary_unary("/pkg.Servicer/Handler")(b"request")
612+
finally:
613+
server.stop(None)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[[{"name" "grpc"
2+
"service" "grpc-client"
3+
"resource" "/Servicer/Handler"
4+
"type" "grpc"
5+
"error" 0
6+
"span_id" 0
7+
"trace_id" 0
8+
"parent_id" nil
9+
"start" 1620852382523305000
10+
"duration" 2042000
11+
"meta" {"runtime-id" "e77b2d3fc0304fc7bc9b7520f69177fc"
12+
"grpc.port" "58317"
13+
"grpc.host" "localhost"
14+
"grpc.method.kind" "unary"
15+
"span.kind" "client"
16+
"grpc.method.path" "/Servicer/Handler"
17+
"grpc.status.code" "StatusCode.OK"
18+
"grpc.method.service" "Servicer"
19+
"grpc.method.name" "Handler"}
20+
"metrics" {"_dd.agent_psr" 1.0
21+
"_dd.measured" 1
22+
"_sampling_priority_v1" 1
23+
"system.pid" 25491
24+
"_dd.tracer_kr" 1.0}}
25+
{"name" "grpc"
26+
"service" "grpc-server"
27+
"resource" "/Servicer/Handler"
28+
"type" "grpc"
29+
"error" 0
30+
"span_id" 1
31+
"trace_id" 0
32+
"parent_id" 0
33+
"start" 1620852382524760000
34+
"duration" 59000
35+
"meta" {"grpc.method.path" "/Servicer/Handler"
36+
"span.kind" "server"
37+
"grpc.method.name" "Handler"
38+
"grpc.method.service" "Servicer"
39+
"grpc.method.kind" "unary"}
40+
"metrics" {"_dd.measured" 1
41+
"_sampling_priority_v1" 1
42+
"_dd.tracer_kr" 1.0}}]
43+
[{"name" "grpc"
44+
"service" "grpc-client"
45+
"resource" "/pkg.Servicer/Handler"
46+
"type" "grpc"
47+
"error" 0
48+
"span_id" 0
49+
"trace_id" 1
50+
"parent_id" nil
51+
"start" 1620852382525600000
52+
"duration" 1335000
53+
"meta" {"runtime-id" "e77b2d3fc0304fc7bc9b7520f69177fc"
54+
"grpc.port" "58317"
55+
"grpc.method.package" "pkg"
56+
"grpc.host" "localhost"
57+
"grpc.method.kind" "unary"
58+
"span.kind" "client"
59+
"grpc.method.path" "/pkg.Servicer/Handler"
60+
"grpc.status.code" "StatusCode.OK"
61+
"grpc.method.service" "Servicer"
62+
"grpc.method.name" "Handler"}
63+
"metrics" {"_dd.agent_psr" 1.0
64+
"_dd.measured" 1
65+
"_sampling_priority_v1" 1
66+
"system.pid" 25491
67+
"_dd.tracer_kr" 1.0}}
68+
{"name" "grpc"
69+
"service" "grpc-server"
70+
"resource" "/pkg.Servicer/Handler"
71+
"type" "grpc"
72+
"error" 0
73+
"span_id" 1
74+
"trace_id" 1
75+
"parent_id" 0
76+
"start" 1620852382526578000
77+
"duration" 50000
78+
"meta" {"grpc.method.package" "pkg"
79+
"grpc.method.service" "Servicer"
80+
"grpc.method.path" "/pkg.Servicer/Handler"
81+
"grpc.method.kind" "unary"
82+
"grpc.method.name" "Handler"
83+
"span.kind" "server"}
84+
"metrics" {"_dd.measured" 1
85+
"_sampling_priority_v1" 1
86+
"_dd.tracer_kr" 1.0}}]]

0 commit comments

Comments
 (0)