Remove trailing backslash from tag keys/values (#7652)

This commit is contained in:
Daniel Nelson 2020-06-12 17:54:49 -07:00 committed by GitHub
parent 777ca44d45
commit 643fb7decc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 2 deletions

View File

@ -84,4 +84,9 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v1.x] HTTP or UDP ser
# influx_uint_support = false
```
### Metrics
Reference the [influx serializer][] for details about metric production.
[InfluxDB v1.x]: https://github.com/influxdata/influxdb
[influx serializer]: /plugins/serializers/influx/README.md#Metrics

View File

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

View File

@ -5,6 +5,7 @@ protocol]. This is the recommended format unless another format is required
for interoperability.
### Configuration
```toml
[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
@ -31,4 +32,13 @@ for interoperability.
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/

View File

@ -8,6 +8,7 @@ import (
"math"
"sort"
"strconv"
"strings"
"github.com/influxdata/telegraf"
)
@ -154,8 +155,16 @@ func (s *Serializer) buildHeader(m telegraf.Metric) error {
key := escape(tag.Key)
value := escape(tag.Value)
// Some keys and values are not encodeable as line protocol, such as
// those with a trailing '\' or empty strings.
// Tag keys and values that end with a backslash cannot be encoded by
// 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 == "" {
continue
}

View File

@ -323,6 +323,102 @@ var tests = []struct {
),
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",
input: MustMetric(