Skip to content

Commit 3eaadc8

Browse files
committed
fix(harness): decode chunked OTLP bodies and record request metadata
The Node OTLP exporter streams with Transfer-Encoding: chunked and sends no Content-Length. The Receiver read Content-Length only, so every Node export came back 400 with zero spans; two TH-8103 children had to put a de-chunking relay in front of it. Receiver now accepts both framings. It also records one entry per accepted export (path, lower-cased headers, flattened resource attributes) via requests(), so a contract test can assert X-Api-Key/X-Secret-Key and project_name through the shared harness instead of a private recorder. Verified: 6 harness tests pass, and the TanStack example's real Node exporter delivered 4 chunked exports (4 spans, collector path, both auth headers, project_name/project_type) with no relay. Refs: TH-8339, TH-8103
1 parent 32f5c9d commit 3eaadc8

2 files changed

Lines changed: 133 additions & 4 deletions

File tree

‎python/tests/harness/__init__.py‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,36 @@ class Receiver:
2323

2424
def __init__(self) -> None:
2525
self._spans: list[dict[str, Any]] = []
26+
self._requests: list[dict[str, Any]] = []
2627
self._lock = threading.Lock()
2728
owner = self
2829

2930
class Handler(BaseHTTPRequestHandler):
31+
def _read_body(self) -> bytes:
32+
# The Node OTLP exporter streams with Transfer-Encoding: chunked
33+
# and sends no Content-Length, so both framings are accepted.
34+
if "chunked" in self.headers.get("Transfer-Encoding", "").lower():
35+
chunks = []
36+
while True:
37+
size_line = self.rfile.readline().split(b";", 1)[0].strip()
38+
size = int(size_line, 16)
39+
if size == 0:
40+
while self.rfile.readline() not in (b"\r\n", b"\n", b""):
41+
pass
42+
return b"".join(chunks)
43+
chunks.append(self.rfile.read(size))
44+
self.rfile.readline()
45+
length = int(self.headers.get("Content-Length", "0"))
46+
return self.rfile.read(length)
47+
3048
def do_POST(self) -> None: # noqa: N802 - required by BaseHTTPRequestHandler
31-
if urlsplit(self.path).path not in ("/v1/traces", "/tracer/v1/traces"):
49+
path = urlsplit(self.path).path
50+
if path not in ("/v1/traces", "/tracer/v1/traces"):
3251
self.send_error(HTTPStatus.NOT_FOUND)
3352
return
3453

3554
try:
36-
length = int(self.headers.get("Content-Length", "0"))
37-
body = self.rfile.read(length)
55+
body = self._read_body()
3856
content_type = self.headers.get("Content-Type", "").lower()
3957

4058
if content_type.startswith("application/json"):
@@ -53,9 +71,15 @@ def do_POST(self) -> None: # noqa: N802 - required by BaseHTTPRequestHandler
5371
return
5472

5573
spans: list[dict[str, Any]] = []
74+
resource_attributes: list[dict[str, Any]] = []
5675
for resource_spans in request.get(
5776
"resourceSpans", request.get("resource_spans", [])
5877
):
78+
resource_attributes.append(
79+
_flatten_attributes(
80+
resource_spans.get("resource", {}).get("attributes", [])
81+
)
82+
)
5983
for scope_spans in resource_spans.get(
6084
"scopeSpans", resource_spans.get("scope_spans", [])
6185
):
@@ -66,6 +90,15 @@ def do_POST(self) -> None: # noqa: N802 - required by BaseHTTPRequestHandler
6690

6791
with owner._lock:
6892
owner._spans.extend(copy.deepcopy(spans))
93+
owner._requests.append(
94+
{
95+
"path": path,
96+
"headers": {
97+
key.lower(): value for key, value in self.headers.items()
98+
},
99+
"resource_attributes": resource_attributes,
100+
}
101+
)
69102
self.send_response(HTTPStatus.OK)
70103
self.send_header("Content-Length", "0")
71104
self.end_headers()
@@ -93,10 +126,30 @@ def spans(self) -> list[dict[str, Any]]:
93126
with self._lock:
94127
return copy.deepcopy(self._spans)
95128

