Skip to content

Commit fd28869

Browse files
authored
feat: add trace list span api (#19)
1 parent 34aa29c commit fd28869

File tree

6 files changed

+218
-11
lines changed

6 files changed

+218
-11
lines changed

libs/api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# coding:utf-8
2+
# Copyright (c) 2024 Bytedance Ltd. and/or its affiliates
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import os
16+
17+
from dotenv import load_dotenv
18+
from hiagent_api.observe import ObserveService
19+
from hiagent_api.observe_types import ListTraceSpansRequestSortBy, SortOrderType
20+
21+
load_dotenv()
22+
23+
24+
if __name__ == "__main__":
25+
observe = ObserveService(
26+
endpoint=os.getenv("HIAGENT_TOP_ENDPOINT"), region="cn-north-1"
27+
)
28+
29+
result = observe.ListTraceSpans(
30+
{
31+
"WorkspaceID": os.getenv("WORKSPACE_ID"),
32+
"PageSize": 100,
33+
"Sort": [
34+
{
35+
"SortOrder": SortOrderType.Desc,
36+
"SortBy": ListTraceSpansRequestSortBy.StartTime,
37+
}
38+
],
39+
}
40+
)
41+
42+
print(result)

libs/api/hiagent_api/observe.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def get_api_info():
6565
{},
6666
{},
6767
),
68+
"ListTraceSpans": ApiInfo(
69+
"POST",
70+
"/",
71+
{"Action": "ListTraceSpans", "Version": "2025-05-01"},
72+
{},
73+
{},
74+
),
6875
}
6976
return api_info
7077

@@ -96,6 +103,26 @@ def CreateApiToken(
96103
self.__request("CreateApiToken", params.model_dump())
97104
)
98105

106+
def ListTraceSpans(
107+
self, params: observe_types.ListTraceSpansRequest
108+
) -> observe_types.ListTraceSpansResponse:
109+
"""
110+
获取 trace spans
111+
112+
Args:
113+
params (observe_types.CreateApiTokenRequest):
114+
115+
Raises:
116+
Exception:
117+
118+
Returns:
119+
Dict:
120+
121+
"""
122+
return observe_types.ListTraceSpansResponse.model_validate(
123+
self.__request("ListTraceSpans", params)
124+
)
125+
99126
def __request(self, action, params):
100127
res = self.json(action, dict(), json.dumps(params))
101128
if res == "":

libs/api/hiagent_api/observe_types.py

Lines changed: 142 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,162 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
from enum import Enum
16+
from typing import Dict, List
17+
1518
from pydantic import BaseModel, Field
1619

1720

1821
class CreateApiTokenRequest(BaseModel):
1922
WorkspaceID: str = Field(
20-
title="工作空间 ID",
21-
description="工作空间 ID",
22-
example="wcxxxxxxxxxxxxxxxxxxx"
23+
title="工作空间 ID", description="工作空间 ID", example="wcxxxxxxxxxxxxxxxxxxx"
2324
)
2425
CustomAppID: str = Field(
2526
title="自定义应用 ID",
2627
description="自定义应用 ID",
27-
example="appxxxxxxxxxxxxxxxxxxx"
28+
example="appxxxxxxxxxxxxxxxxxxx",
2829
)
2930

3031

