diff --git a/veadk/configs/tracing_configs.py b/veadk/configs/tracing_configs.py index 2b913d73..ff0b7424 100644 --- a/veadk/configs/tracing_configs.py +++ b/veadk/configs/tracing_configs.py @@ -62,6 +62,24 @@ class CozeloopConfig(BaseSettings): default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME" ) + # TODO: auto fetching via AK/SK pair + # @cached_property + # def otel_exporter_api_key(self) -> str: + # pass + + # TODO: auto fetching workspace id + # @cached_property + # def otel_exporter_space_id(self) -> str: + # workspace_id = os.getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", "") + + # if not workspace_id: + # # create a default one + # workspace_id = VeCozeloop(self.otel_exporter_api_key).create_workspace( + # workspace_name=DEFAULT_COZELOOP_SPACE_NAME + # ) + + # return workspace_id + class TLSConfig(BaseSettings): otel_exporter_endpoint: str = Field( diff --git a/veadk/consts.py b/veadk/consts.py index 3163a52f..9c8d7eb7 100644 --- a/veadk/consts.py +++ b/veadk/consts.py @@ -58,4 +58,4 @@ DEFAULT_TOS_BUCKET_NAME = "veadk-default-bucket" -DEFAULT_COZELOOP_SPACE_NAME = "veadk-space" +DEFAULT_COZELOOP_SPACE_NAME = "VeADK Space" diff --git a/veadk/integrations/ve_cozeloop/__init__.py b/veadk/integrations/ve_cozeloop/__init__.py new file mode 100644 index 00000000..7f463206 --- /dev/null +++ b/veadk/integrations/ve_cozeloop/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/veadk/integrations/ve_cozeloop/ve_cozeloop.py b/veadk/integrations/ve_cozeloop/ve_cozeloop.py new file mode 100644 index 00000000..278ffc15 --- /dev/null +++ b/veadk/integrations/ve_cozeloop/ve_cozeloop.py @@ -0,0 +1,90 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests + +from veadk.consts import DEFAULT_COZELOOP_SPACE_NAME +from veadk.utils.logger import get_logger + +logger = get_logger(__name__) + + +class VeCozeloop: + def __init__(self, api_key: str) -> None: + self.api_key = api_key + + def create_workspace( + self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME + ) -> str: + logger.info( + f"Automatically create Cozeloop workspace with name {workspace_name}" + ) + + URL = "https://api.coze.cn/v1/workspaces" + + headers = { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + } + + data = { + "name": workspace_name, + "description": "Created by Volcengine Agent Development Kit (VeADK)", + } + + response = requests.post(URL, headers=headers, json=data) + + if response.json().get("code") == 0: + workspace_id = response.json().get("data").get("id") + logger.info(f"Cozeloop workspace ID: {workspace_id}") + return workspace_id + else: + raise Exception( + f"Failed to automatically create workspace: {response.json()}" + ) + + def get_workspace_id( + self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME + ) -> str: + logger.info( + f"Automatically fetching Cozeloop workspace ID with name {workspace_name}" + ) + + URL = "https://api.coze.cn/v1/workspaces" + + headers = { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + } + + data = { + "page_num": 1, + "page_size": 50, + } + + response = requests.post(URL, headers=headers, json=data) + + if response.json().get("code") == 0: + workspaces = response.json().get("data").get("workspaces", []) + + workspace_id = "" + for workspace in workspaces: + if workspace.get("name") == workspace_name: + workspace_id = workspace.get("id") + logger.info(f"Get Cozeloop workspace ID: {workspace_id}") + return workspace_id + + raise Exception(f"Workspace with name {workspace_name} not found.") + else: + raise Exception(f"Failed to get workspace ID: {response.json()}") diff --git a/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh b/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh index 58bdcb96..62e2abeb 100755 --- a/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh +++ b/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh @@ -15,7 +15,10 @@ TIMEOUT=${_FAAS_FUNC_TIMEOUT} export SERVER_HOST=$HOST export SERVER_PORT=$PORT -export PYTHONPATH=$PYTHONPATH:./site-packages +# use cached veadk-python and corresponding deps in VeFaaS +# `./preinstalled-site-packages` stores veadk-python and its dependencies +export PYTHONPATH=$PYTHONPATH:./site-packages:./preinstalled-site-packages + # Parse arguments while [[ $# -gt 0 ]]; do case $1 in