2323TRUE_LIST = ["true" , "on" , "1" ]
2424
2525_publish = utils .get_env_variable ("OPENLAYER_DISABLE_PUBLISH" ) not in TRUE_LIST
26- _verify_ssl = (
27- utils .get_env_variable ("OPENLAYER_VERIFY_SSL" ) or "true"
28- ).lower () in TRUE_LIST
26+ _verify_ssl = (utils .get_env_variable ("OPENLAYER_VERIFY_SSL" ) or "true" ).lower () in TRUE_LIST
2927_client = None
3028
29+ # Configuration variables for programmatic setup
30+ _configured_api_key : Optional [str ] = None
31+ _configured_pipeline_id : Optional [str ] = None
32+ _configured_base_url : Optional [str ] = None
33+
34+
35+ def configure (
36+ api_key : Optional [str ] = None ,
37+ inference_pipeline_id : Optional [str ] = None ,
38+ base_url : Optional [str ] = None ,
39+ ) -> None :
40+ """Configure the Openlayer tracer with custom settings.
41+
42+ This function allows you to programmatically set the API key, inference pipeline ID,
43+ and base URL for the Openlayer client, instead of relying on environment variables.
44+
45+ Args:
46+ api_key: The Openlayer API key. If not provided, falls back to OPENLAYER_API_KEY environment variable.
47+ inference_pipeline_id: The default inference pipeline ID to use for tracing.
48+ If not provided, falls back to OPENLAYER_INFERENCE_PIPELINE_ID environment variable.
49+ base_url: The base URL for the Openlayer API. If not provided, falls back to
50+ OPENLAYER_BASE_URL environment variable or the default.
51+
52+ Examples:
53+ >>> import openlayer.lib.tracing.tracer as tracer
54+ >>> # Configure with API key and pipeline ID
55+ >>> tracer.configure(api_key="your_api_key_here", inference_pipeline_id="your_pipeline_id_here")
56+ >>> # Now use the decorators normally
57+ >>> @tracer.trace()
58+ >>> def my_function():
59+ ... return "result"
60+ """
61+ global _configured_api_key , _configured_pipeline_id , _configured_base_url , _client
62+
63+ _configured_api_key = api_key
64+ _configured_pipeline_id = inference_pipeline_id
65+ _configured_base_url = base_url
66+
67+ # Reset the client so it gets recreated with new configuration
68+ _client = None
69+
3170
3271def _get_client () -> Optional [Openlayer ]:
3372 """Get or create the Openlayer client with lazy initialization."""
@@ -37,13 +76,24 @@ def _get_client() -> Optional[Openlayer]:
3776
3877 if _client is None :
3978 # Lazy initialization - create client when first needed
79+ client_kwargs = {}
80+
81+ # Use configured API key if available, otherwise fall back to environment variable
82+ if _configured_api_key is not None :
83+ client_kwargs ["api_key" ] = _configured_api_key
84+
85+ # Use configured base URL if available, otherwise fall back to environment variable
86+ if _configured_base_url is not None :
87+ client_kwargs ["base_url" ] = _configured_base_url
88+
4089 if _verify_ssl :
41- _client = Openlayer ()
90+ _client = Openlayer (** client_kwargs )
4291 else :
4392 _client = Openlayer (
4493 http_client = DefaultHttpxClient (
4594 verify = False ,
4695 ),
96+ ** client_kwargs ,
4797 )
4898 return _client
4999
@@ -163,9 +213,7 @@ def wrapper(*func_args, **func_kwargs):
163213 if step_kwargs .get ("name" ) is None :
164214 step_kwargs ["name" ] = func .__name__
165215
166- with create_step (
167- * step_args , inference_pipeline_id = inference_pipeline_id , ** step_kwargs
168- ) as step :
216+ with create_step (* step_args , inference_pipeline_id = inference_pipeline_id , ** step_kwargs ) as step :
169217 output = exception = None
170218 try :
171219 output = func (* func_args , ** func_kwargs )
@@ -252,14 +300,12 @@ async def __anext__(self):
252300 # Initialize tracing on first iteration only
253301 if not self ._trace_initialized :
254302 self ._original_gen = func (* func_args , ** func_kwargs )
255- self ._step , self ._is_root_step , self ._token = (
256- _create_and_initialize_step (
257- step_name = step_name ,
258- step_type = enums .StepType .USER_CALL ,
259- inputs = None ,
260- output = None ,
261- metadata = None ,
262- )
303+ self ._step , self ._is_root_step , self ._token = _create_and_initialize_step (
304+ step_name = step_name ,
305+ step_type = enums .StepType .USER_CALL ,
306+ inputs = None ,
307+ output = None ,
308+ metadata = None ,
263309 )
264310 self ._inputs = _extract_function_inputs (
265311 func_signature = func_signature ,
@@ -453,9 +499,7 @@ def _create_and_initialize_step(
453499 return new_step , is_root_step , token
454500
455501
456- def _handle_trace_completion (
457- is_root_step : bool , step_name : str , inference_pipeline_id : Optional [str ] = None
458- ) -> None :
502+ def _handle_trace_completion (is_root_step : bool , step_name : str , inference_pipeline_id : Optional [str ] = None ) -> None :
459503 """Handle trace completion and data streaming."""
460504 if is_root_step :
461505 logger .debug ("Ending the trace..." )
@@ -486,8 +530,12 @@ def _handle_trace_completion(
486530 )
487531 if _publish :
488532 try :
489- inference_pipeline_id = inference_pipeline_id or utils .get_env_variable (
490- "OPENLAYER_INFERENCE_PIPELINE_ID"
533+ # Use provided pipeline_id, or fall back to configured default,
534+ # or finally to environment variable
535+ inference_pipeline_id = (
536+ inference_pipeline_id
537+ or _configured_pipeline_id
538+ or utils .get_env_variable ("OPENLAYER_INFERENCE_PIPELINE_ID" )
491539 )
492540 client = _get_client ()
493541 if client :
@@ -503,8 +551,7 @@ def _handle_trace_completion(
503551 except Exception as err : # pylint: disable=broad-except
504552 logger .error (traceback .format_exc ())
505553 logger .error (
506- "Could not stream data to Openlayer (pipeline_id: %s, base_url: %s)"
507- " Error: %s" ,
554+ "Could not stream data to Openlayer (pipeline_id: %s, base_url: %s) Error: %s" ,
508555 inference_pipeline_id ,
509556 client .base_url ,
510557 err ,
@@ -536,9 +583,7 @@ def _process_wrapper_inputs_and_outputs(
536583 func_kwargs = func_kwargs ,
537584 context_kwarg = context_kwarg ,
538585 )
539- _finalize_step_logging (
540- step = step , inputs = inputs , output = output , start_time = step .start_time
541- )
586+ _finalize_step_logging (step = step , inputs = inputs , output = output , start_time = step .start_time )
542587
543588
544589def _extract_function_inputs (
@@ -606,9 +651,7 @@ def _finalize_async_generator_step(
606651) -> None :
607652 """Finalize async generator step - called when generator is consumed."""
608653 _current_step .reset (token )
609- _finalize_step_logging (
610- step = step , inputs = inputs , output = output , start_time = step .start_time
611- )
654+ _finalize_step_logging (step = step , inputs = inputs , output = output , start_time = step .start_time )
612655 _handle_trace_completion (
613656 is_root_step = is_root_step ,
614657 step_name = step_name ,
0 commit comments