Skip to content

Commit 84fbeaf

Browse files
committed
Adding speced out env vars and args to BLRP
1 parent b6a1b22 commit 84fbeaf

File tree

4 files changed

+204
-9
lines changed

4 files changed

+204
-9
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
([#2964](https://github.com/open-telemetry/opentelemetry-python/pull/2964))
2424
- Add OpenCensus trace bridge/shim
2525
([#3210](https://github.com/open-telemetry/opentelemetry-python/pull/3210))
26+
- Add speced out environment variables and arguments for BatchLogRecordProcessor
27+
([#3237](https://github.com/open-telemetry/opentelemetry-python/pull/3237))
2628

2729
## Version 1.16.0/0.37b0 (2023-02-17)
2830

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

+57-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
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

2626
from opentelemetry.context import attach, detach, set_value
2727
from opentelemetry.sdk._logs import LogData, LogRecord, LogRecordProcessor
28+
from opentelemetry.sdk.environment_variables import (
29+
OTEL_BLRP_EXPORT_TIMEOUT,
30+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
31+
OTEL_BLRP_MAX_QUEUE_SIZE,
32+
OTEL_BLRP_SCHEDULE_DELAY,
33+
)
2834
from opentelemetry.util._once import Once
2935

3036
_logger = logging.getLogger(__name__)
@@ -137,20 +143,66 @@ class BatchLogRecordProcessor(LogRecordProcessor):
137143
"""This is an implementation of LogRecordProcessor which creates batches of
138144
received logs in the export-friendly LogData representation and
139145
send to the configured LogExporter, as soon as they are emitted.
146+
147+
`BatchLogRecordProcessor` is configurable with the following environment
148+
variables which correspond to constructor parameters:
149+
150+
- :envvar:`OTEL_BLRP_SCHEDULE_DELAY`
151+
- :envvar:`OTEL_BLRP_MAX_QUEUE_SIZE`
152+
- :envvar:`OTEL_BLRP_MAX_EXPORT_BATCH_SIZE`
153+
- :envvar:`OTEL_BLRP_EXPORT_TIMEOUT`
140154
"""
141155

142156
def __init__(
143157
self,
144158
exporter: LogExporter,
145-
schedule_delay_millis: int = 5000,
146-
max_export_batch_size: int = 512,
147-
export_timeout_millis: int = 30000,
159+
max_queue_size: int = None,
160+
schedule_delay_millis: int = None,
161+
max_export_batch_size: int = None,
162+
export_timeout_millis: int = None,
148163
):
164+
if max_queue_size is None:
165+
max_queue_size = int(environ.get(OTEL_BLRP_MAX_QUEUE_SIZE, 2048))
166+
167+
if schedule_delay_millis is None:
168+
schedule_delay_millis = int(
169+
environ.get(OTEL_BLRP_SCHEDULE_DELAY, 5000)
170+
)
171+
172+
if max_export_batch_size is None:
173+
max_export_batch_size = int(
174+
environ.get(OTEL_BLRP_MAX_EXPORT_BATCH_SIZE, 512)
175+
)
176+
177+
if export_timeout_millis is None:
178+
export_timeout_millis = int(
179+
environ.get(OTEL_BLRP_EXPORT_TIMEOUT, 30000)
180+
)
181+
182+
if max_queue_size <= 0:
183+
raise ValueError("max_queue_size must be a positive integer.")
184+
185+
if schedule_delay_millis <= 0:
186+
raise ValueError("schedule_delay_millis must be positive.")
187+
188+
if max_export_batch_size <= 0:
189+
raise ValueError(
190+
"max_export_batch_size must be a positive integer."
191+
)
192+
193+
if max_export_batch_size > max_queue_size:
194+
raise ValueError(
195+
"max_export_batch_size must be less than or equal to max_queue_size."
196+
)
197+
149198
self._exporter = exporter
199+
self._max_queue_size = max_queue_size
150200
self._schedule_delay_millis = schedule_delay_millis
151201
self._max_export_batch_size = max_export_batch_size
152202
self._export_timeout_millis = export_timeout_millis
153-
self._queue = collections.deque() # type: Deque[LogData]
203+
self._queue = collections.deque(
204+
[], max_queue_size
205+
) # type: Deque[LogData]
154206
self._worker_thread = threading.Thread(
155207
name="OtelBatchLogRecordProcessor",
156208
target=self.worker,

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

Diff for: opentelemetry-sdk/tests/logs/test_export.py

+109
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
InMemoryLogExporter,
3636
SimpleLogRecordProcessor,
3737
)
38+
from opentelemetry.sdk.environment_variables import (
39+
OTEL_BLRP_EXPORT_TIMEOUT,
40+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
41+
OTEL_BLRP_MAX_QUEUE_SIZE,
42+
OTEL_BLRP_SCHEDULE_DELAY,
43+
)
3844
from opentelemetry.sdk.resources import Resource as SDKResource
3945
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
4046
from opentelemetry.test.concurrency_test import ConcurrencyTestBase
@@ -175,6 +181,109 @@ def test_emit_call_log_record(self):
175181
logger.error("error")
176182
self.assertEqual(log_record_processor.emit.call_count, 1)
177183

184+
def test_args(self):
185+
exporter = InMemoryLogExporter()
186+
log_record_processor = BatchLogRecordProcessor(
187+
exporter,
188+
max_queue_size=1024,
189+
schedule_delay_millis=2500,
190+
max_export_batch_size=256,
191+
export_timeout_millis=15000,
192+
)
193+
self.assertEqual(log_record_processor._exporter, exporter)
194+
self.assertEqual(log_record_processor._max_queue_size, 1024)
195+
self.assertEqual(log_record_processor._schedule_delay_millis, 2500)
196+
self.assertEqual(log_record_processor._max_export_batch_size, 256)
197+
self.assertEqual(log_record_processor._export_timeout_millis, 15000)
198+
199+
@patch.dict(
200+
"os.environ",
201+
{
202+
OTEL_BLRP_MAX_QUEUE_SIZE: "1024",
203+
OTEL_BLRP_SCHEDULE_DELAY: "2500",
204+
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE: "256",
205+
OTEL_BLRP_EXPORT_TIMEOUT: "15000",
206+
},
207+
)
208+
def test_env_vars(self):
209+
exporter = InMemoryLogExporter()
210+
log_record_processor = BatchLogRecordProcessor(exporter)
211+
self.assertEqual(log_record_processor._exporter, exporter)
212+
self.assertEqual(log_record_processor._max_queue_size, 1024)
213+
self.assertEqual(log_record_processor._schedule_delay_millis, 2500)
214+
self.assertEqual(log_record_processor._max_export_batch_size, 256)
215+
self.assertEqual(log_record_processor._export_timeout_millis, 15000)
216+
217+
def test_args_defaults(self):
218+
exporter = InMemoryLogExporter()
219+
log_record_processor = BatchLogRecordProcessor(exporter)
220+
self.assertEqual(log_record_processor._exporter, exporter)
221+
self.assertEqual(log_record_processor._max_queue_size, 2048)
222+
self.assertEqual(log_record_processor._schedule_delay_millis, 5000)
223+
self.assertEqual(log_record_processor._max_export_batch_size, 512)
224+
self.assertEqual(log_record_processor._export_timeout_millis, 30000)
225+
226+
def test_args_none_defaults(self):
227+
exporter = InMemoryLogExporter()
228+
log_record_processor = BatchLogRecordProcessor(
229+
exporter,
230+
max_queue_size=None,
231+
schedule_delay_millis=None,
232+
max_export_batch_size=None,
233+
export_timeout_millis=None,
234+
)
235+
self.assertEqual(log_record_processor._exporter, exporter)
236+
self.assertEqual(log_record_processor._max_queue_size, 2048)
237+
self.assertEqual(log_record_processor._schedule_delay_millis, 5000)
238+
self.assertEqual(log_record_processor._max_export_batch_size, 512)
239+
self.assertEqual(log_record_processor._export_timeout_millis, 30000)
240+
241+
def test_validation_negative_max_queue_size(self):
242+
exporter = InMemoryLogExporter()
243+
self.assertRaises(
244+
ValueError,
245+
BatchLogRecordProcessor,
246+
exporter,
247+
max_queue_size=0,
248+
)
249+
self.assertRaises(
250+
ValueError,
251+
BatchLogRecordProcessor,
252+
exporter,
253+
max_queue_size=-1,
254+
)
255+
self.assertRaises(
256+
ValueError,
257+
BatchLogRecordProcessor,
258+
exporter,
259+
schedule_delay_millis=0,
260+
)
261+
self.assertRaises(
262+
ValueError,
263+
BatchLogRecordProcessor,
264+
exporter,
265+
schedule_delay_millis=-1,
266+
)
267+
self.assertRaises(
268+
ValueError,
269+
BatchLogRecordProcessor,
270+
exporter,
271+
max_export_batch_size=0,
272+
)
273+
self.assertRaises(
274+
ValueError,
275+
BatchLogRecordProcessor,
276+
exporter,
277+
max_export_batch_size=-1,
278+
)
279+
self.assertRaises(
280+
ValueError,
281+
BatchLogRecordProcessor,
282+
exporter,
283+
max_queue_size=100,
284+
max_export_batch_size=101,
285+
)
286+
178287
def test_shutdown(self):
179288
exporter = InMemoryLogExporter()
180289
log_record_processor = BatchLogRecordProcessor(exporter)

0 commit comments

Comments
 (0)