Python OpenTelemetry helpers for Moduna AI traces.
Project description
moduna
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:
- Add or update focused pytest coverage for behavior changes.
- Run
uv run pytest. - Run
uv run ruff formatanduv run ruff check. - Run
uv buildfor packaging changes. - Keep telemetry failures non-fatal to user application code.
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c27aa3b54e5accbf4e565ed5d9de0e97bf7dbaa61c7d2812cbc86dc1ab91a59
|
|
| MD5 |
e8b253dcc1871100197ab826a47396a2
|
|
| BLAKE2b-256 |
60add97e2c04f926f129d445759020b8444a496965488222264605a11a93936a
|
Provenance
The following attestation bundles were made for moduna-0.1.6.tar.gz:
Publisher:
python-publish.yml on Moduna-AI/python-otel-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moduna-0.1.6.tar.gz -
Subject digest:
1c27aa3b54e5accbf4e565ed5d9de0e97bf7dbaa61c7d2812cbc86dc1ab91a59 - Sigstore transparency entry: 1658223722
- Sigstore integration time:
-
Permalink:
Moduna-AI/python-otel-sdk@8f6cbc1c6d6d85512e1bae18e3acfb4cf39dffc2 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/Moduna-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8f6cbc1c6d6d85512e1bae18e3acfb4cf39dffc2 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85ecd4bdfec9b887eb16399118410a58db93f69eb9642bb2fa85d44eb21e4b8b
|
|
| MD5 |
1c82071d02ad4965810701340c927056
|
|
| BLAKE2b-256 |
66c500b594ac7e07f5a9d40e5b6c5be27e525a8ed4f1f9692192f5b3115d97b7
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moduna-0.1.6-py3-none-any.whl -
Subject digest:
85ecd4bdfec9b887eb16399118410a58db93f69eb9642bb2fa85d44eb21e4b8b - Sigstore transparency entry: 1658223831
- Sigstore integration time:
-
Permalink:
Moduna-AI/python-otel-sdk@8f6cbc1c6d6d85512e1bae18e3acfb4cf39dffc2 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/Moduna-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8f6cbc1c6d6d85512e1bae18e3acfb4cf39dffc2 -
Trigger Event:
release
-
Statement type: