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 , QueueConf
23+ from cozeloop .internal .trace .trace import default_finish_event_processor
2024from cozeloop .span import SpanContext , Span
2125
2226logger = logging .getLogger (__name__ )
3539_default_client = None
3640_client_lock = threading .Lock ()
3741
42+ class APIBasePath :
43+ def __init__ (
44+ self ,
45+ trace_span_upload_path : str = None ,
46+ trace_file_upload_path : str = None ,
47+ ):
48+ self .trace_span_upload_path = trace_span_upload_path
49+ self .trace_file_upload_path = trace_file_upload_path
50+
3851
3952def _generate_cache_key (* args ) -> str :
4053 key_str = "\t " .join (str (arg ) for arg in args )
@@ -54,8 +67,13 @@ def new_client(
5467 prompt_cache_max_count : int = consts .DEFAULT_PROMPT_CACHE_MAX_COUNT ,
5568 prompt_cache_refresh_interval : int = consts .DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL ,
5669 prompt_trace : bool = False ,
70+ http_client : Optional [httpx .Client ] = None ,
71+ trace_finish_event_processor : Optional [Callable [[FinishEventInfo ], None ]] = None ,
72+ tag_truncate_conf : Optional [TagTruncateConf ] = None ,
73+ api_base_path : Optional [APIBasePath ] = None ,
74+ trace_queue_conf : Optional [QueueConf ] = None ,
5775) -> Client :
58- cache_key = _generate_cache_key (
76+ cache_key = _generate_cache_key ( # all args are used to generate cache key
5977 api_base_url ,
6078 workspace_id ,
6179 api_token ,
@@ -67,7 +85,12 @@ def new_client(
6785 ultra_large_report ,
6886 prompt_cache_max_count ,
6987 prompt_cache_refresh_interval ,
70- prompt_trace
88+ prompt_trace ,
89+ http_client ,
90+ trace_finish_event_processor ,
91+ tag_truncate_conf ,
92+ api_base_path ,
93+ trace_queue_conf ,
7194 )
7295
7396 with _cache_lock :
@@ -88,6 +111,11 @@ def new_client(
88111 prompt_cache_max_count = prompt_cache_max_count ,
89112 prompt_cache_refresh_interval = prompt_cache_refresh_interval ,
90113 prompt_trace = prompt_trace ,
114+ arg_http_client = http_client ,
115+ trace_finish_event_processor = trace_finish_event_processor ,
116+ tag_truncate_conf = tag_truncate_conf ,
117+ api_base_path = api_base_path ,
118+ trace_queue_conf = trace_queue_conf ,
91119 )
92120 _client_cache [cache_key ] = client
93121 return client
@@ -113,7 +141,12 @@ def __init__(
113141 ultra_large_report : bool = False ,
114142 prompt_cache_max_count : int = consts .DEFAULT_PROMPT_CACHE_MAX_COUNT ,
115143 prompt_cache_refresh_interval : int = consts .DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL ,
116- prompt_trace : bool = False
144+ prompt_trace : bool = False ,
145+ arg_http_client : Optional [httpx .Client ] = None ,
146+ trace_finish_event_processor : Optional [Callable [[FinishEventInfo ], None ]] = None ,
147+ tag_truncate_conf : Optional [TagTruncateConf ] = None ,
148+ api_base_path : Optional [APIBasePath ] = None ,
149+ trace_queue_conf : Optional [QueueConf ] = None ,
117150 ):
118151 workspace_id = self ._get_from_env (workspace_id , ENV_WORKSPACE_ID )
119152 api_base_url = self ._get_from_env (api_base_url , ENV_API_BASE_URL )
@@ -136,6 +169,8 @@ def __init__(
136169
137170 self ._workspace_id = workspace_id
138171 inner_client = httpclient .HTTPClient ()
172+ if arg_http_client :
173+ inner_client = arg_http_client
139174 auth = self ._build_auth (
140175 api_base_url = api_base_url ,
141176 http_client = inner_client ,
@@ -151,10 +186,26 @@ def __init__(
151186 timeout = timeout ,
152187 upload_timeout = upload_timeout
153188 )
189+ finish_pro = default_finish_event_processor
190+ if trace_finish_event_processor :
191+ def combined_processor (event_info : FinishEventInfo ):
192+ default_finish_event_processor (event_info )
193+ trace_finish_event_processor (event_info )
194+ finish_pro = combined_processor
195+ span_upload_path = None
196+ file_upload_path = None
197+ if api_base_path :
198+ span_upload_path = api_base_path .trace_span_upload_path
199+ file_upload_path = api_base_path .trace_file_upload_path
154200 self ._trace_provider = TraceProvider (
155201 http_client = http_client ,
156202 workspace_id = workspace_id ,
157- ultra_large_report = ultra_large_report
203+ ultra_large_report = ultra_large_report ,
204+ finish_event_processor = finish_pro ,
205+ tag_truncate_conf = tag_truncate_conf ,
206+ span_upload_path = span_upload_path ,
207+ file_upload_path = file_upload_path ,
208+ queue_conf = trace_queue_conf ,
158209 )
159210 self ._prompt_provider = PromptProvider (
160211 workspace_id = workspace_id ,
@@ -234,7 +285,7 @@ def start_span(
234285 else :
235286 return self ._trace_provider .start_span (name = name , span_type = span_type , start_time = start_time ,
236287 parent_span_id = child_of .span_id , trace_id = child_of .trace_id ,
237- baggage = child_of .baggage , start_new_trace = start_new_trace )
288+ baggage = child_of .baggage () , start_new_trace = start_new_trace )
238289 except Exception as e :
239290 logger .warning (f"Start span failed, returning noop span. Error: { e } " )
240291 return NOOP_SPAN
0 commit comments