Skip to main content

Python OpenTelemetry helpers for Moduna AI traces.

Project description

moduna

PyPI version Python versions Package status License GitHub release Upload Python Package Downloads

Python OpenTelemetry helpers for Moduna AI traces.

moduna 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 PyPI:

pip install moduna

For LangChain callback support, install the optional LangChain dependency:

pip install "moduna[langchain]"

The PyPI distribution is moduna; the Python import package is moduna_otel.

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
)

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.6.tar.gz (176.6 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.6-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: moduna-0.1.6.tar.gz
  • Upload date:
  • Size: 176.6 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.6.tar.gz
Algorithm Hash digest
SHA256 1c27aa3b54e5accbf4e565ed5d9de0e97bf7dbaa61c7d2812cbc86dc1ab91a59
MD5 e8b253dcc1871100197ab826a47396a2
BLAKE2b-256 60add97e2c04f926f129d445759020b8444a496965488222264605a11a93936a

See more details on using hashes here.

Provenance

The following attestation bundles were made for moduna-0.1.6.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.6-py3-none-any.whl.

File metadata

  • Download URL: moduna-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 19.0 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 85ecd4bdfec9b887eb16399118410a58db93f69eb9642bb2fa85d44eb21e4b8b
MD5 1c82071d02ad4965810701340c927056
BLAKE2b-256 66c500b594ac7e07f5a9d40e5b6c5be27e525a8ed4f1f9692192f5b3115d97b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for moduna-0.1.6-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