-
Notifications
You must be signed in to change notification settings - Fork 137
feat: add OTLP logging export #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajbozarth
merged 2 commits into
generative-computing:main
from
ajbozarth:feat/otlp-logging-export-mvp
Mar 13, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """OpenTelemetry logging instrumentation for Mellea. | ||
|
|
||
| Provides log export using OpenTelemetry Logs API with OTLP exporter support. | ||
|
|
||
| Configuration via environment variables: | ||
| - MELLEA_LOGS_OTLP: Enable OTLP logs exporter (default: false) | ||
| - OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: Logs-specific endpoint (optional, overrides general) | ||
| - OTEL_EXPORTER_OTLP_ENDPOINT: General endpoint for all signals (fallback) | ||
| - OTEL_SERVICE_NAME: Service name for logs (default: mellea) | ||
|
|
||
| Example: | ||
| export MELLEA_LOGS_OTLP=true | ||
| export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 | ||
|
|
||
| Programmatic usage: | ||
| from mellea.telemetry.logging import get_otlp_log_handler | ||
| import logging | ||
|
|
||
| logger = logging.getLogger("my_logger") | ||
| handler = get_otlp_log_handler() | ||
| if handler: | ||
| logger.addHandler(handler) | ||
| logger.info("This log will be exported via OTLP") | ||
| """ | ||
|
|
||
| import os | ||
| import warnings | ||
| from typing import Any | ||
|
|
||
| # Try to import OpenTelemetry, but make it optional | ||
| try: | ||
| from opentelemetry._logs import set_logger_provider | ||
| from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter | ||
| from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler | ||
| from opentelemetry.sdk._logs.export import BatchLogRecordProcessor | ||
| from opentelemetry.sdk.resources import Resource | ||
|
|
||
| _OTEL_AVAILABLE = True | ||
| except ImportError: | ||
| _OTEL_AVAILABLE = False | ||
|
|
||
| # Configuration from environment variables | ||
| _LOGS_OTLP = _OTEL_AVAILABLE and os.getenv("MELLEA_LOGS_OTLP", "false").lower() in ( | ||
| "true", | ||
| "1", | ||
| "yes", | ||
| ) | ||
| _OTLP_LOGS_ENDPOINT = os.getenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT") or os.getenv( | ||
| "OTEL_EXPORTER_OTLP_ENDPOINT" | ||
| ) | ||
| _SERVICE_NAME = os.getenv("OTEL_SERVICE_NAME", "mellea") | ||
|
|
||
|
|
||
| def _setup_logger_provider() -> Any: | ||
| """Set up the LoggerProvider with OTLP exporter. | ||
|
|
||
| Returns: | ||
| LoggerProvider instance or None if OpenTelemetry is not available or no endpoint configured | ||
| """ | ||
| if not _OTEL_AVAILABLE: | ||
| return None | ||
|
|
||
| if not _OTLP_LOGS_ENDPOINT: | ||
| warnings.warn( | ||
| "OTLP logs exporter is enabled (MELLEA_LOGS_OTLP=true) but no endpoint is configured. " | ||
| "Set OTEL_EXPORTER_OTLP_LOGS_ENDPOINT or OTEL_EXPORTER_OTLP_ENDPOINT to export logs.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return None | ||
|
|
||
| resource = Resource.create({"service.name": _SERVICE_NAME}) # type: ignore | ||
| logger_provider = LoggerProvider(resource=resource) # type: ignore | ||
|
|
||
| try: | ||
| otlp_exporter = OTLPLogExporter(endpoint=_OTLP_LOGS_ENDPOINT) # type: ignore | ||
| logger_provider.add_log_record_processor( | ||
| BatchLogRecordProcessor(otlp_exporter) # type: ignore | ||
| ) | ||
| except Exception as e: | ||
| warnings.warn( | ||
| f"Failed to initialize OTLP logs exporter: {e}. " | ||
| "Logs will not be exported via OTLP.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return None | ||
|
|
||
| set_logger_provider(logger_provider) # type: ignore | ||
| return logger_provider | ||
|
|
||
|
|
||
| # Initialize logger provider if OTLP logging is enabled | ||
| _logger_provider = None | ||
|
|
||
| if _LOGS_OTLP: | ||
| _logger_provider = _setup_logger_provider() | ||
|
|
||
|
|
||
| def get_otlp_log_handler() -> Any: | ||
| """Get an OTLP logging handler for Python's logging module. | ||
|
|
||
| Returns: | ||
| LoggingHandler instance if OTLP logging is enabled and configured, | ||
| None otherwise. | ||
|
|
||
| Example: | ||
| import logging | ||
| from mellea.telemetry.logging import get_otlp_log_handler | ||
|
|
||
| logger = logging.getLogger("my_app") | ||
| handler = get_otlp_log_handler() | ||
| if handler: | ||
| logger.addHandler(handler) | ||
| logger.info("This log will be exported via OTLP") | ||
| """ | ||
| if _logger_provider is None: | ||
| return None | ||
|
|
||
| try: | ||
| handler = LoggingHandler(logger_provider=_logger_provider) # type: ignore | ||
| return handler | ||
| except Exception as e: | ||
| warnings.warn( | ||
| f"Failed to create OTLP logging handler: {e}. " | ||
| "Logs will not be exported via OTLP.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) | ||
| return None |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs probably need to make their way into the published docs website at some point right?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do they not? I've just been making updates to the docs where they already existed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, only things in
docs/docsare added to the website properAPI docs will get picked up, but not the docs/dev/telemetry.md file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look and see if it's a quick fix, otherwise I'll open an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #648 and will tackle it once I finish #608