Skip to content

Commit

Permalink
LOG-4095: fix labels with more then one dot
Browse files Browse the repository at this point in the history
  • Loading branch information
jcantrill committed Jun 14, 2023
1 parent da21570 commit da1257a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit da1257a

Please sign in to comment.