Skip to content

Commit d6ac497

Browse files
liustveyiyuan-he
authored andcommitted
Genesis - Truncate Log Record Data Size Calculation (#372)
*Description of changes:* Continue to allow estimation for all types of logs but we instead truncate calculation to for Maps and Arrays to 1 layer. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 49db770 commit d6ac497

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/otlp/aws/logs/aws_batch_log_record_processor.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,45 @@ def _get_size_of_log(log_data: LogData) -> int:
107107
@staticmethod
108108
def _get_size_of_any_value(val: AnyValue) -> int:
109109
"""
110-
Calculates the size of an AnyValue type in bytes.
110+
Calculates the size of an AnyValue type.
111+
If AnyValue is an instance of a Map or Array, calculation is truncated to one layer.
111112
"""
112113

113-
if isinstance(val, (str, bytes)):
114-
return len(val)
114+
# Use a stack to prevent excessive recursive calls.
115+
stack = [val]
116+
size = 0
117+
depth = 0
115118

116-
if isinstance(val, bool):
117-
return 4 if val else 5
119+
while stack:
120+
next = stack.pop()
118121

119-
if isinstance(val, (float, int)):
120-
return len(str(val))
122+
if isinstance(next, (str, bytes)):
123+
size += len(next)
124+
continue
121125

122-
if isinstance(val, Mapping):
123-
content = val.get("content", None)
124-
return len(content) if content else 0
126+
if isinstance(next, bool):
127+
size += 4 if next else 5
128+
continue
125129

126-
return 0
130+
if isinstance(next, (float, int)):
131+
size += len(str(next))
132+
continue
133+
134+
if isinstance(next, Mapping):
135+
if depth < 1:
136+
for key, content in next.items():
137+
size += len(key)
138+
stack.append(content)
139+
140+
depth += 1
141+
continue
142+
143+
if isinstance(next, Sequence):
144+
if depth < 1:
145+
for content in next:
146+
stack.append(content)
147+
148+
depth += 1
149+
continue
150+
151+
return size

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/otlp/aws/logs/otlp_aws_logs_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
class OTLPAwsLogExporter(OTLPLogExporter):
27-
_LARGE_LOG_HEADER = {"x-aws-large-log-path": "body.content"}
27+
_LARGE_LOG_HEADER = {"x-aws-extractable-fields": "body/content"}
2828
_RETRY_AFTER_HEADER = "Retry-After" # https://opentelemetry.io/docs/specs/otlp/#otlphttp-throttling
2929

3030
def __init__(

0 commit comments

Comments
 (0)