77import os
88import threading
99from datetime import datetime
10- from typing import Dict , Any , List , Optional
10+ from typing import Dict , Any , List , Optional , Callable
11+
12+ import httpx
1113
1214from cozeloop .client import Client
1315from cozeloop ._noop import NOOP_SPAN , _NoopClient
1719from cozeloop .internal .httpclient import Auth
1820from cozeloop .internal .prompt import PromptProvider
1921from cozeloop .internal .trace import TraceProvider
22+ from cozeloop .internal .trace .model .model import FinishEventInfo , TagTruncateConf
23+ from cozeloop .internal .trace .trace import default_finish_event_processor
2024from cozeloop .span import SpanContext , Span
2125
2226logger = logging .getLogger (__name__ )
@@ -54,8 +58,11 @@ def new_client(
5458 prompt_cache_max_count : int = consts .DEFAULT_PROMPT_CACHE_MAX_COUNT ,
5559 prompt_cache_refresh_interval : int = consts .DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL ,
5660 prompt_trace : bool = False ,
61+ http_client : Optional [httpx .Client ] = None ,
62+ trace_finish_event_processor : Optional [Callable [[FinishEventInfo ], None ]] = None ,
63+ tag_truncate_conf : Optional [TagTruncateConf ] = None ,
5764) -> Client :
58- cache_key = _generate_cache_key (
65+ cache_key = _generate_cache_key ( # all args are used to generate cache key
5966 api_base_url ,
6067 workspace_id ,
6168 api_token ,
@@ -67,7 +74,10 @@ def new_client(
6774 ultra_large_report ,
6875 prompt_cache_max_count ,
6976 prompt_cache_refresh_interval ,
70- prompt_trace
77+ prompt_trace ,
78+ http_client ,
79+ trace_finish_event_processor ,
80+ tag_truncate_conf ,
7181 )
7282
7383 with _cache_lock :
@@ -88,6 +98,8 @@ def new_client(
8898 prompt_cache_max_count = prompt_cache_max_count ,
8999 prompt_cache_refresh_interval = prompt_cache_refresh_interval ,
90100 prompt_trace = prompt_trace ,
101+ arg_http_client = http_client ,
102+ trace_finish_event_processor = trace_finish_event_processor ,
91103 )
92104 _client_cache [cache_key ] = client
93105 return client
@@ -113,7 +125,10 @@ def __init__(
113125 ultra_large_report : bool = False ,
114126 prompt_cache_max_count : int = consts .DEFAULT_PROMPT_CACHE_MAX_COUNT ,
115127 prompt_cache_refresh_interval : int = consts .DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL ,
116- prompt_trace : bool = False
128+ prompt_trace : bool = False ,
129+ arg_http_client : Optional [httpx .Client ] = None ,
130+ trace_finish_event_processor : Optional [Callable [[FinishEventInfo ], None ]] = None ,
131+ tag_truncate_conf : Optional [TagTruncateConf ] = None ,
117132 ):
118133 workspace_id = self ._get_from_env (workspace_id , ENV_WORKSPACE_ID )
119134 api_base_url = self ._get_from_env (api_base_url , ENV_API_BASE_URL )
@@ -136,6 +151,8 @@ def __init__(
136151
137152 self ._workspace_id = workspace_id
138153 inner_client = httpclient .HTTPClient ()
154+ if arg_http_client :
155+ inner_client = arg_http_client
139156 auth = self ._build_auth (
140157 api_base_url = api_base_url ,
141158 http_client = inner_client ,
@@ -151,10 +168,18 @@ def __init__(
151168 timeout = timeout ,
152169 upload_timeout = upload_timeout
153170 )
171+ finish_pro = default_finish_event_processor
172+ if trace_finish_event_processor :
173+ def combined_processor (event_info : FinishEventInfo ):
174+ default_finish_event_processor (event_info )
175+ trace_finish_event_processor (event_info )
176+ finish_pro = combined_processor
154177 self ._trace_provider = TraceProvider (
155178 http_client = http_client ,
156179 workspace_id = workspace_id ,
157- ultra_large_report = ultra_large_report
180+ ultra_large_report = ultra_large_report ,
181+ finish_event_processor = finish_pro ,
182+ tag_truncate_conf = tag_truncate_conf ,
158183 )
159184 self ._prompt_provider = PromptProvider (
160185 workspace_id = workspace_id ,
@@ -234,7 +259,7 @@ def start_span(
234259 else :
235260 return self ._trace_provider .start_span (name = name , span_type = span_type , start_time = start_time ,
236261 parent_span_id = child_of .span_id , trace_id = child_of .trace_id ,
237- baggage = child_of .baggage , start_new_trace = start_new_trace )
262+ baggage = child_of .baggage () , start_new_trace = start_new_trace )
238263 except Exception as e :
239264 logger .warning (f"Start span failed, returning noop span. Error: { e } " )
240265 return NOOP_SPAN
0 commit comments