Skip to content

Commit 815e7ce

Browse files
committed
modify header_injector
1 parent 021daa5 commit 815e7ce

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

cozeloop/_client.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def new_client(
7474
tag_truncate_conf: Optional[TagTruncateConf] = None,
7575
api_base_path: Optional[APIBasePath] = None,
7676
trace_queue_conf: Optional[QueueConf] = None,
77-
header_injector: Optional[Callable[[], Dict[str, str]]] = None,
7877
) -> Client:
7978
cache_key = _generate_cache_key( # all args are used to generate cache key
8079
api_base_url,
@@ -94,7 +93,6 @@ def new_client(
9493
tag_truncate_conf,
9594
api_base_path,
9695
trace_queue_conf,
97-
header_injector,
9896
)
9997

10098
with _cache_lock:
@@ -120,7 +118,6 @@ def new_client(
120118
tag_truncate_conf=tag_truncate_conf,
121119
api_base_path=api_base_path,
122120
trace_queue_conf=trace_queue_conf,
123-
header_injector=header_injector,
124121
)
125122
_client_cache[cache_key] = client
126123
return client
@@ -152,7 +149,6 @@ def __init__(
152149
tag_truncate_conf: Optional[TagTruncateConf] = None,
153150
api_base_path: Optional[APIBasePath] = None,
154151
trace_queue_conf: Optional[QueueConf] = None,
155-
header_injector: Optional[Callable[[], Dict[str, str]]] = None,
156152
):
157153
workspace_id = self._get_from_env(workspace_id, ENV_WORKSPACE_ID)
158154
api_base_url = self._get_from_env(api_base_url, ENV_API_BASE_URL)
@@ -185,18 +181,14 @@ def __init__(
185181
jwt_oauth_private_key=jwt_oauth_private_key,
186182
jwt_oauth_public_key_id=jwt_oauth_public_key_id
187183
)
188-
189-
# 创建默认的header注入函数
190-
if header_injector is None:
191-
header_injector = self._create_default_header_injector()
192-
184+
193185
http_client = httpclient.Client(
194186
api_base_url=api_base_url,
195187
http_client=inner_client,
196188
auth=auth,
197189
timeout=timeout,
198190
upload_timeout=upload_timeout,
199-
header_injector=header_injector,
191+
header_injector=self._create_default_header_injector(),
200192
)
201193
finish_pro = default_finish_event_processor
202194
if trace_finish_event_processor:
@@ -230,14 +222,12 @@ def combined_processor(event_info: FinishEventInfo):
230222
)
231223

232224
def _create_default_header_injector(self) -> Callable[[], Dict[str, str]]:
233-
"""创建默认的header注入函数,包含span header注入逻辑"""
234225
def default_header_injector() -> Dict[str, str]:
235226
try:
236227
span = self.get_span_from_context()
237228
if span and hasattr(span, 'to_header'):
238229
return span.to_header()
239230
except Exception:
240-
# 静默处理异常,不影响正常请求
241231
pass
242232
return {}
243233
return default_header_injector

cozeloop/internal/httpclient/client.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ def _build_url(self, path: str) -> str:
4242
return f"{self.api_base_url}{path}"
4343
def _set_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, str]:
4444
res = user_agent_header()
45-
46-
# 调用传入的header注入函数
47-
if self.header_injector:
48-
try:
49-
injected_headers = self.header_injector()
50-
if injected_headers:
51-
res.update(injected_headers)
52-
except Exception as e:
53-
# 静默处理异常,不影响正常请求
54-
logger.debug(f"Header injection failed: {e}")
55-
5645
if headers:
5746
res.update(headers)
5847
res[consts.AUTHORIZE_HEADER] = f"Bearer {self.auth.token}"
@@ -64,6 +53,14 @@ def _set_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, st
6453
if ppe_env:
6554
res["x-use-ppe"] = "1"
6655

56+
if self.header_injector:
57+
try:
58+
injected_headers = self.header_injector()
59+
if injected_headers:
60+
res.update(injected_headers)
61+
except Exception as e:
62+
logger.debug(f"Header injection failed: {e}")
63+
6764
return res
6865

6966
def request(

examples/prompt/ptaas/ptaas.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import asyncio
1515
import os
1616

17+
from anyio import sleep
18+
1719
from cozeloop import new_client, Client
1820
from cozeloop.entities.prompt import Message, Role, ExecuteResult
1921

@@ -141,7 +143,8 @@ async def async_stream_example(client: Client) -> None:
141143
async def main():
142144
"""Main function"""
143145
client = setup_client()
144-
146+
147+
root_span = client.start_span("root", "custom")
145148
try:
146149
# Sync non-stream call
147150
sync_non_stream_example(client)
@@ -157,6 +160,7 @@ async def main():
157160

158161
finally:
159162
# Close client
163+
root_span.finish()
160164
if hasattr(client, 'close'):
161165
client.close()
162166

0 commit comments

Comments
 (0)