-
Notifications
You must be signed in to change notification settings - Fork 770
Improve to json support #3365
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
Closed
Closed
Improve to json support #3365
Changes from all commits
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import logging | ||
| import threading | ||
| import traceback | ||
| import typing | ||
| from os import environ | ||
| from time import time_ns | ||
| from typing import Any, Callable, Optional, Tuple, Union # noqa | ||
|
|
@@ -38,7 +39,7 @@ | |
| OTEL_ATTRIBUTE_COUNT_LIMIT, | ||
| OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, | ||
| ) | ||
| from opentelemetry.sdk.resources import Resource | ||
| from opentelemetry.sdk.resources import Resource, ResourceDict | ||
| from opentelemetry.sdk.util import ns_to_iso_str | ||
| from opentelemetry.sdk.util.instrumentation import InstrumentationScope | ||
| from opentelemetry.semconv.trace import SpanAttributes | ||
|
|
@@ -148,6 +149,21 @@ def _from_env_if_absent( | |
| ) | ||
|
|
||
|
|
||
| class LogRecordDict(typing.TypedDict): | ||
| """Dictionary representation of a LogRecord.""" | ||
|
|
||
| body: typing.Optional[typing.Any] | ||
| severity_number: int | ||
| severity_text: typing.Optional[str] | ||
| attributes: Attributes | ||
| dropped_attributes: int | ||
| timestamp: typing.Optional[str] | ||
| trace_id: str | ||
| span_id: str | ||
| trace_flags: typing.Optional[int] | ||
| resource: typing.Optional[ResourceDict] | ||
|
|
||
|
|
||
| class LogRecord(APILogRecord): | ||
| """A LogRecord instance represents an event being logged. | ||
|
|
||
|
|
@@ -195,30 +211,28 @@ def __eq__(self, other: object) -> bool: | |
| return NotImplemented | ||
| return self.__dict__ == other.__dict__ | ||
|
|
||
| def to_dict(self) -> LogRecordDict: | ||
|
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. We should not add a new method here, but use # dict_test.py
class LogRecord:
def __init__(self, body, severity_number):
self.body = body
self.severity_number = severity_number
def __iter__(self):
for key in self.__dict__:
yield key, getattr(self, key)
log_record = LogRecord("the body", "1")
print(dict(log_record)) |
||
| return { | ||
| "body": self.body, | ||
| "severity_number": self.severity_number.value | ||
| if self.severity_number is not None | ||
pmcollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else SeverityNumber.UNSPECIFIED.value, | ||
| "severity_text": self.severity_text, | ||
| "attributes": dict(self.attributes or {}), | ||
| "dropped_attributes": self.dropped_attributes, | ||
| "timestamp": ns_to_iso_str(self.timestamp), | ||
| "trace_id": f"0x{format_trace_id(self.trace_id)}" | ||
| if self.trace_id is not None | ||
| else "", | ||
| "span_id": f"0x{format_span_id(self.span_id)}" | ||
| if self.span_id is not None | ||
| else "", | ||
| "trace_flags": self.trace_flags, | ||
| "resource": self.resource.to_dict() if self.resource else None, | ||
| } | ||
|
|
||
| def to_json(self, indent=4) -> str: | ||
| return json.dumps( | ||
| { | ||
| "body": self.body, | ||
| "severity_number": repr(self.severity_number), | ||
| "severity_text": self.severity_text, | ||
| "attributes": dict(self.attributes) | ||
| if bool(self.attributes) | ||
| else None, | ||
| "dropped_attributes": self.dropped_attributes, | ||
| "timestamp": ns_to_iso_str(self.timestamp), | ||
| "trace_id": f"0x{format_trace_id(self.trace_id)}" | ||
| if self.trace_id is not None | ||
| else "", | ||
| "span_id": f"0x{format_span_id(self.span_id)}" | ||
| if self.span_id is not None | ||
| else "", | ||
| "trace_flags": self.trace_flags, | ||
| "resource": repr(self.resource.attributes) | ||
| if self.resource | ||
| else "", | ||
| }, | ||
| indent=indent, | ||
| ) | ||
| return json.dumps(self.to_dict(), indent=indent) | ||
|
|
||
| @property | ||
| def dropped_attributes(self) -> int: | ||
|
|
||
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.
typing.TypedDictwas introduced in 3.8. This is why tests are failing forpypyand 3.7.