3132
class CreateApiTokenResponse(BaseModel):
3233
Token: str = Field(
33-
title="API Token",
34-
description="API Token",
35-
example="wcxxxxxxxxxxxxxxxxxxx"
34+
title="API Token", description="API Token", example="wcxxxxxxxxxxxxxxxxxxx"
3635
)
3736
ExpiresIn: int = Field(
38-
title="有效时间",
39-
description="有效时间,单位为秒",
40-
example=3600
37+
title="有效时间", description="有效时间,单位为秒", example=3600
38+
)
39+
40+
41+
class ListTraceSpansRequestSortBy(str, Enum):
42+
StartTime = "StartTime"
43+
Latency = "Latency"
44+
LatencyFirstResp = "LatencyFirstResp"
45+
TotalTokens = "TotalTokens"
46+
47+
48+
class SortOrderType(str, Enum):
49+
Desc = "Desc"
50+
Asc = "Asc"
51+
52+
53+
class ListTraceSpansRequestSort(BaseModel):
54+
SortOrder: SortOrderType = Field(
55+
title="排序顺序", description="排序顺序", example=SortOrderType.Desc
56+
)
57+
SortBy: ListTraceSpansRequestSortBy = Field(
58+
title="排序字段",
59+
description="排序字段",
60+
example=ListTraceSpansRequestSortBy.StartTime,
61+
)
62+
63+
64+
class ListTraceSpansRequest(BaseModel):
65+
WorkspaceID: str = Field(
66+
title="工作空间 ID", description="工作空间 ID", example="wcxxxxxxxxxxxxxxxxxxx"
67+
)
68+
PageSize: int = Field(title="分页大小", description="分页大小", example=10)
69+
LastID: str = Field(
70+
title="上一页最后一条记录的 ID",
71+
description="上一页最后一条记录的 ID",
72+
example="xxxxxxxxxxxxxxxxxxx",
73+
)
74+
Sort: List[ListTraceSpansRequestSort] = Field(
75+
title="排序条件",
76+
description="排序条件",
77+
example=[
78+
{
79+
"SortOrder": SortOrderType.Desc,
80+
"SortBy": ListTraceSpansRequestSortBy.StartTime,
81+
}
82+
],
83+
)
84+
85+
86+
class TraceSpanContext(BaseModel):
87+
TraceID: str = Field(
88+
title="Trace ID",
89+
description="Trace ID",
90+
example="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
91+
)
92+
SpanID: str = Field(
93+
title="Span ID",
94+
description="Span ID",
95+
example="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
96+
)
97+
ParentSpanID: str = Field(
98+
title="Parent Span ID",
99+
description="Parent Span ID",
100+
example="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
101+
)
102+
103+
104+
class TraceSpanStatusCode(str, Enum):
105+
Unset = "UNSET"
106+
OK = "OK"
107+
Error = "ERROR"
108+
Unknown = "UNKNOWN"
109+
Cancelled = "CANCELLED"
110+
111+
112+
class TraceSpanStatus(BaseModel):
113+
Code: TraceSpanStatusCode = Field(
114+
title="状态码",
115+
description="状态码",
116+
)
117+
Message: str = Field(
118+
title="状态信息",
119+
description="状态信息",
120+
)
121+
122+
123+
class TraceSpanItem(BaseModel):
124+
DocID: str = Field(
125+
title="文档 ID",
126+
description="文档 ID",
127+
example="xxxxxxxxxxxxxxxxxxx",
128+
)
129+
Context: TraceSpanContext = Field(
130+
title="Span 上下文信息",
131+
description="Span 上下文信息",
132+
)
133+
Name: str = Field(
134+
title="Span 名称",
135+
description="Span 名称",
136+
)
137+
Status: TraceSpanStatus = Field(
138+
title="Span 状态信息",
139+
description="Span 状态信息",
140+
)
141+
StartTime: str = Field(
142+
title="Span 开始时间",
143+
description="Span 开始时间",
144+
)
145+
EndTime: str = Field(
146+
title="Span 结束时间",
147+
description="Span 结束时间",
148+
)
149+
Attributes: Dict[str, str] = Field(
150+
title="Span 属性信息",
151+
description="Span 属性信息",
152+
)
153+
Resource: Dict[str, str] = Field(
154+
title="Span 资源信息",
155+
description="Span 资源信息",
156+
)
157+
158+
159+
class ListTraceSpansResponse(BaseModel):
160+
Total: int = Field(
161+
title="总记录数",
162+
description="总记录数",
163+
example=1,
164+
)
165+
HasMore: bool = Field(
166+
title="是否有更多数据",
167+
description="是否有更多数据",
168+
example=False,
169+
)
170+
Items: List[TraceSpanItem] = Field(
171+
title="Span 列表",
172+
description="Span 列表",
41173
)

libs/api/pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ packages = ["."]
2323
[tool.hatch.build.targets.sdist]
2424
exclude = [
2525
"examples/",
26-
]
26+
]
27+
28+
[dependency-groups]
29+
dev = [
30+
"dotenv>=0.9.9",
31+
]

0 commit comments

Comments
 (0)