Skip to content

Commit 6c4aed8

Browse files
Ali-Alnosairilzchenxrmxocelotl
authored
Add more restrictive typing to Log body field (#4185)
* 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]>
1 parent fd169a7 commit 6c4aed8

File tree

2 files changed

+16
-2
lines changed
  • opentelemetry-api/src/opentelemetry/util
  • opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal

2 files changed

+16
-2
lines changed

opentelemetry-api/src/opentelemetry/util/types.py

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515

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

18+
# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
19+
# For more details, refer to the OTel specification:
20+
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#type-any
21+
AnyValue = Union[
22+
str,
23+
bool,
24+
int,
25+
float,
26+
bytes,
27+
Sequence["AnyValue"],
28+
Mapping[str, "AnyValue"],
29+
None,
30+
]
31+
1832
AttributeValue = Union[
1933
str,
2034
bool,

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
get_current_span,
5151
)
5252
from opentelemetry.trace.span import TraceFlags
53-
from opentelemetry.util.types import Attributes
53+
from opentelemetry.util.types import AnyValue, Attributes
5454

5555
_logger = logging.getLogger(__name__)
5656

@@ -179,7 +179,7 @@ def __init__(
179179
trace_flags: Optional[TraceFlags] = None,
180180
severity_text: Optional[str] = None,
181181
severity_number: Optional[SeverityNumber] = None,
182-
body: Optional[Any] = None,
182+
body: Optional[AnyValue] = None,
183183
resource: Optional[Resource] = None,
184184
attributes: Optional[Attributes] = None,
185185
limits: Optional[LogLimits] = _UnsetLogLimits,

0 commit comments

Comments
 (0)