Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5936e09
test(harness): add shared OTLP harness for TH-8103 contract tests
nik13 Oct 4, 2026
32f5c9d
test(harness): accept the collector /tracer/v1/traces path
nik13 Oct 4, 2026
3eaadc8
fix(harness): decode chunked OTLP bodies and record request metadata
nik13 Oct 4, 2026
9298bd5
feat(examples): add TanStack AI otelMiddleware recipe for Future AGI
nik13 Oct 4, 2026
04409aa
fix(examples): count TanStack AI model-call tokens once per trace
nik13 Oct 4, 2026
73d9943
fix(examples): export TanStack AI traces when the model call fails
nik13 Oct 4, 2026
4a114d1
fix(examples): move every TanStack AI usage key off the chat root
nik13 Oct 4, 2026
056bb44
fix(examples): pin the sdk-trace span the TanStack AI usage move edits
nik13 Oct 4, 2026
48e2af0
feat(examples): send the caller's TanStack AI thread id as session.id
nik13 Oct 4, 2026
c6001eb
fix(examples): bound the TanStack AI trace flush on the request path
nik13 Oct 5, 2026
98f7444
docs(examples): make the TanStack AI README recipe the one that is te…
nik13 Oct 5, 2026
5d175af
test(examples): keep placeholder keys out of TanStack AI spans and ou…
nik13 Oct 5, 2026
cbb2aa0
fix(examples): keep the TanStack AI recipe off the global tracer prov…
nik13 Oct 5, 2026
a9555c4
fix(examples): end TanStack AI spans when the client disconnects
nik13 Oct 5, 2026
186831f
fix(examples): align TanStack AI example engines with the pinned SDK
nik13 Oct 5, 2026
43f4e23
docs(examples): say TanStack AI error text is exported regardless of …
nik13 Oct 5, 2026
87a19fb
docs(examples): tell TanStack AI recipe copiers to copy the SDK pin
nik13 Oct 5, 2026
9bf9cf8
docs(examples): advise an opaque TanStack AI thread id for session.id
nik13 Oct 5, 2026
d871239
docs(examples): state where TanStack AI spans can still be lost
nik13 Oct 5, 2026
a6c3331
fix(examples): pin @tanstack/openai-base so npm install matches the t…
nik13 Oct 12, 2026
175fb97
fix(examples): mark a single-call TanStack AI chat root as CHAIN
nik13 Oct 12, 2026
9ee8a33
fix(examples): send gen_ai.provider.name for TanStack AI model calls
nik13 Oct 12, 2026
2ab70c5
build(examples): commit the TanStack AI example's package-lock.json
nik13 Oct 12, 2026
de821d7
build(examples): pin the TanStack AI example to @tanstack/ai 0.69.0
nik13 Oct 17, 2026
879b418
fix(examples): keep TanStack AI error text and stack traces out of spans
nik13 Oct 17, 2026
f2295cb
fix(examples): start each TanStack AI chat in its own trace
nik13 Oct 17, 2026
c5494d5
Merge dev into feat/th-8238-tanstack-ai
nik13 Oct 21, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ output/
*.log
.DS_Store
**/package-lock.json
# Standalone npm example (outside the pnpm workspace): its lockfile pins the tested dependency set.
!typescript/examples/tanstack-ai/package-lock.json
**/tsconfig.tsbuildinfo
**/.fi
**/.pytest_cache
Expand Down
238 changes: 238 additions & 0 deletions python/tests/harness/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
"""Small, loopback-only utilities for OTLP contract tests."""

from __future__ import annotations

import copy
import difflib
import json
import subprocess
import threading
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any, Mapping, Optional, Sequence, Union
from urllib.parse import urlsplit
from urllib.request import Request, urlopen


class Receiver:
"""An in-memory OTLP/HTTP receiver for tests.

The receiver only listens on loopback and never forwards received spans.
"""

def __init__(self) -> None:
self._spans: list[dict[str, Any]] = []
self._requests: list[dict[str, Any]] = []
self._lock = threading.Lock()
owner = self

class Handler(BaseHTTPRequestHandler):
def _read_body(self) -> bytes:
# The Node OTLP exporter streams with Transfer-Encoding: chunked
# and sends no Content-Length, so both framings are accepted.
if "chunked" in self.headers.get("Transfer-Encoding", "").lower():
chunks = []
while True:
size_line = self.rfile.readline().split(b";", 1)[0].strip()
size = int(size_line, 16)
if size == 0:
while self.rfile.readline() not in (b"\r\n", b"\n", b""):
pass
return b"".join(chunks)
chunks.append(self.rfile.read(size))
self.rfile.readline()
length = int(self.headers.get("Content-Length", "0"))
return self.rfile.read(length)

def do_POST(self) -> None: # noqa: N802 - required by BaseHTTPRequestHandler
path = urlsplit(self.path).path
if path not in ("/v1/traces", "/tracer/v1/traces"):
self.send_error(HTTPStatus.NOT_FOUND)
return

try:
body = self._read_body()
content_type = self.headers.get("Content-Type", "").lower()

if content_type.startswith("application/json"):
request = json.loads(body.decode("utf-8"))
elif content_type.startswith(("application/x-protobuf", "application/protobuf")):
from google.protobuf.json_format import MessageToDict
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
ExportTraceServiceRequest,
)

