From 0ab458a12b07a1b6751ee77b416d9ad93bf7a4e4 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:14:23 +0100 Subject: [PATCH 1/3] fix relabel-config error in loki client --- pkgutils/configchecker.go | 8 +++++++ pkgutils/configchecker_test.go | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkgutils/configchecker.go b/pkgutils/configchecker.go index f4fa2121..25307dd6 100644 --- a/pkgutils/configchecker.go +++ b/pkgutils/configchecker.go @@ -11,6 +11,8 @@ import ( "github.com/dmachard/go-dnscollector/dnsutils" "github.com/dmachard/go-dnscollector/pkgconfig" "github.com/pkg/errors" + "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/model/relabel" "gopkg.in/yaml.v3" ) @@ -85,6 +87,12 @@ func CheckConfig(userConfigPath string, dmRef dnsutils.DNSMessage) error { defaultConfig.IngoingTransformers.Relabeling.Remove = append(defaultConfig.IngoingTransformers.Relabeling.Remove, pkgconfig.RelabelingConfig{}) defaultConfig.IngoingTransformers.Relabeling.Rename = append(defaultConfig.IngoingTransformers.Relabeling.Rename, pkgconfig.RelabelingConfig{}) + // add fake value in relabel.Config because yaml tag is set to omitempty + defaultConfig.Loggers.LokiClient.RelabelConfigs = append(defaultConfig.Loggers.LokiClient.RelabelConfigs, + &relabel.Config{TargetLabel: "default", + SourceLabels: []model.LabelName{"test"}, + Separator: ",", Replacement: "test"}) + // Convert default config to map // And get unique YAML keys defaultConfigMap, err := convertConfigToMap(defaultConfig) diff --git a/pkgutils/configchecker_test.go b/pkgutils/configchecker_test.go index 33130427..856002f1 100644 --- a/pkgutils/configchecker_test.go +++ b/pkgutils/configchecker_test.go @@ -356,3 +356,45 @@ pipelines: t.Errorf("Expected error, but got nil") } } + +// test for issue https://github.com/dmachard/go-dnscollector/issues/643 +func TestConfig_CheckConfig_SpecificsItems(t *testing.T) { + userConfigFile, err := os.CreateTemp("", "user-config.yaml") + if err != nil { + t.Fatal("Error creating temporary file:", err) + } + defer os.Remove(userConfigFile.Name()) + defer userConfigFile.Close() + + userConfigContent := ` +multiplexer: + collectors: + - name: tap + dnstap: + listen-ip: 0.0.0.0 + listen-port: 6000 + loggers: + - name: loki + lokiclient: + server-url: "https://grafana-loki.example.com/loki/api/v1/push" + job-name: "dnscollector" + mode: "flat-json" + tls-insecure: true + tenant-id: fake + relabel-configs: + - source_labels: ["__dns_qtype"] + target_label: "qtype" + routes: + - from: [ tap ] + to: [ loki ] +` + err = os.WriteFile(userConfigFile.Name(), []byte(userConfigContent), 0644) + if err != nil { + t.Fatal("Error writing to user configuration file:", err) + } + + dm := dnsutils.GetReferenceDNSMessage() + if err := CheckConfig(userConfigFile.Name(), dm); err != nil { + t.Errorf("failed: Unexpected error: %v", err) + } +} From 494244ce492217f8a61e909b75dc216e187fca07 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:20:22 +0100 Subject: [PATCH 2/3] add more check --- pkgutils/configchecker.go | 12 +++++++++--- pkgutils/configchecker_test.go | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkgutils/configchecker.go b/pkgutils/configchecker.go index 25307dd6..121616a4 100644 --- a/pkgutils/configchecker.go +++ b/pkgutils/configchecker.go @@ -89,9 +89,15 @@ func CheckConfig(userConfigPath string, dmRef dnsutils.DNSMessage) error { // add fake value in relabel.Config because yaml tag is set to omitempty defaultConfig.Loggers.LokiClient.RelabelConfigs = append(defaultConfig.Loggers.LokiClient.RelabelConfigs, - &relabel.Config{TargetLabel: "default", - SourceLabels: []model.LabelName{"test"}, - Separator: ",", Replacement: "test"}) + &relabel.Config{ + TargetLabel: "target", + SourceLabels: []model.LabelName{"source"}, + Separator: "separator", + Replacement: "replacement", + Action: relabel.Action("action"), + Regex: relabel.Regexp{}, + }, + ) // Convert default config to map // And get unique YAML keys diff --git a/pkgutils/configchecker_test.go b/pkgutils/configchecker_test.go index 856002f1..8019e834 100644 --- a/pkgutils/configchecker_test.go +++ b/pkgutils/configchecker_test.go @@ -384,6 +384,10 @@ multiplexer: relabel-configs: - source_labels: ["__dns_qtype"] target_label: "qtype" + replacement: "test" + action: "update" + separator: "," + regex: "test" routes: - from: [ tap ] to: [ loki ] From 9afdf2fa8395525adb5bd7682f69504087b3174e Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:20:48 +0100 Subject: [PATCH 3/3] final commit --- pkgutils/configchecker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgutils/configchecker.go b/pkgutils/configchecker.go index 121616a4..23863063 100644 --- a/pkgutils/configchecker.go +++ b/pkgutils/configchecker.go @@ -88,6 +88,7 @@ func CheckConfig(userConfigPath string, dmRef dnsutils.DNSMessage) error { defaultConfig.IngoingTransformers.Relabeling.Rename = append(defaultConfig.IngoingTransformers.Relabeling.Rename, pkgconfig.RelabelingConfig{}) // add fake value in relabel.Config because yaml tag is set to omitempty + // avoid this type of exception in future .... defaultConfig.Loggers.LokiClient.RelabelConfigs = append(defaultConfig.Loggers.LokiClient.RelabelConfigs, &relabel.Config{ TargetLabel: "target",