Skip to content

Commit cde5db2

Browse files
authored
custom trace connect ptaas trace (#31)
1. support custom trace connect ptaas trace 2. fix async ptaas httpclient, modify to use AsyncClient
1 parent d29c69a commit cde5db2

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

CHANGLOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.1.16] - 2025-09-24
2+
### Added
3+
- support custom trace connect ptaas trace
4+
- fix async ptaas httpclient, modify to use AsyncClient
5+
16
## [0.1.15] - 2025-09-17
27
### Added
38
- modify cachetools version to >=5.5.2,<7.0.0

cozeloop/_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ def __init__(
181181
jwt_oauth_private_key=jwt_oauth_private_key,
182182
jwt_oauth_public_key_id=jwt_oauth_public_key_id
183183
)
184+
184185
http_client = httpclient.Client(
185186
api_base_url=api_base_url,
186187
http_client=inner_client,
187188
auth=auth,
188189
timeout=timeout,
189-
upload_timeout=upload_timeout
190+
upload_timeout=upload_timeout,
191+
header_injector=self._create_default_header_injector(),
190192
)
191193
finish_pro = default_finish_event_processor
192194
if trace_finish_event_processor:
@@ -219,6 +221,17 @@ def combined_processor(event_info: FinishEventInfo):
219221
prompt_trace=prompt_trace
220222
)
221223

224+
def _create_default_header_injector(self) -> Callable[[], Dict[str, str]]:
225+
def default_header_injector() -> Dict[str, str]:
226+
try:
227+
span = self.get_span_from_context()
228+
if span and hasattr(span, 'to_header'):
229+
return span.to_header()
230+
except Exception:
231+
pass
232+
return {}
233+
return default_header_injector
234+
222235
def _get_from_env(self, val: str, env_key: str) -> str:
223236
if val:
224237
return val

cozeloop/internal/httpclient/client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55
import os
6-
from typing import Optional, Dict, Union, IO, Type, Tuple, Any
6+
from typing import Optional, Dict, Union, IO, Type, Tuple, Any, Callable
77

88
import httpx
99
import pydantic
@@ -29,16 +29,17 @@ def __init__(
2929
auth: Auth,
3030
timeout: int = consts.DEFAULT_TIMEOUT,
3131
upload_timeout: int = consts.DEFAULT_UPLOAD_TIMEOUT,
32+
header_injector: Optional[Callable[[], Dict[str, str]]] = None,
3233
):
3334
self.api_base_url = api_base_url
3435
self.http_client = http_client
3536
self.auth = auth
3637
self.timeout = timeout
3738
self.upload_timeout = upload_timeout
39+
self.header_injector = header_injector
3840

3941
def _build_url(self, path: str) -> str:
4042
return f"{self.api_base_url}{path}"
41-
4243
def _set_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, str]:
4344
res = user_agent_header()
4445
if headers:
@@ -52,6 +53,14 @@ def _set_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, st
5253
if ppe_env:
5354
res["x-use-ppe"] = "1"
5455

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+
5564
return res
5665

5766
def request(

cozeloop/internal/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
22
# SPDX-License-Identifier: MIT
33

4-
VERSION = 'v0.1.11'
4+
VERSION = 'v0.1.16'

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cozeloop"
3-
version = "0.1.15"
3+
version = "0.1.16"
44
description = "coze loop sdk"
55
authors = ["JiangQi715 <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)