#Example of adding custom otel spans to python sdk

1 messages · Page 1 of 1 (latest)

marsh flume
#

I saw an example of using custom spans with go, but was wondering if there is an example with the python sdk?

pliant pike
#

Sure!

import anyio
from opentelemetry import trace
import dagger

tracer = trace.get_tracer("my_module")


@dagger.object_type
class MyModule:
    ...

    @function
    async def test_all(self):
        """Run the test suite on multiple Python versions."""
        async with anyio.create_task_group() as tg:
            for version in self.supported_versions():
                tg.start_soon(self.test_version, version)

    async def test_version(self, version: str):
        with tracer.start_as_current_span(f"Test Python {version}"):
            await self.test(version).default().sync()

Documentation here: https://opentelemetry.io/docs/languages/python/cookbook/#create-a-new-span