Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix serialization of extended attributes for logs signal
([#4342](https://github.com/open-telemetry/opentelemetry-python/pull/4342))

## Version 1.32.0/0.53b0 (2025-04-10)

- Fix user agent in OTLP HTTP metrics exporter
Expand Down
4 changes: 4 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
"py:class",
"_contextvars.Token",
),
(
"py:class",
"AnyValue",
),
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
from opentelemetry.sdk.trace import Resource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import _ExtendedAttributes

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -136,14 +136,17 @@ def _encode_trace_id(trace_id: int) -> bytes:


def _encode_attributes(
attributes: Attributes,
attributes: _ExtendedAttributes,
allow_null: bool = False,
) -> Optional[List[PB2KeyValue]]:
if attributes:
pb2_attributes = []
for key, value in attributes.items():
# pylint: disable=broad-exception-caught
try:
pb2_attributes.append(_encode_key_value(key, value))
pb2_attributes.append(
_encode_key_value(key, value, allow_null=allow_null)
)
except Exception as error:
_logger.exception("Failed to encode key %s: %s", key, error)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def _encode_log(log_data: LogData) -> PB2LogRecord:
flags=int(log_data.log_record.trace_flags),
body=_encode_value(body, allow_null=True),
severity_text=log_data.log_record.severity_text,
attributes=_encode_attributes(log_data.log_record.attributes),
attributes=_encode_attributes(
log_data.log_record.attributes, allow_null=True
),
dropped_attributes_count=log_data.log_record.dropped_attributes,
severity_number=log_data.log_record.severity_number.value,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,28 @@ def _get_sdk_log_data() -> List[LogData]:
),
)

return [log1, log2, log3, log4, log5, log6, log7]
log8 = LogData(
log_record=SDKLogRecord(
timestamp=1644650584292683044,
observed_timestamp=1644650584292683044,
trace_id=212592107417388365804938480559624925566,
span_id=6077757853989569466,
trace_flags=TraceFlags(0x01),
severity_text="INFO",
severity_number=SeverityNumber.INFO,
body="Test export of extended attributes",
resource=SDKResource({}),
attributes={
"extended": {
"sequence": [{"inner": "mapping", "none": None}]
}
},
),
instrumentation_scope=InstrumentationScope(
"extended_name", "extended_version"
),
)
return [log1, log2, log3, log4, log5, log6, log7, log8]

def get_test_logs(
self,
Expand Down Expand Up @@ -265,7 +286,8 @@ def get_test_logs(
"Do not go gentle into that good night. Rage, rage against the dying of the light"
),
attributes=_encode_attributes(
{"a": 1, "b": "c"}
{"a": 1, "b": "c"},
allow_null=True,
),
)
],
Expand Down Expand Up @@ -295,7 +317,8 @@ def get_test_logs(
{
"filename": "model.py",
"func_name": "run_method",
}
},
allow_null=True,
),
)
],
Expand Down Expand Up @@ -326,7 +349,8 @@ def get_test_logs(
{
"filename": "model.py",
"func_name": "run_method",
}
},
allow_null=True,
),
)
],
Expand All @@ -336,7 +360,8 @@ def get_test_logs(
name="scope_with_attributes",
version="scope_with_attributes_version",
attributes=_encode_attributes(
{"one": 1, "two": "2"}
{"one": 1, "two": "2"},
allow_null=True,
),
),
schema_url="instrumentation_schema_url",
Expand All @@ -360,7 +385,8 @@ def get_test_logs(
{
"filename": "model.py",
"func_name": "run_method",
}
},
allow_null=True,
),
)
],
Expand Down Expand Up @@ -416,7 +442,8 @@ def get_test_logs(
severity_number=SeverityNumber.DEBUG.value,
body=_encode_value("To our galaxy"),
attributes=_encode_attributes(
{"a": 1, "b": "c"}
{"a": 1, "b": "c"},
allow_null=True,
),
),
],
Expand Down Expand Up @@ -471,6 +498,43 @@ def get_test_logs(
),
],
),
PB2ScopeLogs(
scope=PB2InstrumentationScope(
name="extended_name",
version="extended_version",
),
log_records=[
PB2LogRecord(
time_unix_nano=1644650584292683044,
observed_time_unix_nano=1644650584292683044,
trace_id=_encode_trace_id(
212592107417388365804938480559624925566
),
span_id=_encode_span_id(
6077757853989569466,
),
flags=int(TraceFlags(0x01)),
severity_text="INFO",
severity_number=SeverityNumber.INFO.value,
body=_encode_value(
"Test export of extended attributes"
),
attributes=_encode_attributes(
{
"extended": {
"sequence": [
{
"inner": "mapping",
"none": None,
}
]
}
},
allow_null=True,
),
),
],
),
],
),
]
Expand Down
27 changes: 15 additions & 12 deletions opentelemetry-api/src/opentelemetry/_events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from abc import ABC, abstractmethod
from logging import getLogger
from os import environ
from typing import Any, Optional, cast
from typing import Optional, cast

