Providing metrics information to Prometheus
Former-commit-id: 5d84c61c35ecba3aaf7f63f59e6efdf4e2752f99
This commit is contained in:
commit
6dcb3862d1
|
@ -22,7 +22,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"`
|
||||||
|
@ -30,6 +30,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"`
|
||||||
|
@ -620,7 +634,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"`
|
||||||
|
|
|
@ -104,6 +104,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二级接口
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -124,6 +124,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"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -24,6 +24,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"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue