Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "sap-cloud-sdk"
version = "0.6.0"
version = "0.6.1"
description = "SAP Cloud SDK for Python"
readme = "README.md"
license = "Apache-2.0"
Expand Down
9 changes: 7 additions & 2 deletions src/sap_cloud_sdk/core/telemetry/auto_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@


@record_metrics(Module.AICORE, Operation.AICORE_AUTO_INSTRUMENT)
def auto_instrument():
def auto_instrument(disable_batch: bool = False):
"""
Initialize meta-instrumentation for GenAI tracing. Should be initialized before any AI frameworks.

Traces are exported to the OTEL collector endpoint configured in environment with
OTEL_EXPORTER_OTLP_ENDPOINT, or printed to console when OTEL_TRACES_EXPORTER=console.

Args:
disable_batch: If True, uses SimpleSpanProcessor (synchronous, lower throughput).
Defaults to False, which uses BatchSpanProcessor (asynchronous,
recommended for production workloads).
"""
otel_endpoint = os.getenv(ENV_OTLP_ENDPOINT, "")
console_traces = os.getenv(ENV_TRACES_EXPORTER, "").lower() == "console"
Expand All @@ -54,7 +59,7 @@ def auto_instrument():
exporter=exporter,
resource_attributes=resource,
should_enrich_metrics=True,
disable_batch=True,
disable_batch=disable_batch,
)

_set_baggage_processor()
Expand Down
8 changes: 8 additions & 0 deletions src/sap_cloud_sdk/core/telemetry/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ Use an OTLP collector:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otel-collector.example.com"
```

### Span processor

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`:

```python
auto_instrument(disable_batch=True)
```

### System role

```bash
Expand Down
13 changes: 12 additions & 1 deletion tests/core/unit/telemetry/test_auto_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_auto_instrument_with_endpoint_success(self, mock_traceloop_components):
call_kwargs = mock_traceloop_components['traceloop'].init.call_args[1]
assert call_kwargs['app_name'] == 'test-app'
assert call_kwargs['should_enrich_metrics'] is True
assert call_kwargs['disable_batch'] is True
assert call_kwargs['disable_batch'] is False

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

def test_auto_instrument_disable_batch_can_be_set_to_true(self, mock_traceloop_components):
"""Test that disable_batch=True can be explicitly passed to use SimpleSpanProcessor."""
mock_traceloop_components['get_app_name'].return_value = 'test-app'
mock_traceloop_components['create_resource'].return_value = {}

with patch.dict('os.environ', {'OTEL_EXPORTER_OTLP_ENDPOINT': 'http://localhost:4317'}, clear=True):
auto_instrument(disable_batch=True)

call_kwargs = mock_traceloop_components['traceloop'].init.call_args[1]
assert call_kwargs['disable_batch'] is True

def test_auto_instrument_passes_baggage_span_processor(self, mock_traceloop_components):
"""Test that auto_instrument registers a BaggageSpanProcessor on the tracer provider."""
mock_traceloop_components['get_app_name'].return_value = 'test-app'
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading