|
17 | 17 | import enum |
18 | 18 | import logging |
19 | 19 | import sys |
20 | | -import traceback |
21 | 20 | from os import environ, linesep |
22 | 21 | from typing import IO, Callable, Optional, Sequence |
23 | 22 |
|
24 | 23 | from typing_extensions import deprecated |
25 | 24 |
|
26 | 25 | from opentelemetry.context import ( |
| 26 | + _ON_EMIT_RECURSION_COUNT_KEY, |
27 | 27 | _SUPPRESS_INSTRUMENTATION_KEY, |
28 | 28 | attach, |
29 | 29 | detach, |
| 30 | + get_value, |
30 | 31 | set_value, |
31 | 32 | ) |
32 | 33 | from opentelemetry.sdk._logs import ( |
@@ -150,28 +151,23 @@ def __init__(self, exporter: LogRecordExporter): |
150 | 151 |
|
151 | 152 | def on_emit(self, log_record: ReadWriteLogRecord): |
152 | 153 | # Prevent entering a recursive loop. |
153 | | - if ( |
154 | | - sum( |
155 | | - item.name == "on_emit" |
156 | | - and ( |
157 | | - item.filename.endswith("export/__init__.py") |
158 | | - or item.filename.endswith( |
159 | | - r"export\__init__.py" |
160 | | - ) # backward slash on windows.. |
161 | | - ) |
162 | | - for item in traceback.extract_stack() |
163 | | - ) |
164 | | - # Recursive depth of 3 is sort of arbitrary. It's possible that an Exporter.export call |
165 | | - # emits a log which returns us to this function, but when we call Exporter.export again the log |
166 | | - # is no longer emitted and we exit this recursive loop naturally, a depth of >3 allows 3 |
167 | | - # recursive log calls but exits after because it's likely endless. |
168 | | - > 3 |
169 | | - ): |
| 154 | + cnt = get_value(_ON_EMIT_RECURSION_COUNT_KEY) or 0 |
| 155 | + # Recursive depth of 3 is sort of arbitrary. It's possible that an Exporter.export call |
| 156 | + # emits a log which returns us to this function, but when we call Exporter.export again the log |
| 157 | + # is no longer emitted and we exit this recursive loop naturally, a depth of >3 allows 3 |
| 158 | + # recursive log calls but exits after because it's likely endless. |
| 159 | + if cnt > 3: # pyright: ignore[reportOperatorIssue] |
170 | 160 | _propagate_false_logger.warning( |
171 | 161 | "SimpleLogRecordProcessor.on_emit has entered a recursive loop. Dropping log and exiting the loop." |
172 | 162 | ) |
173 | 163 | return |
174 | | - token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) |
| 164 | + token = attach( |
| 165 | + set_value( |
| 166 | + _SUPPRESS_INSTRUMENTATION_KEY, |
| 167 | + True, |
| 168 | + set_value(_ON_EMIT_RECURSION_COUNT_KEY, cnt + 1), # pyright: ignore[reportOperatorIssue] |
| 169 | + ) |
| 170 | + ) |
175 | 171 | try: |
176 | 172 | if self._shutdown: |
177 | 173 | _logger.warning("Processor is already shutdown, ignoring call") |
|
0 commit comments