-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Histogram as filter plugin
- Loading branch information
Showing
7 changed files
with
188 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package telegraf | ||
|
||
|
||
type Filter interface { | ||
// SampleConfig returns the default configuration of the Input | ||
SampleConfig() string | ||
|
||
// Description returns a one-sentence description on the Input | ||
Description() string | ||
|
||
//Output metric to outputs list | ||
OutputMetric(output interface{}) | ||
//Add metric to the middleware | ||
AddMetric(metric Metric) | ||
//Called on each metric to check if this middle ware enabled | ||
//or not for that metric | ||
IsEnabled(name string) bool | ||
//clear metrics to output | ||
Reset() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package all | ||
|
||
import ( | ||
_ "github.com/influxdata/telegraf/plugins/filters/histogram" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package histogram | ||
|
||
import ( | ||
"github.com/VividCortex/gohistogram" | ||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/filters" | ||
"github.com/influxdata/telegraf/internal/models" | ||
"strconv" | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Histogram struct { | ||
sync.RWMutex | ||
Bucketsize int | ||
Metrics map[string][]float64 | ||
fieldMap map[string]map[string]*gohistogram.NumericHistogram | ||
metricTags map[string]map[string]string | ||
} | ||
|
||
func (h *Histogram) Description() string { | ||
return "Read Histogram-formatted JSON metrics from one or more HTTP endpoints" | ||
} | ||
|
||
func (h *Histogram) SampleConfig() string { | ||
return ` | ||
## Histogram Filter | ||
## This filter can be used to generate | ||
## (mean varince percentile count) | ||
## values generated are approxmation please refer to | ||
## Ben-Haim & Yom-Tov's A Streaming Parallel Decision Tree Algorithm | ||
## http://jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf | ||
##[filter.histogram] | ||
## bucket size if increase it will increase accuracy | ||
## but it will increase memory usage | ||
## bucketsize = 20 | ||
## [filter.histogram.metrics] | ||
## metric name = [percentiles] ## if array is empty only count mean | ||
## and variance will be cacluated | ||
## tail = [0.90] | ||
` | ||
} | ||
|
||
func (h *Histogram) AddMetric(metric telegraf.Metric) { | ||
name := metric.Name() | ||
h.Lock() | ||
defer h.Unlock() | ||
if h.fieldMap[name] == nil { | ||
h.fieldMap[name] = make(map[string]*gohistogram.NumericHistogram) | ||
} | ||
if h.metricTags[name] == nil { | ||
h.metricTags[name] = make(map[string]string) | ||
} | ||
h.metricTags[name] = metric.Tags() | ||
for key, val := range metric.Fields() { | ||
switch v := val.(type) { | ||
case float64: | ||
if h.fieldMap[name][key] == nil { | ||
h.fieldMap[name][key] = gohistogram.NewHistogram(h.Bucketsize) | ||
} | ||
hist := h.fieldMap[name][key] | ||
hist.Add(v) | ||
default: | ||
log.Printf("When stats enabled all the fields should be of type float64 [field name %s]", key) | ||
} | ||
} | ||
} | ||
|
||
func (h *Histogram) IsEnabled(name string) bool { | ||
_, ok := h.Metrics[name] | ||
return ok | ||
} | ||
|
||
func (h *Histogram) OutputMetric(output interface{}) { | ||
h.RLock() | ||
defer h.RUnlock() | ||
for name, fields := range h.fieldMap { | ||
mFields := make(map[string]interface{}) | ||
for key, val := range fields { | ||
for _, perc := range h.Metrics[name] { | ||
p := strconv.FormatFloat(perc*100, 'f', 0, 64) | ||
mFields[key+"_p"+p] = val.Quantile(perc) | ||
} | ||
mFields[key+"_variance"] = val.Variance() | ||
mFields[key+"_mean"] = val.Mean() | ||
mFields[key+"_count"] = val.Count() | ||
} | ||
metric, _ := telegraf.NewMetric(name, h.metricTags[name], mFields, time.Now().UTC()) | ||
if out, ok := output.(*internal_models.RunningOutput); ok { | ||
out.AddMetric(metric) | ||
} | ||
} | ||
} | ||
|
||
func (h *Histogram) Reset() { | ||
h.Lock() | ||
defer h.Unlock() | ||
h.fieldMap = make(map[string]map[string]*gohistogram.NumericHistogram) | ||
h.metricTags = make(map[string]map[string]string) | ||
} | ||
|
||
func init() { | ||
filters.Add("histogram", func() telegraf.Filter { | ||
return &Histogram{ | ||
fieldMap: make(map[string]map[string]*gohistogram.NumericHistogram), | ||
metricTags: make(map[string]map[string]string), | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package filters | ||
|
||
import "github.com/influxdata/telegraf" | ||
|
||
type Creator func() telegraf.Filter | ||
|
||
var Filters = map[string]Creator{} | ||
|
||
func Add(name string, creator Creator) { | ||
Filters[name] = creator | ||
} |