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

Uploaded Python 3

File details

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

File metadata

  • Download URL: moduna-0.1.7.tar.gz
  • Upload date:
  • Size: 181.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for moduna-0.1.7.tar.gz
Algorithm Hash digest
SHA256 0a457c1fce1fcdd9b4692f4220003daa86dbfe7115a1eb3e844f8dfba9921ec2
MD5 936c648b2c4e85b0b30b70e18b687fc1
BLAKE2b-256 272f32d69a3e9aebe4bdd17a40e96fb58252b70ef5c366d861a7daeab56d504d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: moduna-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for moduna-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 41b8f9f469aaf05c06dcdc8ce3291dcbfaad8de02b55e83f3de3d3a010fdb6ec
MD5 4ab9456ac5cec97dd3e5c5a43422c106
BLAKE2b-256 f50ed02e57a05b89dfe36b83e9a81b7b6421669d44c1bc5da502ee685fa34157

See more details on using hashes here.

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