metrics rename some function

This commit is contained in:
yedf2 2021-09-25 22:58:04 +08:00
parent ae60bdd359
commit 32610be3ec
3 changed files with 12 additions and 12 deletions

View File

@ -23,7 +23,7 @@ var metricsPort = 8889
func StartSvr() { func StartSvr() {
dtmcli.Logf("start dtmsvr") dtmcli.Logf("start dtmsvr")
app := common.GetGinApp() app := common.GetGinApp()
app = HTTP_metrics(app) app = httpMetrics(app)
addRoute(app) addRoute(app)
dtmcli.Logf("dtmsvr listen at: %d", dtmsvrPort) dtmcli.Logf("dtmsvr listen at: %d", dtmsvrPort)
go app.Run(fmt.Sprintf(":%d", dtmsvrPort)) go app.Run(fmt.Sprintf(":%d", dtmsvrPort))
@ -32,7 +32,7 @@ func StartSvr() {
dtmcli.FatalIfError(err) dtmcli.FatalIfError(err)
s := grpc.NewServer( s := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc.UnaryServerInterceptor(GRPC_metrics), grpc.UnaryServerInterceptor(dtmgrpc.GrpcServerLog)), grpc.UnaryServerInterceptor(grpcMetrics), grpc.UnaryServerInterceptor(dtmgrpc.GrpcServerLog)),
)) ))
dtmgrpc.RegisterDtmServer(s, &dtmServer{}) dtmgrpc.RegisterDtmServer(s, &dtmServer{})
dtmcli.Logf("grpc listening at %v", lis.Addr()) dtmcli.Logf("grpc listening at %v", lis.Addr())
@ -43,7 +43,7 @@ func StartSvr() {
// prometheus exporter // prometheus exporter
dtmcli.Logf("prometheus exporter listen at: %d", metricsPort) dtmcli.Logf("prometheus exporter listen at: %d", metricsPort)
PrometheusHttpRun(fmt.Sprintf("%d", metricsPort)) prometheusHTTPRun(fmt.Sprintf("%d", metricsPort))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }

View File

@ -37,14 +37,14 @@ var (
[]string{"model", "gid", "branchid", "branchtype", "status"}) []string{"model", "gid", "branchid", "branchtype", "status"})
) )
func PrometheusHttpRun(port string) { func prometheusHTTPRun(port string) {
go func() { go func() {
http.Handle("/metrics", promhttp.Handler()) http.Handle("/api/metrics", promhttp.Handler())
http.ListenAndServe(":"+port, nil) http.ListenAndServe(":"+port, nil)
}() }()
} }
func HTTP_metrics(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)
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) { timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
@ -64,7 +64,7 @@ func HTTP_metrics(app *gin.Engine) *gin.Engine {
return app return app
} }
func GRPC_metrics(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { func grpcMetrics(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
api := extractFromPath(info.FullMethod) api := extractFromPath(info.FullMethod)
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) { timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
responseTime.WithLabelValues("grpc", api).Observe(v) responseTime.WithLabelValues("grpc", api).Observe(v)
@ -79,7 +79,7 @@ func GRPC_metrics(ctx context.Context, req interface{}, info *grpc.UnaryServerIn
return m, err return m, err
} }
func TransactionMetrics(global *TransGlobal, status bool) { func transactionMetrics(global *TransGlobal, status bool) {
if status { if status {
transactionTotal.WithLabelValues(global.TransType, global.Gid, "ok").Inc() transactionTotal.WithLabelValues(global.TransType, global.Gid, "ok").Inc()
} else { } else {
@ -87,7 +87,7 @@ func TransactionMetrics(global *TransGlobal, status bool) {
} }
} }
func BranchMetrics(global *TransGlobal, branch *TransBranch, status bool) { func branchMetrics(global *TransGlobal, branch *TransBranch, status bool) {
if status { if status {
branchTotal.WithLabelValues(global.TransType, global.Gid, branch.BranchID, branch.BranchType, "ok").Inc() branchTotal.WithLabelValues(global.TransType, global.Gid, branch.BranchID, branch.BranchType, "ok").Inc()
} else { } else {

View File

@ -119,7 +119,7 @@ func (t *TransGlobal) getProcessor() transProcessor {
// Process process global transaction once // Process process global transaction once
func (t *TransGlobal) Process(db *common.DB, waitResult bool) dtmcli.M { func (t *TransGlobal) Process(db *common.DB, waitResult bool) dtmcli.M {
r := t.process(db, waitResult) r := t.process(db, waitResult)
TransactionMetrics(t, r["dtm_result"] == dtmcli.ResultSuccess) transactionMetrics(t, r["dtm_result"] == dtmcli.ResultSuccess)
} }
func (t *TransGlobal) process(db *common.DB, waitResult bool) dtmcli.M { func (t *TransGlobal) process(db *common.DB, waitResult bool) dtmcli.M {
@ -208,11 +208,11 @@ func (t *TransGlobal) execBranch(db *common.DB, branch *TransBranch) {
if strings.Contains(body, dtmcli.ResultSuccess) { if strings.Contains(body, dtmcli.ResultSuccess) {
t.touch(db, config.TransCronInterval) t.touch(db, config.TransCronInterval)
branch.changeStatus(db, dtmcli.StatusSucceed) branch.changeStatus(db, dtmcli.StatusSucceed)
BranchMetrics(t, branch, true) branchMetrics(t, branch, true)
} else if t.TransType == "saga" && branch.BranchType == dtmcli.BranchAction && strings.Contains(body, dtmcli.ResultFailure) { } else if t.TransType == "saga" && branch.BranchType == dtmcli.BranchAction && strings.Contains(body, dtmcli.ResultFailure) {
t.touch(db, config.TransCronInterval) t.touch(db, config.TransCronInterval)
branch.changeStatus(db, dtmcli.StatusFailed) branch.changeStatus(db, dtmcli.StatusFailed)
BranchMetrics(t, branch, false) branchMetrics(t, branch, false)
} else { } else {
panic(fmt.Errorf("http result should contains SUCCESS|FAILURE. grpc error should return nil|Aborted. \nrefer to: https://dtm.pub/summary/arch.html#http\nunkown result will be retried: %s", body)) panic(fmt.Errorf("http result should contains SUCCESS|FAILURE. grpc error should return nil|Aborted. \nrefer to: https://dtm.pub/summary/arch.html#http\nunkown result will be retried: %s", body))
} }