Skip to content

Commit 9d20ffd

Browse files
authored
Fix grpc aio method handling (#3873)
1 parent bb9e34e commit 9d20ffd

File tree

5 files changed

+63
-64
lines changed

5 files changed

+63
-64
lines changed

sentry_sdk/integrations/grpc/aio/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ async def intercept_unary_unary(
4444
request: Message,
4545
) -> Union[UnaryUnaryCall, Message]:
4646
method = client_call_details.method
47+
if isinstance(method, bytes):
48+
method = method.decode()
4749

4850
with sentry_sdk.start_span(
4951
op=OP.GRPC_CLIENT,
50-
name="unary unary call to %s" % method.decode(),
52+
name="unary unary call to %s" % method,
5153
origin=SPAN_ORIGIN,
5254
only_if_parent=True,
5355
) as span:
@@ -75,10 +77,12 @@ async def intercept_unary_stream(
7577
request: Message,
7678
) -> Union[AsyncIterable[Any], UnaryStreamCall]:
7779
method = client_call_details.method
80+
if isinstance(method, bytes):
81+
method = method.decode()
7882

7983
with sentry_sdk.start_span(
8084
op=OP.GRPC_CLIENT,
81-
name="unary stream call to %s" % method.decode(),
85+
name="unary stream call to %s" % method,
8286
origin=SPAN_ORIGIN,
8387
only_if_parent=True,
8488
) as span:

sentry_sdk/integrations/rust_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import json
3434
from enum import Enum, auto
35-
from typing import Any, Callable, Dict, Tuple, Optional
35+
from typing import Any, Callable, Dict, Optional
3636

3737
import sentry_sdk
3838
from sentry_sdk.integrations import Integration

tests/integrations/grpc/test_grpc.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List, Optional
88
from unittest.mock import Mock
99

10-
from sentry_sdk import start_span, start_transaction
10+
from sentry_sdk import start_span
1111
from sentry_sdk.consts import OP
1212
from sentry_sdk.integrations.grpc import GRPCIntegration
1313
from tests.conftest import ApproxDict
@@ -41,7 +41,7 @@ def _tear_down(server: grpc.Server):
4141

4242

4343
@pytest.mark.forked
44-
def test_grpc_server_starts_transaction(sentry_init, capture_events_forksafe):
44+
def test_grpc_server_starts_root_span(sentry_init, capture_events_forksafe):
4545
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
4646
events = capture_events_forksafe()
4747

@@ -99,7 +99,7 @@ def test_grpc_server_other_interceptors(sentry_init, capture_events_forksafe):
9999

100100

101101
@pytest.mark.forked
102-
def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe):
102+
def test_grpc_server_continues_trace(sentry_init, capture_events_forksafe):
103103
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
104104
events = capture_events_forksafe()
105105

@@ -108,20 +108,20 @@ def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe)
108108
with grpc.insecure_channel("localhost:{}".format(PORT)) as channel:
109109
stub = gRPCTestServiceStub(channel)
110110

111-
with start_transaction() as transaction:
111+
with start_span() as root_span:
112112
metadata = (
113113
(
114114
"baggage",
115115
"sentry-trace_id={trace_id},sentry-environment=test,"
116116
"sentry-transaction=test-transaction,sentry-sample_rate=1.0".format(
117-
trace_id=transaction.trace_id
117+
trace_id=root_span.trace_id
118118
),
119119
),
120120
(
121121
"sentry-trace",
122122
"{trace_id}-{parent_span_id}-{sampled}".format(
123-
trace_id=transaction.trace_id,
124-
parent_span_id=transaction.span_id,
123+
trace_id=root_span.trace_id,
124+
parent_span_id=root_span.span_id,
125125
sampled=1,
126126
),
127127
),
@@ -139,7 +139,7 @@ def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe)
139139
"source": "custom",
140140
}
141141
assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER
142-
assert event["contexts"]["trace"]["trace_id"] == transaction.trace_id
142+
assert event["contexts"]["trace"]["trace_id"] == root_span.trace_id
143143
assert span["op"] == "test"
144144

145145

@@ -153,17 +153,17 @@ def test_grpc_client_starts_span(sentry_init, capture_events_forksafe):
153153
with grpc.insecure_channel("localhost:{}".format(PORT)) as channel:
154154
stub = gRPCTestServiceStub(channel)
155155

156-
with start_transaction():
156+
with start_span():
157157
stub.TestServe(gRPCTestMessage(text="test"))
158158

159159
_tear_down(server=server)
160160

161161
events.write_file.close()
162162
events.read_event()
163-
local_transaction = events.read_event()
164-
span = local_transaction["spans"][0]
163+
local_root_span = events.read_event()
164+
span = local_root_span["spans"][0]
165165

