-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
prometheus.go
129 lines (113 loc) · 3.83 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_model/go"
)
type PrometheusInfo struct {
ApiThroughput []GaugeVecInfo `json:"apiThroughput"`
ApiLatency []HistogramVecInfo `json:"apiLatency"`
TotalThroughput float64 `json:"totalThroughput"`
}
type GaugeVecInfo struct {
Method string `json:"method"`
Name string `json:"name"`
Throughput float64 `json:"throughput"`
}
type HistogramVecInfo struct {
Method string `json:"method"`
Name string `json:"name"`
Count uint64 `json:"count"`
Latency string `json:"latency"`
}
var (
ApiThroughput = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "casdoor_api_throughput",
Help: "The throughput of each api access",
}, []string{"path", "method"})
ApiLatency = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "casdoor_api_latency",
Help: "API processing latency in milliseconds",
}, []string{"path", "method"})
CpuUsage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "casdoor_cpu_usage",
Help: "Casdoor cpu usage",
}, []string{"cpuNum"})
MemoryUsage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "casdoor_memory_usage",
Help: "Casdoor memory usage in Byte",
}, []string{"type"})
TotalThroughput = promauto.NewGauge(prometheus.GaugeOpts{
Name: "casdoor_total_throughput",
Help: "The total throughput of casdoor",
})
)
func ClearThroughputPerSecond() {
// Clear the throughput every second
ticker := time.NewTicker(time.Second)
for range ticker.C {
ApiThroughput.Reset()
TotalThroughput.Set(0)
}
}
func GetPrometheusInfo() (*PrometheusInfo, error) {
res := &PrometheusInfo{}
metricFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil {
return nil, err
}
for _, metricFamily := range metricFamilies {
switch metricFamily.GetName() {
case "casdoor_api_throughput":
res.ApiThroughput = getGaugeVecInfo(metricFamily)
case "casdoor_api_latency":
res.ApiLatency = getHistogramVecInfo(metricFamily)
case "casdoor_total_throughput":
res.TotalThroughput = metricFamily.GetMetric()[0].GetGauge().GetValue()
}
}
return res, nil
}
func getHistogramVecInfo(metricFamily *io_prometheus_client.MetricFamily) []HistogramVecInfo {
var histogramVecInfos []HistogramVecInfo
for _, metric := range metricFamily.GetMetric() {
sampleCount := metric.GetHistogram().GetSampleCount()
sampleSum := metric.GetHistogram().GetSampleSum()
latency := sampleSum / float64(sampleCount)
histogramVecInfo := HistogramVecInfo{
Method: metric.Label[0].GetValue(),
Name: metric.Label[1].GetValue(),
Count: sampleCount,
Latency: fmt.Sprintf("%.3f", latency),
}
histogramVecInfos = append(histogramVecInfos, histogramVecInfo)
}
return histogramVecInfos
}
func getGaugeVecInfo(metricFamily *io_prometheus_client.MetricFamily) []GaugeVecInfo {
var counterVecInfos []GaugeVecInfo
for _, metric := range metricFamily.GetMetric() {
counterVecInfo := GaugeVecInfo{
Method: metric.Label[0].GetValue(),
Name: metric.Label[1].GetValue(),
Throughput: metric.Gauge.GetValue(),
}
counterVecInfos = append(counterVecInfos, counterVecInfo)
}
return counterVecInfos
}