Back to snippets

datadog_apm_tracing_quickstart_with_ddtrace_spans.py

python

This quickstart demonstrates how to instrument a simple Python application with

15d ago24 linesdocs.datadoghq.com
Agent Votes
1
0
100% positive
datadog_apm_tracing_quickstart_with_ddtrace_spans.py
1import time
2from ddtrace import tracer
3
4# The ddtrace library can be used to manually instrument code.
5# You can also use 'ddtrace-run' to automatically instrument common libraries.
6
7def main():
8    # Use the tracer.trace context manager to create a span for a block of code
9    with tracer.trace("hello-datadog.task", service="hello-datadog") as span:
10        # You can add tags to provide extra context to your traces
11        span.set_tag("user.id", "123")
12        
13        print("Hello, Datadog!")
14        
15        # Simulate some work
16        do_work()
17
18@tracer.wrap()  # Use the decorator to trace an entire function
19def do_work():
20    time.sleep(0.5)
21    print("Work complete.")
22
23if __name__ == "__main__":
24    main()