n9e-server: add http request stat

This commit is contained in:
Ulric Qin 2022-07-07 10:50:25 +08:00
parent aa97ac54d1
commit 5907817cba
2 changed files with 31 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
@ -13,6 +14,8 @@ import (
"github.com/didi/nightingale/v5/src/pkg/aop"
"github.com/didi/nightingale/v5/src/server/config"
"github.com/didi/nightingale/v5/src/server/naming"
promstat "github.com/didi/nightingale/v5/src/server/stat"
)
func New(version string) *gin.Engine {
@ -66,7 +69,7 @@ func configRoute(r *gin.Engine, version string) {
})
// use apiKey not basic auth
r.POST("/datadog/api/v1/series", datadogSeries)
r.POST("/datadog/api/v1/series", stat(), datadogSeries)
r.POST("/datadog/api/v1/check_run", datadogCheckRun)
r.GET("/datadog/api/v1/validate", datadogValidate)
r.POST("/datadog/api/v1/metadata", datadogMetadata)
@ -77,10 +80,10 @@ func configRoute(r *gin.Engine, version string) {
r.Use(auth)
}
r.POST("/opentsdb/put", handleOpenTSDB)
r.POST("/openfalcon/push", falconPush)
r.POST("/prometheus/v1/write", remoteWrite)
r.POST("/prometheus/v1/query", queryPromql)
r.POST("/opentsdb/put", stat(), handleOpenTSDB)
r.POST("/openfalcon/push", stat(), falconPush)
r.POST("/prometheus/v1/write", stat(), remoteWrite)
r.POST("/prometheus/v1/query", stat(), queryPromql)
r.GET("/memory/alert-rule", alertRuleGet)
r.GET("/memory/idents", identsGets)
@ -95,3 +98,16 @@ func configRoute(r *gin.Engine, version string) {
service := r.Group("/v1/n9e")
service.POST("/event", pushEventToQueue)
}
func stat() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
code := fmt.Sprintf("%d", c.Writer.Status())
method := c.Request.Method
labels := []string{"n9e-server", code, c.FullPath(), method}
promstat.RequestDuration.WithLabelValues(labels...).Observe(float64(time.Since(start).Seconds()))
}
}

View File

@ -57,6 +57,15 @@ var (
Name: "sample_queue_size",
Help: "The size of sample queue.",
}, []string{"cluster", "channel_number"})
// 一些重要的请求,比如接收数据的请求,应该统计一下延迟情况
RequestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Buckets: []float64{.01, .1, 1},
Name: "http_request_duration_seconds",
Help: "HTTP request latencies in seconds.",
}, []string{"service", "code", "path", "method"},
)
)
func Init() {
@ -68,5 +77,6 @@ func Init() {
CounterAlertsTotal,
GaugeAlertQueueSize,
GaugeSampleQueueSize,
RequestDuration,
)
}