Skip to content

Commit 05f5202

Browse files
authored
feat: enable batch for instrumentation (#43)
1 parent 0aa3cf7 commit 05f5202

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "sap-cloud-sdk"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
description = "SAP Cloud SDK for Python"
55
readme = "README.md"
66
license = "Apache-2.0"

src/sap_cloud_sdk/core/telemetry/auto_instrument.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@
3030

3131

3232
@record_metrics(Module.AICORE, Operation.AICORE_AUTO_INSTRUMENT)
33-
def auto_instrument():
33+
def auto_instrument(disable_batch: bool = False):
3434
"""
3535
Initialize meta-instrumentation for GenAI tracing. Should be initialized before any AI frameworks.
3636
3737
Traces are exported to the OTEL collector endpoint configured in environment with
3838
OTEL_EXPORTER_OTLP_ENDPOINT, or printed to console when OTEL_TRACES_EXPORTER=console.
39+
40+
Args:
41+
disable_batch: If True, uses SimpleSpanProcessor (synchronous, lower throughput).
42+
Defaults to False, which uses BatchSpanProcessor (asynchronous,
43+
recommended for production workloads).
3944
"""
4045
otel_endpoint = os.getenv(ENV_OTLP_ENDPOINT, "")
4146
console_traces = os.getenv(ENV_TRACES_EXPORTER, "").lower() == "console"
@@ -54,7 +59,7 @@ def auto_instrument():
5459
exporter=exporter,
5560
resource_attributes=resource,
5661
should_enrich_metrics=True,
57-
disable_batch=True,
62+
disable_batch=disable_batch,
5863
)
5964

6065
_set_baggage_processor()

src/sap_cloud_sdk/core/telemetry/user-guide.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ Use an OTLP collector:
210210
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otel-collector.example.com"
211211
```
212212

213+
### Span processor
214+
215+
By default, `auto_instrument` uses `BatchSpanProcessor`, which exports spans asynchronously in a background thread and is recommended for production workloads. If you need synchronous span processing (e.g. in short-lived scripts or tests where the process may exit before the batch is flushed), pass `disable_batch=True`:
216+
217+
```python
218+
auto_instrument(disable_batch=True)
219+
```
220+
213221
### System role
214222

215223
```bash

tests/core/unit/telemetry/test_auto_instrument.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_auto_instrument_with_endpoint_success(self, mock_traceloop_components):
4343
call_kwargs = mock_traceloop_components['traceloop'].init.call_args[1]
4444
assert call_kwargs['app_name'] == 'test-app'
4545
assert call_kwargs['should_enrich_metrics'] is True
46-
assert call_kwargs['disable_batch'] is True
46+
assert call_kwargs['disable_batch'] is False
4747

4848
def test_auto_instrument_uses_grpc_exporter_by_default(self, mock_traceloop_components):
4949
"""Test that auto_instrument uses gRPC exporter by default, letting it read endpoint from env."""
@@ -209,6 +209,17 @@ def test_auto_instrument_without_endpoint_or_console(self):
209209
warning_message = mock_logger.warning.call_args[0][0]
210210
assert "OTEL_EXPORTER_OTLP_ENDPOINT not set" in warning_message
211211

212+
def test_auto_instrument_disable_batch_can_be_set_to_true(self, mock_traceloop_components):
213+
"""Test that disable_batch=True can be explicitly passed to use SimpleSpanProcessor."""
214+
mock_traceloop_components['get_app_name'].return_value = 'test-app'
215+
mock_traceloop_components['create_resource'].return_value = {}
216+
217+
with patch.dict('os.environ', {'OTEL_EXPORTER_OTLP_ENDPOINT': 'http://localhost:4317'}, clear=True):
218+
auto_instrument(disable_batch=True)
219+
220+
call_kwargs = mock_traceloop_components['traceloop'].init.call_args[1]
221+
assert call_kwargs['disable_batch'] is True
222+
212223
def test_auto_instrument_passes_baggage_span_processor(self, mock_traceloop_components):
213224
"""Test that auto_instrument registers a BaggageSpanProcessor on the tracer provider."""
214225
mock_traceloop_components['get_app_name'].return_value = 'test-app'

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)