Skip to content

Commit

Permalink
fix relabel-config error in loki client (#664)
Browse files Browse the repository at this point in the history
* fix relabel-config error in loki client
* add more check
* final commit
  • Loading branch information
dmachard authored Mar 29, 2024
1 parent 5e42732 commit 0dd545a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkgutils/configchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -85,6 +87,19 @@ 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
// avoid this type of exception in future ....
defaultConfig.Loggers.LokiClient.RelabelConfigs = append(defaultConfig.Loggers.LokiClient.RelabelConfigs,
&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
defaultConfigMap, err := convertConfigToMap(defaultConfig)
Expand Down
46 changes: 46 additions & 0 deletions pkgutils/configchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,49 @@ 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"
replacement: "test"
action: "update"
separator: ","
regex: "test"
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)
}
}

0 comments on commit 0dd545a

Please sign in to comment.