add query p90
parent
69928086c3
commit
a20aa7af82
@ -0,0 +1,66 @@
|
||||
import json
|
||||
from collections.abc import Generator
|
||||
from typing import Any, Optional
|
||||
|
||||
import requests
|
||||
|
||||
from configs import dify_config
|
||||
from core.tools.builtin_tool.tool import BuiltinTool
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from libs.apo_utils import APOUtils
|
||||
|
||||
|
||||
class ThreadPolarisP90(BuiltinTool):
|
||||
def _invoke(
|
||||
self,
|
||||
user_id: str,
|
||||
tool_parameters: dict[str, Any],
|
||||
conversation_id: Optional[str] = None,
|
||||
app_id: Optional[str] = None,
|
||||
message_id: Optional[str] = None,
|
||||
) -> Generator[ToolInvokeMessage, None, None]:
|
||||
pod = tool_parameters.get('pod', '.*')
|
||||
start_time = tool_parameters.get("startTime")
|
||||
end_time = tool_parameters.get("endTime")
|
||||
type = tool_parameters.get('type')
|
||||
metrics = self.get_metrics(type, pod, start_time, end_time)
|
||||
res = self.get_max_min(metrics)
|
||||
list = json.dumps({
|
||||
'type': 'p90',
|
||||
'data': res
|
||||
})
|
||||
yield self.create_text_message(list)
|
||||
|
||||
def get_max_min(self, data_json) -> str:
|
||||
results = data_json["data"]["result"]
|
||||
|
||||
stats = {}
|
||||
|
||||
for entry in results:
|
||||
tid = entry["metric"]["tid"]
|
||||
values = [float(value[1]) for value in entry["values"]]
|
||||
|
||||
stats[tid] = {
|
||||
"max": max(values),
|
||||
"min": min(values)
|
||||
}
|
||||
|
||||
return json.dumps(stats)
|
||||
|
||||
def get_metrics(self, type: str, pod: str, start: int, end: int) -> dict:
|
||||
pre_query = '''histogram_quantile(0.9, sum(rate(originx_thread_polaris_nanoseconds_bucket{pod="'''
|
||||
query = pre_query + pod + '''", type="''' + type + '''"}[1m])) by (tid, vmrange))'''
|
||||
step = '10m'
|
||||
hour = 3600 * 1000
|
||||
|
||||
res = requests.get(
|
||||
dify_config.APO_VM_URL + "/prometheus/api/v1/query_range",
|
||||
params={
|
||||
'query': query,
|
||||
'start': end / 1000 - hour,
|
||||
'end': end / 1000,
|
||||
'step': step
|
||||
}
|
||||
)
|
||||
|
||||
return res.json()
|
||||
@ -0,0 +1,60 @@
|
||||
identity:
|
||||
name: 线程北极星P90数据
|
||||
author: APO
|
||||
label:
|
||||
en_US: Thread Polaris Metrics P90
|
||||
zh_Hans: 线程北极星P90数据
|
||||
description:
|
||||
human:
|
||||
en_US:
|
||||
zh_Hans: 线程北极星P90数据
|
||||
llm: Thread Polaris Metrics P90
|
||||
parameters:
|
||||
- name: pod
|
||||
type: string
|
||||
required: False
|
||||
label:
|
||||
en_US: Pod name
|
||||
zh_Hans: Pod名称
|
||||
human_description:
|
||||
en_US: Pod name
|
||||
zh_Hans: Pod名称
|
||||
llm_description: Pod name
|
||||
form: llm
|
||||
- name: type
|
||||
type: string
|
||||
required: False
|
||||
label:
|
||||
en_US: Type (cpu,net,file,epoll,runq)
|
||||
zh_Hans: 线程执行类型(cpu,net,file,epoll,runq)
|
||||
human_description:
|
||||
en_US: Type (cpu,net,file,epoll,runq)
|
||||
zh_Hans: 线程执行类型(cpu,net,file,epoll,runq)
|
||||
llm_description: Type (cpu,net,file,epoll,runq)
|
||||
form: llm
|
||||
- name: startTime
|
||||
type: number
|
||||
required: true
|
||||
label:
|
||||
en_US: startTime
|
||||
zh_Hans: startTime
|
||||
pt_BR: startTime
|
||||
human_description:
|
||||
en_US: Data query start time(Microsecond)
|
||||
zh_Hans: 开始时间 (微秒)
|
||||
pt_BR: Data query start time
|
||||
llm_description: Data query start time(Microsecond)
|
||||
form: llm
|
||||
- name: endTime
|
||||
type: number
|
||||
required: true
|
||||
label:
|
||||
en_US: endTime
|
||||
zh_Hans: endTime
|
||||
pt_BR: endTime
|
||||
human_description:
|
||||
en_US: Data query end time(Microsecond)
|
||||
zh_Hans: 结束时间 (微秒)
|
||||
pt_BR: Data query end time(Microsecond)
|
||||
llm_description: Data query end time(Microsecond)
|
||||
form: llm
|
||||
Loading…
Reference in New Issue