Browse Source

fix(server): use http as prom server

pull/89/head
lsytj0413 4 years ago
parent
commit
df19a851b3
  1. 5
      common/types.go
  2. 7
      dtmsvr/api_http.go
  3. 3
      dtmsvr/dtmsvr.go
  4. 9
      dtmsvr/metrics.go

5
common/types.go

@ -18,9 +18,8 @@ import (
) )
const ( const (
DtmHttpPort = 36789 DtmHttpPort = 36789
DtmGrpcPort = 36790 DtmGrpcPort = 36790
DtmMetricsPort = 8889
) )
// MicroService config type for micro service // MicroService config type for micro service

7
dtmsvr/api_http.go

@ -11,6 +11,7 @@ import (
"math" "math"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/yedf/dtm/common" "github.com/yedf/dtm/common"
"github.com/yedf/dtm/dtmcli" "github.com/yedf/dtm/dtmcli"
"github.com/yedf/dtm/dtmcli/dtmimp" "github.com/yedf/dtm/dtmcli/dtmimp"
@ -26,6 +27,12 @@ func addRoute(engine *gin.Engine) {
engine.POST("/api/dtmsvr/registerTccBranch", common.WrapHandler(registerBranch)) // compatible for old sdk engine.POST("/api/dtmsvr/registerTccBranch", common.WrapHandler(registerBranch)) // compatible for old sdk
engine.GET("/api/dtmsvr/query", common.WrapHandler(query)) engine.GET("/api/dtmsvr/query", common.WrapHandler(query))
engine.GET("/api/dtmsvr/all", common.WrapHandler(all)) engine.GET("/api/dtmsvr/all", common.WrapHandler(all))
// add prometheus exporter
h := promhttp.Handler()
engine.GET("/api/metrics", func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
})
} }
func newGid(c *gin.Context) (interface{}, error) { func newGid(c *gin.Context) (interface{}, error) {

3
dtmsvr/dtmsvr.go

@ -48,9 +48,6 @@ func StartSvr() {
}() }()
go updateBranchAsync() go updateBranchAsync()
// prometheus exporter
dtmimp.Logf("prometheus exporter listen at: %d", common.DtmMetricsPort)
prometheusHTTPRun(fmt.Sprintf("%d", common.DtmMetricsPort))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
err = dtmdriver.Use(config.MicroService.Driver) err = dtmdriver.Use(config.MicroService.Driver)
dtmimp.FatalIfError(err) dtmimp.FatalIfError(err)

9
dtmsvr/metrics.go

@ -8,13 +8,11 @@ package dtmsvr
import ( import (
"context" "context"
"net/http"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -44,13 +42,6 @@ var (
[]string{"model", "gid", "branchid", "branchtype", "status"}) []string{"model", "gid", "branchid", "branchtype", "status"})
) )
func prometheusHTTPRun(port string) {
go func() {
http.Handle("/api/metrics", promhttp.Handler())
http.ListenAndServe(":"+port, nil)
}()
}
func httpMetrics(app *gin.Engine) *gin.Engine { func httpMetrics(app *gin.Engine) *gin.Engine {
app.Use(func(c *gin.Context) { app.Use(func(c *gin.Context) {
api := extractFromPath(c.Request.RequestURI) api := extractFromPath(c.Request.RequestURI)

Loading…
Cancel
Save