Add Prometheus monitoring function (#1337)
* Code adaptation k8s: service discovery and registration adaptation, configuration adaptation * Initial submission of the help charts script for openim API * change the help charts script * change the help charts script * change helm chart codes * change dockerfiles script * change chart script:add configmap mounts * change chart script:change repository * change chart script:msggateway add one service * change config.yaml * roll back some config values * change chart script:change Ingress rule with a rewrite annotation * add mysql charts scrible * change chart script:add mysql.config.yaml * add nfs provisioner charts * change chart script:add nfs.config.yaml * add ingress-nginx charts * change chart script:add ingress-nginx.config.yaml * add redis &mongodb charts * add kafka&minio charts * change chart script:change redis.values.yaml * change chart script:add redis.config.yaml * change chart script:change redis.config.yaml * change chart script:change mongodb.value.yaml * change chart script:change mongodb.value.yaml * change chart script:add mongodb.config.yaml * change chart script:change minio.values.yaml * change chart script:add minio.config.yaml * change chart script:change kafka.values.yaml * change chart script:add kafka.config.yaml * change chart script:change services.config.yaml * bug fix:Delete websocket's Port restrictions * bug fix:change port value * change chart script:Submit a stable version script * fix bug:Implement option interface * fix bug:change K8sDR.Register * change config.yaml * change chats script:minio service add ingress * change chats script:minio service add ingress * change chats script:kafka.replicaCount=3& change minio.api ingress * delete change chats script * change config.yaml * change openim.yaml * merge go.sum * Add monitoring function and struct for Prometheus on gin and GRPC * Add GRPC and gin server monitoring logic * Add GRPC and gin server monitoring logic2 * Add GRPC and gin server monitoring logic3 * Add GRPC and gin server monitoring logic4 * Add GRPC and gin server monitoring logic5 * Add GRPC and gin server monitoring logic6 * Add GRPC and gin server monitoring logic7 * delete:old monitoring code * add for test * fix bug:change packname * fix bug:delete getPromPort funciton * fix bug:delete getPromPort funciton * fix bug:change logs * fix bug:change registerName logic in GetGrpcCusMetrics function * add getPrometheus url api * fix:config path logic * fix:prometheus enable function * fix:prometheus enable function * fix:transfer Multi process monitoring logic * del:del not using manifest * fix:openim-msgtransfer.sh * fix:openim-msgtransfer.sh --------- Co-authored-by: lin.huang <lin.huang@apulis.com> Co-authored-by: Xinwei Xiong <3293172751@qq.com>pull/1353/head
parent
297a8db788
commit
82a8f3351f
@ -0,0 +1,417 @@
|
|||||||
|
package ginPrometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultMetricPath = "/metrics"
|
||||||
|
|
||||||
|
// counter, counter_vec, gauge, gauge_vec,
|
||||||
|
// histogram, histogram_vec, summary, summary_vec
|
||||||
|
var reqCnt = &Metric{
|
||||||
|
ID: "reqCnt",
|
||||||
|
Name: "requests_total",
|
||||||
|
Description: "How many HTTP requests processed, partitioned by status code and HTTP method.",
|
||||||
|
Type: "counter_vec",
|
||||||
|
Args: []string{"code", "method", "handler", "host", "url"}}
|
||||||
|
|
||||||
|
var reqDur = &Metric{
|
||||||
|
ID: "reqDur",
|
||||||
|
Name: "request_duration_seconds",
|
||||||
|
Description: "The HTTP request latencies in seconds.",
|
||||||
|
Type: "histogram_vec",
|
||||||
|
Args: []string{"code", "method", "url"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var resSz = &Metric{
|
||||||
|
ID: "resSz",
|
||||||
|
Name: "response_size_bytes",
|
||||||
|
Description: "The HTTP response sizes in bytes.",
|
||||||
|
Type: "summary"}
|
||||||
|
|
||||||
|
var reqSz = &Metric{
|
||||||
|
ID: "reqSz",
|
||||||
|
Name: "request_size_bytes",
|
||||||
|
Description: "The HTTP request sizes in bytes.",
|
||||||
|
Type: "summary"}
|
||||||
|
|
||||||
|
var standardMetrics = []*Metric{
|
||||||
|
reqCnt,
|
||||||
|
reqDur,
|
||||||
|
resSz,
|
||||||
|
reqSz,
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
RequestCounterURLLabelMappingFn is a function which can be supplied to the middleware to control
|
||||||
|
the cardinality of the request counter's "url" label, which might be required in some contexts.
|
||||||
|
For instance, if for a "/customer/:name" route you don't want to generate a time series for every
|
||||||
|
possible customer name, you could use this function:
|
||||||
|
|
||||||
|
func(c *gin.Context) string {
|
||||||
|
url := c.Request.URL.Path
|
||||||
|
for _, p := range c.Params {
|
||||||
|
if p.Key == "name" {
|
||||||
|
url = strings.Replace(url, p.Value, ":name", 1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".
|
||||||
|
*/
|
||||||
|
type RequestCounterURLLabelMappingFn func(c *gin.Context) string
|
||||||
|
|
||||||
|
// Metric is a definition for the name, description, type, ID, and
|
||||||
|
// prometheus.Collector type (i.e. CounterVec, Summary, etc) of each metric
|
||||||
|
type Metric struct {
|
||||||
|
MetricCollector prometheus.Collector
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Type string
|
||||||
|
Args []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prometheus contains the metrics gathered by the instance and its path
|
||||||
|
type Prometheus struct {
|
||||||
|
reqCnt *prometheus.CounterVec
|
||||||
|
reqDur *prometheus.HistogramVec
|
||||||
|
reqSz, resSz prometheus.Summary
|
||||||
|
router *gin.Engine
|
||||||
|
listenAddress string
|
||||||
|
Ppg PrometheusPushGateway
|
||||||
|
|
||||||
|
MetricsList []*Metric
|
||||||
|
MetricsPath string
|
||||||
|
|
||||||
|
ReqCntURLLabelMappingFn RequestCounterURLLabelMappingFn
|
||||||
|
|
||||||
|
// gin.Context string to use as a prometheus URL label
|
||||||
|
URLLabelFromContext string
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional)
|
||||||
|
type PrometheusPushGateway struct {
|
||||||
|
|
||||||
|
// Push interval in seconds
|
||||||
|
PushIntervalSeconds time.Duration
|
||||||
|
|
||||||
|
// Push Gateway URL in format http://domain:port
|
||||||
|
// where JOBNAME can be any string of your choice
|
||||||
|
PushGatewayURL string
|
||||||
|
|
||||||
|
// Local metrics URL where metrics are fetched from, this could be ommited in the future
|
||||||
|
// if implemented using prometheus common/expfmt instead
|
||||||
|
MetricsURL string
|
||||||
|
|
||||||
|
// pushgateway job name, defaults to "gin"
|
||||||
|
Job string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPrometheus generates a new set of metrics with a certain subsystem name
|
||||||
|
func NewPrometheus(subsystem string, customMetricsList ...[]*Metric) *Prometheus {
|
||||||
|
subsystem = "app"
|
||||||
|
|
||||||
|
var metricsList []*Metric
|
||||||
|
|
||||||
|
if len(customMetricsList) > 1 {
|
||||||
|
panic("Too many args. NewPrometheus( string, <optional []*Metric> ).")
|
||||||
|
} else if len(customMetricsList) == 1 {
|
||||||
|
metricsList = customMetricsList[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, metric := range standardMetrics {
|
||||||
|
metricsList = append(metricsList, metric)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &Prometheus{
|
||||||
|
MetricsList: metricsList,
|
||||||
|
MetricsPath: defaultMetricPath,
|
||||||
|
ReqCntURLLabelMappingFn: func(c *gin.Context) string {
|
||||||
|
return c.Request.URL.Path
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p.registerMetrics(subsystem)
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPushGateway sends metrics to a remote pushgateway exposed on pushGatewayURL
|
||||||
|
// every pushIntervalSeconds. Metrics are fetched from metricsURL
|
||||||
|
func (p *Prometheus) SetPushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration) {
|
||||||
|
p.Ppg.PushGatewayURL = pushGatewayURL
|
||||||
|
p.Ppg.MetricsURL = metricsURL
|
||||||
|
p.Ppg.PushIntervalSeconds = pushIntervalSeconds
|
||||||
|
p.startPushTicker()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPushGatewayJob job name, defaults to "gin"
|
||||||
|
func (p *Prometheus) SetPushGatewayJob(j string) {
|
||||||
|
p.Ppg.Job = j
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetListenAddress for exposing metrics on address. If not set, it will be exposed at the
|
||||||
|
// same address of the gin engine that is being used
|
||||||
|
func (p *Prometheus) SetListenAddress(address string) {
|
||||||
|
p.listenAddress = address
|
||||||
|
if p.listenAddress != "" {
|
||||||
|
p.router = gin.Default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetListenAddressWithRouter for using a separate router to expose metrics. (this keeps things like GET /metrics out of
|
||||||
|
// your content's access log).
|
||||||
|
func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *gin.Engine) {
|
||||||
|
p.listenAddress = listenAddress
|
||||||
|
if len(p.listenAddress) > 0 {
|
||||||
|
p.router = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMetricsPath set metrics paths
|
||||||
|
func (p *Prometheus) SetMetricsPath(e *gin.Engine) {
|
||||||
|
|
||||||
|
if p.listenAddress != "" {
|
||||||
|
p.router.GET(p.MetricsPath, prometheusHandler())
|
||||||
|
p.runServer()
|
||||||
|
} else {
|
||||||
|
e.GET(p.MetricsPath, prometheusHandler())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMetricsPathWithAuth set metrics paths with authentication
|
||||||
|
func (p *Prometheus) SetMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts) {
|
||||||
|
|
||||||
|
if p.listenAddress != "" {
|
||||||
|
p.router.GET(p.MetricsPath, gin.BasicAuth(accounts), prometheusHandler())
|
||||||
|
p.runServer()
|
||||||
|
} else {
|
||||||
|
e.GET(p.MetricsPath, gin.BasicAuth(accounts), prometheusHandler())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) runServer() {
|
||||||
|
if p.listenAddress != "" {
|
||||||
|
go p.router.Run(p.listenAddress)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) getMetrics() []byte {
|
||||||
|
response, _ := http.Get(p.Ppg.MetricsURL)
|
||||||
|
|
||||||
|
defer response.Body.Close()
|
||||||
|
body, _ := ioutil.ReadAll(response.Body)
|
||||||
|
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) getPushGatewayURL() string {
|
||||||
|
h, _ := os.Hostname()
|
||||||
|
if p.Ppg.Job == "" {
|
||||||
|
p.Ppg.Job = "gin"
|
||||||
|
}
|
||||||
|
return p.Ppg.PushGatewayURL + "/metrics/job/" + p.Ppg.Job + "/instance/" + h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) sendMetricsToPushGateway(metrics []byte) {
|
||||||
|
req, err := http.NewRequest("POST", p.getPushGatewayURL(), bytes.NewBuffer(metrics))
|
||||||
|
client := &http.Client{}
|
||||||
|
if _, err = client.Do(req); err != nil {
|
||||||
|
fmt.Println("Error sending to push gateway error:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) startPushTicker() {
|
||||||
|
ticker := time.NewTicker(time.Second * p.Ppg.PushIntervalSeconds)
|
||||||
|
go func() {
|
||||||
|
for range ticker.C {
|
||||||
|
p.sendMetricsToPushGateway(p.getMetrics())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMetric associates prometheus.Collector based on Metric.Type
|
||||||
|
func NewMetric(m *Metric, subsystem string) prometheus.Collector {
|
||||||
|
var metric prometheus.Collector
|
||||||
|
switch m.Type {
|
||||||
|
case "counter_vec":
|
||||||
|
metric = prometheus.NewCounterVec(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
m.Args,
|
||||||
|
)
|
||||||
|
case "counter":
|
||||||
|
metric = prometheus.NewCounter(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
case "gauge_vec":
|
||||||
|
metric = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
m.Args,
|
||||||
|
)
|
||||||
|
case "gauge":
|
||||||
|
metric = prometheus.NewGauge(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
case "histogram_vec":
|
||||||
|
metric = prometheus.NewHistogramVec(
|
||||||
|
prometheus.HistogramOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
m.Args,
|
||||||
|
)
|
||||||
|
case "histogram":
|
||||||
|
metric = prometheus.NewHistogram(
|
||||||
|
prometheus.HistogramOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
case "summary_vec":
|
||||||
|
metric = prometheus.NewSummaryVec(
|
||||||
|
prometheus.SummaryOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
m.Args,
|
||||||
|
)
|
||||||
|
case "summary":
|
||||||
|
metric = prometheus.NewSummary(
|
||||||
|
prometheus.SummaryOpts{
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: m.Name,
|
||||||
|
Help: m.Description,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return metric
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Prometheus) registerMetrics(subsystem string) {
|
||||||
|
|
||||||
|
for _, metricDef := range p.MetricsList {
|
||||||
|
metric := NewMetric(metricDef, subsystem)
|
||||||
|
if err := prometheus.Register(metric); err != nil {
|
||||||
|
fmt.Println("could not be registered in Prometheus,metricDef.Name:", metricDef.Name, " error:", err.Error())
|
||||||
|
}
|
||||||
|
switch metricDef {
|
||||||
|
case reqCnt:
|
||||||
|
p.reqCnt = metric.(*prometheus.CounterVec)
|
||||||
|
case reqDur:
|
||||||
|
p.reqDur = metric.(*prometheus.HistogramVec)
|
||||||
|
case resSz:
|
||||||
|
p.resSz = metric.(prometheus.Summary)
|
||||||
|
case reqSz:
|
||||||
|
p.reqSz = metric.(prometheus.Summary)
|
||||||
|
}
|
||||||
|
metricDef.MetricCollector = metric
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use adds the middleware to a gin engine.
|
||||||
|
func (p *Prometheus) Use(e *gin.Engine) {
|
||||||
|
e.Use(p.HandlerFunc())
|
||||||
|
p.SetMetricsPath(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseWithAuth adds the middleware to a gin engine with BasicAuth.
|
||||||
|
func (p *Prometheus) UseWithAuth(e *gin.Engine, accounts gin.Accounts) {
|
||||||
|
e.Use(p.HandlerFunc())
|
||||||
|
p.SetMetricsPathWithAuth(e, accounts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandlerFunc defines handler function for middleware
|
||||||
|
func (p *Prometheus) HandlerFunc() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if c.Request.URL.Path == p.MetricsPath {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
reqSz := computeApproximateRequestSize(c.Request)
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
status := strconv.Itoa(c.Writer.Status())
|
||||||
|
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||||
|
resSz := float64(c.Writer.Size())
|
||||||
|
|
||||||
|
url := p.ReqCntURLLabelMappingFn(c)
|
||||||
|
if len(p.URLLabelFromContext) > 0 {
|
||||||
|
u, found := c.Get(p.URLLabelFromContext)
|
||||||
|
if !found {
|
||||||
|
u = "unknown"
|
||||||
|
}
|
||||||
|
url = u.(string)
|
||||||
|
}
|
||||||
|
p.reqDur.WithLabelValues(status, c.Request.Method, url).Observe(elapsed)
|
||||||
|
p.reqCnt.WithLabelValues(status, c.Request.Method, c.HandlerName(), c.Request.Host, url).Inc()
|
||||||
|
p.reqSz.Observe(float64(reqSz))
|
||||||
|
p.resSz.Observe(resSz)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prometheusHandler() gin.HandlerFunc {
|
||||||
|
h := promhttp.Handler()
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
h.ServeHTTP(c.Writer, c.Request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func computeApproximateRequestSize(r *http.Request) int {
|
||||||
|
s := 0
|
||||||
|
if r.URL != nil {
|
||||||
|
s = len(r.URL.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
s += len(r.Method)
|
||||||
|
s += len(r.Proto)
|
||||||
|
for name, values := range r.Header {
|
||||||
|
s += len(name)
|
||||||
|
for _, value := range values {
|
||||||
|
s += len(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s += len(r.Host)
|
||||||
|
|
||||||
|
// r.Form and r.MultipartForm are assumed to be included in r.URL.
|
||||||
|
|
||||||
|
if r.ContentLength != -1 {
|
||||||
|
s += int(r.ContentLength)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||||
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/ginPrometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewGrpcPromObj(cusMetrics []prometheus.Collector) (*prometheus.Registry, *grpc_prometheus.ServerMetrics, error) {
|
||||||
|
////////////////////////////////////////////////////////
|
||||||
|
reg := prometheus.NewRegistry()
|
||||||
|
grpcMetrics := grpc_prometheus.NewServerMetrics()
|
||||||
|
grpcMetrics.EnableHandlingTimeHistogram()
|
||||||
|
cusMetrics = append(cusMetrics, grpcMetrics, collectors.NewGoCollector())
|
||||||
|
reg.MustRegister(cusMetrics...)
|
||||||
|
return reg, grpcMetrics, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGrpcCusMetrics(registerName string) []prometheus.Collector {
|
||||||
|
switch registerName {
|
||||||
|
case config2.Config.RpcRegisterName.OpenImMessageGatewayName:
|
||||||
|
return []prometheus.Collector{OnlineUserGauge}
|
||||||
|
case config2.Config.RpcRegisterName.OpenImMsgName:
|
||||||
|
return []prometheus.Collector{SingleChatMsgProcessSuccessCounter, SingleChatMsgProcessFailedCounter, GroupChatMsgProcessSuccessCounter, GroupChatMsgProcessFailedCounter}
|
||||||
|
case "Transfer":
|
||||||
|
return []prometheus.Collector{MsgInsertRedisSuccessCounter, MsgInsertRedisFailedCounter, MsgInsertMongoSuccessCounter, MsgInsertMongoFailedCounter, SeqSetFailedCounter}
|
||||||
|
case config2.Config.RpcRegisterName.OpenImPushName:
|
||||||
|
return []prometheus.Collector{MsgOfflinePushFailedCounter}
|
||||||
|
case config2.Config.RpcRegisterName.OpenImAuthName:
|
||||||
|
return []prometheus.Collector{UserLoginCounter}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGinCusMetrics(name string) []*ginPrometheus.Metric {
|
||||||
|
switch name {
|
||||||
|
case "Api":
|
||||||
|
return []*ginPrometheus.Metric{ApiCustomCnt}
|
||||||
|
default:
|
||||||
|
return []*ginPrometheus.Metric{ApiCustomCnt}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import ginProm "github.com/openimsdk/open-im-server/v3/pkg/common/ginPrometheus"
|
||||||
|
|
||||||
|
/*
|
||||||
|
labels := prometheus.Labels{"label_one": "any", "label_two": "value"}
|
||||||
|
ApiCustomCnt.MetricCollector.(*prometheus.CounterVec).With(labels).Inc()
|
||||||
|
*/
|
||||||
|
var (
|
||||||
|
ApiCustomCnt = &ginProm.Metric{
|
||||||
|
Name: "custom_total",
|
||||||
|
Description: "Custom counter events.",
|
||||||
|
Type: "counter_vec",
|
||||||
|
Args: []string{"label_one", "label_two"},
|
||||||
|
}
|
||||||
|
)
|
@ -0,0 +1,12 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
UserLoginCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "user_login_total",
|
||||||
|
Help: "The number of user login",
|
||||||
|
})
|
||||||
|
)
|
@ -0,0 +1,24 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
SingleChatMsgProcessSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "single_chat_msg_process_success_total",
|
||||||
|
Help: "The number of single chat msg successful processed",
|
||||||
|
})
|
||||||
|
SingleChatMsgProcessFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "single_chat_msg_process_failed_total",
|
||||||
|
Help: "The number of single chat msg failed processed",
|
||||||
|
})
|
||||||
|
GroupChatMsgProcessSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "group_chat_msg_process_success_total",
|
||||||
|
Help: "The number of group chat msg successful processed",
|
||||||
|
})
|
||||||
|
GroupChatMsgProcessFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "group_chat_msg_process_failed_total",
|
||||||
|
Help: "The number of group chat msg failed processed",
|
||||||
|
})
|
||||||
|
)
|
@ -0,0 +1,12 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
OnlineUserGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Name: "online_user_num",
|
||||||
|
Help: "The number of online user num",
|
||||||
|
})
|
||||||
|
)
|
@ -0,0 +1,12 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
MsgOfflinePushFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "msg_offline_push_failed_total",
|
||||||
|
Help: "The number of msg failed offline pushed",
|
||||||
|
})
|
||||||
|
)
|
@ -0,0 +1,28 @@
|
|||||||
|
package prom_metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
MsgInsertRedisSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "msg_insert_redis_success_total",
|
||||||
|
Help: "The number of successful insert msg to redis",
|
||||||
|
})
|
||||||
|
MsgInsertRedisFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "msg_insert_redis_failed_total",
|
||||||
|
Help: "The number of failed insert msg to redis",
|
||||||
|
})
|
||||||
|
MsgInsertMongoSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "msg_insert_mongo_success_total",
|
||||||
|
Help: "The number of successful insert msg to mongo",
|
||||||
|
})
|
||||||
|
MsgInsertMongoFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "msg_insert_mongo_failed_total",
|
||||||
|
Help: "The number of failed insert msg to mongo",
|
||||||
|
})
|
||||||
|
SeqSetFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "seq_set_failed_total",
|
||||||
|
Help: "The number of failed set seq",
|
||||||
|
})
|
||||||
|
)
|
@ -1,15 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. 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 prome // import "github.com/openimsdk/open-im-server/v3/pkg/common/prome"
|
|
@ -1,470 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. 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 prome
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// auth rpc.
|
|
||||||
UserLoginCounter prometheus.Counter
|
|
||||||
UserRegisterCounter prometheus.Counter
|
|
||||||
|
|
||||||
// seg.
|
|
||||||
SeqGetSuccessCounter prometheus.Counter
|
|
||||||
SeqGetFailedCounter prometheus.Counter
|
|
||||||
SeqSetSuccessCounter prometheus.Counter
|
|
||||||
SeqSetFailedCounter prometheus.Counter
|
|
||||||
|
|
||||||
// msg-db.
|
|
||||||
MsgInsertRedisSuccessCounter prometheus.Counter
|
|
||||||
MsgInsertRedisFailedCounter prometheus.Counter
|
|
||||||
MsgInsertMongoSuccessCounter prometheus.Counter
|
|
||||||
MsgInsertMongoFailedCounter prometheus.Counter
|
|
||||||
MsgPullFromRedisSuccessCounter prometheus.Counter
|
|
||||||
MsgPullFromRedisFailedCounter prometheus.Counter
|
|
||||||
MsgPullFromMongoSuccessCounter prometheus.Counter
|
|
||||||
MsgPullFromMongoFailedCounter prometheus.Counter
|
|
||||||
|
|
||||||
// msg-ws.
|
|
||||||
MsgRecvTotalCounter prometheus.Counter
|
|
||||||
GetNewestSeqTotalCounter prometheus.Counter
|
|
||||||
PullMsgBySeqListTotalCounter prometheus.Counter
|
|
||||||
|
|
||||||
SingleChatMsgRecvSuccessCounter prometheus.Counter
|
|
||||||
GroupChatMsgRecvSuccessCounter prometheus.Counter
|
|
||||||
WorkSuperGroupChatMsgRecvSuccessCounter prometheus.Counter
|
|
||||||
OnlineUserGauge prometheus.Gauge
|
|
||||||
|
|
||||||
// msg-msg.
|
|
||||||
SingleChatMsgProcessSuccessCounter prometheus.Counter
|
|
||||||
SingleChatMsgProcessFailedCounter prometheus.Counter
|
|
||||||
GroupChatMsgProcessSuccessCounter prometheus.Counter
|
|
||||||
GroupChatMsgProcessFailedCounter prometheus.Counter
|
|
||||||
WorkSuperGroupChatMsgProcessSuccessCounter prometheus.Counter
|
|
||||||
WorkSuperGroupChatMsgProcessFailedCounter prometheus.Counter
|
|
||||||
|
|
||||||
// msg-push.
|
|
||||||
MsgOnlinePushSuccessCounter prometheus.Counter
|
|
||||||
MsgOfflinePushSuccessCounter prometheus.Counter
|
|
||||||
MsgOfflinePushFailedCounter prometheus.Counter
|
|
||||||
// api.
|
|
||||||
ApiRequestCounter prometheus.Counter
|
|
||||||
ApiRequestSuccessCounter prometheus.Counter
|
|
||||||
ApiRequestFailedCounter prometheus.Counter
|
|
||||||
|
|
||||||
// grpc.
|
|
||||||
GrpcRequestCounter prometheus.Counter
|
|
||||||
GrpcRequestSuccessCounter prometheus.Counter
|
|
||||||
GrpcRequestFailedCounter prometheus.Counter
|
|
||||||
|
|
||||||
SendMsgCounter prometheus.Counter
|
|
||||||
|
|
||||||
// conversation.
|
|
||||||
ConversationCreateSuccessCounter prometheus.Counter
|
|
||||||
ConversationCreateFailedCounter prometheus.Counter
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewUserLoginCounter() {
|
|
||||||
if UserLoginCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
UserLoginCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "user_login",
|
|
||||||
Help: "The number of user login",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUserRegisterCounter() {
|
|
||||||
if UserRegisterCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
UserRegisterCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "user_register",
|
|
||||||
Help: "The number of user register",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSeqGetSuccessCounter() {
|
|
||||||
if SeqGetSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SeqGetSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "seq_get_success",
|
|
||||||
Help: "The number of successful get seq",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSeqGetFailedCounter() {
|
|
||||||
if SeqGetFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SeqGetFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "seq_get_failed",
|
|
||||||
Help: "The number of failed get seq",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSeqSetSuccessCounter() {
|
|
||||||
if SeqSetSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SeqSetSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "seq_set_success",
|
|
||||||
Help: "The number of successful set seq",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSeqSetFailedCounter() {
|
|
||||||
if SeqSetFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SeqSetFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "seq_set_failed",
|
|
||||||
Help: "The number of failed set seq",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApiRequestCounter() {
|
|
||||||
if ApiRequestCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ApiRequestCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "api_request",
|
|
||||||
Help: "The number of api request",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApiRequestSuccessCounter() {
|
|
||||||
if ApiRequestSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ApiRequestSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "api_request_success",
|
|
||||||
Help: "The number of api request success",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApiRequestFailedCounter() {
|
|
||||||
if ApiRequestFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ApiRequestFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "api_request_failed",
|
|
||||||
Help: "The number of api request failed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGrpcRequestCounter() {
|
|
||||||
if GrpcRequestCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GrpcRequestCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "grpc_request",
|
|
||||||
Help: "The number of api request",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGrpcRequestSuccessCounter() {
|
|
||||||
if GrpcRequestSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GrpcRequestSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "grpc_request_success",
|
|
||||||
Help: "The number of grpc request success",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGrpcRequestFailedCounter() {
|
|
||||||
if GrpcRequestFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GrpcRequestFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "grpc_request_failed",
|
|
||||||
Help: "The number of grpc request failed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSendMsgCount() {
|
|
||||||
if SendMsgCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SendMsgCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "send_msg",
|
|
||||||
Help: "The number of send msg",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgInsertRedisSuccessCounter() {
|
|
||||||
if MsgInsertRedisSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgInsertRedisSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_insert_redis_success",
|
|
||||||
Help: "The number of successful insert msg to redis",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgInsertRedisFailedCounter() {
|
|
||||||
if MsgInsertRedisFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgInsertRedisFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_insert_redis_failed",
|
|
||||||
Help: "The number of failed insert msg to redis",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgInsertMongoSuccessCounter() {
|
|
||||||
if MsgInsertMongoSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgInsertMongoSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_insert_mongo_success",
|
|
||||||
Help: "The number of successful insert msg to mongo",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgInsertMongoFailedCounter() {
|
|
||||||
if MsgInsertMongoFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgInsertMongoFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_insert_mongo_failed",
|
|
||||||
Help: "The number of failed insert msg to mongo",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgPullFromRedisSuccessCounter() {
|
|
||||||
if MsgPullFromRedisSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgPullFromRedisSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_pull_from_redis_success",
|
|
||||||
Help: "The number of successful pull msg from redis",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgPullFromRedisFailedCounter() {
|
|
||||||
if MsgPullFromRedisFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgPullFromRedisFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_pull_from_redis_failed",
|
|
||||||
Help: "The number of failed pull msg from redis",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgPullFromMongoSuccessCounter() {
|
|
||||||
if MsgPullFromMongoSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgPullFromMongoSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_pull_from_mongo_success",
|
|
||||||
Help: "The number of successful pull msg from mongo",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgPullFromMongoFailedCounter() {
|
|
||||||
if MsgPullFromMongoFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgPullFromMongoFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_pull_from_mongo_failed",
|
|
||||||
Help: "The number of failed pull msg from mongo",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgRecvTotalCounter() {
|
|
||||||
if MsgRecvTotalCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgRecvTotalCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_recv_total",
|
|
||||||
Help: "The number of msg received",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetNewestSeqTotalCounter() {
|
|
||||||
if GetNewestSeqTotalCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GetNewestSeqTotalCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "get_newest_seq_total",
|
|
||||||
Help: "the number of get newest seq",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPullMsgBySeqListTotalCounter() {
|
|
||||||
if PullMsgBySeqListTotalCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
PullMsgBySeqListTotalCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "pull_msg_by_seq_list_total",
|
|
||||||
Help: "The number of pull msg by seq list",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingleChatMsgRecvSuccessCounter() {
|
|
||||||
if SingleChatMsgRecvSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SingleChatMsgRecvSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "single_chat_msg_recv_success",
|
|
||||||
Help: "The number of single chat msg successful received ",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupChatMsgRecvSuccessCounter() {
|
|
||||||
if GroupChatMsgRecvSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GroupChatMsgRecvSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "group_chat_msg_recv_success",
|
|
||||||
Help: "The number of group chat msg successful received",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWorkSuperGroupChatMsgRecvSuccessCounter() {
|
|
||||||
if WorkSuperGroupChatMsgRecvSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
WorkSuperGroupChatMsgRecvSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "work_super_group_chat_msg_recv_success",
|
|
||||||
Help: "The number of work/super group chat msg successful received",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewOnlineUserGauges() {
|
|
||||||
if OnlineUserGauge != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
OnlineUserGauge = promauto.NewGauge(prometheus.GaugeOpts{
|
|
||||||
Name: "online_user_num",
|
|
||||||
Help: "The number of online user num",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingleChatMsgProcessSuccessCounter() {
|
|
||||||
if SingleChatMsgProcessSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SingleChatMsgProcessSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "single_chat_msg_process_success",
|
|
||||||
Help: "The number of single chat msg successful processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingleChatMsgProcessFailedCounter() {
|
|
||||||
if SingleChatMsgProcessFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SingleChatMsgProcessFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "single_chat_msg_process_failed",
|
|
||||||
Help: "The number of single chat msg failed processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupChatMsgProcessSuccessCounter() {
|
|
||||||
if GroupChatMsgProcessSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GroupChatMsgProcessSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "group_chat_msg_process_success",
|
|
||||||
Help: "The number of group chat msg successful processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupChatMsgProcessFailedCounter() {
|
|
||||||
if GroupChatMsgProcessFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
GroupChatMsgProcessFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "group_chat_msg_process_failed",
|
|
||||||
Help: "The number of group chat msg failed processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWorkSuperGroupChatMsgProcessSuccessCounter() {
|
|
||||||
if WorkSuperGroupChatMsgProcessSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
WorkSuperGroupChatMsgProcessSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "work_super_group_chat_msg_process_success",
|
|
||||||
Help: "The number of work/super group chat msg successful processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWorkSuperGroupChatMsgProcessFailedCounter() {
|
|
||||||
if WorkSuperGroupChatMsgProcessFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
WorkSuperGroupChatMsgProcessFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "work_super_group_chat_msg_process_failed",
|
|
||||||
Help: "The number of work/super group chat msg failed processed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgOnlinePushSuccessCounter() {
|
|
||||||
if MsgOnlinePushSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgOnlinePushSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_online_push_success",
|
|
||||||
Help: "The number of msg successful online pushed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgOfflinePushSuccessCounter() {
|
|
||||||
if MsgOfflinePushSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgOfflinePushSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_offline_push_success",
|
|
||||||
Help: "The number of msg successful offline pushed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgOfflinePushFailedCounter() {
|
|
||||||
if MsgOfflinePushFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
MsgOfflinePushFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "msg_offline_push_failed",
|
|
||||||
Help: "The number of msg failed offline pushed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConversationCreateSuccessCounter() {
|
|
||||||
if ConversationCreateSuccessCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ConversationCreateSuccessCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "conversation_push_success",
|
|
||||||
Help: "The number of conversation successful pushed",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConversationCreateFailedCounter() {
|
|
||||||
if ConversationCreateFailedCounter != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ConversationCreateFailedCounter = promauto.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "conversation_push_failed",
|
|
||||||
Help: "The number of conversation failed pushed",
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,97 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. 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 prome
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func StartPrometheusSrv(prometheusPort int) error {
|
|
||||||
if config.Config.Prometheus.Enable {
|
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
|
||||||
err := http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrometheusHandler() gin.HandlerFunc {
|
|
||||||
h := promhttp.Handler()
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
h.ServeHTTP(c.Writer, c.Request)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type responseBodyWriter struct {
|
|
||||||
gin.ResponseWriter
|
|
||||||
body *bytes.Buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
|
||||||
r.body.Write(b)
|
|
||||||
return r.ResponseWriter.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrometheusMiddleware(c *gin.Context) {
|
|
||||||
Inc(ApiRequestCounter)
|
|
||||||
w := &responseBodyWriter{body: &bytes.Buffer{}, ResponseWriter: c.Writer}
|
|
||||||
c.Writer = w
|
|
||||||
c.Next()
|
|
||||||
if c.Writer.Status() == http.StatusOK {
|
|
||||||
Inc(ApiRequestSuccessCounter)
|
|
||||||
} else {
|
|
||||||
Inc(ApiRequestFailedCounter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Inc(counter prometheus.Counter) {
|
|
||||||
if config.Config.Prometheus.Enable {
|
|
||||||
if counter != nil {
|
|
||||||
counter.Inc()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Add(counter prometheus.Counter, add int) {
|
|
||||||
if config.Config.Prometheus.Enable {
|
|
||||||
if counter != nil {
|
|
||||||
counter.Add(float64(add))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GaugeInc(gauges prometheus.Gauge) {
|
|
||||||
if config.Config.Prometheus.Enable {
|
|
||||||
if gauges != nil {
|
|
||||||
gauges.Inc()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GaugeDec(gauges prometheus.Gauge) {
|
|
||||||
if config.Config.Prometheus.Enable {
|
|
||||||
if gauges != nil {
|
|
||||||
gauges.Dec()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue