Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions plugins/outputs/sematext/processors/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ func NewRename() BatchProcessor { return &Rename{} }
func (r *Rename) Process(points []telegraf.Metric) []telegraf.Metric {
for _, point := range points {
originalName := point.Name()

// Fix corrupted measurement names (e.g., "apache.apache.apache" → "apache")
originalName = deduplicatePrefix(originalName)

replace, ok := measurementReplaces[originalName]
if !ok {
replace = ChangeNames(originalName)
Expand Down Expand Up @@ -275,3 +279,27 @@ func ChangeNames(name string) string {
}

func (Rename) Close() {}

// deduplicatePrefix fixes recursive prefix corruption.
// Example: "apache.apache.apache" → "apache"
func deduplicatePrefix(name string) string {
parts := strings.Split(name, ".")
if len(parts) < 2 {
return name
}

prefix := parts[0]
count := 0
for count < len(parts) && parts[count] == prefix {
count++
}

if count > 1 {
remaining := parts[count:]
if len(remaining) > 0 {
return prefix + "." + strings.Join(remaining, ".")
}
return prefix
}
return name
}
43 changes: 43 additions & 0 deletions plugins/outputs/sematext/processors/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,49 @@ func TestRename(t *testing.T) {
assert.Equal(t, "mongodb_shard_stats.in_use", results[8].FieldList()[0].Key)
}

func TestDeduplicatePrefix(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"apache", "apache"},
{"apache.apache", "apache"},
{"apache.apache.apache", "apache"},
{"apache.apache.apache.apache", "apache"},
{"nginx", "nginx"},
{"nginx.nginx", "nginx"},
{"nginx.nginx.nginx", "nginx"},
{"mongodb", "mongodb"},
{"mongodb.mongodb.mongodb", "mongodb"},
// Edge cases
{"", ""},
{"a", "a"},
{"a.b", "a.b"},
{"a.a.b", "a.b"},
{"foo.foo.bar.baz", "foo.bar.baz"},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := deduplicatePrefix(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}

func TestRenameWithCorruptedMeasurementName(t *testing.T) {
r := Rename{}

// Simulate corrupted measurement name with 64+ repeated prefixes
corruptedName := "apache.apache.apache.apache.apache"
m := newMetric(corruptedName, nil, map[string]interface{}{"BusyWorkers": 10})

results := r.Process([]telegraf.Metric{m})

assert.Equal(t, "apache", results[0].Name())
assert.Equal(t, "workers.busy", results[0].FieldList()[0].Key)
}

func newMetric(name string, tags map[string]string, fields map[string]interface{}) telegraf.Metric {
if tags == nil {
tags = map[string]string{}
Expand Down