OpenTelemetry Tortoise ORM Instrumentation

pypi

This library allows tracing queries made by tortoise ORM backends, mysql, postgres and sqlite.

Installation

pip install opentelemetry-instrumentation-tortoiseorm

References

Instrument tortoise-orm to report SQL queries.

Usage

from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.instrumentation.tortoiseorm import TortoiseORMInstrumentor

app = FastAPI()
tracer = TracerProvider(resource=Resource({SERVICE_NAME: "FastAPI"}))
TortoiseORMInstrumentor().instrument(tracer_provider=tracer)

register_tortoise(
    app,
    db_url="sqlite://sample.db",
    modules={"models": ["example_app.db_models"]}
)

API

class opentelemetry.instrumentation.tortoiseorm.TortoiseORMInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor

An instrumentor for Tortoise-ORM See BaseInstrumentor

instrumentation_dependencies()[source]

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.