Files

63 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Real Python OTLP SDK wire-format test against a local in-process receiver.
Requires requirements.txt (does not require Docker or external services).
This is not a Collector/backend integration test.
"""
import os
import sys
import time
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from opentelemetry.trace import SpanKind
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import ExportLogsServiceRequest
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import ExportMetricsServiceRequest
from app import telemetry as ot
received={k:[] for k in ('/v1/traces','/v1/logs','/v1/metrics')}
class Receiver(BaseHTTPRequestHandler):
def do_POST(self):
data=self.rfile.read(int(self.headers['Content-Length']))
received[self.path].append(data)
self.send_response(200);self.send_header('Content-Type','application/x-protobuf');self.send_header('Content-Length','0');self.end_headers()
def log_message(self,*args): pass
def main():
server=ThreadingHTTPServer(('127.0.0.1',0),Receiver)
threading.Thread(target=server.serve_forever,daemon=True).start()
os.environ['OTEL_ENABLED']='yes'
os.environ['OTEL_EXPORTER_OTLP_ENDPOINT']='http://127.0.0.1:'+str(server.server_port)
ot.init('worker')
with ot.span('POST /api/v1/chats/{chat_id}/attachments',kind=SpanKind.SERVER) as root:
stored=ot.inject(); trace_id=ot.trace_id()
with ot.span('rabbitmq.publish',kind=SpanKind.PRODUCER,context=ot.extract(stored)) as producer:
sent=ot.inject()
with ot.span('rabbitmq.consume',kind=SpanKind.CONSUMER,context=ot.extract(sent)) as consumer:
ot.event('job.ready',job_id='synthetic-id',attempt=2)
ot.count('lab_jobs',{'job_type':'attachment.process.v1','outcome':'ready'})
ot.observe('lab_job_duration',2.5,{'job_type':'attachment.process.v1','outcome':'ready'})
ot.observe('lab_ready_duration',5.0,{'job_type':'attachment.process.v1'})
class FakeS3:
def put_object(self,**kw): return True
ot.S3Tracing(FakeS3()).put_object(Key='SECRET_FILENAME_SENTINEL',Body=b'SECRET_CONTENT_SENTINEL')
ot.flush()
for provider in reversed(ot.providers): provider.shutdown()
server.shutdown()
spans=[s for raw in received['/v1/traces'] for r in ExportTraceServiceRequest.FromString(raw).resource_spans for scope in r.scope_spans for s in scope.spans]
byname={s.name:s for s in spans}
assert byname['rabbitmq.publish'].parent_span_id==root.get_span_context().span_id.to_bytes(8,'big')
assert byname['rabbitmq.consume'].parent_span_id==producer.get_span_context().span_id.to_bytes(8,'big')
assert all(s.trace_id.hex()==trace_id for s in spans)
assert 's3.put_object' in byname
logs=[l for raw in received['/v1/logs'] for r in ExportLogsServiceRequest.FromString(raw).resource_logs for scope in r.scope_logs for l in scope.log_records]
assert any(l.trace_id.hex()==trace_id and l.span_id==consumer.get_span_context().span_id.to_bytes(8,'big') and l.body.string_value=='job.ready' for l in logs)
points=[m for raw in received['/v1/metrics'] for r in ExportMetricsServiceRequest.FromString(raw).resource_metrics for scope in r.scope_metrics for m in scope.metrics]
assert any(m.name=='lab_jobs' and m.sum.data_points[0].as_int==1 for m in points)
assert any(m.name=='lab_job_duration' and list(m.histogram.data_points[0].explicit_bounds)==list(ot.BUCKETS) for m in points)
for raw in sum(received.values(),[]): assert b'SECRET_FILENAME_SENTINEL' not in raw and b'SECRET_CONTENT_SENTINEL' not in raw
print('PASS: real OTLP trace/log/metric encoding, parent links, histogram buckets, safe S3 attributes')
if __name__=='__main__': main()