Skip to content

Commit

Permalink
Merge branch 'feat-allow-overwrite-default-relabelings' of https://gi…
Browse files Browse the repository at this point in the history
…thub.com/tangxinfa/prometheus-nginxlog-exporter into tangxinfa-feat-allow-overwrite-default-relabelings
  • Loading branch information
martin-helmich committed Apr 26, 2020
2 parents b585c44 + bd0c337 commit e2dae47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions relabeling/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e2dae47

Please sign in to comment.