diff --git a/main.go b/main.go index 81b2327..c436f4c 100644 --- a/main.go +++ b/main.go @@ -50,20 +50,31 @@ type Metrics struct { parseErrorsTotal prometheus.Counter } +func inLabels(label string, labels []string) bool { + for _, l := range labels { + if label == l { + return true + } + } + return false +} + // Init initializes a metrics struct func (m *Metrics) Init(cfg *config.NamespaceConfig) { cfg.MustCompile() labels := cfg.OrderedLabelNames - for _, r := range relabeling.DefaultRelabelings { - labels = append(labels, r.TargetLabel) - } - for i := range cfg.RelabelConfigs { labels = append(labels, cfg.RelabelConfigs[i].TargetLabel) } + for _, r := range relabeling.DefaultRelabelings { + if !inLabels(r.TargetLabel, labels) { + labels = append(labels, r.TargetLabel) + } + } + m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: cfg.Name, Name: "http_response_count_total", @@ -283,7 +294,8 @@ func processNamespace(nsCfg config.NamespaceConfig) { func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser *gonx.Parser, metrics *Metrics) { relabelings := relabeling.NewRelabelings(nsCfg.RelabelConfigs) - relabelings = append(relabeling.DefaultRelabelings, relabelings...) + relabelings = append(relabelings, relabeling.DefaultRelabelings...) + relabelings = relabeling.UniqueRelabelings(relabelings) staticLabelValues := nsCfg.OrderedLabelValues diff --git a/relabeling/types.go b/relabeling/types.go index 834fedc..c052520 100644 --- a/relabeling/types.go +++ b/relabeling/types.go @@ -24,3 +24,17 @@ func NewRelabelings(cfgs []config.RelabelConfig) []*Relabeling { func NewRelabeling(cfg *config.RelabelConfig) *Relabeling { return &Relabeling{*cfg} } + +// UniqueRelabelings creates a unique relabelings, the duplicated one at the end will discard. +func UniqueRelabelings(relabelings []*Relabeling) []*Relabeling { + result := make([]*Relabeling, 0, len(relabelings)) + found := make(map[string]struct{}) + for _, r := range relabelings { + if _, ok := found[r.TargetLabel]; ok { + continue + } + found[r.TargetLabel] = struct{}{} + result = append(result, r) + } + return result +}