Skip to content

otel

configure(processors=None)

Configures the OpenTelemetry tracer, this function should only be called once.

Parameters:

Name Type Description Default
processors Optional[Sequence[SpanProcessor]]

Optional[Sequence[SpanProcessor]] The span processors to use, if None, a console exporter will be used.

None

Returns:

Type Description
Tracer

The configured tracer.

Source code in mirascope/otel/otel.py
def configure(
    processors: Optional[Sequence[SpanProcessor]] = None,
) -> Tracer:
    """Configures the OpenTelemetry tracer, this function should only be called once.

    Args:
        processors: Optional[Sequence[SpanProcessor]]
            The span processors to use, if None, a console exporter will be used.

    Returns:
        The configured tracer.
    """
    provider = TracerProvider()
    if processors is None:
        provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
    else:
        for processor in processors:
            provider.add_span_processor(processor)
    # NOTE: Sets the global trace provider, should only be called once
    set_tracer_provider(provider)
    return get_tracer("otel")

with_otel(cls)

Wraps a pydantic class with a OTel span.

Source code in mirascope/otel/otel.py
def with_otel(cls):
    """Wraps a pydantic class with a OTel span."""

    provider = get_tracer_provider()
    if not isinstance(provider, TracerProvider):
        configure()
    wrap_mirascope_class_functions(
        cls,
        handle_before_call=handle_before_call,
        handle_after_call=handle_after_call,
    )
    if hasattr(cls, "configuration"):
        cls.configuration = cls.configuration.model_copy(
            update={"llm_ops": [*cls.configuration.llm_ops, mirascope_otel(cls)]}
        )
    return cls