Skip to content

mirascope.integrations.otel

configure

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

Parameters:

Name Type Description Default
processors Sequence[SpanProcessor] | None

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/integrations/otel/_utils.py
def configure(
    processors: Sequence[SpanProcessor] | None = 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

Wraps a Mirascope function with OpenTelemetry.

Example:

from mirascope.core import anthropic, prompt_template
from mirascope.integrations.otel import with_otel, configure

configure()


def format_book(title: str, author: str) -> str:
    return f"{title} by {author}"


@with_otel()
@anthropic.call(model="claude-3-5-sonnet-20240620", tools=[format_book])
def recommend_book(genre: str) -> str:
    return f"Recommend a {genre} book"


print(recommend_book("fantasy"))
Source code in mirascope/integrations/otel/_with_otel.py
def with_otel() -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
    """Wraps a Mirascope function with OpenTelemetry.

    Example:

    ```python
    from mirascope.core import anthropic, prompt_template
    from mirascope.integrations.otel import with_otel, configure

    configure()


    def format_book(title: str, author: str) -> str:
        return f"{title} by {author}"


    @with_otel()
    @anthropic.call(model="claude-3-5-sonnet-20240620", tools=[format_book])
    def recommend_book(genre: str) -> str:
        return f"Recommend a {genre} book"


    print(recommend_book("fantasy"))
    ```
    """

    return middleware_factory(
        custom_context_manager=custom_context_manager,
        handle_call_response=handle_call_response,
        handle_call_response_async=handle_call_response_async,
        handle_stream=handle_stream,
        handle_stream_async=handle_stream_async,
        handle_response_model=handle_response_model,
        handle_response_model_async=handle_response_model_async,
        handle_structured_stream=handle_structured_stream,
        handle_structured_stream_async=handle_structured_stream_async,
    )

with_hyperdx

Decorator to wrap a function with hyperdx.

Example:

import os

from mirascope.core import anthropic, prompt_template
from mirascope.integrations.otel import with_hyperdx

os.environ["HYPERDX_API_KEY"] = "YOUR_API_KEY"


def format_book(title: str, author: str):
    return f"{title} by {author}"


@with_hyperdx()
@anthropic.call(model="claude-3-5-sonnet-20240620", tools=[format_book])
def recommend_book(genre: str) -> str:
    return f"Recommend a {genre} book"


print(recommend_book("fantasy"))
Source code in mirascope/integrations/otel/_with_hyperdx.py
def with_hyperdx() -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
    """Decorator to wrap a function with hyperdx.

    Example:

    ```python
    import os

    from mirascope.core import anthropic, prompt_template
    from mirascope.integrations.otel import with_hyperdx

    os.environ["HYPERDX_API_KEY"] = "YOUR_API_KEY"


    def format_book(title: str, author: str):
        return f"{title} by {author}"


    @with_hyperdx()
    @anthropic.call(model="claude-3-5-sonnet-20240620", tools=[format_book])
    def recommend_book(genre: str) -> str:
        return f"Recommend a {genre} book"


    print(recommend_book("fantasy"))
    ```
    """
    provider = trace.get_tracer_provider()
    if not isinstance(provider, TracerProvider):
        configure(
            processors=[
                BatchSpanProcessor(
                    OTLPSpanExporter(
                        endpoint="https://in-otel.hyperdx.io/v1/traces",
                        headers={"authorization": os.getenv("HYPERDX_API_KEY", "")},
                    )
                )
            ]
        )
    return with_otel()