-
Notifications
You must be signed in to change notification settings - Fork 152
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """Internal utilities for Temporal logging. | ||
| This module is internal and may change at any time. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Mapping, MutableMapping | ||
| from typing import Any, Literal | ||
|
|
||
| TemporalLogExtraMode = Literal["dict", "flatten", "json"] | ||
| """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. | ||
| json: Add context as a JSON string under a single key. Useful when | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually have any known use case for json mode? |
||
| downstream systems expect string values but you want structured data. | ||
| """ | ||
|
|
||
|
|
||
| def _apply_temporal_context_to_extra( | ||
| extra: MutableMapping[str, Any], | ||
| *, | ||
| key: str, | ||
| prefix: 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 key to use for dict/json modes (e.g., "temporal_workflow"). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a great reason to distinguish key and prefix. Do you have any compelling argument not to use the same one for both? |
||
| prefix: The prefix to use for flatten mode keys (e.g., "temporal.workflow"). | ||
| ctx: The context mapping containing temporal fields. | ||
| mode: The mode controlling how context is added. | ||
| """ | ||
| if mode == "dict": | ||
| extra[key] = dict(ctx) | ||
| elif mode == "json": | ||
| extra[key] = json.dumps(ctx, separators=(",", ":"), default=str) | ||
| elif mode == "flatten": | ||
| 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: | ||
| # Fallback to dict for any unknown mode (shouldn't happen with typing) | ||
| extra[key] = dict(ctx) | ||
|
|
||
|
|
||
| def _update_temporal_context_in_extra( | ||
| extra: MutableMapping[str, Any], | ||
| *, | ||
| key: str, | ||
| prefix: 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 key used for dict/json modes (e.g., "temporal_workflow"). | ||
| prefix: The prefix used for flatten mode keys (e.g., "temporal.workflow"). | ||
| update_ctx: Additional context fields to add/update. | ||
| mode: The mode controlling how context is added. | ||
| """ | ||
| if mode == "dict": | ||
| extra.setdefault(key, {}).update(update_ctx) | ||
| elif mode == "json": | ||
| # For JSON mode, we need to parse, update, and re-serialize | ||
| existing = extra.get(key) | ||
| if existing is not None: | ||
| try: | ||
| existing_dict = json.loads(existing) | ||
| existing_dict.update(update_ctx) | ||
| extra[key] = json.dumps( | ||
| existing_dict, separators=(",", ":"), default=str | ||
| ) | ||
| except (json.JSONDecodeError, TypeError): | ||
| # If parsing fails, just create a new JSON object with update_ctx | ||
| extra[key] = json.dumps( | ||
| dict(update_ctx), separators=(",", ":"), default=str | ||
| ) | ||
| else: | ||
| extra[key] = json.dumps( | ||
| dict(update_ctx), separators=(",", ":"), default=str | ||
| ) | ||
| elif mode == "flatten": | ||
| 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: | ||
| # Fallback to dict for any unknown mode | ||
| extra.setdefault(key, {}).update(update_ctx) | ||
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.
There seem to be a number of unrelated readme changes.