router.go 3.1 KB
Newer Older
U
UlricQin 已提交
1 2 3 4 5 6
package router

import (
	"fmt"
	"os"
	"strings"
U
Ulric Qin 已提交
7
	"time"
U
UlricQin 已提交
8 9 10 11 12 13 14 15 16

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/toolkits/pkg/ginx"

	"github.com/didi/nightingale/v5/src/pkg/aop"
	"github.com/didi/nightingale/v5/src/server/config"
	"github.com/didi/nightingale/v5/src/server/naming"
U
Ulric Qin 已提交
17
	promstat "github.com/didi/nightingale/v5/src/server/stat"
U
UlricQin 已提交
18 19
)

X
xiaoziv 已提交
20
func New(version string, reloadFunc func()) *gin.Engine {
U
UlricQin 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	gin.SetMode(config.C.RunMode)

	loggerMid := aop.Logger()
	recoveryMid := aop.Recovery()

	if strings.ToLower(config.C.RunMode) == "release" {
		aop.DisableConsoleColor()
	}

	r := gin.New()

	r.Use(recoveryMid)

	// whether print access log
	if config.C.HTTP.PrintAccessLog {
		r.Use(loggerMid)
	}

X
xiaoziv 已提交
39
	configRoute(r, version, reloadFunc)
U
UlricQin 已提交
40 41 42 43

	return r
}

X
xiaoziv 已提交
44
func configRoute(r *gin.Engine, version string, reloadFunc func()) {
U
UlricQin 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	if config.C.HTTP.PProf {
		pprof.Register(r, "/api/debug/pprof")
	}

	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong")
	})

	r.GET("/pid", func(c *gin.Context) {
		c.String(200, fmt.Sprintf("%d", os.Getpid()))
	})

	r.GET("/addr", func(c *gin.Context) {
		c.String(200, c.Request.RemoteAddr)
	})

	r.GET("/version", func(c *gin.Context) {
		c.String(200, version)
	})

X
xiaoziv 已提交
65 66 67 68 69
	r.POST("/-/reload", func(c *gin.Context) {
		reloadFunc()
		c.String(200, "reload success")
	})

U
UlricQin 已提交
70
	r.GET("/servers/active", func(c *gin.Context) {
71
		lst, err := naming.ActiveServers(ginx.QueryStr(c, "cluster"))
U
UlricQin 已提交
72 73 74
		ginx.NewRender(c).Data(lst, err)
	})

U
Ulric Qin 已提交
75
	// use apiKey not basic auth
U
Ulric Qin 已提交
76
	r.POST("/datadog/api/v1/series", stat(), datadogSeries)
U
Ulric Qin 已提交
77 78 79 80
	r.POST("/datadog/api/v1/check_run", datadogCheckRun)
	r.GET("/datadog/api/v1/validate", datadogValidate)
	r.POST("/datadog/api/v1/metadata", datadogMetadata)
	r.POST("/datadog/intake/", datadogIntake)
U
Ulric Qin 已提交
81

U
UlricQin 已提交
82 83 84 85 86
	if len(config.C.BasicAuth) > 0 {
		auth := gin.BasicAuth(config.C.BasicAuth)
		r.Use(auth)
	}

U
Ulric Qin 已提交
87 88 89 90
	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)
U
UlricQin 已提交
91 92

	r.GET("/memory/alert-rule", alertRuleGet)
U
Ulric Qin 已提交
93
	r.GET("/memory/alert-rule-location", alertRuleLocationGet)
U
UlricQin 已提交
94 95 96 97 98 99 100 101
	r.GET("/memory/idents", identsGets)
	r.GET("/memory/alert-mutes", mutesGets)
	r.GET("/memory/alert-subscribes", subscribesGets)
	r.GET("/memory/target", targetGet)
	r.GET("/memory/user", userGet)
	r.GET("/memory/user-group", userGroupGet)

	r.GET("/metrics", gin.WrapH(promhttp.Handler()))
102

103 104 105 106
	r.GET("/log-sample-filter", logSampleFilterGet)
	r.POST("/log-sample-filter", logSampleFilterAdd)
	r.DELETE("/log-sample-filter", logSampleFilterDel)

107 108
	service := r.Group("/v1/n9e")
	service.POST("/event", pushEventToQueue)
109 110
	service.POST("/make-event", makeEvent)
	service.POST("/judge-event", judgeEvent)
U
UlricQin 已提交
111
}
U
Ulric Qin 已提交
112 113 114 115 116 117 118 119

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
120
		labels := []string{code, c.FullPath(), method}
U
Ulric Qin 已提交
121

U
Ulric Qin 已提交
122
		promstat.RequestDuration.WithLabelValues(labels...).Observe(time.Since(start).Seconds())
U
Ulric Qin 已提交
123 124
	}
}