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

fix relabel-config error in loki client #664

Merged
merged 3 commits into from
Mar 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading