Use operation subtables in enum and rename processors (#4672)
This commit is contained in:
@@ -13,13 +13,13 @@ source field is overwritten.
|
||||
|
||||
```toml
|
||||
[[processors.enum]]
|
||||
[[processors.enum.fields]]
|
||||
[[processors.enum.mapping]]
|
||||
## Name of the field to map
|
||||
source = "name"
|
||||
field = "status"
|
||||
|
||||
## Destination field to be used for the mapped value. By default the source
|
||||
## field is used, overwriting the original value.
|
||||
# destination = "mapped"
|
||||
# dest = "status_code"
|
||||
|
||||
## Default value to be used for all values not contained in the mapping
|
||||
## table. When unset, the unmodified value for the field will be used if no
|
||||
@@ -27,7 +27,15 @@ source field is overwritten.
|
||||
# default = 0
|
||||
|
||||
## Table of mappings
|
||||
[processors.enum.fields.value_mappings]
|
||||
value1 = 1
|
||||
value2 = 2
|
||||
[processors.enum.mapping.value_mappings]
|
||||
green = 1
|
||||
amber = 2
|
||||
red = 3
|
||||
```
|
||||
|
||||
### Example:
|
||||
|
||||
```diff
|
||||
- xyzzy status="green" 1502489900000000000
|
||||
+ xyzzy status="green",status_code=1i 1502489900000000000
|
||||
```
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
)
|
||||
|
||||
var sampleConfig = `
|
||||
[[processors.enum.fields]]
|
||||
[[processors.enum.mapping]]
|
||||
## Name of the field to map
|
||||
source = "name"
|
||||
field = "status"
|
||||
|
||||
## Destination field to be used for the mapped value. By default the source
|
||||
## field is used, overwriting the original value.
|
||||
# destination = "mapped"
|
||||
# dest = "status_code"
|
||||
|
||||
## Default value to be used for all values not contained in the mapping
|
||||
## table. When unset, the unmodified value for the field will be used if no
|
||||
@@ -22,18 +22,19 @@ var sampleConfig = `
|
||||
# default = 0
|
||||
|
||||
## Table of mappings
|
||||
[processors.enum.fields.value_mappings]
|
||||
value1 = 1
|
||||
value2 = 2
|
||||
[processors.enum.mapping.value_mappings]
|
||||
green = 1
|
||||
yellow = 2
|
||||
red = 3
|
||||
`
|
||||
|
||||
type EnumMapper struct {
|
||||
Fields []Mapping
|
||||
Mappings []Mapping `toml:"mapping"`
|
||||
}
|
||||
|
||||
type Mapping struct {
|
||||
Source string
|
||||
Destination string
|
||||
Field string
|
||||
Dest string
|
||||
Default interface{}
|
||||
ValueMappings map[string]interface{}
|
||||
}
|
||||
@@ -54,8 +55,8 @@ func (mapper *EnumMapper) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
||||
}
|
||||
|
||||
func (mapper *EnumMapper) applyMappings(metric telegraf.Metric) telegraf.Metric {
|
||||
for _, mapping := range mapper.Fields {
|
||||
if originalValue, isPresent := metric.GetField(mapping.Source); isPresent == true {
|
||||
for _, mapping := range mapper.Mappings {
|
||||
if originalValue, isPresent := metric.GetField(mapping.Field); isPresent == true {
|
||||
if adjustedValue, isString := adjustBoolValue(originalValue).(string); isString == true {
|
||||
if mappedValue, isMappedValuePresent := mapping.mapValue(adjustedValue); isMappedValuePresent == true {
|
||||
writeField(metric, mapping.getDestination(), mappedValue)
|
||||
@@ -84,16 +85,14 @@ func (mapping *Mapping) mapValue(original string) (interface{}, bool) {
|
||||
}
|
||||
|
||||
func (mapping *Mapping) getDestination() string {
|
||||
if mapping.Destination != "" {
|
||||
return mapping.Destination
|
||||
if mapping.Dest != "" {
|
||||
return mapping.Dest
|
||||
}
|
||||
return mapping.Source
|
||||
return mapping.Field
|
||||
}
|
||||
|
||||
func writeField(metric telegraf.Metric, name string, value interface{}) {
|
||||
if metric.HasField(name) {
|
||||
metric.RemoveField(name)
|
||||
}
|
||||
metric.RemoveField(name)
|
||||
metric.AddField(name, value)
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestRetainsMetric(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapsSingleStringValue(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "string_value", ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestMapsSingleStringValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNoFailureOnMappingsOnNonStringValuedFields(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "int_value", ValueMappings: map[string]interface{}{"13i": int64(7)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "int_value", ValueMappings: map[string]interface{}{"13i": int64(7)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestNoFailureOnMappingsOnNonStringValuedFields(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapSingleBoolValue(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "true_value", ValueMappings: map[string]interface{}{"true": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "true_value", ValueMappings: map[string]interface{}{"true": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestMapSingleBoolValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapsToDefaultValueOnUnknownSourceValue(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "string_value", Default: int64(42), ValueMappings: map[string]interface{}{"other": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", Default: int64(42), ValueMappings: map[string]interface{}{"other": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -81,7 +81,7 @@ func TestMapsToDefaultValueOnUnknownSourceValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDoNotMapToDefaultValueKnownSourceValue(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "string_value", Default: int64(42), ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", Default: int64(42), ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -89,7 +89,7 @@ func TestDoNotMapToDefaultValueKnownSourceValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNoMappingWithoutDefaultOrDefinedMappingValue(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "string_value", ValueMappings: map[string]interface{}{"other": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", ValueMappings: map[string]interface{}{"other": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestNoMappingWithoutDefaultOrDefinedMappingValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWritesToDestination(t *testing.T) {
|
||||
mapper := EnumMapper{Fields: []Mapping{{Source: "string_value", Destination: "string_code", ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
mapper := EnumMapper{Mappings: []Mapping{{Field: "string_value", Dest: "string_code", ValueMappings: map[string]interface{}{"test": int64(1)}}}}
|
||||
|
||||
fields := calculateProcessedValues(mapper, createTestMetric())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user