From 6d770ed48734f38de986bcb53ab663064b87c688 Mon Sep 17 00:00:00 2001 From: Teo Date: Sun, 5 Jan 2025 17:23:09 +0100 Subject: [PATCH] add gen spans script Signed-off-by: Teo --- examples/open-telemetry/generate_spans.py | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/open-telemetry/generate_spans.py diff --git a/examples/open-telemetry/generate_spans.py b/examples/open-telemetry/generate_spans.py new file mode 100644 index 00000000..55ca30c5 --- /dev/null +++ b/examples/open-telemetry/generate_spans.py @@ -0,0 +1,33 @@ +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 +import time +import random + +# Initialize tracer +provider = TracerProvider() +processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")) +provider.add_span_processor(processor) +trace.set_tracer_provider(provider) + +tracer = trace.get_tracer(__name__) + +def generate_spans(): + while True: + # Create a parent span + with tracer.start_as_current_span("parent_operation") as parent: + parent.set_attribute("custom.attribute", "parent_value") + + # Add some random sleep to simulate work + time.sleep(random.uniform(0.1, 0.5)) + + # Create some child spans + for i in range(random.randint(2, 5)): + with tracer.start_span(f"child_operation_{i}") as child: + child.set_attribute("custom.child.attribute", f"child_value_{i}") + time.sleep(random.uniform(0.1, 0.3)) + +if __name__ == "__main__": + print("Generating spans... Check Jaeger UI at http://localhost:16686") + generate_spans() \ No newline at end of file