Skip to content

Commit 0df8771

Browse files
authored
Only log unknown attributes if debug is enabled (#184)
Using [`timeit.Timer.repeat`](https://docs.python.org/3/library/timeit.html), I found that `_logger.log` could add a lot of slowness, even if it does not log. It seems to caused by the fact that calling `log` checks if `isInstance`, and that slows things down. Adding this logic feels redundant, but the data suggests it is a good idea. Testing done (10000 iterations, 10 repeats): * Always call _log_unknown_attribute * Average: '1.2189' * Repeat results: ['1.2502', '1.2157', '1.2180', '1.2184', '1.2127', '1.2176', '1.2141', '1.2187', '1.2104', '1.2136']) * Wrap _log_unknown_attribute with if _logger.isEnabledFor(DEBUG): * Average: '0.3957' * Repeat results: ['0.4270', '0.3732', '0.4228', '0.3735', '0.3726', '0.3919', '0.3820', '0.3768', '0.4235', '0.4140']) Suggests 4x perf hit, in worse case scenario 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 26e62a6 commit 0df8771

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_metric_attribute_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,5 @@ def _set_span_kind_for_dependency(span: ReadableSpan, attributes: BoundedAttribu
373373

374374
def _log_unknown_attribute(attribute_key: str, span: ReadableSpan) -> None:
375375
message: str = "No valid %s value found for %s span %s"
376-
_logger.log(DEBUG, message, attribute_key, span.kind.name, str(span.context.span_id))
376+
if _logger.isEnabledFor(DEBUG):
377+
_logger.log(DEBUG, message, attribute_key, span.kind.name, str(span.context.span_id))

0 commit comments

Comments
 (0)