Skip to content

Commit 4af1341

Browse files
authored
Add support for OTEL_BSP_* environment variables (#1120)
Fixes #1105
1 parent 90d7400 commit 4af1341

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

Diff for: opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Update sampling result names
66
([#1128](https://github.com/open-telemetry/opentelemetry-python/pull/1128))
7+
- Add support for `OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_SCHEDULE_DELAY_MILLIS`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE` and `OTEL_BSP_EXPORT_TIMEOUT_MILLIS` environment variables
8+
([#1105](https://github.com/open-telemetry/opentelemetry-python/pull/1120))
79

810
## Version 0.13b0
911

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

+30-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import typing
2121
from enum import Enum
2222

23+
from opentelemetry.configuration import Configuration
2324
from opentelemetry.context import attach, detach, set_value
24-
from opentelemetry.sdk.trace import sampling
2525
from opentelemetry.util import time_ns
2626

2727
from .. import Span, SpanProcessor
@@ -113,10 +113,30 @@ class BatchExportSpanProcessor(SpanProcessor):
113113
def __init__(
114114
self,
115115
span_exporter: SpanExporter,
116-
max_queue_size: int = 2048,
117-
schedule_delay_millis: float = 5000,
118-
max_export_batch_size: int = 512,
116+
max_queue_size: int = None,
117+
schedule_delay_millis: float = None,
118+
max_export_batch_size: int = None,
119+
export_timeout_millis: float = None,
119120
):
121+
122+
if max_queue_size is None:
123+
max_queue_size = Configuration().get("BSP_MAX_QUEUE_SIZE", 2048)
124+
125+
if schedule_delay_millis is None:
126+
schedule_delay_millis = Configuration().get(
127+
"BSP_SCHEDULE_DELAY_MILLIS", 5000
128+
)
129+
130+
if max_export_batch_size is None:
131+
max_export_batch_size = Configuration().get(
132+
"BSP_MAX_EXPORT_BATCH_SIZE", 512
133+
)
134+
135+
if export_timeout_millis is None:
136+
export_timeout_millis = Configuration().get(
137+
"BSP_EXPORT_TIMEOUT_MILLIS", 30000
138+
)
139+
120140
if max_queue_size <= 0:
121141
raise ValueError("max_queue_size must be a positive integer.")
122142

@@ -143,6 +163,7 @@ def __init__(
143163
self.schedule_delay_millis = schedule_delay_millis
144164
self.max_export_batch_size = max_export_batch_size
145165
self.max_queue_size = max_queue_size
166+
self.export_timeout_millis = export_timeout_millis
146167
self.done = False
147168
# flag that indicates that spans are being dropped
148169
self._spans_dropped = False
@@ -306,7 +327,11 @@ def _drain_queue(self):
306327
while self.queue:
307328
self._export_batch()
308329

309-
def force_flush(self, timeout_millis: int = 30000) -> bool:
330+
def force_flush(self, timeout_millis: int = None) -> bool:
331+
332+
if timeout_millis is None:
333+
timeout_millis = self.export_timeout_millis
334+
310335
if self.done:
311336
logger.warning("Already shutdown, ignoring call to force_flush().")
312337
return True

Diff for: opentelemetry-sdk/tests/trace/export/test_export.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ def _create_start_and_end_span(name, span_processor):
136136

137137

138138
class TestBatchExportSpanProcessor(unittest.TestCase):
139+
@mock.patch.dict(
140+
"os.environ",
141+
{
142+
"OTEL_BSP_MAX_QUEUE_SIZE": "10",
143+
"OTEL_BSP_SCHEDULE_DELAY_MILLIS": "2",
144+
"OTEL_BSP_MAX_EXPORT_BATCH_SIZE": "3",
145+
"OTEL_BSP_EXPORT_TIMEOUT_MILLIS": "4",
146+
},
147+
)
148+
def test_batch_span_processor_environment_variables(self):
149+
150+
batch_span_processor = export.BatchExportSpanProcessor(
151+
MySpanExporter(destination=[])
152+
)
153+
154+
self.assertEqual(batch_span_processor.max_queue_size, 10)
155+
self.assertEqual(batch_span_processor.schedule_delay_millis, 2)
156+
self.assertEqual(batch_span_processor.max_export_batch_size, 3)
157+
self.assertEqual(batch_span_processor.export_timeout_millis, 4)
158+
139159
def test_shutdown(self):
140160
spans_names_list = []
141161

@@ -266,7 +286,7 @@ def test_batch_span_processor_many_spans(self):
266286
for _ in range(256):
267287
_create_start_and_end_span("foo", span_processor)
268288

269-
time.sleep(0.05) # give some time for the exporter to upload spans
289+
time.sleep(0.1) # give some time for the exporter to upload spans
270290

271291
self.assertTrue(span_processor.force_flush())
272292
self.assertEqual(len(spans_names_list), 1024)

0 commit comments

Comments
 (0)