-
Notifications
You must be signed in to change notification settings - Fork 157
fix(logging): make LoggerAdapter OpenTelemetry-safe (Issue #837) #1283
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
Open
harsh543
wants to merge
11
commits into
temporalio:main
from
harsh543:fix/loggeradapter-otel-extra-837
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ac1e8a
fix(logging): add OTel-safe extra modes for LoggerAdapter
harsh543 d3c5ad0
test: add explicit OTel safety tests for flatten mode
harsh543 2307d25
refactor: address review feedback on LoggerAdapter extra modes
harsh543 3e29635
test: parameterize activity logging tests, revert unrelated README ch...
harsh543 3b51ef2
test: improve workflow logging test structure and cleanup
harsh543 24194d2
Merge branch 'main' into fix/loggeradapter-otel-extra-837
tconley1428 71b0100
Merge branch 'main' into fix/loggeradapter-otel-extra-837
harsh543 0a8d3d0
Merge branch 'main' into fix/loggeradapter-otel-extra-837
harsh543 730abbf
style: fix ruff formatting in test files
harsh543 302af5d
fix: resolve pyright and basedpyright lint errors in CI
harsh543 8d66e91
Merge branch 'main' into fix/loggeradapter-otel-extra-837
tconley1428 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Internal utilities for Temporal logging. | ||
|
|
||
| This module is internal and may change at any time. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping, MutableMapping | ||
| from typing import Any, Literal | ||
|
|
||
| TemporalLogExtraMode = Literal["dict", "flatten"] | ||
| """Mode controlling how Temporal context is added to log record extra. | ||
|
|
||
| Values: | ||
| dict: (default) Add context as a nested dictionary under a single key. | ||
| This is the original behavior. Suitable for logging handlers that | ||
| support nested structures. | ||
| flatten: Add each context field as a separate top-level key with a | ||
| namespaced prefix. Values that are not primitives (str/int/float/bool) | ||
| are converted to strings. This mode is recommended for OpenTelemetry | ||
| and other logging pipelines that require flat, scalar attributes. | ||
| """ | ||
|
|
||
|
|
||
| def _apply_temporal_context_to_extra( # pyright: ignore[reportUnusedFunction] | ||
| extra: MutableMapping[str, Any], | ||
| *, | ||
| key: str, | ||
| ctx: Mapping[str, Any], | ||
| mode: TemporalLogExtraMode, | ||
| ) -> None: | ||
| """Apply temporal context to log record extra based on the configured mode. | ||
|
|
||
| Args: | ||
| extra: The mutable extra dict to update. | ||
| key: The base key (e.g., "temporal_workflow"). In dict mode this is | ||
| used directly. In flatten mode the prefix is derived by replacing | ||
| underscores with dots (e.g., "temporal.workflow"). | ||
| ctx: The context mapping containing temporal fields. | ||
| mode: The mode controlling how context is added. | ||
| """ | ||
| if mode == "flatten": | ||
| prefix = key.replace("_", ".") | ||
| for k, v in ctx.items(): | ||
| # Ensure value is a primitive type safe for OTel attributes | ||
| if not isinstance(v, (str, int, float, bool, type(None))): | ||
| v = str(v) | ||
| extra[f"{prefix}.{k}"] = v | ||
| else: | ||
| extra[key] = dict(ctx) | ||
|
|
||
|
|
||
| def _update_temporal_context_in_extra( # pyright: ignore[reportUnusedFunction] | ||
| extra: MutableMapping[str, Any], | ||
| *, | ||
| key: str, | ||
| update_ctx: Mapping[str, Any], | ||
| mode: TemporalLogExtraMode, | ||
| ) -> None: | ||
| """Update existing temporal context in extra with additional fields. | ||
|
|
||
| This is used when adding update info to existing workflow context. | ||
|
|
||
| Args: | ||
| extra: The mutable extra dict to update. | ||
| key: The base key (e.g., "temporal_workflow"). In dict mode this is | ||
| used directly. In flatten mode the prefix is derived by replacing | ||
| underscores with dots (e.g., "temporal.workflow"). | ||
| update_ctx: Additional context fields to add/update. | ||
| mode: The mode controlling how context is added. | ||
| """ | ||
| if mode == "flatten": | ||
| prefix = key.replace("_", ".") | ||
| for k, v in update_ctx.items(): | ||
| # Ensure value is a primitive type safe for OTel attributes | ||
| if not isinstance(v, (str, int, float, bool, type(None))): | ||
| v = str(v) | ||
| extra[f"{prefix}.{k}"] = v | ||
| else: | ||
| extra.setdefault(key, {}).update(update_ctx) |
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
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.