fix(outputs.mqtt): Preserve leading slash in topic (#14582)

This commit is contained in:
Yuan Zhang 2024-01-19 01:30:23 +08:00 committed by GitHub
parent 9aee2681bb
commit 7cafdaf2ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -908,6 +908,11 @@ func TestGenerateTopicName(t *testing.T) {
pattern: "double//slashes//are//ignored",
want: "double/slashes/are/ignored",
},
{
name: "preserve leading forward slash",
pattern: "/this/is/a/topic",
want: "/this/is/a/topic",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -54,5 +54,8 @@ func (t *TopicNameGenerator) Generate(hostname string, m telegraf.Metric) (strin
if topic == "" {
return m.Name(), nil
}
if strings.HasPrefix(b.String(), "/") {
topic = "/" + topic
}
return topic, nil
}