protobuf_request = ExportTraceServiceRequest()
protobuf_request.ParseFromString(body)
request = MessageToDict(protobuf_request)
else:
self.send_error(HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
return

spans: list[dict[str, Any]] = []
resource_attributes: list[dict[str, Any]] = []
for resource_spans in request.get(
"resourceSpans", request.get("resource_spans", [])
):
resource_attributes.append(
_flatten_attributes(
resource_spans.get("resource", {}).get("attributes", [])
)
)
for scope_spans in resource_spans.get(
"scopeSpans", resource_spans.get("scope_spans", [])
):
spans.extend(scope_spans.get("spans", []))
except (ImportError, TypeError, UnicodeDecodeError, ValueError) as error:
self.send_error(HTTPStatus.BAD_REQUEST, str(error))
return

with owner._lock:
owner._spans.extend(copy.deepcopy(spans))
owner._requests.append(
{
"path": path,
"headers": {
key.lower(): value for key, value in self.headers.items()
},
"resource_attributes": resource_attributes,
}
)
self.send_response(HTTPStatus.OK)
self.send_header("Content-Length", "0")
self.end_headers()

def log_message(self, _format: str, *args: Any) -> None:
return

self._server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
self.origin = "http://127.0.0.1:{0}".format(self._server.server_port)
self.endpoint = "{0}/v1/traces".format(self.origin)
self.collector_endpoint = "{0}/tracer/v1/traces".format(self.origin)
self._thread = threading.Thread(target=self._server.serve_forever, daemon=True)
self._thread.start()

def __enter__(self) -> "Receiver":
return self

def __exit__(self, _type: Any, _value: Any, _traceback: Any) -> None:
self._server.shutdown()
self._server.server_close()
self._thread.join()

def spans(self) -> list[dict[str, Any]]:
"""Return a copy of the decoded spans received so far."""
with self._lock:
return copy.deepcopy(self._spans)

def requests(self) -> list[dict[str, Any]]:
"""Return one record per accepted export: path, lower-cased headers, resource attributes."""
with self._lock:
return copy.deepcopy(self._requests)

def clear(self) -> None:
"""Remove every decoded span and request record received so far."""
with self._lock:
self._spans.clear()
self._requests.clear()


def _flatten_attributes(attributes: Sequence[Mapping[str, Any]]) -> dict[str, Any]:
"""Turn OTLP key/value attributes into a plain dict of scalar values."""
flat: dict[str, Any] = {}
for attribute in attributes:
value = attribute.get("value", {})
for kind in ("stringValue", "boolValue", "intValue", "doubleValue"):
if kind in value:
flat[attribute.get("key", "")] = value[kind]
break
else:
flat[attribute.get("key", "")] = value
return flat


def run(
argv: Sequence[str],
env: Mapping[str, str],
stdin: Optional[bytes],
timeout: float,
) -> subprocess.CompletedProcess[bytes]:
"""Run one process and return its captured output, including after a timeout."""
process = subprocess.Popen(
argv,
env=dict(env),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
timed_out = False
try:
stdout, stderr = process.communicate(input=stdin, timeout=timeout)
except subprocess.TimeoutExpired:
timed_out = True
process.kill()
stdout, stderr = process.communicate()

result = subprocess.CompletedProcess(argv, process.returncode, stdout, stderr)
result.timed_out = timed_out
return result


def post_otlp(spans: dict[str, Any], endpoint: str) -> int:
"""Post one JSON-encoded OTLP request to a loopback receiver."""
parsed = urlsplit(endpoint)
if parsed.scheme != "http" or parsed.hostname != "127.0.0.1":
raise ValueError("post_otlp only sends to a loopback HTTP endpoint")

request = Request(
endpoint,
data=json.dumps(spans).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urlopen(request, timeout=5) as response:
return response.status


def compare(actual: Sequence[dict[str, Any]], golden_path: Union[str, Path]) -> None:
"""Fail with a diff unless span names, attributes, and statuses match a golden."""
with Path(golden_path).open(encoding="utf-8") as golden_file:
expected = json.load(golden_file)

def canonical(spans: Sequence[dict[str, Any]]) -> list[dict[str, Any]]:
normalized = []
for span in spans:
attributes = span.get("attributes", [])
if isinstance(attributes, dict):
attributes = [
{"key": key, "value": value} for key, value in attributes.items()
]
normalized.append(
{
"name": span.get("name", ""),
"attributes": sorted(
attributes,
key=lambda attribute: json.dumps(
attribute, sort_keys=True, separators=(",", ":")
),
),
"status": span.get("status", {}),
}
)
return normalized

actual_normalized = canonical(actual)
expected_normalized = canonical(expected)
if actual_normalized != expected_normalized:
diff = "\n".join(
difflib.unified_diff(
json.dumps(expected_normalized, indent=2, sort_keys=True).splitlines(),
json.dumps(actual_normalized, indent=2, sort_keys=True).splitlines(),
fromfile=str(golden_path),
tofile="actual",
lineterm="",
)
)
print(diff)
raise AssertionError("spans do not match golden")
Loading
Loading