-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package loki | ||
|
||
import ( | ||
"log/slog" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func filterLabelFormat(labels map[string]string) { | ||
|
||
for key, value := range labels { | ||
|
||
if stripped := stripLabelKey(key); stripped == "" { | ||
|
||
slog.Error("FORWARDER: LOKI: LABEL FILTER: Label removed (illformed key)", | ||
slog.String("key", key)) | ||
|
||
delete(labels, key) | ||
continue | ||
|
||
} else if stripped != key { | ||
|
||
slog.Warn("FORWARDER: LOKI: LABEL FILTER: Label moved (illformed key)", | ||
slog.String("key", key), | ||
slog.String("new_key", stripped)) | ||
|
||
delete(labels, key) | ||
key = stripped | ||
labels[key] = value | ||
} | ||
|
||
if stripped := stripLabelValue(value); stripped == "" { | ||
|
||
slog.Error("FORWARDER: LOKI: LABEL FILTER: Label removed (stripped value is empty)", | ||
slog.String("key", key)) | ||
|
||
delete(labels, key) | ||
|
||
} else if stripped != value { | ||
|
||
slog.Warn("FORWARDER: LOKI: LABEL FILTER: Label value stripped", | ||
slog.String("key", key)) | ||
|
||
labels[key] = stripped | ||
} | ||
} | ||
} | ||
|
||
func stripLabelKey(key string) string { | ||
|
||
var stripped string | ||
|
||
for _, next := range key { | ||
|
||
switch next { | ||
case '_', '-', '+', '*', '=': | ||
stripped += "_" | ||
continue | ||
} | ||
|
||
switch { | ||
case | ||
(next >= 'A' && next <= 'Z'), | ||
(next >= 'a' && next <= 'z'), | ||
(next >= '0' && next <= '9'): | ||
stripped += string(next) | ||
continue | ||
} | ||
} | ||
|
||
return strings.TrimSpace(stripped) | ||
} | ||
|
||
func stripLabelValue(key string) string { | ||
|
||
var stripped string | ||
|
||
for _, next := range key { | ||
|
||
switch { | ||
case next == '\\': | ||
stripped += "/" | ||
case unicode.IsPrint(next): | ||
stripped += string(next) | ||
default: | ||
stripped += "?" | ||
} | ||
} | ||
|
||
return stripped | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters