router.go 2.7 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 18

	promstat "github.com/didi/nightingale/v5/src/server/stat"
U
UlricQin 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
)

func New(version string) *gin.Engine {
	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)
	}

	configRoute(r, version)

	return r
}

func configRoute(r *gin.Engine, version string) {
	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)
	})

	r.GET("/servers/active", func(c *gin.Context) {
		lst, err := naming.ActiveServers(c.Request.Context(), config.C.ClusterName)
		ginx.NewRender(c).Data(lst, err)
	})

U
Ulric Qin 已提交
71
	// use apiKey not basic auth
U
Ulric Qin 已提交
72
	r.POST("/datadog/api/v1/series", stat(), datadogSeries)
U
Ulric Qin 已提交
73 74 75 76
	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 已提交
77

U
UlricQin 已提交
78 79 80 81 82
	if len(config.C.BasicAuth) > 0 {
		auth := gin.BasicAuth(config.C.BasicAuth)
		r.Use(auth)
	}

U
Ulric Qin 已提交
83 84 85 86
	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 已提交
87 88 89 90 91 92 93 94 95 96

	r.GET("/memory/alert-rule", alertRuleGet)
	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()))
97 98 99

	service := r.Group("/v1/n9e")
	service.POST("/event", pushEventToQueue)
U
UlricQin 已提交
100
}
U
Ulric Qin 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113

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()))
	}
}