Spaces:
Sleeping
Sleeping
File size: 2,663 Bytes
1fff71f |
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 |
#!/usr/bin/env python3
"""Test script to verify distributed tracing between webapp and API."""
import sys
import time
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
import requests
# Setup OpenTelemetry (use Docker network hostname)
otlp_exporter = OTLPSpanExporter(
endpoint="http://jaeger:4318/v1/traces",
timeout=2,
)
provider = TracerProvider()
processor = BatchSpanProcessor(otlp_exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("test-distributed-tracing")
# Create a parent span and make HTTP request to API
print("Creating parent span and calling API...")
with tracer.start_as_current_span("test-webapp-request") as parent_span:
parent_span.set_attribute("test.type", "distributed_tracing")
# Inject trace context into headers
headers = {
"Authorization": "Bearer dev-token-change-in-production",
"X-User-ID": "test-user",
"Content-Type": "application/json",
}
TraceContextTextMapPropagator().inject(headers)
print(f"Injected headers: {headers}")
print(f"Parent span trace_id: {format(parent_span.get_span_context().trace_id, '032x')}")
print(f"Parent span span_id: {format(parent_span.get_span_context().span_id, '016x')}")
# Make API request (use Docker network hostname)
with tracer.start_as_current_span("http.request"):
try:
response = requests.get(
"http://api:4004/sessions",
headers=headers,
timeout=5,
)
print(f"\nAPI response status: {response.status_code}")
print(f"Sessions returned: {len(response.json())}")
except Exception as e:
print(f"Error calling API: {e}", file=sys.stderr)
sys.exit(1)
print("\nWaiting 2 seconds for spans to flush to Jaeger...")
time.sleep(2)
trace_id = format(parent_span.get_span_context().trace_id, '032x')
print(f"\nβ
Test complete!")
print(f"View trace in Jaeger UI:")
print(f" http://localhost:16686/trace/{trace_id}")
print(f"\nExpected structure:")
print(f" test-webapp-request (this script)")
print(f" ββ http.request (HTTP client)")
print(f" ββ GET /sessions (API middleware) <-- Should be child of http.request")
print(f" ββ SessionManager.ListSessions")
print(f" ββ PostgresStorage.ListSessions")
|