Browse Source

Merge pull request #191 from authurguo/main

more metrics for dtm server
pull/193/head v1.10.2
yedf2 4 years ago
committed by GitHub
parent
commit
456dc31341
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      dtmgrpc/dtmgimp/grpc_clients.go
  2. 18
      dtmsvr/metrics.go
  3. 11
      dtmsvr/svr.go
  4. 1
      go.mod
  5. 4
      go.sum

3
dtmgrpc/dtmgimp/grpc_clients.go

@ -13,7 +13,6 @@ import (
"github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmcli/dtmimp"
"github.com/dtm-labs/dtm/dtmcli/logger" "github.com/dtm-labs/dtm/dtmcli/logger"
"github.com/dtm-labs/dtm/dtmgrpc/dtmgpb" "github.com/dtm-labs/dtm/dtmgrpc/dtmgpb"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc "google.golang.org/grpc" grpc "google.golang.org/grpc"
) )
@ -58,7 +57,7 @@ func GetGrpcConn(grpcServer string, isRaw bool) (conn *grpc.ClientConn, rerr err
} }
logger.Debugf("grpc client connecting %s", grpcServer) logger.Debugf("grpc client connecting %s", grpcServer)
interceptors := append(ClientInterceptors, GrpcClientLog) interceptors := append(ClientInterceptors, GrpcClientLog)
inOpt := grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(interceptors...)) inOpt := grpc.WithChainUnaryInterceptor(interceptors...)
conn, rerr := grpc.Dial(grpcServer, inOpt, grpc.WithInsecure(), opts) conn, rerr := grpc.Dial(grpcServer, inOpt, grpc.WithInsecure(), opts)
if rerr == nil { if rerr == nil {
clients.Store(grpcServer, conn) clients.Store(grpcServer, conn)

18
dtmsvr/metrics.go

@ -9,6 +9,7 @@ package dtmsvr
import ( import (
"context" "context"
"strings" "strings"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -17,6 +18,12 @@ import (
) )
var ( var (
serverInfoGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "dtm_server_info",
Help: "The information of this dtm server.",
},
[]string{"gin_version", "grpc_version"})
processTotal = promauto.NewCounterVec(prometheus.CounterOpts{ processTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "dtm_server_process_total", Name: "dtm_server_process_total",
Help: "All request received by dtm", Help: "All request received by dtm",
@ -35,6 +42,12 @@ var (
}, },
[]string{"model", "gid", "status"}) []string{"model", "gid", "status"})
transactionHandledTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "dtm_transaction_handled_duration",
Help: "Histogram of handling latency of the transaction that handled by the server.",
},
[]string{"model", "gid"})
branchTotal = promauto.NewCounterVec(prometheus.CounterOpts{ branchTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "dtm_branch_process_total", Name: "dtm_branch_process_total",
Help: "All branches processed by dtm", Help: "All branches processed by dtm",
@ -42,6 +55,10 @@ var (
[]string{"model", "gid", "branchid", "branchtype", "status"}) []string{"model", "gid", "branchid", "branchtype", "status"})
) )
func setServerInfoMetrics() {
serverInfoGauge.WithLabelValues(gin.Version, grpc.Version).Set(1)
}
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)
@ -81,6 +98,7 @@ func transactionMetrics(global *TransGlobal, status bool) {
} else { } else {
transactionTotal.WithLabelValues(global.TransType, global.Gid, "fail").Inc() transactionTotal.WithLabelValues(global.TransType, global.Gid, "fail").Inc()
} }
transactionHandledTime.WithLabelValues(global.TransType, global.Gid).Observe(time.Since(*global.CreateTime).Seconds())
} }
func branchMetrics(global *TransGlobal, branch *TransBranch, status bool) { func branchMetrics(global *TransGlobal, branch *TransBranch, status bool) {

11
dtmsvr/svr.go

@ -19,19 +19,22 @@ import (
"github.com/dtm-labs/dtm/dtmgrpc/dtmgpb" "github.com/dtm-labs/dtm/dtmgrpc/dtmgpb"
"github.com/dtm-labs/dtm/dtmutil" "github.com/dtm-labs/dtm/dtmutil"
"github.com/dtm-labs/dtmdriver" "github.com/dtm-labs/dtmdriver"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
// StartSvr StartSvr // StartSvr StartSvr
func StartSvr() { func StartSvr() {
logger.Infof("start dtmsvr") logger.Infof("start dtmsvr")
setServerInfoMetrics()
dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second) dtmcli.GetRestyClient().SetTimeout(time.Duration(conf.RequestTimeout) * time.Second)
dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { dtmgrpc.AddUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second) ctx2, cancel := context.WithTimeout(ctx, time.Duration(conf.RequestTimeout)*time.Second)
defer cancel() defer cancel()
return invoker(ctx2, method, req, reply, cc, opts...) return invoker(ctx2, method, req, reply, cc, opts...)
}) })
// start gin server
app := dtmutil.GetGinApp() app := dtmutil.GetGinApp()
app = httpMetrics(app) app = httpMetrics(app)
addRoute(app) addRoute(app)
@ -43,12 +46,10 @@ func StartSvr() {
} }
}() }()
// start grpc server
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.GrpcPort)) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.GrpcPort))
logger.FatalIfError(err) logger.FatalIfError(err)
s := grpc.NewServer( s := grpc.NewServer(grpc.ChainUnaryInterceptor(grpcMetrics, dtmgimp.GrpcServerLog))
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc.UnaryServerInterceptor(grpcMetrics), grpc.UnaryServerInterceptor(dtmgimp.GrpcServerLog)),
))
dtmgpb.RegisterDtmServer(s, &dtmServer{}) dtmgpb.RegisterDtmServer(s, &dtmServer{})
logger.Infof("grpc listening at %v", lis.Addr()) logger.Infof("grpc listening at %v", lis.Addr())
go func() { go func() {

1
go.mod

@ -12,7 +12,6 @@ require (
github.com/go-redis/redis/v8 v8.11.4 github.com/go-redis/redis/v8 v8.11.4
github.com/go-resty/resty/v2 v2.7.0 github.com/go-resty/resty/v2 v2.7.0
github.com/go-sql-driver/mysql v1.6.0 github.com/go-sql-driver/mysql v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/lib/pq v1.10.3 github.com/lib/pq v1.10.3
github.com/lithammer/shortuuid v2.0.3+incompatible github.com/lithammer/shortuuid v2.0.3+incompatible
github.com/lithammer/shortuuid/v3 v3.0.7 github.com/lithammer/shortuuid/v3 v3.0.7

4
go.sum

@ -251,8 +251,6 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
@ -420,7 +418,6 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c= github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/openzipkin/zipkin-go v0.2.5/go.mod h1:KpXfKdgRDnnhsxw4pNIH9Md5lyFqKUa4YDFlwRYAMyE= github.com/openzipkin/zipkin-go v0.2.5/go.mod h1:KpXfKdgRDnnhsxw4pNIH9Md5lyFqKUa4YDFlwRYAMyE=
github.com/openzipkin/zipkin-go v0.3.0/go.mod h1:4c3sLeE8xjNqehmF5RpAFLPLJxXscc0R4l6Zg0P1tTQ= github.com/openzipkin/zipkin-go v0.3.0/go.mod h1:4c3sLeE8xjNqehmF5RpAFLPLJxXscc0R4l6Zg0P1tTQ=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
@ -812,7 +809,6 @@ google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=

Loading…
Cancel
Save