Skip to content

Commit d2eb862

Browse files
committed
Simplify filtering of boto span attributes
1 parent 0f0eb62 commit d2eb862

File tree

1 file changed

+27
-23
lines changed
  • instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto

1 file changed

+27
-23
lines changed

instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py

+27-23
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
logger = logging.getLogger(__name__)
5757

58+
SERVICE_PARAMS_BLOCK_LIST = {"s3": ["params.Body"]}
59+
5860

5961
def _get_instance_region_name(instance):
6062
region = getattr(instance, "region", None)
@@ -203,7 +205,24 @@ def _patched_auth_request(self, original_func, instance, args, kwargs):
203205
)
204206

205207

206-
def add_span_arg_tags(span, endpoint_name, args, args_names, args_traced):
208+
def flatten_dict(dict_, sep=".", prefix=""):
209+
"""
210+
Returns a normalized dict of depth 1 with keys in order of embedding
211+
"""
212+
# NOTE: This should probably be in `opentelemetry.instrumentation.utils`.
213+
# adapted from https://stackoverflow.com/a/19647596
214+
return (
215+
{
216+
prefix + sep + k if prefix else k: v
217+
for kk, vv in dict_.items()
218+
for k, v in flatten_dict(vv, sep, kk).items()
219+
}
220+
if isinstance(dict_, dict)
221+
else {prefix: dict_}
222+
)
223+
224+
225+
def add_span_arg_tags(span, aws_service, args, args_names, args_traced):
207226
def truncate_arg_value(value, max_len=1024):
208227
"""Truncate values which are bytes and greater than `max_len`.
209228
Useful for parameters like "Body" in `put_object` operations.
@@ -213,34 +232,19 @@ def truncate_arg_value(value, max_len=1024):
213232

214233
return value
215234

216-
def flatten_dict(dict_, sep=".", prefix=""):
217-
"""
218-
Returns a normalized dict of depth 1 with keys in order of embedding
219-
"""
220-
# adapted from https://stackoverflow.com/a/19647596
221-
return (
222-
{
223-
prefix + sep + k if prefix else k: v
224-
for kk, vv in dict_.items()
225-
for k, v in flatten_dict(vv, sep, kk).items()
226-
}
227-
if isinstance(dict_, dict)
228-
else {prefix: dict_}
229-
)
230-
231235
if not span.is_recording():
232236
return
233237

234-
if endpoint_name not in {"kms", "sts"}:
238+
if aws_service not in {"kms", "sts"}:
235239
tags = dict(
236240
(name, value)
237241
for (name, value) in zip(args_names, args)
238242
if name in args_traced
239243
)
240244
tags = flatten_dict(tags)
241-
for key, value in {
242-
k: truncate_arg_value(v)
243-
for k, v in tags.items()
244-
if k not in {"s3": ["params.Body"]}.get(endpoint_name, [])
245-
}.items():
246-
span.set_attribute(key, value)
245+
246+
for param_key, value in tags.items():
247+
if param_key in SERVICE_PARAMS_BLOCK_LIST.get(aws_service, {}):
248+
continue
249+
250+
span.set_attribute(param_key, truncate_arg_value(value))

0 commit comments

Comments
 (0)