Skip to content

Commit 236293e

Browse files
committed
chore(tracing): support auto fetch cozeloop id
1 parent d51e3f7 commit 236293e

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

veadk/configs/tracing_configs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ class CozeloopConfig(BaseSettings):
6262
default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME"
6363
)
6464

65+
# TODO: auto fetching via AK/SK pair
66+
# @cached_property
67+
# def otel_exporter_api_key(self) -> str:
68+
# pass
69+
70+
# TODO: auto fetching workspace id
71+
# @cached_property
72+
# def otel_exporter_space_id(self) -> str:
73+
# workspace_id = os.getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", "")
74+
75+
# if not workspace_id:
76+
# # create a default one
77+
# workspace_id = VeCozeloop(self.otel_exporter_api_key).create_workspace(
78+
# workspace_name=DEFAULT_COZELOOP_SPACE_NAME
79+
# )
80+
81+
# return workspace_id
82+
6583

6684
class TLSConfig(BaseSettings):
6785
otel_exporter_endpoint: str = Field(

veadk/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858

5959
DEFAULT_TOS_BUCKET_NAME = "veadk-default-bucket"
6060

61-
DEFAULT_COZELOOP_SPACE_NAME = "veadk-space"
61+
DEFAULT_COZELOOP_SPACE_NAME = "VeADK Space"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import requests
16+
17+
from veadk.consts import DEFAULT_COZELOOP_SPACE_NAME
18+
from veadk.utils.logger import get_logger
19+
20+
logger = get_logger(__name__)
21+
22+
23+
class VeCozeloop:
24+
def __init__(self, api_key: str) -> None:
25+
self.api_key = api_key
26+
27+
def create_workspace(
28+
self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME
29+
) -> str:
30+
logger.info(
31+
f"Automatically create Cozeloop workspace with name {workspace_name}"
32+
)
33+
34+
URL = "https://api.coze.cn/v1/workspaces"
35+
36+
headers = {
37+
"Authorization": f"Bearer {self.api_key}",
38+
"Content-Type": "application/json",
39+
}
40+
41+
data = {
42+
"name": workspace_name,
43+
"description": "Created by Volcengine Agent Development Kit (VeADK)",
44+
}
45+
46+
response = requests.post(URL, headers=headers, json=data)
47+
48+
if response.json().get("code") == 0:
49+
workspace_id = response.json().get("data").get("id")
50+
logger.info(f"Cozeloop workspace ID: {workspace_id}")
51+
return workspace_id
52+
else:
53+
raise Exception(
54+
f"Failed to automatically create workspace: {response.json()}"
55+
)
56+
57+
def get_workspace_id(
58+
self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME
59+
) -> str:
60+
logger.info(
61+
f"Automatically fetching Cozeloop workspace ID with name {workspace_name}"
62+
)
63+
64+
URL = "https://api.coze.cn/v1/workspaces"
65+
66+
headers = {
67+
"Authorization": f"Bearer {self.api_key}",
68+
"Content-Type": "application/json",
69+
}
70+
71+
data = {
72+
"page_num": 1,
73+
"page_size": 50,
74+
}
75+
76+
response = requests.post(URL, headers=headers, json=data)
77+
78+
if response.json().get("code") == 0:
79+
workspaces = response.json().get("data").get("workspaces", [])
80+
81+
workspace_id = ""
82+
for workspace in workspaces:
83+
if workspace.get("name") == workspace_name:
84+
workspace_id = workspace.get("id")
85+
logger.info(f"Get Cozeloop workspace ID: {workspace_id}")
86+
return workspace_id
87+
88+
raise Exception(f"Workspace with name {workspace_name} not found.")
89+
else:
90+
raise Exception(f"Failed to get workspace ID: {response.json()}")

veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ TIMEOUT=${_FAAS_FUNC_TIMEOUT}
1515
export SERVER_HOST=$HOST
1616
export SERVER_PORT=$PORT
1717

18-
export PYTHONPATH=$PYTHONPATH:./site-packages
18+
# use cached veadk-python and corresponding deps in VeFaaS
19+
# `./preinstalled-site-packages` stores veadk-python and its dependencies
20+
export PYTHONPATH=$PYTHONPATH:./site-packages:./preinstalled-site-packages
21+
1922
# Parse arguments
2023
while [[ $# -gt 0 ]]; do
2124
case $1 in

0 commit comments

Comments
 (0)