Skip to content

Commit 816ce49

Browse files
Logs API/SDK accepts additional otel context
1 parent c9ad4bc commit 816ce49

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from typing import Optional, cast
4141

4242
from opentelemetry._logs.severity import SeverityNumber
43+
from opentelemetry.context.context import Context
4344
from opentelemetry.environment_variables import _OTEL_PYTHON_LOGGER_PROVIDER
4445
from opentelemetry.trace.span import TraceFlags
4546
from opentelemetry.util._once import Once
@@ -61,6 +62,7 @@ def __init__(
6162
self,
6263
timestamp: Optional[int] = None,
6364
observed_timestamp: Optional[int] = None,
65+
context: Optional[Context] = None,
6466
trace_id: Optional[int] = None,
6567
span_id: Optional[int] = None,
6668
trace_flags: Optional["TraceFlags"] = None,
@@ -73,6 +75,7 @@ def __init__(
7375
if observed_timestamp is None:
7476
observed_timestamp = time_ns()
7577
self.observed_timestamp = observed_timestamp
78+
self.context = context
7679
self.trace_id = trace_id
7780
self.span_id = span_id
7881
self.trace_flags = trace_flags

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
std_to_otel,
3838
)
3939
from opentelemetry.attributes import _VALID_ANY_VALUE_TYPES, BoundedAttributes
40+
from opentelemetry.context.context import Context
4041
from opentelemetry.sdk.environment_variables import (
4142
OTEL_ATTRIBUTE_COUNT_LIMIT,
4243
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
@@ -176,6 +177,7 @@ def __init__(
176177
self,
177178
timestamp: int | None = None,
178179
observed_timestamp: int | None = None,
180+
context: Context | None = None,
179181
trace_id: int | None = None,
180182
span_id: int | None = None,
181183
trace_flags: TraceFlags | None = None,
@@ -190,6 +192,7 @@ def __init__(
190192
**{
191193
"timestamp": timestamp,
192194
"observed_timestamp": observed_timestamp,
195+
"context": context,
193196
"trace_id": trace_id,
194197
"span_id": span_id,
195198
"trace_flags": trace_flags,
@@ -234,6 +237,9 @@ def to_json(self, indent: int | None = 4) -> str:
234237
"dropped_attributes": self.dropped_attributes,
235238
"timestamp": ns_to_iso_str(self.timestamp),
236239
"observed_timestamp": ns_to_iso_str(self.observed_timestamp),
240+
"context": (
241+
dict(self.context) if self.context is not None else ""
242+
),
237243
"trace_id": (
238244
f"0x{format_trace_id(self.trace_id)}"
239245
if self.trace_id is not None

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_log_record_to_json(self):
4242
"dropped_attributes": 0,
4343
"timestamp": "1970-01-01T00:00:00.000000Z",
4444
"observed_timestamp": "1970-01-01T00:00:00.000000Z",
45+
"context": "",
4546
"trace_id": "",
4647
"span_id": "",
4748
"trace_flags": None,
@@ -68,7 +69,7 @@ def test_log_record_to_json(self):
6869
self.assertEqual(expected, actual.to_json(indent=4))
6970
self.assertEqual(
7071
actual.to_json(indent=None),
71-
'{"body": "a log line", "severity_number": null, "severity_text": null, "attributes": {"mapping": {"key": "value"}, "none": null, "sequence": [1, 2], "str": "string"}, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}',
72+
'{"body": "a log line", "severity_number": null, "severity_text": null, "attributes": {"mapping": {"key": "value"}, "none": null, "sequence": [1, 2], "str": "string"}, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "context": "", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}',
7273
)
7374

7475
def test_log_record_to_json_serializes_severity_number_as_int(self):

0 commit comments

Comments
 (0)