-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Xu Dongyan
committed
Aug 15, 2023
1 parent
afb32cd
commit edba6ff
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters