#!/usr/bin/env python3 """Create fresh OTLP JSON examples locally. This script makes no network requests.""" import argparse import json import secrets import time from pathlib import Path def attributes(values): return [{"key": key, "value": {"stringValue": value}} for key, value in values.items()] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output-dir", type=Path, required=True) parser.add_argument("--service", default="corridora-quickstart") args = parser.parse_args() if not args.service.strip(): parser.error("--service must not be empty") end_ns = time.time_ns() start_ns = end_ns - 100_000_000 trace_id = secrets.token_hex(16) span_id = secrets.token_hex(8) resource = {"attributes": attributes({ "service.name": args.service, "service.namespace": "documentation", "service.version": "quickstart", "deployment.environment.name": "documentation", })} scope = {"name": "corridora.public-docs.example", "version": "1.0"} span = { "traceId": trace_id, "spanId": span_id, "name": "quickstart.operation", "kind": 1, "startTimeUnixNano": str(start_ns), "endTimeUnixNano": str(end_ns), "status": {"code": 1}, "attributes": attributes({"example.source": "public-docs"}), } log = { "timeUnixNano": str(end_ns), "observedTimeUnixNano": str(end_ns), "severityNumber": 9, "severityText": "INFO", "body": {"stringValue": "Quick start operation completed"}, "traceId": trace_id, "spanId": span_id, "flags": 1, } event = { **log, "body": {"stringValue": "Documentation example deployment"}, "attributes": attributes({ "event.name": "deployment", "deployment.id": "example-" + trace_id, }), } payloads = { "traces.json": {"resourceSpans": [{ "resource": resource, "scopeSpans": [{"scope": scope, "spans": [span]}], }]}, "logs.json": {"resourceLogs": [{ "resource": resource, "scopeLogs": [{"scope": scope, "logRecords": [log]}], }]}, "metrics.json": {"resourceMetrics": [{ "resource": resource, "scopeMetrics": [{"scope": scope, "metrics": [{ "name": "corridora.quickstart.value", "description": "Synthetic documentation example gauge", "unit": "1", "gauge": {"dataPoints": [{ "timeUnixNano": str(end_ns), "asDouble": 1.0, }]}, }]}], }]}, "events.json": {"resourceLogs": [{ "resource": resource, "scopeLogs": [{"scope": scope, "logRecords": [event]}], }]}, } args.output_dir.mkdir(parents=True, exist_ok=True) for filename, payload in payloads.items(): (args.output_dir / filename).write_text(json.dumps(payload, indent=2) + "\n") print(json.dumps({ "service": args.service, "trace_id": trace_id, "span_id": span_id, "timestamp_unix_nano": str(end_ns), "output_dir": str(args.output_dir), }, indent=2)) if __name__ == "__main__": main()