Skip to content

Commit 0e900b7

Browse files
committed
support auto fetch cozeloop space id
1 parent a6b9169 commit 0e900b7

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

veadk/configs/tracing_configs.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
DEFAULT_APMPLUS_OTEL_EXPORTER_ENDPOINT,
2424
DEFAULT_APMPLUS_OTEL_EXPORTER_SERVICE_NAME,
2525
DEFAULT_COZELOOP_OTEL_EXPORTER_ENDPOINT,
26+
DEFAULT_COZELOOP_SPACE_NAME,
2627
DEFAULT_TLS_OTEL_EXPORTER_ENDPOINT,
2728
DEFAULT_TLS_OTEL_EXPORTER_REGION,
2829
)
30+
from veadk.integrations.ve_cozeloop.ve_cozeloop import VeCozeloop
2931
from veadk.integrations.ve_tls.ve_tls import VeTLS
3032

3133

@@ -58,27 +60,24 @@ class CozeloopConfig(BaseSettings):
5860
default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_API_KEY"
5961
)
6062

61-
otel_exporter_space_id: str = Field(
62-
default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME"
63-
)
64-
6563
# TODO: auto fetching via AK/SK pair
6664
# @cached_property
6765
# def otel_exporter_api_key(self) -> str:
6866
# pass
6967

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", "")
68+
@cached_property
69+
def otel_exporter_space_id(self) -> str:
70+
workspace_id = os.getenv(
71+
"OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", ""
72+
)
7473

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-
# )
74+
if not workspace_id:
75+
# create a default one
76+
workspace_id = VeCozeloop(self.otel_exporter_api_key).create_workspace(
77+
workspace_name=DEFAULT_COZELOOP_SPACE_NAME
78+
)
8079

81-
# return workspace_id
80+
return workspace_id
8281

8382

8483
class TLSConfig(BaseSettings):

veadk/integrations/ve_cozeloop/ve_cozeloop.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,36 @@ def create_workspace(
3131
f"Automatically create Cozeloop workspace with name {workspace_name}"
3232
)
3333

34-
URL = "https://api.coze.cn/v1/workspaces"
34+
try:
35+
workspace_id = self.search_workspace_id(workspace_name=workspace_name)
36+
logger.info(f"Get existing Cozeloop workspace ID: {workspace_id}")
3537

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}")
5138
return workspace_id
52-
else:
53-
raise Exception(
54-
f"Failed to automatically create workspace: {response.json()}"
55-
)
56-
57-
def get_workspace_id(
39+
except Exception as _:
40+
URL = "https://api.coze.cn/v1/workspaces"
41+
42+
headers = {
43+
"Authorization": f"Bearer {self.api_key}",
44+
"Content-Type": "application/json",
45+
}
46+
47+
data = {
48+
"name": workspace_name,
49+
"description": "Created by Volcengine Agent Development Kit (VeADK)",
50+
}
51+
52+
response = requests.post(URL, headers=headers, json=data)
53+
54+
if response.json().get("code") == 0:
55+
workspace_id = response.json().get("data").get("id")
56+
logger.info(f"New created Cozeloop workspace ID: {workspace_id}")
57+
return workspace_id
58+
else:
59+
raise Exception(
60+
f"Failed to automatically create workspace: {response.json()}"
61+
)
62+
63+
def search_workspace_id(
5864
self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME
5965
) -> str:
6066
logger.info(
@@ -73,7 +79,7 @@ def get_workspace_id(
7379
"page_size": 50,
7480
}
7581

76-
response = requests.post(URL, headers=headers, json=data)
82+
response = requests.get(URL, headers=headers, json=data)
7783

7884
if response.json().get("code") == 0:
7985
workspaces = response.json().get("data").get("workspaces", [])

0 commit comments

Comments
 (0)