This commit is contained in:
Zheng Kai
2023-03-30 10:07:57 +08:00
parent 1b107ed035
commit 049277f1f6
19 changed files with 889 additions and 20 deletions

View File

@@ -0,0 +1,26 @@
package metrics
import "github.com/prometheus/client_golang/prometheus"
func newCounter(name, help string) prometheus.Counter {
return prometheus.NewCounter(
prometheus.CounterOpts{
Name: name,
Help: help,
},
)
}
func newSummary(name, help string) prometheus.Summary {
return prometheus.NewSummary(prometheus.SummaryOpts{
Name: name,
Help: help,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
})
}
func newGauge(name, help string) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: help,
})
}

View File

@@ -0,0 +1,37 @@
package metrics
import (
"strconv"
"github.com/prometheus/client_golang/prometheus"
)
var (
reqCount = newCounter(`orca_req_count`, `HTTP 请求次数`)
reqFailCount = newCounter(`orca_req_fail_count`, `无法响应的 HTTP 请求数`)
reqBytes = newCounter(`orca_req_bytes`, `文件总上传量`)
errorCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: `orca_error_code`,
Help: `API 返回报错`,
},
[]string{`code`},
)
)
// ReqFailCount ...
func ReqFailCount() {
reqFailCount.Inc()
}
// ReqBytes ...
func ReqBytes(n int) {
reqCount.Inc()
reqBytes.Add(float64(n))
}
// ErrorCount ...
func ErrorCount(code int32) {
c := strconv.Itoa(int(code))
errorCount.WithLabelValues(c).Inc()
}

View File

@@ -0,0 +1,10 @@
package metrics
import "github.com/prometheus/client_golang/prometheus"
func init() {
prometheus.MustRegister(reqCount)
prometheus.MustRegister(reqFailCount)
prometheus.MustRegister(reqBytes)
prometheus.MustRegister(errorCount)
}