Skip to content

Commit

Permalink
feat: allow overwrite the default relabelings.
Browse files Browse the repository at this point in the history
The "method" label in default relabelings may incorrect if we not use "$request"
in the nginx log_format, but already separated to "$request_method" and
"$request_uri", allow user to overwrite default relabelings to fix it.
  • Loading branch information
tangxinfa committed Jul 15, 2019
1 parent c625bef commit bd0c337
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 @@ -47,20 +47,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 @@ -249,7 +260,8 @@ func processNamespace(nsCfg config.NamespaceConfig) {

func processSourceFile(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 bd0c337

Please sign in to comment.