-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_writer.py
122 lines (90 loc) · 3.58 KB
/
test_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from contextlib import contextmanager
from typing import Any, Generator
from ddtrace import Tracer
from ddtrace.trace import tracer
from pytest_httpserver import HTTPServer
from troncos.tracing._exporter import Exporter, ExporterType
from troncos.tracing._writer import OTELWriter
@contextmanager
def tracer_test(
httpserver: HTTPServer,
service_name: str,
resource_attributes: dict[str, Any] | None = None,
) -> Generator[Tracer, Any, Any]:
httpserver.expect_request("/v1/trace").respond_with_data("OK")
assert tracer.current_span() is None
tracer.configure(
writer=OTELWriter(
service_name=service_name,
exporter=Exporter(
host=httpserver.host,
port=f"{httpserver.port}",
path="/v1/trace",
exporter_type=ExporterType.HTTP,
),
resource_attributes=resource_attributes,
)
)
yield tracer
tracer.flush() # type: ignore[no-untyped-call]
def tracer_assert(httpserver: HTTPServer) -> bytes:
assert len(httpserver.log), "We should have gotten at least 1 request"
data: bytes = b""
for req, res in httpserver.log:
assert res.status_code == 200, "Response should have been 200"
data += req.data
return data
def test_simple_span(httpserver: HTTPServer) -> None:
with tracer_test(httpserver, "test_service") as tracer:
with tracer.trace("test", service="test_service"):
pass
data = tracer_assert(httpserver)
assert b"service.name\x12\x0e\n\x0ctest_service" in data
def test_attributes(httpserver: HTTPServer) -> None:
with tracer_test(
httpserver,
"test_attributes",
resource_attributes={"resource_attribute": "working"},
) as tracer:
with tracer.trace("test", service="test_attributes") as span:
span.set_tag("span_attribute", "also_working")
data = tracer_assert(httpserver)
assert b"service.name\x12\x11\n\x0ftest_attributes" in data
assert b"resource_attribute\x12\t\n\x07working" in data
assert b"span_attribute\x12\x0e\n\x0calso_working" in data
def test_exceptions(httpserver: HTTPServer) -> None:
with tracer_test(httpserver, "test_exception") as tracer:
try:
with tracer.trace("test", service="test_exception"):
raise AssertionError("TestFailure")
except AssertionError:
pass
data = tracer_assert(httpserver)
assert b"service.name\x12\x10\n\x0etest_exception" in data
assert b"exception.type\x12\x19\n\x17builtins.AssertionError" in data
def test_headers(httpserver: HTTPServer) -> None:
httpserver.expect_request("/v1/trace").respond_with_data("OK")
httpserver.expect_request("/v1/trace/custom-header").respond_with_data("OK")
tracer.configure(
writer=OTELWriter(
service_name="test_headers",
exporter=Exporter(
host=httpserver.host,
port=f"{httpserver.port}",
path="/v1/trace/custom-header",
exporter_type=ExporterType.HTTP,
headers={"test-header": "works"},
),
resource_attributes={},
)
)
assert tracer.current_span() is None
with tracer.trace("test"):
pass
tracer.flush() # type: ignore[no-untyped-call]
relevant_requests = [
entry for entry in httpserver.log if entry[0].path == "/v1/trace/custom-header"
]
assert len(relevant_requests) == 1, "We should have gotten 1 request"
req, _ = relevant_requests[0]
assert req.headers.get("test-header") == "works"