-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add otel logger #481
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
Merged
Changes from 1 commit
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
105 changes: 105 additions & 0 deletions
105
...durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/log_filter.py
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,105 @@ | ||
| """Root-logger filter that stamps OTel trace context onto every log record. | ||
|
|
||
| The filter attaches to a stdlib logging handler and enriches *every* record | ||
| that flows through it: direct ``logging.getLogger().info(...)`` calls, | ||
| child-logger records that propagate to root, and third-party library logs. | ||
|
|
||
| The span/trace identifiers are added as ``LogRecord`` attributes (using | ||
| underscore names, since dotted names are not valid record attributes): | ||
|
|
||
| - ``otel_trace_id``: 32-char hex trace identifier | ||
| - ``otel_span_id``: 16-char hex span identifier | ||
| - ``otel_trace_sampled``: boolean indicating if the trace is sampled | ||
|
|
||
| These attributes are only set when a valid span context is active. Records | ||
| emitted outside an active invocation (e.g. during Lambda teardown) pass through | ||
| unmodified, so any log formatter or schema must treat the fields as optional. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from opentelemetry.trace import TraceFlags | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from aws_durable_execution_sdk_python_otel.plugin import DurableExecutionOtelPlugin | ||
|
|
||
|
|
||
| class OtelContextLogFilter(logging.Filter): | ||
| """Logging filter that injects the active OTel span context onto records. | ||
|
|
||
| The filter is a pure reader of the plugin's current span context. It | ||
| resolves the span at emit time, on the thread that emits the record, via | ||
| ``plugin.get_current_span_context()``. That method returns the active | ||
| operation span inside steps and child contexts (attached to the worker | ||
| thread's OTel context) and falls back to the invocation span for top-level | ||
| handler code. | ||
|
|
||
| The filter never caches identifiers and always returns ``True`` so it never | ||
| drops a record. | ||
|
|
||
| Args: | ||
| plugin: The OTel plugin instance that resolves the current span context. | ||
| """ | ||
|
|
||
| def __init__(self, plugin: DurableExecutionOtelPlugin) -> None: | ||
| super().__init__() | ||
| self._plugin = plugin | ||
|
|
||
| def filter(self, record: logging.LogRecord) -> bool: | ||
| """Stamp the active span context onto the record, then allow it through.""" | ||
| span_context = self._plugin.get_current_span_context() | ||
| if span_context and span_context.is_valid: | ||
| record.otel_trace_id = format(span_context.trace_id, "032x") | ||
| record.otel_span_id = format(span_context.span_id, "016x") | ||
| record.otel_trace_sampled = bool( | ||
| span_context.trace_flags & TraceFlags.SAMPLED | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| def install_log_filter( | ||
| plugin: DurableExecutionOtelPlugin, | ||
| target_logger: logging.Logger | None = None, | ||
| ) -> OtelContextLogFilter | None: | ||
| """Attach an OtelContextLogFilter to a logger's handlers, idempotently. | ||
|
|
||
| The filter is attached to each handler on ``target_logger`` (the root logger | ||
| by default). Attaching to handlers rather than the logger itself ensures | ||
| records propagated from child loggers are also enriched, since handler | ||
| filters run for every record reaching the handler. | ||
|
|
||
| This is safe to call on every invocation: if a handler already has an | ||
| OtelContextLogFilter, it is left as-is, so warm Lambda reuse will not stack | ||
| duplicate filters. A single shared filter instance is reused across all | ||
| handlers. | ||
|
|
||
| Args: | ||
| plugin: The OTel plugin that resolves the current span context. | ||
| target_logger: Logger whose handlers receive the filter. Defaults to the | ||
| root logger, which in AWS Lambda is where runtime log handlers live. | ||
|
|
||
| Returns: | ||
| The filter instance attached to the handlers, or ``None`` if the target | ||
| logger has no handlers to attach to. | ||
| """ | ||
| logger = target_logger if target_logger is not None else logging.getLogger() | ||
|
|
||
| context_filter: OtelContextLogFilter | None = None | ||
| for handler in logger.handlers: | ||
| existing = next( | ||
| (f for f in handler.filters if isinstance(f, OtelContextLogFilter)), | ||
| None, | ||
| ) | ||
| if existing is not None: | ||
| # Reuse the already-installed filter so a single instance is shared. | ||
| context_filter = existing | ||
| continue | ||
| if context_filter is None: | ||
| context_filter = OtelContextLogFilter(plugin) | ||
| handler.addFilter(context_filter) | ||
|
|
||
| return context_filter |
86 changes: 0 additions & 86 deletions
86
...aws-durable-execution-sdk-python-otel/src/aws_durable_execution_sdk_python_otel/logger.py
This file was deleted.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.