Skip to content

Commit 1e71d91

Browse files
committed
fix trace name
1 parent ad11076 commit 1e71d91

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

sentience/cloud_tracing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ def close(
146146

147147
self._closed = True
148148

149-
# Close file first
149+
# Flush and sync file to disk before closing to ensure all data is written
150+
# This is critical on CI systems where file system operations may be slower
151+
self._trace_file.flush()
152+
try:
153+
# Force OS to write buffered data to disk
154+
os.fsync(self._trace_file.fileno())
155+
except (OSError, AttributeError):
156+
# Some file handles don't support fsync (e.g., StringIO in tests)
157+
# This is fine - flush() is usually sufficient
158+
pass
150159
self._trace_file.close()
151160

152161
# Ensure file exists and has content before proceeding

tests/test_cloud_tracing.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -391,41 +391,43 @@ class TestTracerFactory:
391391

392392
def test_create_tracer_pro_tier_success(self, capsys):
393393
"""Test create_tracer returns CloudTraceSink for Pro tier."""
394-
with patch("sentience.tracer_factory.requests.post") as mock_post:
395-
with patch("sentience.cloud_tracing.requests.put") as mock_put:
396-
# Mock API response
397-
mock_response = Mock()
398-
mock_response.status_code = 200
399-
mock_response.json.return_value = {
400-
"upload_url": "https://sentience.nyc3.digitaloceanspaces.com/upload"
401-
}
402-
mock_post.return_value = mock_response
403-
404-
# Mock upload response
405-
mock_put.return_value = Mock(status_code=200)
406-
407-
run_id = f"test-run-{uuid.uuid4().hex[:8]}"
408-
tracer = create_tracer(api_key="sk_pro_test123", run_id=run_id, upload_trace=True)
394+
# Patch orphaned trace recovery to avoid extra API calls
395+
with patch("sentience.tracer_factory._recover_orphaned_traces"):
396+
with patch("sentience.tracer_factory.requests.post") as mock_post:
397+
with patch("sentience.cloud_tracing.requests.put") as mock_put:
398+
# Mock API response
399+
mock_response = Mock()
400+
mock_response.status_code = 200
401+
mock_response.json.return_value = {
402+
"upload_url": "https://sentience.nyc3.digitaloceanspaces.com/upload"
403+
}
404+
mock_post.return_value = mock_response
409405

410-
# Verify Pro tier message
411-
captured = capsys.readouterr()
412-
assert "☁️ [Sentience] Cloud tracing enabled (Pro tier)" in captured.out
406+
# Mock upload response
407+
mock_put.return_value = Mock(status_code=200)
413408

414-
# Verify tracer works
415-
assert tracer.run_id == run_id
416-
# Check if sink is CloudTraceSink (it should be)
417-
assert isinstance(
418-
tracer.sink, CloudTraceSink
419-
), f"Expected CloudTraceSink, got {type(tracer.sink)}"
420-
assert tracer.sink.run_id == run_id # Verify run_id is passed
409+
run_id = f"test-run-{uuid.uuid4().hex[:8]}"
410+
tracer = create_tracer(api_key="sk_pro_test123", run_id=run_id, upload_trace=True)
421411

422-
# Verify the init API was called
423-
assert mock_post.called
424-
assert mock_post.call_count == 1
425-
426-
# Cleanup - emit at least one event so file exists before close
427-
tracer.emit("test", {"v": 1, "seq": 1})
428-
tracer.close()
412+
# Verify Pro tier message
413+
captured = capsys.readouterr()
414+
assert "☁️ [Sentience] Cloud tracing enabled (Pro tier)" in captured.out
415+
416+
# Verify tracer works
417+
assert tracer.run_id == run_id
418+
# Check if sink is CloudTraceSink (it should be)
419+
assert isinstance(
420+
tracer.sink, CloudTraceSink
421+
), f"Expected CloudTraceSink, got {type(tracer.sink)}"
422+
assert tracer.sink.run_id == run_id # Verify run_id is passed
423+
424+
# Verify the init API was called (only once, since orphaned recovery is patched)
425+
assert mock_post.called
426+
assert mock_post.call_count == 1
427+
428+
# Cleanup - emit at least one event so file exists before close
429+
tracer.emit("test", {"v": 1, "seq": 1})
430+
tracer.close()
429431

430432
def test_create_tracer_free_tier_fallback(self, capsys):
431433
"""Test create_tracer falls back to local for free tier."""

0 commit comments

Comments
 (0)