-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(opentelemetry-instrumentation-logging): Promote otel.event.name to LogRecord.event_name in _translate
#4864
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
rads-1996
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
rads-1996:promote-event-name-in-logging-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−6
Open
Changes from 2 commits
Commits
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 @@ | ||
| `opentelemetry-instrumentation-logging`: Promote otel.event.name to LogRecord.event_name in _translate |
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 |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| import traceback | ||
| from contextvars import ContextVar | ||
| from time import time_ns | ||
| from typing import Callable, Mapping | ||
| from typing import Callable | ||
|
|
||
| from opentelemetry._logs import ( | ||
| LoggerProvider, | ||
|
|
@@ -20,8 +20,14 @@ | |
| ) | ||
| from opentelemetry.context import get_current | ||
| from opentelemetry.instrumentation.log_utils import std_to_otel | ||
| from opentelemetry.semconv._incubating.attributes import code_attributes | ||
| from opentelemetry.semconv.attributes import exception_attributes | ||
| from opentelemetry.semconv._incubating.attributes import ( | ||
| code_attributes, | ||
| event_attributes, | ||
| ) | ||
| from opentelemetry.semconv.attributes import ( | ||
| exception_attributes, | ||
| otel_attributes, | ||
| ) | ||
| from opentelemetry.util.types import AnyValue | ||
|
|
||
| _internal_logger = logging.getLogger(__name__ + ".internal") | ||
|
|
@@ -132,11 +138,25 @@ def __init__( | |
|
|
||
| def _get_attributes( | ||
| self, record: logging.LogRecord | ||
| ) -> Mapping[str, AnyValue]: | ||
| ) -> tuple[dict[str, AnyValue], str | None]: | ||
| attributes = { | ||
| k: v for k, v in vars(record).items() if k not in _RESERVED_ATTRS | ||
| } | ||
|
|
||
| # Promote otel.event.name (stable) or event.name (deprecated) to the | ||
| # first-class LogRecord.event_name field instead of leaving it as a | ||
| # plain attribute. otel.event.name takes precedence; event.name is a | ||
| # deprecated fallback per the OTel semantic conventions. | ||
| # Both keys are always popped so neither leaks into attributes. | ||
| event_name: str | None = attributes.pop( | ||
| otel_attributes.OTEL_EVENT_NAME, None | ||
| ) | ||
|
Member
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. can you leave a todo comment to remove the deprecated branch path before making the package stable? |
||
| deprecated_event_name: str | None = attributes.pop( | ||
| event_attributes.EVENT_NAME, None | ||
| ) | ||
| if event_name is None: | ||
| event_name = deprecated_event_name | ||
|
|
||
| if self._log_code_attributes: | ||
| # Add standard code attributes for logs. | ||
| attributes[code_attributes.CODE_FILE_PATH] = record.pathname | ||
|
|
@@ -158,7 +178,7 @@ def _get_attributes( | |
| attributes[exception_attributes.EXCEPTION_STACKTRACE] = ( | ||
| "".join(traceback.format_exception(*record.exc_info)) | ||
| ) | ||
| return attributes | ||
| return attributes, event_name | ||
|
|
||
| def _translate(self, record: logging.LogRecord) -> LogRecord: | ||
| timestamp = int(record.created * 1e9) | ||
|
|
@@ -168,7 +188,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord: | |
| body = self.format(record) | ||
| else: | ||
| body = record.getMessage() | ||
| attributes = self._get_attributes(record) | ||
| attributes, event_name = self._get_attributes(record) | ||
|
|
||
| # Map Python log level names to OTel severity text as defined in | ||
| # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity | ||
|
|
@@ -188,6 +208,7 @@ def _translate(self, record: logging.LogRecord) -> LogRecord: | |
| severity_number=severity_number, | ||
| body=body, | ||
| attributes=attributes, | ||
| event_name=event_name, | ||
| ) | ||
|
|
||
| def emit(self, record: logging.LogRecord) -> None: | ||
|
|
||
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.
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 think code_attributes are already stable no?
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.
https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-semantic-conventions/src/opentelemetry/semconv/_incubating/attributes/code_attributes.py, it exists both in the stable and experimental folder. I can change the import to import from stable path. This attribute was already being imported. Not part of this change.