|
| 1 | +package strings |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/influxdata/telegraf" |
| 7 | + "github.com/influxdata/telegraf/plugins/processors" |
| 8 | +) |
| 9 | + |
| 10 | +type Strings struct { |
| 11 | + Lowercase []converter |
| 12 | + Uppercase []converter |
| 13 | + Trim []converter |
| 14 | + TrimLeft []converter |
| 15 | + TrimRight []converter |
| 16 | + TrimPrefix []converter |
| 17 | + TrimSuffix []converter |
| 18 | +} |
| 19 | + |
| 20 | +type converter struct { |
| 21 | + Tag string |
| 22 | + Field string |
| 23 | + ResultKey string |
| 24 | + Argument string |
| 25 | +} |
| 26 | + |
| 27 | +const sampleConfig = ` |
| 28 | + ## Tag and field conversions defined in a separate sub-tables |
| 29 | +
|
| 30 | + # [[processors.strings.uppercase]] |
| 31 | + # tag = "method" |
| 32 | +
|
| 33 | + # [[processors.strings.lowercase]] |
| 34 | + # field = "uri_stem" |
| 35 | + # result_key = "uri_stem_normalised" |
| 36 | +` |
| 37 | + |
| 38 | +func (r *Strings) SampleConfig() string { |
| 39 | + return sampleConfig |
| 40 | +} |
| 41 | + |
| 42 | +func (r *Strings) Description() string { |
| 43 | + return "Transforms tag and field values to lower case" |
| 44 | +} |
| 45 | + |
| 46 | +func ApplyFunction( |
| 47 | + metric telegraf.Metric, |
| 48 | + c converter, |
| 49 | + fn func(string) string) { |
| 50 | + |
| 51 | + if value, ok := metric.Tags()[c.Tag]; ok { |
| 52 | + metric.AddTag( |
| 53 | + getKey(c), |
| 54 | + fn(value), |
| 55 | + ) |
| 56 | + } else if value, ok := metric.Fields()[c.Field]; ok { |
| 57 | + switch value := value.(type) { |
| 58 | + case string: |
| 59 | + metric.AddField( |
| 60 | + getKey(c), |
| 61 | + fn(value), |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (r *Strings) Apply(in ...telegraf.Metric) []telegraf.Metric { |
| 68 | + for _, metric := range in { |
| 69 | + for _, converter := range r.Lowercase { |
| 70 | + ApplyFunction(metric, converter, strings.ToLower) |
| 71 | + } |
| 72 | + for _, converter := range r.Uppercase { |
| 73 | + ApplyFunction(metric, converter, strings.ToUpper) |
| 74 | + } |
| 75 | + for _, converter := range r.Trim { |
| 76 | + ApplyFunction(metric, converter, |
| 77 | + func(s string) string { return strings.Trim(s, converter.Argument) }) |
| 78 | + } |
| 79 | + for _, converter := range r.TrimPrefix { |
| 80 | + ApplyFunction(metric, converter, |
| 81 | + func(s string) string { return strings.TrimPrefix(s, converter.Argument) }) |
| 82 | + } |
| 83 | + for _, converter := range r.TrimSuffix{ |
| 84 | + ApplyFunction(metric, converter, |
| 85 | + func(s string) string { return strings.TrimSuffix(s, converter.Argument) }) |
| 86 | + } |
| 87 | + for _, converter := range r.TrimRight { |
| 88 | + ApplyFunction(metric, converter, |
| 89 | + func(s string) string { return strings.TrimRight(s, converter.Argument) }) |
| 90 | + } |
| 91 | + for _, converter := range r.TrimLeft { |
| 92 | + ApplyFunction(metric, converter, |
| 93 | + func(s string) string { return strings.TrimLeft(s, converter.Argument) }) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return in |
| 98 | +} |
| 99 | + |
| 100 | +func getKey(c converter) string { |
| 101 | + if c.ResultKey != "" { |
| 102 | + return c.ResultKey |
| 103 | + } else if c.Field != "" { |
| 104 | + return c.Field |
| 105 | + } else { |
| 106 | + return c.Tag |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func init() { |
| 111 | + processors.Add("strings", func() telegraf.Processor { |
| 112 | + return &Strings{} |
| 113 | + }) |
| 114 | +} |
0 commit comments