Skip to content

Commit 32ed984

Browse files
committed
Always write through a temporary file
1 parent 02cbe28 commit 32ed984

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_upload/completion_hook.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import json
1919
import logging
2020
import posixpath
21+
import shutil
22+
from tempfile import SpooledTemporaryFile
23+
import tempfile
2124
import threading
2225
from base64 import b64encode
2326
from concurrent.futures import Future, ThreadPoolExecutor
@@ -190,15 +193,18 @@ def _do_upload(
190193
else "application/jsonl"
191194
)
192195

193-
with self._fs.open(path, "w", content_type=content_type) as file:
196+
with tempfile.NamedTemporaryFile(mode="w+") as temp_file:
194197
for message in message_lines:
195198
json.dump(
196199
message,
197-
file,
200+
temp_file,
198201
separators=(",", ":"),
199202
cls=Base64JsonEncoder,
200203
)
201-
file.write("\n")
204+
temp_file.write("\n")
205+
206+
temp_file.flush()
207+
self._fs.put_file(temp_file.name, path, content_type=content_type)
202208

203209
def on_completion(
204210
self,

util/opentelemetry-util-genai/tests/test_upload.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def blocked_upload(*args: Any, **kwargs: Any):
139139
return MagicMock()
140140

141141
try:
142-
self.mock_fs.open.side_effect = blocked_upload
142+
self.mock_fs.put_file.side_effect = blocked_upload
143143
yield
144144
finally:
145145
unblock_upload.set()
@@ -157,7 +157,7 @@ def test_upload_then_shutdown(self):
157157
self.hook.shutdown()
158158

159159
self.assertEqual(
160-
self.mock_fs.open.call_count,
160+
self.mock_fs.put_file.call_count,
161161
3,
162162
"should have uploaded 3 files",
163163
)
@@ -173,7 +173,7 @@ def test_upload_blocked(self):
173173
)
174174

175175
self.assertLessEqual(
176-
self.mock_fs.open.call_count,
176+
self.mock_fs.put_file.call_count,
177177
MAXSIZE,
178178
f"uploader should only be called {MAXSIZE=} times",
179179
)
@@ -201,7 +201,7 @@ def test_shutdown_timeout(self):
201201
self.hook.shutdown(timeout_sec=0.01)
202202

203203
def test_failed_upload_logs(self):
204-
self.mock_fs.open.side_effect = RuntimeError("failed to upload")
204+
self.mock_fs.put_file.side_effect = RuntimeError("failed to upload")
205205

206206
with self.assertLogs(level=logging.ERROR) as logs:
207207
self.hook.on_completion(
@@ -234,8 +234,8 @@ def test_upload_format_sets_content_type(self):
234234
)
235235
hook.shutdown()
236236

237-
self.mock_fs.open.assert_called_with(
238-
ANY, "w", content_type=expect_content_type
237+
self.mock_fs.put_file.assert_called_with(
238+
ANY, ANY, content_type=expect_content_type
239239
)
240240

241241
def test_parse_upload_format_envvar(self):

0 commit comments

Comments
 (0)