Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
base: main
Choose a base branch
Loading
from harsh543:fix/loggeradapter-otel-extra-837
Open
Show file tree
Hide file tree
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 Jan 20, 2026
d3c5ad0
test: add explicit OTel safety tests for flatten mode
harsh543 Jan 20, 2026
2307d25
refactor: address review feedback on LoggerAdapter extra modes
harsh543 Jan 28, 2026
3e29635
test: parameterize activity logging tests, revert unrelated README ch...
harsh543 Feb 1, 2026
3b51ef2
test: improve workflow logging test structure and cleanup
harsh543 Feb 3, 2026
24194d2
Merge branch 'main' into fix/loggeradapter-otel-extra-837
tconley1428 Feb 4, 2026
71b0100
Merge branch 'main' into fix/loggeradapter-otel-extra-837
harsh543 Feb 4, 2026
0a8d3d0
Merge branch 'main' into fix/loggeradapter-otel-extra-837
harsh543 Feb 6, 2026
730abbf
style: fix ruff formatting in test files
harsh543 Feb 6, 2026
302af5d
fix: resolve pyright and basedpyright lint errors in CI
harsh543 Feb 22, 2026
8d66e91
Merge branch 'main' into fix/loggeradapter-otel-extra-837
tconley1428 Feb 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions temporalio/_log_utils.py
View file Open in desktop
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)
13 changes: 12 additions & 1 deletion temporalio/activity.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import temporalio.common
import temporalio.converter

from ._log_utils import TemporalLogExtraMode, _apply_temporal_context_to_extra
from .types import CallableType

if TYPE_CHECKING:
Expand Down Expand Up @@ -491,6 +492,10 @@ class LoggerAdapter(logging.LoggerAdapter):
value will be added to the ``extra`` dictionary with the entire
activity info, making it present on the ``LogRecord.__dict__`` for
use by others. Default is False.
temporal_extra_mode: Controls how activity context is added to log
``extra``. Default is ``"dict"`` (current behavior). Set to
``"flatten"`` for OpenTelemetry compatibility (scalar attributes
with ``temporal.activity.`` prefix).
"""

def __init__(self, logger: logging.Logger, extra: Mapping[str, Any] | None) -> None:
Expand All @@ -499,6 +504,7 @@ def __init__(self, logger: logging.Logger, extra: Mapping[str, Any] | None) -> N
self.activity_info_on_message = True
self.activity_info_on_extra = True
self.full_activity_info_on_extra = False
self.temporal_extra_mode: TemporalLogExtraMode = "dict"

def process(
self, msg: Any, kwargs: MutableMapping[str, Any]
Expand All @@ -516,7 +522,12 @@ def process(
if self.activity_info_on_extra:
# Extra can be absent or None, this handles both
extra = kwargs.get("extra", None) or {}
extra["temporal_activity"] = context.logger_details
_apply_temporal_context_to_extra(
extra,
key="temporal_activity",
ctx=context.logger_details,
mode=self.temporal_extra_mode,
)
kwargs["extra"] = extra
if self.full_activity_info_on_extra:
# Extra can be absent or None, this handles both
Expand Down
24 changes: 22 additions & 2 deletions temporalio/workflow.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
import temporalio.workflow
from temporalio.nexus._util import ServiceHandlerT

from ._log_utils import (
TemporalLogExtraMode,
_apply_temporal_context_to_extra,
_update_temporal_context_in_extra,
)
from .types import (
AnyType,
CallableAsyncNoParam,
Expand Down Expand Up @@ -1634,6 +1639,10 @@ class LoggerAdapter(logging.LoggerAdapter):
use by others. Default is False.
log_during_replay: Boolean for whether logs should occur during replay.
Default is False.
temporal_extra_mode: Controls how workflow context is added to log
``extra``. Default is ``"dict"`` (current behavior). Set to
``"flatten"`` for OpenTelemetry compatibility (scalar attributes
with ``temporal.workflow.`` prefix).

Values added to ``extra`` are merged with the ``extra`` dictionary from a
logging call, with values from the logging call taking precedence. I.e. the
Expand All @@ -1647,6 +1656,7 @@ def __init__(self, logger: logging.Logger, extra: Mapping[str, Any] | None) -> N
self.workflow_info_on_extra = True
self.full_workflow_info_on_extra = False
self.log_during_replay = False
self.temporal_extra_mode: TemporalLogExtraMode = "dict"
self.disable_sandbox = False

def process(
Expand All @@ -1667,7 +1677,12 @@ def process(
if self.workflow_info_on_message:
msg_extra.update(workflow_details)
if self.workflow_info_on_extra:
extra["temporal_workflow"] = workflow_details
_apply_temporal_context_to_extra(
extra,
key="temporal_workflow",
ctx=workflow_details,
mode=self.temporal_extra_mode,
)
if self.full_workflow_info_on_extra:
extra["workflow_info"] = runtime.workflow_info()
update_info = current_update_info()
Expand All @@ -1676,7 +1691,12 @@ def process(
if self.workflow_info_on_message:
msg_extra.update(update_details)
if self.workflow_info_on_extra:
extra.setdefault("temporal_workflow", {}).update(update_details)
_update_temporal_context_in_extra(
extra,
key="temporal_workflow",
update_ctx=update_details,
mode=self.temporal_extra_mode,
)

kwargs["extra"] = {**extra, **(kwargs.get("extra") or {})}
if msg_extra:
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /