Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(outputs.iotdb): Handle paths that contain illegal characters #14519

Merged
merged 12 commits into from
Jan 23, 2024
Merged
19 changes: 18 additions & 1 deletion plugins/outputs/iotdb/iotdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -229,6 +230,21 @@ func (s *IoTDB) convertMetricsToRecordsWithTags(metrics []telegraf.Metric) (*rec
return rwt, nil
}

// checks is the tag contains any IoTDB invalid characters
func validateTag(tag string) string {
// watch https://iotdb.apache.org/UserGuide/V0.13.x/Reference/Syntax-Conventions.html#identifiers for reference
// :@#${} have been excluded after some tests as they seem to not be supported
matchForbiddenCharacter, _ := regexp.Compile("[^0-9a-zA-Z_]")
// tags made only of int's should be supported
// but are still not deemed as valid
machNumericString, _ := regexp.Compile("^\\d+$")

if matchForbiddenCharacter.MatchString(tag) || machNumericString.MatchString(tag) {
return "`" + tag + "`"
}
return tag
}

// modify recordsWithTags according to 'TreatTagsAs' Configuration
func (s *IoTDB) modifyRecordsWithTags(rwt *recordsWithTags) error {
switch s.TreatTagsAs {
Expand All @@ -251,7 +267,8 @@ func (s *IoTDB) modifyRecordsWithTags(rwt *recordsWithTags) error {
for index, tags := range rwt.TagsList { // for each record
topic := []string{rwt.DeviceIDList[index]}
for _, tag := range tags { // for each tag, append it's Value
topic = append(topic, tag.Value)
tag_value := validateTag(tag.Value) // validates tag
topic = append(topic, tag_value)
}
rwt.DeviceIDList[index] = strings.Join(topic, ".")
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/outputs/iotdb/iotdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestTagsHandling(t *testing.T) {
name: "treat tags as device IDs",
plugin: func() *IoTDB { s := newIoTDB(); s.TreatTagsAs = "device_id"; return s }(),
expected: recordsWithTags{
DeviceIDList: []string{"root.computer.deviceID.cpu.expensive"},
DeviceIDList: []string{"root.computer.deviceID.cpu.expensive.`Adattatore Ethernet vmxnet3`"},
MeasurementsList: [][]string{{"temperature", "counter"}},
ValuesList: [][]interface{}{
{float64(42.55), int64(987654321)},
Expand All @@ -338,6 +338,7 @@ func TestTagsHandling(t *testing.T) {
TagsList: [][]*telegraf.Tag{{
{Key: "owner", Value: "cpu"},
{Key: "price", Value: "expensive"},
{Key: "netnic", Value: "Adattatore Ethernet vmxnet3"},
}},
},
},
Expand Down
Loading