How-To: Instrument Python Tasks with OTEL
Alli Workflows will automatically run your Python tasks with instrumentation set up for you when you configure your tasks with specific environment variables.
See Setting up your tools for Tracing with DataDog for instructions for how to instrument your code.
In order for Workflows to capture your tracing information, you need to set the WORKFLOWS_PYTHON_OTEL
environment variable to TRUE
and set the OTEL_RESOURCE_ATTRIBUTES
environment variable to name your service, e.g. service.name=my-python-task
.
NOTE: You don’t have to specific the opentelemetry-distro
Python package in your task. The image that Workflows uses has that preinstalled for you.
Example

The above shows up in Datadog as:

Code
import base64
import json
import time
import os
import pprint
import logging
from opentelemetry import trace, propagate
def extract_marketplace_trace_context():
raw_context = os.environ.get('MARKETPLACE_OTEL_PROPAGATION')
if raw_context is None:
return None
try:
raw = json.loads(base64.urlsafe_b64decode(raw_context + '=='))
pprint.pprint(raw)
return propagate.extract(raw)
except Exception as e:
logging.exception(e)
return None
def main():
time.sleep(1)
print('hello, from python main 549')
if __name__ == '__main__':
context = extract_marketplace_trace_context()
tracer = trace.get_tracer("workflows.python.auto-otel")
with tracer.start_as_current_span('python-script-549-main', context=context):
main()