166-
assert len(local_transaction["spans"]) == 1
166+
assert len(local_root_span["spans"]) == 1
167167
assert span["op"] == OP.GRPC_CLIENT
168168
assert (
169169
span["description"]
@@ -188,16 +188,16 @@ def test_grpc_client_unary_stream_starts_span(sentry_init, capture_events_forksa
188188
with grpc.insecure_channel("localhost:{}".format(PORT)) as channel:
189189
stub = gRPCTestServiceStub(channel)
190190

191-
with start_transaction():
191+
with start_span():
192192
[el for el in stub.TestUnaryStream(gRPCTestMessage(text="test"))]
193193

194194
_tear_down(server=server)
195195

196196
events.write_file.close()
197-
local_transaction = events.read_event()
198-
span = local_transaction["spans"][0]
197+
local_root_span = events.read_event()
198+
span = local_root_span["spans"][0]
199199

200-
assert len(local_transaction["spans"]) == 1
200+
assert len(local_root_span["spans"]) == 1
201201
assert span["op"] == OP.GRPC_CLIENT
202202
assert (
203203
span["description"]
@@ -233,7 +233,7 @@ def test_grpc_client_other_interceptor(sentry_init, capture_events_forksafe):
233233
channel = grpc.intercept_channel(channel, MockClientInterceptor())
234234
stub = gRPCTestServiceStub(channel)
235235

236-
with start_transaction():
236+
with start_span():
237237
stub.TestServe(gRPCTestMessage(text="test"))
238238

239239
_tear_down(server=server)
@@ -242,10 +242,10 @@ def test_grpc_client_other_interceptor(sentry_init, capture_events_forksafe):
242242

243243
events.write_file.close()
244244
events.read_event()
245-
local_transaction = events.read_event()
246-
span = local_transaction["spans"][0]
245+
local_root_span = events.read_event()
246+
span = local_root_span["spans"][0]
247247

248-
assert len(local_transaction["spans"]) == 1
248+
assert len(local_root_span["spans"]) == 1
249249
assert span["op"] == OP.GRPC_CLIENT
250250
assert (
251251
span["description"]
@@ -272,18 +272,18 @@ def test_grpc_client_and_servers_interceptors_integration(
272272
with grpc.insecure_channel("localhost:{}".format(PORT)) as channel:
273273
stub = gRPCTestServiceStub(channel)
274274

275-
with start_transaction():
275+
with start_span():
276276
stub.TestServe(gRPCTestMessage(text="test"))
277277

278278
_tear_down(server=server)
279279

280280
events.write_file.close()
281-
server_transaction = events.read_event()
282-
local_transaction = events.read_event()
281+
server_root_span = events.read_event()
282+
local_root_span = events.read_event()
283283

284284
assert (
285-
server_transaction["contexts"]["trace"]["trace_id"]
286-
== local_transaction["contexts"]["trace"]["trace_id"]
285+
server_root_span["contexts"]["trace"]["trace_id"]
286+
== local_root_span["contexts"]["trace"]["trace_id"]
287287
)
288288

289289

@@ -328,26 +328,23 @@ def test_span_origin(sentry_init, capture_events_forksafe):
328328
with grpc.insecure_channel("localhost:{}".format(PORT)) as channel:
329329
stub = gRPCTestServiceStub(channel)
330330

331-
with start_transaction(name="custom_transaction"):
331+
with start_span(name="custom_root"):
332332
stub.TestServe(gRPCTestMessage(text="test"))
333333

334334
_tear_down(server=server)
335335

336336
events.write_file.close()
337337

338-
transaction_from_integration = events.read_event()
339-
custom_transaction = events.read_event()
338+
root_span_from_integration = events.read_event()
339+
custom_root_span = events.read_event()
340340

341+
assert root_span_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc"
341342
assert (
342-
transaction_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc"
343-
)
344-
assert (
345-
transaction_from_integration["spans"][0]["origin"]
346-
== "auto.grpc.grpc.TestService"
343+
root_span_from_integration["spans"][0]["origin"] == "auto.grpc.grpc.TestService"
347344
) # manually created in TestService, not the instrumentation
348345

349-
assert custom_transaction["contexts"]["trace"]["origin"] == "manual"
350-
assert custom_transaction["spans"][0]["origin"] == "auto.grpc.grpc"
346+
assert custom_root_span["contexts"]["trace"]["origin"] == "manual"
347+
assert custom_root_span["spans"][0]["origin"] == "auto.grpc.grpc"
351348

352349

353350
class TestService(gRPCTestServiceServicer):

tests/integrations/grpc/test_grpc_aio.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest_asyncio
77
import sentry_sdk
88

9-
from sentry_sdk import start_span, start_transaction
9+
from sentry_sdk import start_span
1010
from sentry_sdk.consts import OP
1111
from sentry_sdk.integrations.grpc import GRPCIntegration
1212
from tests.conftest import ApproxDict
@@ -60,7 +60,7 @@ async def test_noop_for_unimplemented_method(sentry_init, capture_events):
6060

6161

6262
@pytest.mark.asyncio
63-
async def test_grpc_server_starts_transaction(grpc_server, capture_events):
63+
async def test_grpc_server_starts_root_span(grpc_server, capture_events):
6464
events = capture_events()
6565

6666
async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
@@ -79,26 +79,26 @@ async def test_grpc_server_starts_transaction(grpc_server, capture_events):
7979

8080

8181
@pytest.mark.asyncio
82-
async def test_grpc_server_continues_transaction(grpc_server, capture_events):
82+
async def test_grpc_server_continues_trace(grpc_server, capture_events):
8383
events = capture_events()
8484

8585
async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
8686
stub = gRPCTestServiceStub(channel)
8787

88-
with sentry_sdk.start_transaction() as transaction:
88+
with sentry_sdk.start_span() as root_span:
8989
metadata = (
9090
(
9191
"baggage",
9292
"sentry-trace_id={trace_id},sentry-environment=test,"
9393
"sentry-transaction=test-transaction,sentry-sample_rate=1.0".format(
94-
trace_id=transaction.trace_id
94+
trace_id=root_span.trace_id
9595
),
9696
),
9797
(
9898
"sentry-trace",
9999
"{trace_id}-{parent_span_id}-{sampled}".format(
100-
trace_id=transaction.trace_id,
101-
parent_span_id=transaction.span_id,
100+
trace_id=root_span.trace_id,
101+
parent_span_id=root_span.span_id,
102102
sampled=1,
103103
),
104104
),
@@ -114,7 +114,7 @@ async def test_grpc_server_continues_transaction(grpc_server, capture_events):
114114
"source": "custom",
115115
}
116116
assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER
117-
assert event["contexts"]["trace"]["trace_id"] == transaction.trace_id
117+
assert event["contexts"]["trace"]["trace_id"] == root_span.trace_id
118118
assert span["op"] == "test"
119119

120120

@@ -159,15 +159,15 @@ async def test_grpc_client_starts_span(grpc_server, capture_events_forksafe):
159159

160160
async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
161161
stub = gRPCTestServiceStub(channel)
162-
with start_transaction():
162+
with start_span():
163163
await stub.TestServe(gRPCTestMessage(text="test"))
164164

165165
events.write_file.close()
166166
events.read_event()
167-
local_transaction = events.read_event()
168-
span = local_transaction["spans"][0]
167+
local_root_span = events.read_event()
168+
span = local_root_span["spans"][0]
169169

170-
assert len(local_transaction["spans"]) == 1
170+
assert len(local_root_span["spans"]) == 1
171171
assert span["op"] == OP.GRPC_CLIENT
172172
assert (
173173
span["description"]
@@ -190,15 +190,15 @@ async def test_grpc_client_unary_stream_starts_span(
190190

191191
async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
192192
stub = gRPCTestServiceStub(channel)
193-
with start_transaction():
193+
with start_span():
194194
response = stub.TestUnaryStream(gRPCTestMessage(text="test"))
195195
[_ async for _ in response]
196196

197197
events.write_file.close()
198-
local_transaction = events.read_event()
199-
span = local_transaction["spans"][0]
198+
local_root_span = events.read_event()
199+
span = local_root_span["spans"][0]
200200

201-
assert len(local_transaction["spans"]) == 1
201+
assert len(local_root_span["spans"]) == 1
202202
assert span["op"] == OP.GRPC_CLIENT
203203
assert (
204204
span["description"]
@@ -243,24 +243,22 @@ async def test_span_origin(grpc_server, capture_events_forksafe):
243243

244244
async with grpc.aio.insecure_channel("localhost:{}".format(AIO_PORT)) as channel:
245245
stub = gRPCTestServiceStub(channel)
246-
with start_transaction(name="custom_transaction"):
246+
with start_span(name="custom_root"):
247247
await stub.TestServe(gRPCTestMessage(text="test"))
248248

249249
events.write_file.close()
250250

251-
transaction_from_integration = events.read_event()
252-
custom_transaction = events.read_event()
251+
root_span_from_integration = events.read_event()
252+
custom_root_span = events.read_event()
253253

254+
assert root_span_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc"
254255
assert (
255-
transaction_from_integration["contexts"]["trace"]["origin"] == "auto.grpc.grpc"
256-
)
257-
assert (
258-
transaction_from_integration["spans"][0]["origin"]
256+
root_span_from_integration["spans"][0]["origin"]
259257
== "auto.grpc.grpc.TestService.aio"
260258
) # manually created in TestService, not the instrumentation
261259

262-
assert custom_transaction["contexts"]["trace"]["origin"] == "manual"
263-
assert custom_transaction["spans"][0]["origin"] == "auto.grpc.grpc"
260+
assert custom_root_span["contexts"]["trace"]["origin"] == "manual"
261+
assert custom_root_span["spans"][0]["origin"] == "auto.grpc.grpc"
264262

265263

266264
class TestService(gRPCTestServiceServicer):

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,4 @@ def test_serialize_span_attribute(value, result):
982982
),
983983
)
984984
def test_datetime_from_isoformat(input_str, expected_output):
985-
assert datetime_from_isoformat(input_str) == expected_output, input_str
985+
assert datetime_from_isoformat(input_str) == expected_output, input_str

0 commit comments

Comments
 (0)