From 3f89492e4199d565e9c46694be56a52f410fdf65 Mon Sep 17 00:00:00 2001 From: xjewer Date: Thu, 28 Sep 2017 03:03:58 +0300 Subject: [PATCH] fix stats key prefix --- .gitignore | 2 +- README.md | 4 ++-- cmd/snitch/snitch.go | 2 +- lib/stats/stats.go | 7 +++---- parser.go | 4 ++-- processor.go | 2 +- processor_test.go | 4 ++-- reader.go | 8 +------- 8 files changed, 13 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 2e8c11a..68a0e42 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /reader_test_offset* .idea/ - +config.yml diff --git a/README.md b/README.md index f9bd151..1b82ca2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This tool allows to parse log files and send statistics to statsd endpoint * {string} `config` - configuration file * {string} `statsd` - statsd endpoint -* {string} `prefix` - statsd global key prefix +* {string} `prefix` - statsd global key prefix, e.g. `balancer.` * {int} `buffer` - buffer interval for metrics, `0` default: one metric - one request ### Configuration file @@ -39,7 +39,7 @@ where is: * {boolean} `noFollow` - means no follow new lines that are written in the log file, if true * {boolean} `mustExists` - log file have to be existed, if true * {boolean} `reOpen` - re-open file, if true (e.g. log rotation) -* {string} `prefix` - statsd key prefix +* {string} `prefix` - statsd key prefix, `%HOST%` is used as substitution a hostname * {string} `delimiter` - column delimiter in log files, it is reasonable to use `\t` delimiter in log files * {[]key} `keys` - list of keys, which would be send to statsd diff --git a/cmd/snitch/snitch.go b/cmd/snitch/snitch.go index aa3524a..b8ee1b4 100644 --- a/cmd/snitch/snitch.go +++ b/cmd/snitch/snitch.go @@ -22,7 +22,7 @@ var ( cfg = flag.String("config", config.DefaultConfigPath, "config file name") statsdEndpoint = flag.String("statsd", "", "statsd endpoint") - statsdKeyPrefix = flag.String("prefix", "test", "statsd global key prefix") + statsdKeyPrefix = flag.String("prefix", "", "statsd global key prefix") buffer = flag.Int("buffer", 0, "statsd buffer interval") ) diff --git a/lib/stats/stats.go b/lib/stats/stats.go index ea3a5af..aa29b95 100644 --- a/lib/stats/stats.go +++ b/lib/stats/stats.go @@ -1,13 +1,12 @@ package stats import ( - "fmt" "time" "github.com/quipo/statsd" ) -// NewStatsd return new statsd client +// NewStatsd return the new statsd client func NewStatsd(s string, prefix string, buffer int) statsd.Statsd { if s == "" { return statsd.NewStdoutClient("", prefix) @@ -23,10 +22,10 @@ func NewStatsd(s string, prefix string, buffer int) statsd.Statsd { // SendTiming writes timings in milliseconds func SendTiming(s statsd.Statsd, key string, t int64) { - s.Timing(fmt.Sprintf("timings.%s", key), t) + s.Timing(key, t) } // SendEvent writes event types func SendEvent(s statsd.Statsd, key string) { - s.Incr(fmt.Sprintf("count.%s", key), 1) + s.Incr(key, 1) } diff --git a/parser.go b/parser.go index 1810876..5f46496 100644 --- a/parser.go +++ b/parser.go @@ -12,8 +12,8 @@ import ( ) var ( - ErrProcessorStopped = errors.New("processor is stopped") - ErrEmptyVarName = errors.New("empty var name") + ErrProcessorIsFinished = errors.New("processor is finished") + ErrEmptyVarName = errors.New("empty var name") ) // Parser parses log text from reader and sends statistics diff --git a/processor.go b/processor.go index 558b642..f38679d 100644 --- a/processor.go +++ b/processor.go @@ -28,7 +28,7 @@ func (p *Processor) Close() error { if err != ErrReaderIsFinished { p.l.Println(err) } - p.Kill(ErrProcessorStopped) + p.Kill(ErrProcessorIsFinished) return p.Wait() } diff --git a/processor_test.go b/processor_test.go index ec10e3b..80ebed6 100644 --- a/processor_test.go +++ b/processor_test.go @@ -33,8 +33,8 @@ func Test_ProcessorRun(t *testing.T) { }() lines <- snitch.NewLine("test", nil) - a.Equal(snitch.ErrProcessorStopped, p.Close()) - //a.Equal(0, len(lines)) + a.Equal(snitch.ErrProcessorIsFinished, p.Close()) + a.Equal(0, len(lines)) wg.Wait() close(lines) } diff --git a/reader.go b/reader.go index 3eba156..3b409f8 100644 --- a/reader.go +++ b/reader.go @@ -64,12 +64,7 @@ func (r *fileReader) Close() error { } r.tail.Cleanup() r.tail.Kill(ErrReaderIsFinished) - err := r.tail.Wait() - if err != ErrReaderIsFinished { - return err - } - - return nil + return r.tail.Wait() } // openFile opens simplelog file to read its lines @@ -116,7 +111,6 @@ func (r *fileReader) GetLines(lines chan<- *Line) { } lines <- NewLine(l.Text, l.Err) case <-r.tail.Dying(): - r.l.Println("dying") return } }