Providing metrics information to Prometheus

Former-commit-id: f3afc76fa6e855dfa3861424d062f9d629d7b0a8
This commit is contained in:
zhangwei 2024-03-14 16:57:12 +08:00
parent 074c9175cb
commit 43bd51ce09
10 changed files with 233 additions and 64 deletions

View File

@ -158,7 +158,7 @@ type (
centerResourcesResp { centerResourcesResp {
CentersIndex []CenterIndex `json:"centersIndex"` CentersIndex []CenterIndex `json:"centersIndex"`
} }
CenterIndex{ CenterIndex {
name string `json:"name"` name string `json:"name"`
cpu float32 `json:"cpu"` cpu float32 `json:"cpu"`
memory float32 `json:"memory"` memory float32 `json:"memory"`
@ -166,6 +166,20 @@ type (
centerType string `json:"centerType"` centerType string `json:"centerType"`
} }
) )
type (
syncClusterLoadReq {
clusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"`
}
ClusterLoadRecord {
ClusterName string `json:"clusterName"`
CpuUsage float64 `json:"cpuUsage"`
MemoryUsage float64 `json:"memoryUsage"`
DiskUsage float64 `json:"diskUsage"`
}
)
type ( type (
getClusterListReq { getClusterListReq {
Id int64 `form:"id"` Id int64 `form:"id"`
@ -670,7 +684,7 @@ type (
type ( type (
AdapterQueryReq { AdapterQueryReq {
Id string `form:"id,optional" db:"id"` Id string `form:"id,optional" db:"id"`
Name string `form:"name,optional"` Name string `form:"name,optional"`
Type string `form:"type,optional"` Type string `form:"type,optional"`
Nickname string `form:"nickname,optional"` Nickname string `form:"nickname,optional"`
@ -678,7 +692,7 @@ type (
Server string `form:"server,optional"` Server string `form:"server,optional"`
} }
AdapterReq { AdapterReq {
Id string `json:"id,optional" db:"id"` Id string `json:"id,optional" db:"id"`
Name string `json:"name,optional"` Name string `json:"name,optional"`
Type string `json:"type,optional"` Type string `json:"type,optional"`
Nickname string `json:"nickname,optional"` Nickname string `json:"nickname,optional"`
@ -686,7 +700,7 @@ type (
Server string `json:"server,optional"` Server string `json:"server,optional"`
} }
AdapterCreateReq { AdapterCreateReq {
Id string `json:"id,optional" db:"id"` Id string `json:"id,optional" db:"id"`
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"` Type string `json:"type"`
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
@ -694,7 +708,7 @@ type (
Server string `json:"server"` Server string `json:"server"`
} }
AdapterDelReq { AdapterDelReq {
Id string `form:"id,optional" db:"id"` Id string `form:"id,optional" db:"id"`
} }
AdapterInfo { AdapterInfo {
Id string `json:"id,omitempty" db:"id"` Id string `json:"id,omitempty" db:"id"`
@ -727,51 +741,51 @@ type (
) )
type ClusterReq { type ClusterReq {
Id string `form:"id,optional"` Id string `form:"id,optional"`
AdapterId string `form:"adapterId,optional"` AdapterId string `form:"adapterId,optional"`
Name string `json:"name,optional"` Name string `json:"name,optional"`
Nickname string `json:"nickname,optional"` Nickname string `json:"nickname,optional"`
Description string `json:"description,optional"` Description string `json:"description,optional"`
Server string `json:"server,optional"` Server string `json:"server,optional"`
MonitorServer string `json:"monitorServer,optional"` MonitorServer string `json:"monitorServer,optional"`
Username string `json:"username,optional"` Username string `json:"username,optional"`
Password string `json:"password,optional"` Password string `json:"password,optional"`
Token string `json:"token,optional"` Token string `json:"token,optional"`
Ak string `json:"ak,optional"` Ak string `json:"ak,optional"`
Sk string `json:"sk,optional"` Sk string `json:"sk,optional"`
Region string `json:"region,optional"` Region string `json:"region,optional"`
ProjectId string `json:"projectId,optional"` ProjectId string `json:"projectId,optional"`
Version string `json:"version,optional"` Version string `json:"version,optional"`
Label string `json:"label,optional"` Label string `json:"label,optional"`
OwnerId string `json:"ownerId,omitempty,optional"` OwnerId string `json:"ownerId,omitempty,optional"`
AuthType string `json:"authType,optional"` AuthType string `json:"authType,optional"`
Type string `json:"type,optional"` Type string `json:"type,optional"`
} }
type ClusterDelReq { type ClusterDelReq {
Id string `form:"id,optional"` Id string `form:"id,optional"`
} }
type ClusterInfo { type ClusterInfo {
Id string `json:"id,omitempty" db:"id"` Id string `json:"id,omitempty" db:"id"`
AdapterId string `json:"adapterId,omitempty" db:"adapter_id"` AdapterId string `json:"adapterId,omitempty" db:"adapter_id"`
Name string `json:"name,omitempty" db:"name"` Name string `json:"name,omitempty" db:"name"`
Nickname string `json:"nickname,omitempty" db:"nickname"` Nickname string `json:"nickname,omitempty" db:"nickname"`
Description string `json:"description,omitempty" db:"description"` Description string `json:"description,omitempty" db:"description"`
Server string `json:"server,omitempty" db:"server"` Server string `json:"server,omitempty" db:"server"`
MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"` MonitorServer string `json:"monitorServer,omitempty" db:"monitor_server"`
Username string `json:"username,omitempty" db:"username"` Username string `json:"username,omitempty" db:"username"`
Password string `json:"password,omitempty" db:"password"` Password string `json:"password,omitempty" db:"password"`
Token string `json:"token,omitempty" db:"token"` Token string `json:"token,omitempty" db:"token"`
Ak string `json:"ak,omitempty" db:"ak"` Ak string `json:"ak,omitempty" db:"ak"`
Sk string `json:"sk,omitempty" db:"sk"` Sk string `json:"sk,omitempty" db:"sk"`
Region string `json:"region,omitempty" db:"region"` Region string `json:"region,omitempty" db:"region"`
ProjectId string `json:"projectId,omitempty" db:"project_id"` ProjectId string `json:"projectId,omitempty" db:"project_id"`
Version string `json:"version,omitempty" db:"version"` Version string `json:"version,omitempty" db:"version"`
Label string `json:"label,omitempty" db:"label"` Label string `json:"label,omitempty" db:"label"`
OwnerId string `json:"ownerId,omitempty" db:"owner_id"` OwnerId string `json:"ownerId,omitempty" db:"owner_id"`
AuthType string `json:"authType,omitempty" db:"auth_type"` AuthType string `json:"authType,omitempty" db:"auth_type"`
CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"` CreateTime string `json:"createTime,omitempty" db:"created_time" gorm:"autoCreateTime"`
} }
type ClusterResp { type ClusterResp {
@ -786,7 +800,7 @@ type clusterSumReq {
} }
type clusterSumReqResp{ type clusterSumReqResp {
ClusterSum int `json:"ClusterSum,omitempty"` ClusterSum int `json:"ClusterSum,omitempty"`
AdapterSum int `json:"AdapterSum,omitempty"` AdapterSum int `json:"AdapterSum,omitempty"`
TaskSum int `json:"TaskSum,omitempty"` TaskSum int `json:"TaskSum,omitempty"`
@ -800,23 +814,23 @@ type ClusterRelationInfo {
Version string `json:"version,omitempty" db:"version"` Version string `json:"version,omitempty" db:"version"`
Server string `json:"server,omitempty" db:"server"` Server string `json:"server,omitempty" db:"server"`
CreateTime string `json:"createTime,omitempty" db:"create_time" gorm:"autoCreateTime"` CreateTime string `json:"createTime,omitempty" db:"create_time" gorm:"autoCreateTime"`
CId string `json:"cId,omitempty" db:"id"` CId string `json:"cId,omitempty" db:"id"`
CAdapterId string `json:"cAdapterId,omitempty" db:"adapter_id"` CAdapterId string `json:"cAdapterId,omitempty" db:"adapter_id"`
CName string `json:"cName,omitempty" db:"name"` CName string `json:"cName,omitempty" db:"name"`
CNickname string `json:"cNickname,omitempty" db:"nickname"` CNickname string `json:"cNickname,omitempty" db:"nickname"`
CDescription string `json:"cDescription,omitempty" db:"description"` CDescription string `json:"cDescription,omitempty" db:"description"`
CServer string `json:"cServer,omitempty" db:"server"` CServer string `json:"cServer,omitempty" db:"server"`
CMonitorServer string `json:"cMonitorServer,omitempty" db:"monitor_server"` CMonitorServer string `json:"cMonitorServer,omitempty" db:"monitor_server"`
CUsername string `json:"cUsername,omitempty" db:"username"` CUsername string `json:"cUsername,omitempty" db:"username"`
CPassword string `json:"cPassword,omitempty" db:"password"` CPassword string `json:"cPassword,omitempty" db:"password"`
CToken string `json:"cToken,omitempty" db:"token"` CToken string `json:"cToken,omitempty" db:"token"`
CAk string `json:"cAk,omitempty" db:"ak"` CAk string `json:"cAk,omitempty" db:"ak"`
CSk string `json:"cSk,omitempty" db:"sk"` CSk string `json:"cSk,omitempty" db:"sk"`
CRegion string `json:"cRegion,omitempty" db:"region"` CRegion string `json:"cRegion,omitempty" db:"region"`
CProjectId string `json:"cProjectId,omitempty" db:"project_id"` CProjectId string `json:"cProjectId,omitempty" db:"project_id"`
CVersion string `json:"cVersion,omitempty" db:"version"` CVersion string `json:"cVersion,omitempty" db:"version"`
CLabel string `json:"cLabel,omitempty" db:"label"` CLabel string `json:"cLabel,omitempty" db:"label"`
COwnerId string `json:"cOwnerId,omitempty" db:"owner_id"` COwnerId string `json:"cOwnerId,omitempty" db:"owner_id"`
CAuthType string `json:"cAuthType,omitempty" db:"auth_type"` CAuthType string `json:"cAuthType,omitempty" db:"auth_type"`
CCreateTime string `json:"cCreateTime,omitempty" db:"created_time" gorm:"autoCreateTime"` CCreateTime string `json:"cCreateTime,omitempty" db:"created_time" gorm:"autoCreateTime"`
} }

View File

@ -112,6 +112,14 @@ service pcm {
@doc "Center Resources top3" @doc "Center Resources top3"
@handler centerResourcesHandler @handler centerResourcesHandler
get /core/centerResources returns (centerResourcesResp) get /core/centerResources returns (centerResourcesResp)
@doc "Synchronize Cluster Load Information"
@handler syncClusterLoadHandler
post /core/syncClusterLoad (syncClusterLoadReq)
@doc "metrics"
@handler metricsHandler
get /core/metrics
} }
//hpc二级接口 //hpc二级接口

View File

@ -0,0 +1,12 @@
package core
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
)
func MetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return promhttp.Handler().ServeHTTP
}

View File

@ -0,0 +1,25 @@
package core
import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/core"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
)
func SyncClusterLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SyncClusterLoadReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := core.NewSyncClusterLoadLogic(r.Context(), svcCtx)
err := l.SyncClusterLoad(&req)
result.HttpResult(r, w, nil, err)
}
}

View File

@ -133,6 +133,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/core/centerResources", Path: "/core/centerResources",
Handler: core.CenterResourcesHandler(serverCtx), Handler: core.CenterResourcesHandler(serverCtx),
}, },
{
Method: http.MethodPost,
Path: "/core/syncClusterLoad",
Handler: core.SyncClusterLoadHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/core/metrics",
Handler: core.MetricsHandler(serverCtx),
},
}, },
rest.WithPrefix("/pcm/v1"), rest.WithPrefix("/pcm/v1"),
) )

View File

@ -0,0 +1,28 @@
package core
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
)
type MetricsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMetricsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MetricsLogic {
return &MetricsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MetricsLogic) Metrics() error {
// todo: add your logic here and delete this line
return nil
}

