api prommetrics

pull/2398/head
withchao 1 year ago
parent 1336b83142
commit 6713986887

@ -2,14 +2,10 @@ package api
import ( import (
"fmt" "fmt"
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs" "github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
"github.com/openimsdk/protocol/constant" "github.com/openimsdk/protocol/constant"
@ -19,14 +15,17 @@ import (
"github.com/openimsdk/tools/mw" "github.com/openimsdk/tools/mw"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"net/http"
"strings"
) )
func prommetricsGin() gin.HandlerFunc { func prommetricsGin() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
start := time.Now() //start := time.Now()
c.Next() c.Next()
path := c.FullPath() path := c.FullPath()
prommetrics.HttpCall(path, c.Request.Method, c.Writer.Status(), time.Since(start)) prommetrics.HttpCall(path, c.Request.Method, c.Writer.Status())
//prommetrics.HttpCall(path, c.Request.Method, c.Writer.Status(), time.Since(start))
if c.Request.Method == http.MethodPost { if c.Request.Method == http.MethodPost {
if resp := apiresp.GetGinApiResponse(c); resp == nil { if resp := apiresp.GetGinApiResponse(c); resp == nil {
prommetrics.APICall(path, -1, "NO_GIN_RESPONSE_FOUND") prommetrics.APICall(path, -1, "NO_GIN_RESPONSE_FOUND")

@ -24,7 +24,8 @@ var (
Name: "http_count", Name: "http_count",
Help: "Total number of HTTP calls", Help: "Total number of HTTP calls",
}, },
[]string{"path", "method", "status", "duration"}, //[]string{"path", "method", "status", "duration"},
[]string{"path", "method", "status"},
) )
) )
@ -36,8 +37,12 @@ func APICall(path string, apiCode int, apiType string) {
apiCounter.With(prometheus.Labels{"path": path, "code": strconv.Itoa(apiCode), "type": apiType}).Inc() apiCounter.With(prometheus.Labels{"path": path, "code": strconv.Itoa(apiCode), "type": apiType}).Inc()
} }
func HttpCall(path string, method string, status int, duration time.Duration) { //func HttpCall(path string, method string, status int, duration time.Duration) {
httpCounter.With(prometheus.Labels{"path": path, "method": method, "status": strconv.Itoa(status), "duration": durationRange(duration)}).Inc() // httpCounter.With(prometheus.Labels{"path": path, "method": method, "status": strconv.Itoa(status), "duration": durationRange(duration)}).Inc()
//}
func HttpCall(path string, method string, status int) {
httpCounter.With(prometheus.Labels{"path": path, "method": method, "status": strconv.Itoa(status)}).Inc()
} }
var ( var (

Loading…
Cancel
Save