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

LOG-4095: fix labels with more then one dot #2058

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/generator/vector/output/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ func lokiLabels(lo *logging.Loki) []Label {

func formatLokiLabelValue(value string) string {
if strings.HasPrefix(value, "kubernetes.labels.") || strings.HasPrefix(value, "kubernetes.namespace_labels.") {
parts := strings.SplitAfterN(value, ".", 2)
parts := strings.SplitAfterN(value, "labels.", 2)
key := strings.ReplaceAll(parts[1], "/", "_")
key = strings.ReplaceAll(key, ".", "_")
value = fmt.Sprintf("%s%s", parts[0], key)
}
return fmt.Sprintf("{{%s}}", value)
Expand Down
22 changes: 22 additions & 0 deletions internal/generator/vector/output/loki/loki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package loki

import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)

var _ = Describe("Loki generator helpers", func() {

DescribeTable("#formatLokiLabelValue should correctly format", func(label, exp string) {
Expect(fmt.Sprintf("{{%s}}", exp)).To(Equal(formatLokiLabelValue(label)))
},
Entry(" for a complex label", "kubernetes.labels.app.kubernetes.io/name", "kubernetes.labels.app_kubernetes_io_name"),
Entry(" for a label with only a slash", "kubernetes.labels.foo/bar", "kubernetes.labels.foo_bar"),
Entry(" for a namespacelabel with only a slash", "kubernetes.namespace_labels.foo/bar", "kubernetes.namespace_labels.foo_bar"),
Entry(" for a simple label", "kubernetes.labels.foo", "kubernetes.labels.foo"),
Entry(" for a label without kubernetes.labels prefix", "kubernetes.host", "kubernetes.host"),
)

})
6 changes: 3 additions & 3 deletions test/functional/outputs/loki/application_logs_vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ var _ = Describe("[Functional][Outputs][Loki] Forwarding to Loki", func() {
Context("when label keys are defined that include slashes. Ref LOG-4095", func() {
const myValue = "foobarvalue"
BeforeEach(func() {
f.Labels["foo/bar"] = myValue
f.Labels["app.kubernetes.io/name"] = myValue
f.Forwarder.Spec.Outputs[0].Loki.LabelKeys = []string{
"kubernetes.namespace_name",
"kubernetes.pod_name",
"kubernetes.labels.foo/bar",
"kubernetes.labels.app.kubernetes.io/name",
}
Expect(f.Deploy()).To(BeNil())
})
Expand All @@ -98,7 +98,7 @@ var _ = Describe("[Functional][Outputs][Loki] Forwarding to Loki", func() {
msg := functional.NewFullCRIOLogMessage(tsNow, "Present days")
Expect(f.WriteMessagesToApplicationLog(msg, 1)).To(Succeed())

query := fmt.Sprintf(`{kubernetes_labels_foo_bar=%q}`, myValue)
query := fmt.Sprintf(`{kubernetes_labels_app_kubernetes_io_name=%q}`, myValue)
result, err := l.QueryUntil(query, "", 1)
Expect(err).To(BeNil())
Expect(result).NotTo(BeNil())
Expand Down