View File

@ -0,0 +1,35 @@
package core
import (
"context"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
"github.com/zeromicro/go-zero/core/logx"
)
type SyncClusterLoadLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSyncClusterLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncClusterLoadLogic {
return &SyncClusterLoadLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error {
if len(req.ClusterLoadRecords) != 0 {
for _, record := range req.ClusterLoadRecords {
tracker.ClusterCpuGauge.WithLabelValues(record.ClusterName).Set(record.CpuUsage)
tracker.ClusterMemoryGauge.WithLabelValues(record.ClusterName).Set(record.MemoryUsage)
tracker.ClusterDiskGauge.WithLabelValues(record.ClusterName).Set(record.DiskUsage)
}
}
return nil
}

View File

@ -150,6 +150,17 @@ type CenterIndex struct {
CenterType string `json:"centerType"` CenterType string `json:"centerType"`
} }
type SyncClusterLoadReq struct {
ClusterLoadRecords []ClusterLoadRecord `json:"clusterLoadRecords"`
}
type ClusterLoadRecord struct {
ClusterName string `json:"clusterName"`
CpuUsage float64 `json:"cpuUsage"`
MemoryUsage float64 `json:"memoryUsage"`
DiskUsage float64 `json:"diskUsage"`
}
type GetClusterListReq struct { type GetClusterListReq struct {
Id int64 `form:"id"` Id int64 `form:"id"`
} }

View File

@ -254,6 +254,7 @@ type ControllerOption struct {
Namespace string Namespace string
Kind string Kind string
WorkloadName string WorkloadName string
PodsName string
Level string Level string
} }

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"github.com/prometheus/client_golang/api" "github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1" v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"strconv" "strconv"
"strings" "strings"
@ -25,6 +26,30 @@ import (
"time" "time"
) )
var (
ClusterCpuGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_cpu_usage",
Help: "Cluster CPU Utilization Rate.",
}, []string{"cluster_name"})
ClusterMemoryGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_memory_usage",
Help: "Cluster Memory Utilization Rate.",
}, []string{"cluster_name"})
ClusterDiskGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "cluster_disk_usage",
Help: "Cluster Disk Utilization Rate.",
}, []string{"cluster_name"})
metrics = []prometheus.Collector{
ClusterCpuGauge,
ClusterMemoryGauge,
ClusterDiskGauge,
}
)
func init() {
prometheus.MustRegister(metrics...)
}
type Prometheus struct { type Prometheus struct {
prometheus Interface prometheus Interface
client v1.API client v1.API