From 714cdd726473a0f2314ee9e1f2be627353ad3202 Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Tue, 8 Aug 2023 15:56:28 +0800 Subject: [PATCH] add --prometheus-addr option Signed-off-by: Lin Yang --- cmd/tape/main.go | 52 ++++++++++++++++++++------------ pkg/infra/cmdImpl/fullProcess.go | 8 ++--- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/cmd/tape/main.go b/cmd/tape/main.go index 4180752f..6d329301 100644 --- a/cmd/tape/main.go +++ b/cmd/tape/main.go @@ -13,20 +13,22 @@ import ( ) const ( - loglevel = "TAPE_LOGLEVEL" - logfilename = "Tape.log" + loglevel = "TAPE_LOGLEVEL" + logfilename = "Tape.log" + DEFAULT_PROMETHEUS_ADDR = ":8080" ) var ( app = kingpin.New("tape", "A performance test tool for Hyperledger Fabric") - con = app.Flag("config", "Path to config file").Short('c').String() - num = app.Flag("number", "Number of tx for shot").Short('n').Int() - rate = app.Flag("rate", "[Optional] Creates tx rate, default 0 as unlimited").Default("0").Float64() - burst = app.Flag("burst", "[Optional] Burst size for Tape, should bigger than rate").Default("1000").Int() - signerNumber = app.Flag("signers", "[Optional] signer parallel Number for Tape, default as 5").Default("5").Int() - parallelNumber = app.Flag("parallel", "[Optional] parallel Number for Tape, default as 1").Default("1").Int() - prometheus = app.Flag("prometheus", "[Optional] prometheus enable or not").Default("false").Bool() + con = app.Flag("config", "Path to config file").Short('c').String() + num = app.Flag("number", "Number of tx for shot").Short('n').Int() + rate = app.Flag("rate", "[Optional] Creates tx rate, default 0 as unlimited").Default("0").Float64() + burst = app.Flag("burst", "[Optional] Burst size for Tape, should bigger than rate").Default("1000").Int() + signerNumber = app.Flag("signers", "[Optional] signer parallel Number for Tape, default as 5").Default("5").Int() + parallelNumber = app.Flag("parallel", "[Optional] parallel Number for Tape, default as 1").Default("1").Int() + enablePrometheus = app.Flag("prometheus", "[Optional] prometheus enable or not").Default("false").Bool() + prometheusAddr = app.Flag("prometheus-addr", "[Optional] prometheus address, default as :8080").String() run = app.Command("run", "Start the tape program").Default() @@ -63,20 +65,20 @@ func main() { case version.FullCommand(): fmt.Println(cmdImpl.GetVersionInfo()) case commitOnly.FullCommand(): - checkArgs(rate, burst, signerNumber, parallelNumber, *con, logger) - err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *prometheus, logger, infra.COMMIT) + checkArgs(rate, burst, signerNumber, parallelNumber, *con, *enablePrometheus, prometheusAddr, logger) + err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *enablePrometheus, *prometheusAddr, logger, infra.COMMIT) case endorsementOnly.FullCommand(): - checkArgs(rate, burst, signerNumber, parallelNumber, *con, logger) - err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *prometheus, logger, infra.ENDORSEMENT) + checkArgs(rate, burst, signerNumber, parallelNumber, *con, *enablePrometheus, prometheusAddr, logger) + err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *enablePrometheus, *prometheusAddr, logger, infra.ENDORSEMENT) case run.FullCommand(): - checkArgs(rate, burst, signerNumber, parallelNumber, *con, logger) - err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *prometheus, logger, infra.FULLPROCESS) + checkArgs(rate, burst, signerNumber, parallelNumber, *con, *enablePrometheus, prometheusAddr, logger) + err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *enablePrometheus, *prometheusAddr, logger, infra.FULLPROCESS) case trafficOnly.FullCommand(): - checkArgs(rate, burst, signerNumber, parallelNumber, *con, logger) - err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *prometheus, logger, infra.TRAFFIC) + checkArgs(rate, burst, signerNumber, parallelNumber, *con, *enablePrometheus, prometheusAddr, logger) + err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *enablePrometheus, *prometheusAddr, logger, infra.TRAFFIC) case observerOnly.FullCommand(): - checkArgs(rate, burst, signerNumber, parallelNumber, *con, logger) - err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *prometheus, logger, infra.OBSERVER) + checkArgs(rate, burst, signerNumber, parallelNumber, *con, *enablePrometheus, prometheusAddr, logger) + err = cmdImpl.Process(*con, *num, *burst, *signerNumber, *parallelNumber, *rate, *enablePrometheus, *prometheusAddr, logger, infra.OBSERVER) default: err = errors.Errorf("invalid command: %s", fullCmd) } @@ -89,7 +91,7 @@ func main() { os.Exit(0) } -func checkArgs(rate *float64, burst, signerNumber, parallel *int, con string, logger *log.Logger) { +func checkArgs(rate *float64, burst, signerNumber, parallel *int, con string, enablePrometheus bool, prometheusAddr *string, logger *log.Logger) { if len(con) == 0 { os.Stderr.WriteString("tape: error: required flag --config not provided, try --help") os.Exit(1) @@ -115,6 +117,16 @@ func checkArgs(rate *float64, burst, signerNumber, parallel *int, con string, lo fmt.Printf("As rate %d is bigger than burst %d, real rate is burst\n", int64(*rate), int64(*burst)) } + // enable prometheus but not provide --prometheus-addr option, use default prometheus address ":8080" + if enablePrometheus && len(*prometheusAddr) == 0 { + *prometheusAddr = DEFAULT_PROMETHEUS_ADDR + } + + // not enable prometheus but provide --prometheus-addr option, show help message + if !enablePrometheus && len(*prometheusAddr) != 0 { + fmt.Printf("You've provided the --prometheus-addr option to specify a Prometheus address, but you haven't enabled Prometheus using --prometheus option\n") + } + logger.Infof("Will use rate %f as send rate\n", *rate) logger.Infof("Will use %d as burst\n", burst) } diff --git a/pkg/infra/cmdImpl/fullProcess.go b/pkg/infra/cmdImpl/fullProcess.go index cdc272d3..752f43b3 100644 --- a/pkg/infra/cmdImpl/fullProcess.go +++ b/pkg/infra/cmdImpl/fullProcess.go @@ -15,7 +15,7 @@ import ( log "github.com/sirupsen/logrus" ) -func Process(configPath string, num int, burst, signerNumber, parallel int, rate float64, prometheusOpt bool, logger *log.Logger, processmod int) error { +func Process(configPath string, num int, burst, signerNumber, parallel int, rate float64, enablePrometheus bool, prometheusAddr string, logger *log.Logger, processmod int) error { /*** signal ***/ c := make(chan os.Signal, 1) @@ -54,11 +54,11 @@ func Process(configPath string, num int, burst, signerNumber, parallel int, rate var transactionlatency, readlatency *prometheus.SummaryVec /*** start prometheus ***/ - if prometheusOpt { + if enablePrometheus { go func() { fmt.Println("start prometheus") http.Handle("/metrics", promhttp.Handler()) - server := &http.Server{Addr: ":8080", Handler: nil} + server := &http.Server{Addr: prometheusAddr, Handler: nil} err = server.ListenAndServe() if err != nil { cmdConfig.ErrorCh <- err @@ -86,7 +86,7 @@ func Process(configPath string, num int, burst, signerNumber, parallel int, rate prometheus.MustRegister(transactionlatency) prometheus.MustRegister(readlatency) - basic.InitLatencyMap(transactionlatency, readlatency, processmod, prometheusOpt) + basic.InitLatencyMap(transactionlatency, readlatency, processmod, enablePrometheus) } /*** start workers ***/