Skip to content

Commit ad2de35

Browse files
nstawskiocelotl
andauthored
Add dropped_attributes_count support in exporters (#3351)
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
1 parent 9026027 commit ad2de35

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
- Add max_scale option to Exponential Bucket Histogram Aggregation [#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
11-
- Use BoundedAttributes instead of raw dict to extract attributes from LogRecord and Support dropped_attributes_count in LogRecord ([#3310](https://github.com/open-telemetry/opentelemetry-python/pull/3310))
10+
- Add max_scale option to Exponential Bucket Histogram Aggregation
11+
([#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
12+
- Use BoundedAttributes instead of raw dict to extract attributes from LogRecord
13+
([#3310](https://github.com/open-telemetry/opentelemetry-python/pull/3310))
14+
- Support dropped_attributes_count in LogRecord and exporters
15+
([#3351](https://github.com/open-telemetry/opentelemetry-python/pull/3351))
1216
- Add unit to view instrument selection criteria
1317
([#3341](https://github.com/open-telemetry/opentelemetry-python/pull/3341))
1418
- Upgrade opentelemetry-proto to 0.20 and regen

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def _encode_log(log_data: LogData) -> PB2LogRecord:
4747
body=_encode_value(log_data.log_record.body),
4848
severity_text=log_data.log_record.severity_text,
4949
attributes=_encode_attributes(log_data.log_record.attributes),
50+
dropped_attributes_count=log_data.log_record.dropped_attributes,
5051
severity_number=log_data.log_record.severity_number.value,
5152
)
5253

exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from opentelemetry.proto.resource.v1.resource_pb2 import (
4040
Resource as PB2Resource,
4141
)
42-
from opentelemetry.sdk._logs import LogData
42+
from opentelemetry.sdk._logs import LogData, LogLimits
4343
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
4444
from opentelemetry.sdk.resources import Resource as SDKResource
4545
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
@@ -51,6 +51,19 @@ def test_encode(self):
5151
sdk_logs, expected_encoding = self.get_test_logs()
5252
self.assertEqual(encode_logs(sdk_logs), expected_encoding)
5353

54+
def test_dropped_attributes_count(self):
55+
sdk_logs = self._get_test_logs_dropped_attributes()
56+
encoded_logs = encode_logs(sdk_logs)
57+
self.assertTrue(hasattr(sdk_logs[0].log_record, "dropped_attributes"))
58+
self.assertEqual(
59+
# pylint:disable=no-member
60+
encoded_logs.resource_logs[0]
61+
.scope_logs[0]
62+
.log_records[0]
63+
.dropped_attributes_count,
64+
2,
65+
)
66+
5467
@staticmethod
5568
def _get_sdk_log_data() -> List[LogData]:
5669
log1 = LogData(
@@ -251,3 +264,42 @@ def get_test_logs(
251264
)
252265

253266
return sdk_logs, pb2_service_request
267+
268+
@staticmethod
269+
def _get_test_logs_dropped_attributes() -> List[LogData]:
270+
log1 = LogData(
271+
log_record=SDKLogRecord(
272+
timestamp=1644650195189786880,
273+
trace_id=89564621134313219400156819398935297684,
274+
span_id=1312458408527513268,
275+
trace_flags=TraceFlags(0x01),
276+
severity_text="WARN",
277+
severity_number=SeverityNumber.WARN,
278+
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
279+
resource=SDKResource({"first_resource": "value"}),
280+
attributes={"a": 1, "b": "c", "user_id": "B121092"},
281+
limits=LogLimits(max_attributes=1),
282+
),
283+
instrumentation_scope=InstrumentationScope(
284+
"first_name", "first_version"
285+
),
286+
)
287+
288+
log2 = LogData(
289+
log_record=SDKLogRecord(
290+
timestamp=1644650249738562048,
291+
trace_id=0,
292+
span_id=0,
293+
trace_flags=TraceFlags.DEFAULT,
294+
severity_text="WARN",
295+
severity_number=SeverityNumber.WARN,
296+
body="Cooper, this is no time for caution!",
297+
resource=SDKResource({"second_resource": "CASE"}),
298+
attributes={},
299+
),
300+
instrumentation_scope=InstrumentationScope(
301+
"second_name", "second_version"
302+
),
303+
)
304+
305+
return [log1, log2]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def to_json(self, indent=4) -> str:
203203
"attributes": dict(self.attributes)
204204
if bool(self.attributes)
205205
else None,
206+
"dropped_attributes": self.dropped_attributes,
206207
"timestamp": ns_to_iso_str(self.timestamp),
207208
"trace_id": f"0x{format_trace_id(self.trace_id)}"
208209
if self.trace_id is not None

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_log_record_to_json(self):
2727
"severity_number": "None",
2828
"severity_text": None,
2929
"attributes": None,
30+
"dropped_attributes": 0,
3031
"timestamp": "1970-01-01T00:00:00.000000Z",
3132
"trace_id": "",
3233
"span_id": "",

0 commit comments

Comments
 (0)