129+
def requests(self) -> list[dict[str, Any]]:
130+
"""Return one record per accepted export: path, lower-cased headers, resource attributes."""
131+
with self._lock:
132+
return copy.deepcopy(self._requests)
133+
96134
def clear(self) -> None:
97-
"""Remove every decoded span received so far."""
135+
"""Remove every decoded span and request record received so far."""
98136
with self._lock:
99137
self._spans.clear()
138+
self._requests.clear()
139+
140+
141+
def _flatten_attributes(attributes: Sequence[Mapping[str, Any]]) -> dict[str, Any]:
142+
"""Turn OTLP key/value attributes into a plain dict of scalar values."""
143+
flat: dict[str, Any] = {}
144+
for attribute in attributes:
145+
value = attribute.get("value", {})
146+
for kind in ("stringValue", "boolValue", "intValue", "doubleValue"):
147+
if kind in value:
148+
flat[attribute.get("key", "")] = value[kind]
149+
break
150+
else:
151+
flat[attribute.get("key", "")] = value
152+
return flat
100153

101154

102155
def run(

‎python/tests/harness/test_harness.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,82 @@ def test_receiver_accepts_the_collector_path() -> None:
5050
assert receiver.spans() == [{"name": "collector.path"}]
5151

5252

53+
def _post_chunked(url: str, body: bytes, headers: dict) -> int:
54+
"""Send a body with Transfer-Encoding: chunked, as the Node OTLP exporter does."""
55+
import http.client
56+
from urllib.parse import urlsplit
57+
58+
parts = urlsplit(url)
59+
connection = http.client.HTTPConnection(parts.hostname, parts.port, timeout=5)
60+
try:
61+
chunks = [body[: len(body) // 2], body[len(body) // 2 :]]
62+
connection.request(
63+
"POST",
64+
parts.path,
65+
body=iter(chunks),
66+
headers={**headers, "Transfer-Encoding": "chunked"},
67+
encode_chunked=True,
68+
)
69+
return connection.getresponse().status
70+
finally:
71+
connection.close()
72+
73+
74+
def test_receiver_decodes_a_chunked_body() -> None:
75+
payload = {
76+
"resourceSpans": [
77+
{"scopeSpans": [{"spans": [{"name": "chunked.body"}]}]}
78+
]
79+
}
80+
81+
with Receiver() as receiver:
82+
status = _post_chunked(
83+
receiver.collector_endpoint,
84+
json.dumps(payload).encode("utf-8"),
85+
{"Content-Type": "application/json"},
86+
)
87+
assert status == 200
88+
assert receiver.spans() == [{"name": "chunked.body"}]
89+
90+
91+
def test_receiver_records_path_headers_and_resource_attributes() -> None:
92+
payload = {
93+
"resourceSpans": [
94+
{
95+
"resource": {
96+
"attributes": [
97+
{"key": "project_name", "value": {"stringValue": "harness"}},
98+
{"key": "project_type", "value": {"stringValue": "observe"}},
99+
]
100+
},
101+
"scopeSpans": [{"spans": [{"name": "with.resource"}]}],
102+
}
103+
]
104+
}
105+
106+
with Receiver() as receiver:
107+
status = _post_chunked(
108+
receiver.collector_endpoint,
109+
json.dumps(payload).encode("utf-8"),
110+
{
111+
"Content-Type": "application/json",
112+
"X-Api-Key": "test-key",
113+
"X-Secret-Key": "test-secret",
114+
},
115+
)
116+
assert status == 200
117+
[request] = receiver.requests()
118+
assert request["path"] == "/tracer/v1/traces"
119+
assert request["headers"]["x-api-key"] == "test-key"
120+
assert request["headers"]["x-secret-key"] == "test-secret"
121+
assert "authorization" not in request["headers"]
122+
assert request["resource_attributes"] == [
123+
{"project_name": "harness", "project_type": "observe"}
124+
]
125+
receiver.clear()
126+
assert receiver.requests() == []
127+
128+
53129
def test_run_captures_output_and_kills_timed_out_process() -> None:
54130
completed = run(
55131
[

0 commit comments

Comments
 (0)