|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from concurrent import futures |
| 4 | + |
| 5 | +import grpc |
| 6 | +import pytest |
| 7 | + |
| 8 | +from sentry_sdk import Hub, start_transaction |
| 9 | +from sentry_sdk.consts import OP |
| 10 | +from sentry_sdk.integrations.grpc.server import ServerInterceptor |
| 11 | +from tests.integrations.grpc.test_service_pb2 import TestMessage |
| 12 | +from tests.integrations.grpc.test_service_pb2_grpc import ( |
| 13 | + TestServiceServicer, |
| 14 | + add_TestServiceServicer_to_server, |
| 15 | + TestServiceStub, |
| 16 | +) |
| 17 | + |
| 18 | +PORT = 50051 |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.forked |
| 22 | +def test_grpc_server_starts_transaction(sentry_init, capture_events_forksafe): |
| 23 | + sentry_init(traces_sample_rate=1.0) |
| 24 | + events = capture_events_forksafe() |
| 25 | + |
| 26 | + server = _set_up() |
| 27 | + |
| 28 | + with grpc.insecure_channel(f"localhost:{PORT}") as channel: |
| 29 | + stub = TestServiceStub(channel) |
| 30 | + stub.TestServe(TestMessage(text="test")) |
| 31 | + |
| 32 | + _tear_down(server=server) |
| 33 | + |
| 34 | + events.write_file.close() |
| 35 | + event = events.read_event() |
| 36 | + span = event["spans"][0] |
| 37 | + |
| 38 | + assert event["type"] == "transaction" |
| 39 | + assert event["transaction_info"] == { |
| 40 | + "source": "custom", |
| 41 | + } |
| 42 | + assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER |
| 43 | + assert span["op"] == "test" |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.forked |
| 47 | +def test_grpc_server_continues_transaction(sentry_init, capture_events_forksafe): |
| 48 | + sentry_init(traces_sample_rate=1.0) |
| 49 | + events = capture_events_forksafe() |
| 50 | + |
| 51 | + server = _set_up() |
| 52 | + |
| 53 | + with grpc.insecure_channel(f"localhost:{PORT}") as channel: |
| 54 | + stub = TestServiceStub(channel) |
| 55 | + |
| 56 | + with start_transaction() as transaction: |
| 57 | + metadata = ( |
| 58 | + ( |
| 59 | + "baggage", |
| 60 | + "sentry-trace_id={trace_id},sentry-environment=test," |
| 61 | + "sentry-transaction=test-transaction,sentry-sample_rate=1.0".format( |
| 62 | + trace_id=transaction.trace_id |
| 63 | + ), |
| 64 | + ), |
| 65 | + ( |
| 66 | + "sentry-trace", |
| 67 | + "{trace_id}-{parent_span_id}-{sampled}".format( |
| 68 | + trace_id=transaction.trace_id, |
| 69 | + parent_span_id=transaction.span_id, |
| 70 | + sampled=1, |
| 71 | + ) |
| 72 | + ) |
| 73 | + ) |
| 74 | + stub.TestServe(TestMessage(text="test"), metadata=metadata) |
| 75 | + |
| 76 | + _tear_down(server=server) |
| 77 | + |
| 78 | + events.write_file.close() |
| 79 | + event = events.read_event() |
| 80 | + span = event["spans"][0] |
| 81 | + |
| 82 | + assert event["type"] == "transaction" |
| 83 | + assert event["transaction_info"] == { |
| 84 | + "source": "custom", |
| 85 | + } |
| 86 | + assert event["contexts"]["trace"]["op"] == OP.GRPC_SERVER |
| 87 | + assert event["contexts"]["trace"]["trace_id"] == transaction.trace_id |
| 88 | + assert span["op"] == "test" |
| 89 | + |
| 90 | + |
| 91 | +def _set_up(): |
| 92 | + server = grpc.server( |
| 93 | + futures.ThreadPoolExecutor(max_workers=2), |
| 94 | + interceptors=[ServerInterceptor(find_name=_find_name)], |
| 95 | + ) |
| 96 | + |
| 97 | + add_TestServiceServicer_to_server(TestService, server) |
| 98 | + server.add_insecure_port(f"[::]:{PORT}") |
| 99 | + server.start() |
| 100 | + |
| 101 | + return server |
| 102 | + |
| 103 | + |
| 104 | +def _tear_down(server: grpc.Server): |
| 105 | + server.stop(None) |
| 106 | + |
| 107 | + |
| 108 | +def _find_name(request): |
| 109 | + return request.__class__ |
| 110 | + |
| 111 | + |
| 112 | +class TestService(TestServiceServicer): |
| 113 | + events = [] |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def TestServe(request, context): |
| 117 | + hub = Hub.current |
| 118 | + with hub.start_span(op="test", description="test"): |
| 119 | + pass |
| 120 | + |
| 121 | + return TestMessage(text=request.text) |
0 commit comments