Skip to content

Commit e7c83eb

Browse files
authored
Merge branch 'main' into asgi-suppress-http-spans
2 parents 2c896e3 + 048b4ad commit e7c83eb

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

util/opentelemetry-util-genai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
([#4320](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4320))
1919
- Add workflow invocation type to genAI utils
2020
([https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4310](#4310))
21+
- Check if upload works at startup in initializer of the `UploadCompletionHook`, instead
22+
of repeatedly failing on every upload ([https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4390](#4390)).
2123

2224
## Version 0.3b0 (2026-02-20)
2325

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,30 @@ def __init__(
156156
f"Invalid {upload_format=}. Must be one of {_FORMATS}"
157157
)
158158
self._format = upload_format
159+
self._content_type = (
160+
"application/json"
161+
if self._format == "json"
162+
else "application/jsonl"
163+
)
164+
test_path = posixpath.join(
165+
self._base_path,
166+
f".one_off_test_to_see_if_upload_works.{self._format}",
167+
)
168+
try:
169+
with self._fs.open(
170+
test_path, "w", content_type=self._content_type
171+
) as file:
172+
file.write("\n")
173+
except Exception as exception: # pylint: disable=broad-exception-caught
174+
raise ValueError(
175+
f"Failed to write file to the following path, upload is not working: {test_path}.\n Got error: {exception}"
176+
)
177+
# Try to delete the file.. But we don't explicitly ask people to grant the GCS delete IAM permission in our
178+
# docs, so if delete fails just leave the file..
179+
try:
180+
self._fs.rm_file(test_path) # pyright: ignore[reportUnknownMemberType]
181+
except Exception: # pylint: disable=broad-exception-caught
182+
pass
159183

160184
# Use a ThreadPoolExecutor for its queueing and thread management. The semaphore
161185
# limits the number of queued tasks. If the queue is full, data will be dropped.
@@ -271,13 +295,7 @@ def _do_upload(
271295
for message_idx, line in enumerate(message_lines):
272296
line[_MESSAGE_INDEX_KEY] = message_idx
273297

274-
content_type = (
275-
"application/json"
276-
if self._format == "json"
277-
else "application/jsonl"
278-
)
279-
280-
with self._fs.open(path, "w", content_type=content_type) as file:
298+
with self._fs.open(path, "w", content_type=self._content_type) as file:
281299
for message in message_lines:
282300
gen_ai_json_dump(message, file)
283301
file.write("\n")

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def setUp(self):
102102
self.hook = UploadCompletionHook(
103103
base_path=BASE_PATH, max_queue_size=MAXSIZE, lru_cache_max_size=5
104104
)
105+
# 1 upload is done when creating the UploadHook to ensure upload works. Reset mock.
106+
self.mock_fs.reset_mock()
105107

106108
def tearDown(self) -> None:
107109
self.hook.shutdown()
@@ -145,6 +147,18 @@ def test_upload_then_shutdown(self):
145147
"should have uploaded 4 files",
146148
)
147149

150+
def test_failed_upload_causes_initializer_to_throw(self):
151+
self.mock_fs.open.side_effect = ValueError("Failed for some reason!")
152+
with self.assertRaisesRegex(
153+
ValueError,
154+
"Failed to write file to the following path, upload is not working:",
155+
):
156+
UploadCompletionHook(
157+
base_path=BASE_PATH,
158+
max_queue_size=MAXSIZE,
159+
lru_cache_max_size=5,
160+
)
161+
148162
def test_lru_cache_works(self):
149163
record = LogRecord()
150164
self.hook.on_completion(

0 commit comments

Comments
 (0)