from opentelemetry._logs import LogRecord
from opentelemetry._logs.severity import SeverityNumber
Expand All @@ -25,7 +25,7 @@
from opentelemetry.trace.span import TraceFlags
from opentelemetry.util._once import Once
from opentelemetry.util._providers import _load_provider
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import AnyValue, _ExtendedAttributes

_logger = getLogger(__name__)

Expand All @@ -38,18 +38,21 @@ def __init__(
trace_id: Optional[int] = None,
span_id: Optional[int] = None,
trace_flags: Optional["TraceFlags"] = None,
body: Optional[Any] = None,
body: Optional[AnyValue] = None,
severity_number: Optional[SeverityNumber] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
):
attributes = attributes or {}
event_attributes = {**attributes, "event.name": name}
event_attributes = {
**attributes,
"event.name": name,
}
super().__init__(
timestamp=timestamp,
trace_id=trace_id,
span_id=span_id,
trace_flags=trace_flags,
body=body, # type: ignore
body=body,
severity_number=severity_number,
attributes=event_attributes,
)
Expand All @@ -62,7 +65,7 @@ def __init__(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
):
self._name = name
self._version = version
Expand All @@ -85,7 +88,7 @@ def __init__(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
):
super().__init__(
name=name,
Expand Down Expand Up @@ -122,7 +125,7 @@ def get_event_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> EventLogger:
"""Returns an EventLoggerProvider for use."""

Expand All @@ -133,7 +136,7 @@ def get_event_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> EventLogger:
return NoOpEventLogger(
name, version=version, schema_url=schema_url, attributes=attributes
Expand All @@ -146,7 +149,7 @@ def get_event_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> EventLogger:
if _EVENT_LOGGER_PROVIDER:
return _EVENT_LOGGER_PROVIDER.get_event_logger(
Expand Down Expand Up @@ -208,7 +211,7 @@ def get_event_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
event_logger_provider: Optional[EventLoggerProvider] = None,
) -> "EventLogger":
if event_logger_provider is None:
Expand Down
22 changes: 11 additions & 11 deletions opentelemetry-api/src/opentelemetry/_logs/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
from logging import getLogger
from os import environ
from time import time_ns
from typing import Any, Optional, cast
from typing import Optional, cast

from opentelemetry._logs.severity import SeverityNumber
from opentelemetry.environment_variables import _OTEL_PYTHON_LOGGER_PROVIDER
from opentelemetry.trace.span import TraceFlags
from opentelemetry.util._once import Once
from opentelemetry.util._providers import _load_provider
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import AnyValue, _ExtendedAttributes

_logger = getLogger(__name__)

Expand All @@ -66,8 +66,8 @@ def __init__(
trace_flags: Optional["TraceFlags"] = None,
severity_text: Optional[str] = None,
severity_number: Optional[SeverityNumber] = None,
body: Optional[Any] = None,
attributes: Optional["Attributes"] = None,
body: AnyValue = None,
attributes: Optional[_ExtendedAttributes] = None,
):
self.timestamp = timestamp
if observed_timestamp is None:
Expand All @@ -78,7 +78,7 @@ def __init__(
self.trace_flags = trace_flags
self.severity_text = severity_text
self.severity_number = severity_number
self.body = body # type: ignore
self.body = body
self.attributes = attributes


Expand All @@ -90,7 +90,7 @@ def __init__(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> None:
super().__init__()
self._name = name
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__( # pylint: disable=super-init-not-called
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
):
self._name = name
self._version = version
Expand Down Expand Up @@ -158,7 +158,7 @@ def get_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> Logger:
"""Returns a `Logger` for use by the given instrumentation library.

Expand Down Expand Up @@ -196,7 +196,7 @@ def get_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> Logger:
"""Returns a NoOpLogger."""
return NoOpLogger(
Expand All @@ -210,7 +210,7 @@ def get_logger(
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> Logger:
if _LOGGER_PROVIDER:
return _LOGGER_PROVIDER.get_logger(
Expand Down Expand Up @@ -273,7 +273,7 @@ def get_logger(
instrumenting_library_version: str = "",
logger_provider: Optional[LoggerProvider] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
attributes: Optional[_ExtendedAttributes] = None,
) -> "Logger":
"""Returns a `Logger` for use within a python process.

Expand Down
Loading