kis-flow/metrics/kis_metrics.go

123 lines
3.6 KiB
Go
Raw Normal View History

2024-03-04 14:53:29 +08:00
package metrics
import (
2024-04-16 15:11:23 +08:00
"net/http"
2024-03-26 14:54:50 +08:00
"github.com/aceld/kis-flow/common"
"github.com/aceld/kis-flow/config"
"github.com/aceld/kis-flow/log"
2024-03-04 14:53:29 +08:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
2024-04-15 17:50:02 +08:00
// KisMetrics kisFlow's Prometheus monitoring metrics
type KisMetrics struct {
// Total data quantity
2024-03-04 14:53:29 +08:00
DataTotal prometheus.Counter
2024-04-15 17:50:02 +08:00
// Total data processed by each Flow
2024-03-18 09:29:05 +08:00
FlowDataTotal *prometheus.GaugeVec
2024-04-15 17:50:02 +08:00
// Flow scheduling counts
2024-03-18 09:29:05 +08:00
FlowScheduleCntsToTal *prometheus.GaugeVec
2024-04-15 17:50:02 +08:00
// Function scheduling counts
2024-03-18 09:29:05 +08:00
FuncScheduleCntsTotal *prometheus.GaugeVec
2024-04-15 17:50:02 +08:00
// Function execution time
2024-03-18 09:29:05 +08:00
FunctionDuration *prometheus.HistogramVec
2024-04-15 17:50:02 +08:00
// Flow execution time
2024-03-18 09:29:05 +08:00
FlowDuration *prometheus.HistogramVec
2024-03-04 14:53:29 +08:00
}
2024-04-15 17:50:02 +08:00
var Metrics *KisMetrics
2024-03-04 14:53:29 +08:00
2024-04-15 17:50:02 +08:00
// RunMetricsService starts the Prometheus monitoring service
2024-03-04 14:53:29 +08:00
func RunMetricsService(serverAddr string) error {
2024-04-15 17:50:02 +08:00
// Register Prometheus monitoring route path
http.Handle(common.MetricsRoute, promhttp.Handler())
2024-03-04 14:53:29 +08:00
2024-04-15 17:50:02 +08:00
// Start HttpServer
err := http.ListenAndServe(serverAddr, nil) // Multiple processes cannot listen on the same port
2024-03-04 14:53:29 +08:00
if err != nil {
2024-04-15 11:17:47 +08:00
log.Logger().ErrorF("RunMetricsService err = %s\n", err)
2024-03-04 14:53:29 +08:00
}
return err
}
func InitMetrics() {
2024-04-15 17:50:02 +08:00
Metrics = new(KisMetrics)
2024-03-04 14:53:29 +08:00
2024-04-15 17:50:02 +08:00
// Initialize DataTotal Counter
2024-03-04 14:53:29 +08:00
Metrics.DataTotal = prometheus.NewCounter(prometheus.CounterOpts{
2024-04-15 17:50:02 +08:00
Name: common.CounterKisflowDataTotalName,
Help: common.CounterKisflowDataTotalHelp,
2024-03-04 14:53:29 +08:00
})
2024-04-15 17:50:02 +08:00
// Initialize FlowDataTotal GaugeVec
2024-03-18 09:29:05 +08:00
Metrics.FlowDataTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
2024-04-15 17:50:02 +08:00
Name: common.GamgeFlowDataTotalName,
Help: common.GamgeFlowDataTotalHelp,
2024-03-18 09:29:05 +08:00
},
2024-04-15 17:50:02 +08:00
// Label names
[]string{common.LabelFlowName},
2024-03-18 09:29:05 +08:00
)
2024-04-15 17:50:02 +08:00
// Initialize FlowScheduleCntsToTal GaugeVec
2024-03-18 09:29:05 +08:00
Metrics.FlowScheduleCntsToTal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
2024-04-15 17:50:02 +08:00
Name: common.GangeFlowScheCntsName,
Help: common.GangeFlowScheCntsHelp,
2024-03-18 09:29:05 +08:00
},
2024-04-15 17:50:02 +08:00
// Label names
[]string{common.LabelFlowName},
2024-03-18 09:29:05 +08:00
)
2024-04-15 17:50:02 +08:00
// Initialize FuncScheduleCntsTotal GaugeVec
2024-03-18 09:29:05 +08:00
Metrics.FuncScheduleCntsTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
2024-04-15 17:50:02 +08:00
Name: common.GangeFuncScheCntsName,
Help: common.GangeFuncScheCntsHelp,
2024-03-18 09:29:05 +08:00
},
2024-04-15 17:50:02 +08:00
// Label names
[]string{common.LabelFunctionName, common.LabelFunctionMode},
2024-03-18 09:29:05 +08:00
)
2024-04-15 17:50:02 +08:00
// Initialize FunctionDuration HistogramVec
2024-03-18 09:29:05 +08:00
Metrics.FunctionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
2024-04-15 17:50:02 +08:00
Name: common.HistogramFunctionDurationName,
Help: common.HistogramFunctionDurationHelp,
Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000}, // Unit: ms, maximum half a minute
2024-03-18 09:29:05 +08:00
},
2024-04-15 17:50:02 +08:00
[]string{common.LabelFunctionName, common.LabelFunctionMode},
2024-03-18 09:29:05 +08:00
)
2024-04-15 17:50:02 +08:00
// Initialize FlowDuration HistogramVec
2024-03-18 09:29:05 +08:00
Metrics.FlowDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
2024-04-15 17:50:02 +08:00
Name: common.HistogramFlowDurationName,
Help: common.HistogramFlowDurationHelp,
Buckets: []float64{0.005, 0.01, 0.03, 0.08, 0.1, 0.5, 1.0, 5.0, 10, 100, 1000, 5000, 30000, 60000}, // Unit: ms, maximum 1 minute
2024-03-18 09:29:05 +08:00
},
2024-04-15 17:50:02 +08:00
[]string{common.LabelFlowName},
2024-03-18 09:29:05 +08:00
)
2024-04-15 17:50:02 +08:00
// Register Metrics
2024-03-04 14:53:29 +08:00
prometheus.MustRegister(Metrics.DataTotal)
2024-03-18 09:29:05 +08:00
prometheus.MustRegister(Metrics.FlowDataTotal)
prometheus.MustRegister(Metrics.FlowScheduleCntsToTal)
prometheus.MustRegister(Metrics.FuncScheduleCntsTotal)
prometheus.MustRegister(Metrics.FunctionDuration)
prometheus.MustRegister(Metrics.FlowDuration)
2024-03-04 14:53:29 +08:00
}
2024-04-15 17:50:02 +08:00
// RunMetrics starts the Prometheus metrics service
2024-03-04 14:53:29 +08:00
func RunMetrics() {
2024-04-15 17:50:02 +08:00
// Initialize Prometheus metrics
2024-03-04 14:53:29 +08:00
InitMetrics()
if config.GlobalConfig.EnableProm == true && config.GlobalConfig.PrometheusListen == true {
2024-04-15 17:50:02 +08:00
// Start Prometheus metrics service
2024-03-04 14:53:29 +08:00
go RunMetricsService(config.GlobalConfig.PrometheusServe)
}
}