From 267d6d0e2bd08e8a95ab15cddfa35435b3c49fe8 Mon Sep 17 00:00:00 2001 From: bruce34 Date: Sat, 16 Feb 2019 08:29:51 +0000 Subject: [PATCH] Add adjustable output-http-stats-ms (#633) This adds an option to log output-http-stats at a period desired. --- gor_stat.go | 12 +++++------- output_http.go | 7 ++++--- output_tcp.go | 2 +- settings.go | 3 ++- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gor_stat.go b/gor_stat.go index 2273a5c..8f28e6f 100644 --- a/gor_stat.go +++ b/gor_stat.go @@ -7,21 +7,19 @@ import ( "time" ) -const ( - rate = 5 -) - type GorStat struct { statName string + rateMs int latest int mean int max int count int } -func NewGorStat(statName string) (s *GorStat) { +func NewGorStat(statName string, rateMs int) (s *GorStat) { s = new(GorStat) s.statName = statName + s.rateMs = rateMs s.latest = 0 s.mean = 0 s.max = 0 @@ -55,13 +53,13 @@ func (s *GorStat) Reset() { } func (s *GorStat) String() string { - return s.statName + ":" + strconv.Itoa(s.latest) + "," + strconv.Itoa(s.mean) + "," + strconv.Itoa(s.max) + "," + strconv.Itoa(s.count) + "," + strconv.Itoa(s.count/rate) + "," + strconv.Itoa(runtime.NumGoroutine()) + return s.statName + ":" + strconv.Itoa(s.latest) + "," + strconv.Itoa(s.mean) + "," + strconv.Itoa(s.max) + "," + strconv.Itoa(s.count) + "," + strconv.Itoa(s.count/(s.rateMs/1000.0)) + "," + strconv.Itoa(runtime.NumGoroutine()) } func (s *GorStat) reportStats() { for { log.Println(s) s.Reset() - time.Sleep(rate * time.Second) + time.Sleep(time.Duration(s.rateMs) * time.Millisecond) } } diff --git a/output_http.go b/output_http.go index 0b47799..b55002c 100644 --- a/output_http.go +++ b/output_http.go @@ -21,8 +21,9 @@ type response struct { type HTTPOutputConfig struct { redirectLimit int - stats bool - workers int + stats bool + statsMs int + workers int queueLen int elasticSearch string @@ -69,7 +70,7 @@ func NewHTTPOutput(address string, config *HTTPOutputConfig) io.Writer { o.config = config if o.config.stats { - o.queueStats = NewGorStat("output_http") + o.queueStats = NewGorStat("output_http", o.config.statsMs) } o.queue = make(chan []byte, o.config.queueLen) diff --git a/output_tcp.go b/output_tcp.go index 916ba7d..23b55ba 100644 --- a/output_tcp.go +++ b/output_tcp.go @@ -34,7 +34,7 @@ func NewTCPOutput(address string, config *TCPOutputConfig) io.Writer { o.buf = make(chan []byte, 1000) if Settings.outputTCPStats { - o.bufStats = NewGorStat("output_tcp") + o.bufStats = NewGorStat("output_tcp", 5000) } for i := 0; i < 10; i++ { diff --git a/settings.go b/settings.go index 58831b2..ac18857 100644 --- a/settings.go +++ b/settings.go @@ -155,7 +155,8 @@ func init() { flag.DurationVar(&Settings.outputHTTPConfig.Timeout, "output-http-timeout", 5*time.Second, "Specify HTTP request/response timeout. By default 5s. Example: --output-http-timeout 30s") flag.BoolVar(&Settings.outputHTTPConfig.TrackResponses, "output-http-track-response", false, "If turned on, HTTP output responses will be set to all outputs like stdout, file and etc.") - flag.BoolVar(&Settings.outputHTTPConfig.stats, "output-http-stats", false, "Report http output queue stats to console every 5 seconds.") + flag.BoolVar(&Settings.outputHTTPConfig.stats, "output-http-stats", false, "Report http output queue stats to console every N milliseconds. See output-http-stats-ms") + flag.IntVar(&Settings.outputHTTPConfig.statsMs, "output-http-stats-ms", 5000, "Report http output queue stats to console every N milliseconds. default: 5000") flag.BoolVar(&Settings.outputHTTPConfig.OriginalHost, "http-original-host", false, "Normally gor replaces the Host http header with the host supplied with --output-http. This option disables that behavior, preserving the original Host header.") flag.BoolVar(&Settings.outputHTTPConfig.Debug, "output-http-debug", false, "Enables http debug output.")