Skip to content

Commit c45b1b1

Browse files
feat(tracer): implement lazy initialization for Openlayer client
- Refactored client initialization logic into a new `_get_client` function for better lazy loading. - Ensured the Openlayer client is only created when needed, improving resource management. - Updated data streaming calls to utilize the new client retrieval method, enhancing code readability and maintainability.
1 parent 3e49e0f commit c45b1b1

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/openlayer/lib/tracing/tracer.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@
2424
utils.get_env_variable("OPENLAYER_VERIFY_SSL") or "true"
2525
).lower() in TRUE_LIST
2626
_client = None
27-
if _publish:
28-
if _verify_ssl:
29-
_client = Openlayer()
30-
else:
31-
_client = Openlayer(
32-
http_client=DefaultHttpxClient(
33-
verify=False,
34-
),
35-
)
27+
28+
def _get_client() -> Optional[Openlayer]:
29+
"""Get or create the Openlayer client with lazy initialization."""
30+
global _client
31+
if not _publish:
32+
return None
33+
34+
if _client is None:
35+
# Lazy initialization - create client when first needed
36+
if _verify_ssl:
37+
_client = Openlayer()
38+
else:
39+
_client = Openlayer(
40+
http_client=DefaultHttpxClient(
41+
verify=False,
42+
),
43+
)
44+
return _client
3645

3746
_current_step = contextvars.ContextVar("current_step")
3847
_current_trace = contextvars.ContextVar("current_trace")
@@ -122,12 +131,14 @@ def create_step(
122131
)
123132
if _publish:
124133
try:
125-
_client.inference_pipelines.data.stream(
126-
inference_pipeline_id=inference_pipeline_id
127-
or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"),
128-
rows=[trace_data],
129-
config=config,
130-
)
134+
client = _get_client()
135+
if client:
136+
client.inference_pipelines.data.stream(
137+
inference_pipeline_id=inference_pipeline_id
138+
or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"),
139+
rows=[trace_data],
140+
config=config,
141+
)
131142
except Exception as err: # pylint: disable=broad-except
132143
logger.error("Could not stream data to Openlayer %s", err)
133144
else:
@@ -225,12 +236,14 @@ def _handle_trace_completion(
225236
)
226237
if _publish:
227238
try:
228-
_client.inference_pipelines.data.stream(
229-
inference_pipeline_id=inference_pipeline_id
230-
or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"),
231-
rows=[trace_data],
232-
config=config,
233-
)
239+
client = _get_client()
240+
if client:
241+
client.inference_pipelines.data.stream(
242+
inference_pipeline_id=inference_pipeline_id
243+
or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"),
244+
rows=[trace_data],
245+
config=config,
246+
)
234247
except Exception as err: # pylint: disable=broad-except
235248
logger.error("Could not stream data to Openlayer %s", err)
236249
else:

0 commit comments

Comments
 (0)