2025-12-05 13:05:53 +09:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
|
from flask_restx import Resource, fields
|
|
|
|
|
from pydantic import BaseModel, Field
|
2024-12-17 13:38:57 +08:00
|
|
|
from werkzeug.exceptions import BadRequest
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-11-24 11:04:11 +09:00
|
|
|
from controllers.console import console_ns
|
2024-06-26 17:33:29 +08:00
|
|
|
from controllers.console.app.error import TracingConfigCheckError, TracingConfigIsExist, TracingConfigNotExist
|
2024-11-01 15:51:22 +08:00
|
|
|
from controllers.console.wraps import account_initialization_required, setup_required
|
2024-06-26 17:33:29 +08:00
|
|
|
from libs.login import login_required
|
|
|
|
|
from services.ops_service import OpsService
|
|
|
|
|
|
2025-12-05 13:05:53 +09:00
|
|
|
DEFAULT_REF_TEMPLATE_SWAGGER_2_0 = "#/definitions/{model}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TraceProviderQuery(BaseModel):
|
|
|
|
|
tracing_provider: str = Field(..., description="Tracing provider name")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TraceConfigPayload(BaseModel):
|
|
|
|
|
tracing_provider: str = Field(..., description="Tracing provider name")
|
|
|
|
|
tracing_config: dict[str, Any] = Field(..., description="Tracing configuration data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console_ns.schema_model(
|
|
|
|
|
TraceProviderQuery.__name__,
|
|
|
|
|
TraceProviderQuery.model_json_schema(ref_template=DEFAULT_REF_TEMPLATE_SWAGGER_2_0),
|
|
|
|
|
)
|
|
|
|
|
console_ns.schema_model(
|
|
|
|
|
TraceConfigPayload.__name__, TraceConfigPayload.model_json_schema(ref_template=DEFAULT_REF_TEMPLATE_SWAGGER_2_0)
|
|
|
|
|
)
|
|
|
|
|
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-09-12 11:51:24 +08:00
|
|
|
@console_ns.route("/apps/<uuid:app_id>/trace-config")
|
2024-06-26 17:33:29 +08:00
|
|
|
class TraceAppConfigApi(Resource):
|
|
|
|
|
"""
|
|
|
|
|
Manage trace app configurations
|
|
|
|
|
"""
|
|
|
|
|
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("get_trace_app_config")
|
|
|
|
|
@console_ns.doc(description="Get tracing configuration for an application")
|
|
|
|
|
@console_ns.doc(params={"app_id": "Application ID"})
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[TraceProviderQuery.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(
|
2025-09-12 11:51:24 +08:00
|
|
|
200, "Tracing configuration retrieved successfully", fields.Raw(description="Tracing configuration data")
|
|
|
|
|
)
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(400, "Invalid request parameters")
|
2024-06-26 17:33:29 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, app_id):
|
2025-12-05 13:05:53 +09:00
|
|
|
args = TraceProviderQuery.model_validate(request.args.to_dict(flat=True)) # type: ignore
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
try:
|
2025-12-05 13:05:53 +09:00
|
|
|
trace_config = OpsService.get_tracing_app_config(app_id=app_id, tracing_provider=args.tracing_provider)
|
2024-06-26 17:33:29 +08:00
|
|
|
if not trace_config:
|
|
|
|
|
return {"has_not_configured": True}
|
|
|
|
|
return trace_config
|
|
|
|
|
except Exception as e:
|
2024-12-17 13:38:57 +08:00
|
|
|
raise BadRequest(str(e))
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("create_trace_app_config")
|
|
|
|
|
@console_ns.doc(description="Create a new tracing configuration for an application")
|
|
|
|
|
@console_ns.doc(params={"app_id": "Application ID"})
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[TraceConfigPayload.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(
|
2025-09-12 11:51:24 +08:00
|
|
|
201, "Tracing configuration created successfully", fields.Raw(description="Created configuration data")
|
|
|
|
|
)
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(400, "Invalid request parameters or configuration already exists")
|
2024-06-26 17:33:29 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, app_id):
|
|
|
|
|
"""Create a new trace app configuration"""
|
2025-12-05 13:05:53 +09:00
|
|
|
args = TraceConfigPayload.model_validate(console_ns.payload)
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
result = OpsService.create_tracing_app_config(
|
2025-12-05 13:05:53 +09:00
|
|
|
app_id=app_id, tracing_provider=args.tracing_provider, tracing_config=args.tracing_config
|
2024-06-26 17:33:29 +08:00
|
|
|
)
|
|
|
|
|
if not result:
|
|
|
|
|
raise TracingConfigIsExist()
|
2024-08-26 15:29:10 +08:00
|
|
|
if result.get("error"):
|
2024-06-26 17:33:29 +08:00
|
|
|
raise TracingConfigCheckError()
|
|
|
|
|
return result
|
|
|
|
|
except Exception as e:
|
2024-12-17 13:38:57 +08:00
|
|
|
raise BadRequest(str(e))
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("update_trace_app_config")
|
|
|
|
|
@console_ns.doc(description="Update an existing tracing configuration for an application")
|
|
|
|
|
@console_ns.doc(params={"app_id": "Application ID"})
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[TraceConfigPayload.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(200, "Tracing configuration updated successfully", fields.Raw(description="Success response"))
|
|
|
|
|
@console_ns.response(400, "Invalid request parameters or configuration not found")
|
2024-06-26 17:33:29 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def patch(self, app_id):
|
|
|
|
|
"""Update an existing trace app configuration"""
|
2025-12-05 13:05:53 +09:00
|
|
|
args = TraceConfigPayload.model_validate(console_ns.payload)
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
result = OpsService.update_tracing_app_config(
|
2025-12-05 13:05:53 +09:00
|
|
|
app_id=app_id, tracing_provider=args.tracing_provider, tracing_config=args.tracing_config
|
2024-06-26 17:33:29 +08:00
|
|
|
)
|
|
|
|
|
if not result:
|
|
|
|
|
raise TracingConfigNotExist()
|
|
|
|
|
return {"result": "success"}
|
|
|
|
|
except Exception as e:
|
2024-12-17 13:38:57 +08:00
|
|
|
raise BadRequest(str(e))
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("delete_trace_app_config")
|
|
|
|
|
@console_ns.doc(description="Delete an existing tracing configuration for an application")
|
|
|
|
|
@console_ns.doc(params={"app_id": "Application ID"})
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[TraceProviderQuery.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(204, "Tracing configuration deleted successfully")
|
|
|
|
|
@console_ns.response(400, "Invalid request parameters or configuration not found")
|
2024-06-26 17:33:29 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def delete(self, app_id):
|
|
|
|
|
"""Delete an existing trace app configuration"""
|
2025-12-05 13:05:53 +09:00
|
|
|
args = TraceProviderQuery.model_validate(request.args.to_dict(flat=True)) # type: ignore
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
try:
|
2025-12-05 13:05:53 +09:00
|
|
|
result = OpsService.delete_tracing_app_config(app_id=app_id, tracing_provider=args.tracing_provider)
|
2024-06-26 17:33:29 +08:00
|
|
|
if not result:
|
|
|
|
|
raise TracingConfigNotExist()
|
2025-04-27 12:12:46 +08:00
|
|
|
return {"result": "success"}, 204
|
2024-06-26 17:33:29 +08:00
|
|
|
except Exception as e:
|
2024-12-17 13:38:57 +08:00
|
|
|
raise BadRequest(str(e))
|