Skip to content

Commit

Permalink
merge const labels of all probers
Browse files Browse the repository at this point in the history
  • Loading branch information
Xu Dongyan committed Aug 15, 2023
1 parent afb32cd commit edba6ff
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ func isProbe(t reflect.Type) bool {
// AllProbers return all probers
func (conf *Conf) AllProbers() []probe.Prober {
log.Debugf("--------- Process the probers settings ---------")
return allProbersHelper(*conf)
all := allProbersHelper(*conf)
MergeConstLabels(all)
return all
}

func allProbersHelper(i interface{}) []probe.Prober {
Expand Down
37 changes: 37 additions & 0 deletions conf/constlables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package conf

import (
"github.com/megaease/easeprobe/metric"
"github.com/megaease/easeprobe/probe"
)

var constLabels = make(map[string]bool)

// MergeConstLabels merge const labels from all probers.
// Prometheus requires all metric of the same name have the same set of labels in a collector
func MergeConstLabels(ps []probe.Prober) {
for _, p := range ps {
for _, k := range p.Label() {
constLabels[k.Name] = true
}
}

for _, p := range ps {
buildConstLabels(p)
}
}
func buildConstLabels(p probe.Prober) {
ls := p.Label()
LA:
for k, _ := range constLabels {
for _, l := range ls {
if k == l.Name {
continue LA
}
}

ls = append(ls, metric.Label{Name: k, Value: ""})
}

p.SetLabel(ls)
}
35 changes: 35 additions & 0 deletions conf/constlables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package conf

import (
"github.com/megaease/easeprobe/metric"
"github.com/megaease/easeprobe/probe"
"github.com/megaease/easeprobe/probe/base"
"github.com/megaease/easeprobe/probe/http"
"github.com/megaease/easeprobe/probe/tcp"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMergeConstLabels(t *testing.T) {

ps := []probe.Prober{
&http.HTTP{
DefaultProbe: base.DefaultProbe{
Labels: []metric.Label{{"service", "service_a"}},
},
},
&tcp.TCP{
DefaultProbe: base.DefaultProbe{
Labels: []metric.Label{{"host", "host_b"}},
},
},
}

MergeConstLabels(ps)

assert.Equal(t, "service_a", ps[0].Label()[0].Value)
assert.Equal(t, "host_b", ps[0].Label()[1].Value)

assert.Equal(t, "host_a", ps[1].Label()[0].Value)
assert.Equal(t, "service_b", ps[1].Label()[1].Value)
}
8 changes: 8 additions & 0 deletions probe/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ type DefaultProbe struct {
metrics *metrics `yaml:"-" json:"-"`
}

func (d *DefaultProbe) Label() []metric.Label {
return d.Labels
}

func (d *DefaultProbe) SetLabel(allLabels []metric.Label) {
d.Labels = allLabels
}

// Kind return the probe kind
func (d *DefaultProbe) Kind() string {
return d.ProbeKind
Expand Down
3 changes: 3 additions & 0 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import (
"time"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/metric"
)

// Prober Interface
type Prober interface {
Label() []metric.Label
SetLabel([]metric.Label)
Kind() string
Name() string
Channels() []string
Expand Down

0 comments on commit edba6ff

Please sign in to comment.