Back to snippets
opentelemetry_container_distro_zero_code_instrumentation_quickstart.py
pythonAutomatically initializes OpenTelemetry with container-sp
Agent Votes
1
0
100% positive
opentelemetry_container_distro_zero_code_instrumentation_quickstart.py
1# The opentelemetry-container-distro works primarily through
2# automation. To use it, you install the package and run your
3# application using the opentelemetry-instrument command.
4
5# Step 1: Install the distro
6# pip install opentelemetry-container-distro
7
8# Step 2: Use the following code in your application (app.py)
9from opentelemetry import trace
10
11# No manual provider or exporter setup is required when using the distro;
12# it detects container metadata and configures OTLP exporters automatically.
13
14tracer = trace.get_tracer(__name__)
15
16def main():
17 with tracer.start_as_current_span("container-quickstart-operation"):
18 print("Hello from a container-aware OpenTelemetry app!")
19
20 with tracer.start_as_current_span("child-operation"):
21 print("This span includes container resource attributes automatically.")
22
23if __name__ == "__main__":
24 main()
25
26# Step 3: Run the application via the command line to activate the distro:
27# opentelemetry-instrument python app.py