|
| 1 | +package valuecounter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/influxdata/telegraf" |
| 8 | + "github.com/influxdata/telegraf/plugins/aggregators" |
| 9 | +) |
| 10 | + |
| 11 | +type aggregate struct { |
| 12 | + name string |
| 13 | + tags map[string]string |
| 14 | + fieldCount map[string]int |
| 15 | +} |
| 16 | + |
| 17 | +// ValueCounter an aggregation plugin |
| 18 | +type ValueCounter struct { |
| 19 | + cache map[uint64]aggregate |
| 20 | + Fields []string |
| 21 | +} |
| 22 | + |
| 23 | +// NewValueCounter create a new aggregation plugin which counts the occurances |
| 24 | +// of fields and emits the count. |
| 25 | +func NewValueCounter() telegraf.Aggregator { |
| 26 | + vc := &ValueCounter{} |
| 27 | + vc.Reset() |
| 28 | + return vc |
| 29 | +} |
| 30 | + |
| 31 | +var sampleConfig = ` |
| 32 | + ## General Aggregator Arguments: |
| 33 | + ## The period on which to flush & clear the aggregator. |
| 34 | + period = "30s" |
| 35 | + ## If true, the original metric will be dropped by the |
| 36 | + ## aggregator and will not get sent to the output plugins. |
| 37 | + drop_original = false |
| 38 | + ## The fields for which the values will be counted |
| 39 | + fields = [] |
| 40 | +` |
| 41 | + |
| 42 | +// SampleConfig generates a sample config for the ValueCounter plugin |
| 43 | +func (vc *ValueCounter) SampleConfig() string { |
| 44 | + return sampleConfig |
| 45 | +} |
| 46 | + |
| 47 | +// Description returns the description of the ValueCounter plugin |
| 48 | +func (vc *ValueCounter) Description() string { |
| 49 | + return "Count the occurance of values in fields." |
| 50 | +} |
| 51 | + |
| 52 | +// Add is run on every metric which passes the plugin |
| 53 | +func (vc *ValueCounter) Add(in telegraf.Metric) { |
| 54 | + id := in.HashID() |
| 55 | + |
| 56 | + // Check if the cache already has an entry for this metric, if not create it |
| 57 | + if _, ok := vc.cache[id]; !ok { |
| 58 | + a := aggregate{ |
| 59 | + name: in.Name(), |
| 60 | + tags: in.Tags(), |
| 61 | + fieldCount: make(map[string]int), |
| 62 | + } |
| 63 | + vc.cache[id] = a |
| 64 | + } |
| 65 | + |
| 66 | + // Check if this metric has fields which we need to count, if so increment |
| 67 | + // the count. |
| 68 | + for fk, fv := range in.Fields() { |
| 69 | + for _, cf := range vc.Fields { |
| 70 | + if fk == cf { |
| 71 | + // Do not process float types to prevent memory from blowing up |
| 72 | + switch fv.(type) { |
| 73 | + default: |
| 74 | + log.Printf("I! Valuecounter: Unsupported field type. " + |
| 75 | + "Must be an int, string or bool. Ignoring.") |
| 76 | + continue |
| 77 | + case uint64, int64, string, bool: |
| 78 | + } |
| 79 | + fn := fmt.Sprintf("%v_%v", fk, fv) |
| 80 | + vc.cache[id].fieldCount[fn]++ |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Push emits the counters |
| 87 | +func (vc *ValueCounter) Push(acc telegraf.Accumulator) { |
| 88 | + for _, agg := range vc.cache { |
| 89 | + fields := map[string]interface{}{} |
| 90 | + |
| 91 | + for field, count := range agg.fieldCount { |
| 92 | + fields[field] = count |
| 93 | + } |
| 94 | + |
| 95 | + acc.AddFields(agg.name, fields, agg.tags) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Reset the cache, executed after each push |
| 100 | +func (vc *ValueCounter) Reset() { |
| 101 | + vc.cache = make(map[uint64]aggregate) |
| 102 | +} |
| 103 | + |
| 104 | +func init() { |
| 105 | + aggregators.Add("valuecounter", func() telegraf.Aggregator { |
| 106 | + return NewValueCounter() |
| 107 | + }) |
| 108 | +} |
0 commit comments