Skip to content

Commit 16429ac

Browse files
chore: format file
1 parent 7fb7cbe commit 16429ac

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

src/openlayer/lib/tracing/tracer.py

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
TRUE_LIST = ["true", "on", "1"]
2323

2424
_publish = utils.get_env_variable("OPENLAYER_DISABLE_PUBLISH") not in TRUE_LIST
25-
_verify_ssl = (utils.get_env_variable("OPENLAYER_VERIFY_SSL") or "true").lower() in TRUE_LIST
25+
_verify_ssl = (
26+
utils.get_env_variable("OPENLAYER_VERIFY_SSL") or "true"
27+
).lower() in TRUE_LIST
2628
_client = None
2729

2830

@@ -78,7 +80,11 @@ def create_step(
7880
) -> Generator[steps.Step, None, None]:
7981
"""Starts a trace and yields a Step object."""
8082
new_step, is_root_step, token = _create_and_initialize_step(
81-
step_name=name, step_type=step_type, inputs=inputs, output=output, metadata=metadata
83+
step_name=name,
84+
step_type=step_type,
85+
inputs=inputs,
86+
output=output,
87+
metadata=metadata,
8288
)
8389
try:
8490
yield new_step
@@ -90,7 +96,11 @@ def create_step(
9096
new_step.latency = latency
9197

9298
_current_step.reset(token)
93-
_handle_trace_completion(is_root_step=is_root_step, step_name=name, inference_pipeline_id=inference_pipeline_id)
99+
_handle_trace_completion(
100+
is_root_step=is_root_step,
101+
step_name=name,
102+
inference_pipeline_id=inference_pipeline_id,
103+
)
94104

95105

96106
def add_chat_completion_step_to_trace(**kwargs) -> None:
@@ -152,7 +162,9 @@ def wrapper(*func_args, **func_kwargs):
152162
if step_kwargs.get("name") is None:
153163
step_kwargs["name"] = func.__name__
154164

155-
with create_step(*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs) as step:
165+
with create_step(
166+
*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs
167+
) as step:
156168
output = exception = None
157169
try:
158170
output = func(*func_args, **func_kwargs)
@@ -239,8 +251,12 @@ async def __anext__(self):
239251
# Initialize tracing on first iteration only
240252
if not self._trace_initialized:
241253
self._original_gen = func(*func_args, **func_kwargs)
242-
self._step, self._is_root_step, self._token = _create_step_for_async_generator(
243-
step_name=step_name, inference_pipeline_id=inference_pipeline_id, **step_kwargs
254+
self._step, self._is_root_step, self._token = (
255+
_create_step_for_async_generator(
256+
step_name=step_name,
257+
inference_pipeline_id=inference_pipeline_id,
258+
**step_kwargs,
259+
)
244260
)
245261
self._inputs = _extract_function_inputs(
246262
func_signature=func_signature,
@@ -290,7 +306,11 @@ async def __anext__(self):
290306
# Create wrapper for regular async functions
291307
@wraps(func)
292308
async def async_function_wrapper(*func_args, **func_kwargs):
293-
with create_step(*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs) as step:
309+
with create_step(
310+
*step_args,
311+
inference_pipeline_id=inference_pipeline_id,
312+
**step_kwargs,
313+
) as step:
294314
output = exception = None
295315

296316
try:
@@ -317,7 +337,11 @@ async def async_function_wrapper(*func_args, **func_kwargs):
317337
# For sync functions, use the existing logic with optimizations
318338
@wraps(func)
319339
def sync_wrapper(*func_args, **func_kwargs):
320-
with create_step(*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs) as step:
340+
with create_step(
341+
*step_args,
342+
inference_pipeline_id=inference_pipeline_id,
343+
**step_kwargs,
344+
) as step:
321345
output = exception = None
322346
try:
323347
output = func(*func_args, **func_kwargs)
@@ -382,7 +406,13 @@ def _create_and_initialize_step(
382406
Returns:
383407
Tuple of (step, is_root_step, token)
384408
"""
385-
new_step = steps.step_factory(step_type=step_type, name=step_name, inputs=inputs, output=output, metadata=metadata)
409+
new_step = steps.step_factory(
410+
step_type=step_type,
411+
name=step_name,
412+
inputs=inputs,
413+
output=output,
414+
metadata=metadata,
415+
)
386416
new_step.start_time = time.time()
387417

388418
parent_step = get_current_step()
@@ -403,7 +433,9 @@ def _create_and_initialize_step(
403433
return new_step, is_root_step, token
404434

405435

406-
def _handle_trace_completion(is_root_step: bool, step_name: str, inference_pipeline_id: Optional[str] = None) -> None:
436+
def _handle_trace_completion(
437+
is_root_step: bool, step_name: str, inference_pipeline_id: Optional[str] = None
438+
) -> None:
407439
"""Handle trace completion and data streaming."""
408440
if is_root_step:
409441
logger.debug("Ending the trace...")
@@ -466,13 +498,21 @@ def _process_wrapper_inputs_and_outputs(
466498
) -> None:
467499
"""Extract function inputs and finalize step logging - common pattern across wrappers."""
468500
inputs = _extract_function_inputs(
469-
func_signature=func_signature, func_args=func_args, func_kwargs=func_kwargs, context_kwarg=context_kwarg
501+
func_signature=func_signature,
502+
func_args=func_args,
503+
func_kwargs=func_kwargs,
504+
context_kwarg=context_kwarg,
505+
)
506+
_finalize_step_logging(
507+
step=step, inputs=inputs, output=output, start_time=step.start_time
470508
)
471-
_finalize_step_logging(step=step, inputs=inputs, output=output, start_time=step.start_time)
472509

473510

474511
def _extract_function_inputs(
475-
func_signature: inspect.Signature, func_args: tuple, func_kwargs: dict, context_kwarg: Optional[str] = None
512+
func_signature: inspect.Signature,
513+
func_args: tuple,
514+
func_kwargs: dict,
515+
context_kwarg: Optional[str] = None,
476516
) -> dict:
477517
"""Extract and clean function inputs for logging."""
478518
bound = func_signature.bind(*func_args, **func_kwargs)
@@ -522,7 +562,11 @@ def _create_step_for_async_generator(
522562
) -> Tuple[steps.Step, bool, Any]:
523563
"""Create and initialize step for async generators - no context manager."""
524564
return _create_and_initialize_step(
525-
step_name=step_name, step_type=enums.StepType.USER_CALL, inputs=None, output=None, metadata=None
565+
step_name=step_name,
566+
step_type=enums.StepType.USER_CALL,
567+
inputs=None,
568+
output=None,
569+
metadata=None,
526570
)
527571

528572

@@ -537,9 +581,13 @@ def _finalize_async_generator_step(
537581
) -> None:
538582
"""Finalize async generator step - called when generator is consumed."""
539583
_current_step.reset(token)
540-
_finalize_step_logging(step=step, inputs=inputs, output=output, start_time=step.start_time)
584+
_finalize_step_logging(
585+
step=step, inputs=inputs, output=output, start_time=step.start_time
586+
)
541587
_handle_trace_completion(
542-
is_root_step=is_root_step, step_name=step_name, inference_pipeline_id=inference_pipeline_id
588+
is_root_step=is_root_step,
589+
step_name=step_name,
590+
inference_pipeline_id=inference_pipeline_id,
543591
)
544592

545593

0 commit comments

Comments
 (0)