Remove trailing backslash from tag keys/values (#7652)
This commit is contained in:
parent
777ca44d45
commit
643fb7decc
|
|
@ -84,4 +84,9 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v1.x] HTTP or UDP ser
|
||||||
# influx_uint_support = false
|
# influx_uint_support = false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Metrics
|
||||||
|

|
||||||
|
Reference the [influx serializer][] for details about metric production.
|
||||||
|

|
||||||
[InfluxDB v1.x]: https://github.com/influxdata/influxdb
|
[InfluxDB v1.x]: https://github.com/influxdata/influxdb
|
||||||
|
[influx serializer]: /plugins/serializers/influx/README.md#Metrics
|
||||||
|
|
|
||||||
|
|
@ -58,4 +58,9 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v2.x] HTTP service.
|
||||||
# insecure_skip_verify = false
|
# insecure_skip_verify = false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Metrics
|
||||||
|

|
||||||
|
Reference the [influx serializer][] for details about metric production.
|
||||||
|
|
||||||
[InfluxDB v2.x]: https://github.com/influxdata/influxdb
|
[InfluxDB v2.x]: https://github.com/influxdata/influxdb
|
||||||
|
[influx serializer]: /plugins/serializers/influx/README.md#Metrics
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ protocol]. This is the recommended format unless another format is required
|
||||||
for interoperability.
|
for interoperability.
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[[outputs.file]]
|
[[outputs.file]]
|
||||||
## Files to write to, "stdout" is a specially handled file.
|
## Files to write to, "stdout" is a specially handled file.
|
||||||
|
|
@ -31,4 +32,13 @@ for interoperability.
|
||||||
influx_uint_support = false
|
influx_uint_support = false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Metrics
|
||||||
|
|
||||||
|
Conversion is direct taking into account some limitations of the Line Protocol
|
||||||
|
format:
|
||||||
|
- Float fields that are `NaN` or `Inf` are skipped.
|
||||||
|
- Trailing backslash `\` characters are removed from tag keys and values.
|
||||||
|
- Tags with a key or value that is the empty string are skipped.
|
||||||
|
- When not using `influx_uint_support`, unsigned integers are capped at the max int64.
|
||||||
|
|
||||||
[line protocol]: https://docs.influxdata.com/influxdb/latest/write_protocols/line_protocol_tutorial/
|
[line protocol]: https://docs.influxdata.com/influxdb/latest/write_protocols/line_protocol_tutorial/
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
)
|
)
|
||||||
|
|
@ -154,8 +155,16 @@ func (s *Serializer) buildHeader(m telegraf.Metric) error {
|
||||||
key := escape(tag.Key)
|
key := escape(tag.Key)
|
||||||
value := escape(tag.Value)
|
value := escape(tag.Value)
|
||||||
|
|
||||||
// Some keys and values are not encodeable as line protocol, such as
|
// Tag keys and values that end with a backslash cannot be encoded by
|
||||||
// those with a trailing '\' or empty strings.
|
// line protocol.
|
||||||
|
if strings.HasSuffix(key, `\`) {
|
||||||
|
key = strings.TrimRight(key, `\`)
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(value, `\`) {
|
||||||
|
value = strings.TrimRight(value, `\`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tag keys and values must not be the empty string.
|
||||||
if key == "" || value == "" {
|
if key == "" || value == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -323,6 +323,102 @@ var tests = []struct {
|
||||||
),
|
),
|
||||||
output: []byte("cpu,host=x\\ny value=42i 0\n"),
|
output: []byte("cpu,host=x\\ny value=42i 0\n"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "empty tag value is removed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"cpu",
|
||||||
|
map[string]string{
|
||||||
|
"host": "",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("cpu value=42i 0\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty tag key is removed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"cpu",
|
||||||
|
map[string]string{
|
||||||
|
"": "example.org",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("cpu value=42i 0\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag value ends with backslash is trimmed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"disk",
|
||||||
|
map[string]string{
|
||||||
|
"path": `C:\`,
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("disk,path=C: value=42i 0\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag key ends with backslash is trimmed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"disk",
|
||||||
|
map[string]string{
|
||||||
|
`path\`: "/",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("disk,path=/ value=42i 0\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag key backslash is trimmed and removed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"disk",
|
||||||
|
map[string]string{
|
||||||
|
`\`: "example.org",
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("disk value=42i 0\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tag value backslash is trimmed and removed",
|
||||||
|
input: MustMetric(
|
||||||
|
metric.New(
|
||||||
|
"disk",
|
||||||
|
map[string]string{
|
||||||
|
"host": `\`,
|
||||||
|
},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 42,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
output: []byte("disk value=42i 0\n"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "string newline",
|
name: "string newline",
|
||||||
input: MustMetric(
|
input: MustMetric(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue