Skip to content

Commit

Permalink
Add more restrictive typing to Log body field (open-telemetry#4185)
Browse files Browse the repository at this point in the history
* Add more restrictive custom Any type to LogRecord body field

* Rename custom "Any" type to "OtelAny"

* Rename the custom Any union to "AnyValue"

* Replace AnyValue type to opentelemetry.util.types

* Align AnyValue type definition with OpenTelemetry specification

* Apply suggestions from code review

---------

Co-authored-by: Leighton Chen <[email protected]>
Co-authored-by: Riccardo Magliocchetti <[email protected]>
Co-authored-by: Diego Hurtado <[email protected]>
  • Loading branch information
4 people authored Sep 30, 2024
1 parent fd169a7 commit 6c4aed8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@

from typing import Mapping, Optional, Sequence, Tuple, Union

# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
# For more details, refer to the OTel specification:
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#type-any
AnyValue = Union[
str,
bool,
int,
float,
bytes,
Sequence["AnyValue"],
Mapping[str, "AnyValue"],
None,
]

AttributeValue = Union[
str,
bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
get_current_span,
)
from opentelemetry.trace.span import TraceFlags
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import AnyValue, Attributes

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(
trace_flags: Optional[TraceFlags] = None,
severity_text: Optional[str] = None,
severity_number: Optional[SeverityNumber] = None,
body: Optional[Any] = None,
body: Optional[AnyValue] = None,
resource: Optional[Resource] = None,
attributes: Optional[Attributes] = None,
limits: Optional[LogLimits] = _UnsetLogLimits,
Expand Down

0 comments on commit 6c4aed8

Please sign in to comment.