mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-11 07:17:00 +08:00
* refactor: clietn in msggateway Signed-off-by: rfyiamcool <rfyiamcool@163.com> * perf: add sync.pool for req object Signed-off-by: rfyiamcool <rfyiamcool@163.com> --------- Signed-off-by: rfyiamcool <rfyiamcool@163.com> feat: use dummy pusher by default (#1349) 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> fix: initiateUpload sign list number (#1358) * optimize scheduled deletion * optimize scheduled deletion * optimize scheduled deletion * optimize scheduled deletion * minio cache * fix: conflicts * feat: minio cache * feat: cache optimize * feat: cache optimize * feat: cache optimize * feat: cache optimize * feat: cache optimize * fix: initiateUpload sign list number fix: msg pull change and fcm redis flag fix. (#1367) * fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: get all userID Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: msggateway add online status call Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: log change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * chore: network mode change Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * feat: add api of get server time Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: remove go work sum Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: pull message add isRead field Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: check msg-transfer script Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: script update Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: start don't kill old process Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: check component Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: pull message set isRead only message come from single. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix: multiple gateway kick user each other. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add ex field to update group info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * refactor: change project module name. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: for pressure test. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: message log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fxi: component check output valid info. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * test: send message test log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * cicd: robot automated Change * cicd: robot automated Change * test: remove info log. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * feat: api of send message add sendTime field. Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> * fix: add callback for update user's info. * cicd: robot automated Change * fix: change callback command name. * cicd: robot automated Change * fix: single chat unread status change. * fix: single chat unread status change. * fix: single chat unread status change. * fix: user status change. * cicd: robot automated Change * fix: user status change. * fix: user status change. * fix: user status change. * cicd: robot automated Change * fix: ws close when user logout. * fix: remove repeat platform on online status. * cicd: robot automated Change * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation . * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * re: remove router of unsubscribeStatus. * fix: reset branch * fix: not support redis cluster. CROSSSLOT Keys in request don't hash to the same slot * fix: update user.FaceURL do not trigger GroupMemberInfoSetNotification * cicd: robot automated Change * fix: api send messages for notification conversation. * fix: api send messages for notification conversation. * fix: zk add close to avoid zk block. * fix: go mod update. * fix: msg pull change and fcm redis flag fix. --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Signed-off-by: Gordon <46924906+FGadvancer@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: withchao <993506633@qq.com> Co-authored-by: Xinwei Xiong <3293172751NSS@gmail.com> Co-authored-by: FGadvancer <FGadvancer@users.noreply.github.com> Co-authored-by: withchao <withchao@users.noreply.github.com> fix: sync close ws conn when kick old user avoid wrong trigger order about online status. (#1368) Update README-zh_CN.md Update README-zh_CN.md fix: GetUserReqApplicationList error when there is a disbanded group chat (#1374) fix: error when querying some information about disbanded group (#1376) * fix: GetUserReqApplicationList error when there is a disbanded group chat * fix: error when querying some information about disbanded group fix: GetUserReqApplicationList dismissed group error (#1378) * fix: GetUserReqApplicationList error when there is a disbanded group chat * fix: error when querying some information about disbanded group * fix: GetUserReqApplicationList dismissed group error refactor: lower the level of code nesting (#1370) * refactor: lower the level of code nesting Signed-off-by: rfyiamcool <rfyiamcool@163.com> * refactor: lower the level of code nesting Signed-off-by: rfyiamcool <rfyiamcool@163.com> --------- Signed-off-by: rfyiamcool <rfyiamcool@163.com> ☀️ feat: Enhancing OpenIM with Integrated E2E Testing and CI/CD Enhancements (#1359) * cicd: robot automated Change * feat: add api test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add api test make file Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add openim e2e test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add openim e2e test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: Fixed some unused scripts and some names Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * docs: optimize openim docs Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add prom address Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add openim info test * feat: add openim images config path Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * fix: fix tim file rename * fix: fix tim file rename * fix: fix tim file rename * fix: fix tim file rename * fix: add openim test e2e * feat: add openim test .keep Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: add openim test .keep Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: openim test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: openim test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> * feat: openim test Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com> Co-authored-by: cubxxw <cubxxw@users.noreply.github.com> perf: auto set runtime maxprocs in docker (#1339) Signed-off-by: rfyiamcool <rfyiamcool@163.com> build: build openim image (#1381) perf: improve gzip performance with sync.pool (#1321) Signed-off-by: rfyiamcool <rfyiamcool@163.com> Co-authored-by: Gordon <46924906+FGadvancer@users.noreply.github.com> fix: add kafka compress type and producer ack params (#1310) Signed-off-by: rfyiamcool <rfyiamcool@163.com> delete not using files cicd: robot automated Change add prometheus docker-compose for monitor fix prometheus.yaml fix environment.sh fix init-config.sh fix init-config.sh fix env_template.yaml fix docker-compose.yml fix docker-compose.yml add openim_admin_front service change openim-admin-front del not using files add node-exporter-dashaboard.yaml
418 lines
10 KiB
Go
418 lines
10 KiB
Go
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
|
|
}
|