From c7d6426f51bfcd35bcc601eda1713ef32ac1fb5e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 22 Dec 2021 21:01:43 +0800 Subject: [PATCH] change: remove namespace --- hrp/cmd/root.go | 3 +- internal/boomer/output.go | 74 +++++++++++++--------------------- internal/boomer/output_test.go | 26 ------------ internal/boomer/stats.go | 27 +++++-------- 4 files changed, 39 insertions(+), 91 deletions(-) diff --git a/hrp/cmd/root.go b/hrp/cmd/root.go index 3025d9cd..3db69816 100644 --- a/hrp/cmd/root.go +++ b/hrp/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "strings" @@ -42,7 +41,7 @@ func Execute() { RootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format") if err := RootCmd.Execute(); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("Failed to execute root command") os.Exit(1) } } diff --git a/internal/boomer/output.go b/internal/boomer/output.go index 80214289..3f897e91 100644 --- a/internal/boomer/output.go +++ b/internal/boomer/output.go @@ -246,81 +246,68 @@ func deserializeStatsEntry(stat interface{}) (entryOutput *statsEntryOutput, err return } -const ( - namespace = "boomer" -) - // gauge vectors for requests var ( gaugeNumRequests = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "num_requests", - Help: "The number of requests", + Name: "num_requests", + Help: "The number of requests", }, []string{"method", "name"}, ) gaugeNumFailures = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "num_failures", - Help: "The number of failures", + Name: "num_failures", + Help: "The number of failures", }, []string{"method", "name"}, ) gaugeMedianResponseTime = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "median_response_time", - Help: "The median response time", + Name: "median_response_time", + Help: "The median response time", }, []string{"method", "name"}, ) gaugeAverageResponseTime = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "average_response_time", - Help: "The average response time", + Name: "average_response_time", + Help: "The average response time", }, []string{"method", "name"}, ) gaugeMinResponseTime = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "min_response_time", - Help: "The min response time", + Name: "min_response_time", + Help: "The min response time", }, []string{"method", "name"}, ) gaugeMaxResponseTime = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "max_response_time", - Help: "The max response time", + Name: "max_response_time", + Help: "The max response time", }, []string{"method", "name"}, ) gaugeAverageContentLength = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "average_content_length", - Help: "The average content length", + Name: "average_content_length", + Help: "The average content length", }, []string{"method", "name"}, ) gaugeCurrentRPS = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "current_rps", - Help: "The current requests per second", + Name: "current_rps", + Help: "The current requests per second", }, []string{"method", "name"}, ) gaugeCurrentFailPerSec = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "current_fail_per_sec", - Help: "The current failure number per second", + Name: "current_fail_per_sec", + Help: "The current failure number per second", }, []string{"method", "name"}, ) @@ -330,37 +317,32 @@ var ( var ( gaugeUsers = prometheus.NewGauge( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "users", - Help: "The current number of users", + Name: "users", + Help: "The current number of users", }, ) gaugeTotalRPS = prometheus.NewGauge( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "total_rps", - Help: "The requests per second in total", + Name: "total_rps", + Help: "The requests per second in total", }, ) gaugeTotalFailRatio = prometheus.NewGauge( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "fail_ratio", - Help: "The ratio of request failures in total", + Name: "fail_ratio", + Help: "The ratio of request failures in total", }, ) gaugeTransactionsPassed = prometheus.NewGauge( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "transactions_passed", - Help: "The accumulated number of passed transactions", + Name: "transactions_passed", + Help: "The accumulated number of passed transactions", }, ) gaugeTransactionsFailed = prometheus.NewGauge( prometheus.GaugeOpts{ - Namespace: namespace, - Name: "transactions_failed", - Help: "The accumulated number of failed transactions", + Name: "transactions_failed", + Help: "The accumulated number of failed transactions", }, ) ) diff --git a/internal/boomer/output_test.go b/internal/boomer/output_test.go index be06a50e..76af41d1 100644 --- a/internal/boomer/output_test.go +++ b/internal/boomer/output_test.go @@ -1,9 +1,7 @@ package boomer import ( - "fmt" "math" - "sort" "testing" ) @@ -108,27 +106,3 @@ func TestConsoleOutput(t *testing.T) { o.OnStop() } - -func TestSortString(t *testing.T) { - - stats := []struct { - method string - name string - }{ - {"transaction", "Action"}, - {"request-GET", "get with params"}, - {"request-POST", "post form data"}, - {"request-POST", "post json data"}, - {"transaction", "tran1"}, - } - - sort.Slice(stats, func(i, j int) bool { - if stats[i].method < stats[j].method { - return true - } - - return stats[i].name < stats[j].name - }) - - fmt.Println(stats) -} diff --git a/internal/boomer/stats.go b/internal/boomer/stats.go index 06080586..9978a584 100644 --- a/internal/boomer/stats.go +++ b/internal/boomer/stats.go @@ -1,6 +1,7 @@ package boomer import ( + "encoding/json" "time" ) @@ -311,23 +312,15 @@ func (s *statsEntry) logError(err string) { } func (s *statsEntry) serialize() map[string]interface{} { - result := make(map[string]interface{}) - result["name"] = s.Name - result["method"] = s.Method - result["last_request_timestamp"] = s.LastRequestTimestamp - result["start_time"] = s.StartTime - result["num_requests"] = s.NumRequests - // Boomer doesn't allow None response time for requests like locust. - // num_none_requests is added to keep compatible with locust. - result["num_none_requests"] = 0 - result["num_failures"] = s.NumFailures - result["total_response_time"] = s.TotalResponseTime - result["max_response_time"] = s.MaxResponseTime - result["min_response_time"] = s.MinResponseTime - result["total_content_length"] = s.TotalContentLength - result["response_times"] = s.ResponseTimes - result["num_reqs_per_sec"] = s.NumReqsPerSec - result["num_fail_per_sec"] = s.NumFailPerSec + var result map[string]interface{} + val, err := json.Marshal(s) + if err != nil { + return nil + } + err = json.Unmarshal(val, &result) + if err != nil { + return nil + } return result }