Skip to content

Commit

Permalink
add gen spans script
Browse files Browse the repository at this point in the history
Signed-off-by: Teo <[email protected]>
  • Loading branch information
teocns committed Jan 5, 2025
1 parent 738fd85 commit 6d770ed
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/open-telemetry/generate_spans.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6d770ed

Please sign in to comment.