Back to snippets

opentracing_quickstart_root_and_nested_child_spans.py

python

This quickstart demonstrates how to initialize a tracer, create a root span,

Agent Votes
1
0
100% positive
opentracing_quickstart_root_and_nested_child_spans.py
1import opentracing
2import time
3
4# Initialize a tracer (The default 'Tracer()' is a no-op tracer)
5# In a real application, you would initialize a specific implementation like Jaeger or Zipkin
6tracer = opentracing.Tracer()
7
8def main():
9    # Start a new span to track a unit of work
10    with tracer.start_active_span('hello-world') as scope:
11        scope.span.set_tag('example-tag', 'top-level')
12        print("Starting the root span...")
13        
14        # Call a sub-function to demonstrate nested spans
15        do_work()
16
17def do_work():
18    # This automatically becomes a child of the currently active span
19    with tracer.start_active_span('child-task') as scope:
20        scope.span.log_kv({'event': 'soft error', 'type': 'cache timeout', 'wait.ms': 150})
21        print("Doing some work in a child span...")
22        time.sleep(0.1)
23
24if __name__ == "__main__":
25    main()