Skip to content

Commit c080f4b

Browse files
committed
restore api_url in tracer_factory
1 parent a13bd14 commit c080f4b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ ENV/
4040
htmlcov/
4141
.tox/
4242

43+
# Traces (runtime and test-generated)
44+
traces/
45+
4346
# Jupyter
4447
.ipynb_checkpoints
4548

sentience/tracer_factory.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
def create_tracer(
2222
api_key: str | None = None,
2323
run_id: str | None = None,
24+
api_url: str | None = None,
2425
) -> Tracer:
2526
"""
2627
Create tracer with automatic tier detection.
@@ -34,6 +35,7 @@ def create_tracer(
3435
- Free tier: None or empty
3536
- Pro/Enterprise: Valid API key
3637
run_id: Unique identifier for this agent run. If not provided, generates UUID.
38+
api_url: Sentience API base URL (default: https://api.sentienceapi.com)
3739
3840
Returns:
3941
Tracer configured with appropriate sink
@@ -55,16 +57,19 @@ def create_tracer(
5557
if run_id is None:
5658
run_id = str(uuid.uuid4())
5759

60+
if api_url is None:
61+
api_url = SENTIENCE_API_URL
62+
5863
# 0. Check for orphaned traces from previous crashes (if api_key provided)
5964
if api_key:
60-
_recover_orphaned_traces(api_key, SENTIENCE_API_URL)
65+
_recover_orphaned_traces(api_key, api_url)
6166

6267
# 1. Try to initialize Cloud Sink (Pro/Enterprise tier)
6368
if api_key:
6469
try:
6570
# Request pre-signed upload URL from backend
6671
response = requests.post(
67-
f"{SENTIENCE_API_URL}/v1/traces/init",
72+
f"{api_url}/v1/traces/init",
6873
headers={"Authorization": f"Bearer {api_key}"},
6974
json={"run_id": run_id},
7075
timeout=10,

tests/test_cloud_tracing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,30 @@ def test_create_tracer_uses_constant_api_url(self):
381381

382382
tracer.close()
383383

384+
def test_create_tracer_custom_api_url(self):
385+
"""Test create_tracer accepts custom api_url parameter."""
386+
custom_api_url = "https://custom.api.example.com"
387+
388+
with patch("sentience.tracer_factory.requests.post") as mock_post:
389+
with patch("sentience.cloud_tracing.requests.put") as mock_put:
390+
# Mock API response
391+
mock_response = Mock()
392+
mock_response.status_code = 200
393+
mock_response.json.return_value = {"upload_url": "https://storage.com/upload"}
394+
mock_post.return_value = mock_response
395+
mock_put.return_value = Mock(status_code=200)
396+
397+
tracer = create_tracer(
398+
api_key="sk_test123", run_id="test-run", api_url=custom_api_url
399+
)
400+
401+
# Verify custom API URL was used
402+
assert mock_post.called
403+
call_args = mock_post.call_args
404+
assert call_args[0][0] == f"{custom_api_url}/v1/traces/init"
405+
406+
tracer.close()
407+
384408
def test_create_tracer_missing_upload_url_in_response(self, capsys):
385409
"""Test create_tracer handles missing upload_url gracefully."""
386410
with patch("sentience.tracer_factory.requests.post") as mock_post:

0 commit comments

Comments
 (0)