Skip to main content

Python OpenTelemetry helpers for Moduna AI traces.

Project description

moduna-otel

Python OpenTelemetry helpers for Moduna AI traces.

moduna-otel makes Moduna tracing easy to add with one import and one instantiation. It configures OpenTelemetry once, exports traces to Moduna over OTLP HTTP, and provides ergonomic helpers for Python LangChain applications.

Installation

Install from the built wheel or from PyPI once published:

pip install moduna-otel

For LangChain callback support, install the optional LangChain dependency:

pip install "moduna-otel[langchain]"

When working from this repository, use uv:

uv sync --extra dev --extra langchain

Getting Started

Set your Moduna API key with an environment variable:

export MODUNA_API_KEY="md_live_..."

Create the SDK once near application startup:

from moduna_otel import ModunaOTEL

otel = ModunaOTEL(
    agent_name="support-agent",
    framework="langchain",
)

You can also pass the API key directly:

from moduna_otel import ModunaOTEL

otel = ModunaOTEL(
    agent_name="support-agent",
    framework="langchain",
    api_key="md_live_...",
)

Multiple ModunaOTEL(...) instances share the same OpenTelemetry provider, so creating per-module wrappers will not create duplicate exporters.

LangChain

Create a LangChain callback handler and pass it into your model or chain call:

from moduna_otel import ModunaOTEL

otel = ModunaOTEL(
    agent_name="support-agent",
    framework="langchain",
)

handler = otel.langchain_handler(
    {
        "conversation_id": "conversation-123",
        "session_id": "session-456",
    }
)

result = chain.invoke(
    {"input": "Summarize this ticket"},
    config={
        "callbacks": [handler],
        "metadata": {
            "conversationId": "conversation-123",
            "sessionId": "session-456",
        },
    },
)

The handler records LLM/chat model spans, prompts, completions, token usage when available, streamed token events, Moduna conversation/session attributes, and errors.

For applications that use global LangChain callbacks, registration is available as a best-effort helper:

otel.register_global_langchain_handler(
    {
        "conversation_id": "conversation-123",
        "session_id": "session-456",
    }
)

LangChain global callback APIs vary across versions. If global registration is not available in your installed LangChain version, Moduna logs one warning and continues without breaking application code.

Manual Instrumentation

Use instrument() to wrap custom model calls or application work in a Moduna CLIENT span:

from moduna_otel import ModunaOTEL

otel = ModunaOTEL(agent_name="support-agent", framework="langchain")


def call_model(span):
    span.set_attribute("app.operation", "ticket-summary")
    return model.invoke("Summarize this ticket")


result = otel.instrument(
    "support-agent.summary",
    call_model,
    {
        "conversation_id": "conversation-123",
        "session_id": "session-456",
    },
)

If the callback raises an exception, the exception is recorded on the span and then re-raised.

Configuration

ModunaOTEL accepts keyword arguments:

ModunaOTEL(
    agent_name="support-agent",
    framework="langchain",  # Python currently supports LangChain
    api_key=None,           # defaults to MODUNA_API_KEY
    headers=None,           # extra OTLP HTTP headers
    auto_shutdown=True,     # flush on process exit
)

vercel-ai-sdk is not supported by this Python package because Vercel AI SDK is not available for Python. Use Moduna's TypeScript SDK for Vercel AI SDK applications.

It also accepts a mapping:

otel = ModunaOTEL(
    {
        "agentName": "support-agent",
        "framework": "langchain",
        "autoShutdown": True,
    }
)

Development

Install development dependencies:

uv sync --extra dev --extra langchain

Run tests:

uv run pytest

Format code:

uv run ruff format

Run the linter:

uv run ruff check

Build source and wheel distributions:

uv build

The built artifacts are written to dist/.

Contributing

Contributions are welcome. Please keep changes focused and aligned with the TypeScript SDK behavior.

Before opening a pull request:

  1. Add or update focused pytest coverage for behavior changes.
  2. Run uv run pytest.
  3. Run uv run ruff format and uv run ruff check.
  4. Run uv build for packaging changes.
  5. Keep telemetry failures non-fatal to user application code.
  6. Avoid broad refactors unless they are required for the change.

Useful areas for contribution include LangChain version compatibility, additional token-usage extraction cases, and documentation examples for common framework integrations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moduna-0.1.5.tar.gz (176.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moduna-0.1.5-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file moduna-0.1.5.tar.gz.

File metadata

  • Download URL: moduna-0.1.5.tar.gz
  • Upload date:
  • Size: 176.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for moduna-0.1.5.tar.gz
Algorithm Hash digest
SHA256 3c678bdc35afd09d39c106f1098d32acbbcea9d780551861712afaaebcb7ae00
MD5 3c7fd133f342e646f0fbf8b643ae33b2
BLAKE2b-256 38f6a58a64d8d790532563b55979b99f86faf40c9c4b33ee7ebebe0561d97d3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for moduna-0.1.5.tar.gz:

Publisher: python-publish.yml on Moduna-AI/python-otel-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file moduna-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: moduna-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for moduna-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 06efe93a1e71f91712afe1c7779979a3541f42926455d4e69d4719e5290e626d
MD5 cf2368cf08acb17dc0cdabbda5352c42
BLAKE2b-256 1a1b5191de57727be017ec3b3026e5318aa53d66286063e66b06e4c447c7e8cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for moduna-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on Moduna-AI/python-otel-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page