Skip to content

Commit 3c98eee

Browse files
authored
Add speced out environment variables and arguments for BatchLogRecordProcessor (#3237)
1 parent 2e0d8ee commit 3c98eee

File tree

6 files changed

+434
-33
lines changed

6 files changed

+434
-33
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
([#3226](https://github.com/open-telemetry/opentelemetry-python/pull/3226))
1616
- Fix suppress instrumentation for log batch processor
1717
([#3223](https://github.com/open-telemetry/opentelemetry-python/pull/3223))
18+
- Add speced out environment variables and arguments for BatchLogRecordProcessor
19+
([#3237](https://github.com/open-telemetry/opentelemetry-python/pull/3237))
1820

1921
## Version 1.17.0/0.38b0 (2023-03-22)
2022

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py

+136-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020
import sys
2121
import threading
22-
from os import linesep
22+
from os import environ, linesep
2323
from time import time_ns
2424
from typing import IO, Callable, Deque, List, Optional, Sequence
2525

@@ -30,8 +30,22 @@
3030
set_value,
3131
)
3232
from opentelemetry.sdk._logs import LogData, LogRecord, LogRecordProcessor
33+
from opentelemetry.sdk.environment_variables import (
34+
OTEL_BLRP_EXPORT_TIMEOUT,
35+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
36+
OTEL_BLRP_MAX_QUEUE_SIZE,
37+
OTEL_BLRP_SCHEDULE_DELAY,
38+
)
3339
from opentelemetry.util._once import Once
3440

41+
_DEFAULT_SCHEDULE_DELAY_MILLIS = 5000
42+
_DEFAULT_MAX_EXPORT_BATCH_SIZE = 512
43+
_DEFAULT_EXPORT_TIMEOUT_MILLIS = 30000
44+
_DEFAULT_MAX_QUEUE_SIZE = 2048
45+
_ENV_VAR_INT_VALUE_ERROR_MESSAGE = (
46+
"Unable to parse value for %s as integer. Defaulting to %s."
47+
)
48+
3549
_logger = logging.getLogger(__name__)
3650

3751

@@ -142,20 +156,54 @@ class BatchLogRecordProcessor(LogRecordProcessor):
142156
"""This is an implementation of LogRecordProcessor which creates batches of
143157
received logs in the export-friendly LogData representation and
144158
send to the configured LogExporter, as soon as they are emitted.
159+
160+
`BatchLogRecordProcessor` is configurable with the following environment
161+
variables which correspond to constructor parameters:
162+
163+
- :envvar:`OTEL_BLRP_SCHEDULE_DELAY`
164+
- :envvar:`OTEL_BLRP_MAX_QUEUE_SIZE`
165+
- :envvar:`OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`
166+
- :envvar:`OTEL_BLRP_EXPORT_TIMEOUT`
145167
"""
146168

147169
def __init__(
148170
self,
149171
exporter: LogExporter,
150-
schedule_delay_millis: int = 5000,
151-
max_export_batch_size: int = 512,
152-
export_timeout_millis: int = 30000,
172+
schedule_delay_millis: float = None,
173+
max_export_batch_size: int = None,
174+
export_timeout_millis: float = None,
175+
max_queue_size: int = None,
153176
):
177+
if max_queue_size is None:
178+
max_queue_size = BatchLogRecordProcessor._default_max_queue_size()
179+
180+
if schedule_delay_millis is None:
181+
schedule_delay_millis = (
182+
BatchLogRecordProcessor._default_schedule_delay_millis()
183+
)
184+
185+
if max_export_batch_size is None:
186+
max_export_batch_size = (
187+
BatchLogRecordProcessor._default_max_export_batch_size()
188+
)
189+
190+
if export_timeout_millis is None:
191+
export_timeout_millis = (
192+
BatchLogRecordProcessor._default_export_timeout_millis()
193+
)
194+
195+
BatchLogRecordProcessor._validate_arguments(
196+
max_queue_size, schedule_delay_millis, max_export_batch_size
197+
)
198+
154199
self._exporter = exporter
200+
self._max_queue_size = max_queue_size
155201
self._schedule_delay_millis = schedule_delay_millis
156202
self._max_export_batch_size = max_export_batch_size
157203
self._export_timeout_millis = export_timeout_millis
158-
self._queue = collections.deque() # type: Deque[LogData]
204+
self._queue = collections.deque(
205+
[], max_queue_size
206+
) # type: Deque[LogData]
159207
self._worker_thread = threading.Thread(
160208
name="OtelBatchLogRecordProcessor",
161209
target=self.worker,
@@ -333,3 +381,86 @@ def force_flush(self, timeout_millis: Optional[int] = None) -> bool:
333381
if not ret:
334382
_logger.warning("Timeout was exceeded in force_flush().")
335383
return ret
384+
385+
@staticmethod
386+
def _default_max_queue_size():
387+
try:
388+
return int(
389+
environ.get(OTEL_BLRP_MAX_QUEUE_SIZE, _DEFAULT_MAX_QUEUE_SIZE)
390+
)
391+
except ValueError:
392+
_logger.exception(
393+
_ENV_VAR_INT_VALUE_ERROR_MESSAGE,
394+
OTEL_BLRP_MAX_QUEUE_SIZE,
395+
_DEFAULT_MAX_QUEUE_SIZE,
396+
)
397+
return _DEFAULT_MAX_QUEUE_SIZE
398+
399+
@staticmethod
400+
def _default_schedule_delay_millis():
401+
try:
402+
return int(
403+
environ.get(
404+
OTEL_BLRP_SCHEDULE_DELAY, _DEFAULT_SCHEDULE_DELAY_MILLIS
405+
)
406+
)
407+
except ValueError:
408+
_logger.exception(
409+
_ENV_VAR_INT_VALUE_ERROR_MESSAGE,
410+
OTEL_BLRP_SCHEDULE_DELAY,
411+
_DEFAULT_SCHEDULE_DELAY_MILLIS,
412+
)
413+
return _DEFAULT_SCHEDULE_DELAY_MILLIS
414+
415+
@staticmethod
416+
def _default_max_export_batch_size():
417+
try:
418+
return int(
419+
environ.get(
420+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
421+
_DEFAULT_MAX_EXPORT_BATCH_SIZE,
422+
)
423+
)
424+
except ValueError:
425+
_logger.exception(
426+
_ENV_VAR_INT_VALUE_ERROR_MESSAGE,
427+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
428+
_DEFAULT_MAX_EXPORT_BATCH_SIZE,
429+
)
430+
return _DEFAULT_MAX_EXPORT_BATCH_SIZE
431+
432+
@staticmethod
433+
def _default_export_timeout_millis():
434+
try:
435+
return int(
436+
environ.get(
437+
OTEL_BLRP_EXPORT_TIMEOUT, _DEFAULT_EXPORT_TIMEOUT_MILLIS
438+
)
439+
)
440+
except ValueError:
441+
_logger.exception(
442+
_ENV_VAR_INT_VALUE_ERROR_MESSAGE,
443+
OTEL_BLRP_EXPORT_TIMEOUT,
444+
_DEFAULT_EXPORT_TIMEOUT_MILLIS,
445+
)
446+
return _DEFAULT_EXPORT_TIMEOUT_MILLIS
447+
448+
@staticmethod
449+
def _validate_arguments(
450+
max_queue_size, schedule_delay_millis, max_export_batch_size
451+
):
452+
if max_queue_size <= 0:
453+
raise ValueError("max_queue_size must be a positive integer.")
454+
455+
if schedule_delay_millis <= 0:
456+
raise ValueError("schedule_delay_millis must be positive.")
457+
458+
if max_export_batch_size <= 0:
459+
raise ValueError(
460+
"max_export_batch_size must be a positive integer."
461+
)
462+
463+
if max_export_batch_size > max_queue_size:
464+
raise ValueError(
465+
"max_export_batch_size must be less than or equal to max_queue_size."
466+
)

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,67 @@
6666
i.e. the SDK behaves as if OTEL_TRACES_SAMPLER_ARG is not set.
6767
"""
6868

69+
OTEL_BLRP_SCHEDULE_DELAY = "OTEL_BLRP_SCHEDULE_DELAY"
70+
"""
71+
.. envvar:: OTEL_BLRP_SCHEDULE_DELAY
72+
73+
The :envvar:`OTEL_BLRP_SCHEDULE_DELAY` represents the delay interval between two consecutive exports of the BatchLogRecordProcessor.
74+
Default: 5000
75+
"""
76+
77+
OTEL_BLRP_EXPORT_TIMEOUT = "OTEL_BLRP_EXPORT_TIMEOUT"
78+
"""
79+
.. envvar:: OTEL_BLRP_EXPORT_TIMEOUT
80+
81+
The :envvar:`OTEL_BLRP_EXPORT_TIMEOUT` represents the maximum allowed time to export data from the BatchLogRecordProcessor.
82+
Default: 30000
83+
"""
84+
85+
OTEL_BLRP_MAX_QUEUE_SIZE = "OTEL_BLRP_MAX_QUEUE_SIZE"
86+
"""
87+
.. envvar:: OTEL_BLRP_MAX_QUEUE_SIZE
88+
89+
The :envvar:`OTEL_BLRP_MAX_QUEUE_SIZE` represents the maximum queue size for the data export of the BatchLogRecordProcessor.
90+
Default: 2048
91+
"""
92+
93+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE = "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE"
94+
"""
95+
.. envvar:: OTEL_BLRP_MAX_EXPORT_BATCH_SIZE
96+
97+
The :envvar:`OTEL_BLRP_MAX_EXPORT_BATCH_SIZE` represents the maximum batch size for the data export of the BatchLogRecordProcessor.
98+
Default: 512
99+
"""
100+
69101
OTEL_BSP_SCHEDULE_DELAY = "OTEL_BSP_SCHEDULE_DELAY"
70102
"""
71103
.. envvar:: OTEL_BSP_SCHEDULE_DELAY
72104
73-
The :envvar:`OTEL_BSP_SCHEDULE_DELAY` represents the delay interval between two consecutive exports.
105+
The :envvar:`OTEL_BSP_SCHEDULE_DELAY` represents the delay interval between two consecutive exports of the BatchSpanProcessor.
74106
Default: 5000
75107
"""
76108

77109
OTEL_BSP_EXPORT_TIMEOUT = "OTEL_BSP_EXPORT_TIMEOUT"
78110
"""
79111
.. envvar:: OTEL_BSP_EXPORT_TIMEOUT
80112
81-
The :envvar:`OTEL_BSP_EXPORT_TIMEOUT` represents the maximum allowed time to export data.
113+
The :envvar:`OTEL_BSP_EXPORT_TIMEOUT` represents the maximum allowed time to export data from the BatchSpanProcessor.
82114
Default: 30000
83115
"""
84116

85117
OTEL_BSP_MAX_QUEUE_SIZE = "OTEL_BSP_MAX_QUEUE_SIZE"
86118
"""
87119
.. envvar:: OTEL_BSP_MAX_QUEUE_SIZE
88120
89-
The :envvar:`OTEL_BSP_MAX_QUEUE_SIZE` represents the maximum queue size for the data export.
121+
The :envvar:`OTEL_BSP_MAX_QUEUE_SIZE` represents the maximum queue size for the data export of the BatchSpanProcessor.
90122
Default: 2048
91123
"""
92124

93125
OTEL_BSP_MAX_EXPORT_BATCH_SIZE = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE"
94126
"""
95127
.. envvar:: OTEL_BSP_MAX_EXPORT_BATCH_SIZE
96128
97-
The :envvar:`OTEL_BSP_MAX_EXPORT_BATCH_SIZE` represents the maximum batch size for the data export.
129+
The :envvar:`OTEL_BSP_MAX_EXPORT_BATCH_SIZE` represents the maximum batch size for the data export of the BatchSpanProcessor.
98130
Default: 512
99131
"""
100132

0 commit comments

Comments
 (0)