I recently set up multi-tenant Tempo Instance and wanted a quick solution to dump traces into an OTEL Collector. I couldnā€™t find a simple tutorial from which I could just copy and paste commands, so below is a super quick demo container to achieve this.

Start a Python Container

kubectl run python --image python:3.9 --env="OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector-headless:4317" -- sleep 999d

Exec into the container and install the OTel Python packages, as well as an editor.

pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp
apt update && apt install nano

Insert this simple script into a Python file

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
 
import time
import os
 
endpoint = os.environ['OTEL_EXPORTER_OTLP_ENDPOINT']
 
if __name__ == "__main__":
    trace.set_tracer_provider(TracerProvider())
    trace.get_tracer_provider().add_span_processor(
        BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint))
    )
 
    tracer = trace.get_tracer(__name__)
 
    with tracer.start_as_current_span("foo"):
        time.sleep(0.1)
        print("span1")
        with tracer.start_as_current_span("bar"):
            time.sleep(0.1)
            print("span2")

Run the script & you should see this trace appear.

Remember to delete the container after you are done with kubectl delete pod python

šŸ”­ Happy Tracing! :)

Hits