Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add constant labels for the metrics (#385) #386

Merged
merged 20 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove redundent define of Labels
  • Loading branch information
Xu Dongyan committed Aug 16, 2023
commit 999da10736f6ca021b03707e06ba35863cd0e214
6 changes: 3 additions & 3 deletions conf/constlables_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -15,12 +15,12 @@ func TestMergeConstLabels(t *testing.T) {
ps := []probe.Prober{
&http.HTTP{
DefaultProbe: base.DefaultProbe{
Labels: metric.LabelMap{"service": "service_a"},
Labels: prometheus.Labels{"service": "service_a"},
},
},
&tcp.TCP{
DefaultProbe: base.DefaultProbe{
Labels: metric.LabelMap{"host": "host_b"},
Labels: prometheus.Labels{"host": "host_b"},
},
},
}
Expand Down
10 changes: 4 additions & 6 deletions metric/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
*prometheus.CounterVec | *prometheus.GaugeVec | *prometheus.HistogramVec | *prometheus.SummaryVec
}

type LabelMap map[string]string

var (
registries = make([]*prometheus.Registry, 0)
counterMap = make(map[string]*prometheus.CounterVec)
Expand All @@ -60,7 +58,7 @@

// NewCounter create the counter metric
func NewCounter(namespace, subsystem, name, metric string,
help string, labels []string, constLabels LabelMap) *prometheus.CounterVec {
help string, labels []string, constLabels prometheus.Labels) *prometheus.CounterVec {

metricName, err := getAndValid(namespace, subsystem, name, metric, labels)
if err != nil {
Expand Down Expand Up @@ -89,7 +87,7 @@

// NewGauge create the gauge metric
func NewGauge(namespace, subsystem, name, metric string,
help string, labels []string, constLabels LabelMap) *prometheus.GaugeVec {
help string, labels []string, constLabels prometheus.Labels) *prometheus.GaugeVec {

metricName, err := getAndValid(namespace, subsystem, name, metric, labels)
if err != nil {
Expand All @@ -116,11 +114,11 @@
return gaugeMap[metricName]
}

func mergeLabels(labels []string, constLabels LabelMap) []string {
func mergeLabels(labels []string, constLabels prometheus.Labels) []string {
l := make([]string, 0, len(labels)+len(constLabels))
l = append(l, labels...)

for k, _ := range constLabels {

Check warning on line 121 in metric/prometheus.go

View workflow job for this annotation

GitHub Actions / Code Lint

should omit 2nd value from range; this loop is equivalent to `for k := range ...`
l = append(l, k)
}

Expand Down Expand Up @@ -197,7 +195,7 @@
return string(result)
}

func AddConstLabels(labels prometheus.Labels, constLabels LabelMap) prometheus.Labels {
func AddConstLabels(labels prometheus.Labels, constLabels prometheus.Labels) prometheus.Labels {

Check warning on line 198 in metric/prometheus.go

View workflow job for this annotation

GitHub Actions / Code Lint

exported function AddConstLabels should have comment or be unexported
for k, v := range constLabels {
labels[k] = v
}
Expand Down
16 changes: 8 additions & 8 deletions probe/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ type ProbeFuncType func() (bool, string)

// DefaultProbe is the default options for all probe
type DefaultProbe struct {
ProbeKind string `yaml:"-" json:"-"`
ProbeTag string `yaml:"-" json:"-"`
ProbeName string `yaml:"name" json:"name" jsonschema:"required,title=Probe Name,description=the name of probe must be unique"`
ProbeChannels []string `yaml:"channels" json:"channels,omitempty" jsonschema:"title=Probe Channels,description=the channels of probe message need to send to"`
ProbeTimeout time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" jsonschema:"type=string,format=duration,title=Probe Timeout,description=the timeout of probe"`
ProbeTimeInterval time.Duration `yaml:"interval,omitempty" json:"interval,omitempty" jsonschema:"type=string,format=duration,title=Probe Interval,description=the interval of probe"`
Labels metric.LabelMap `yaml:"labels,omitempty" json:"labels,omitempty" jsonschema:"title=Probe LabelMap,description=the labels of probe"`
ProbeKind string `yaml:"-" json:"-"`
ProbeTag string `yaml:"-" json:"-"`
ProbeName string `yaml:"name" json:"name" jsonschema:"required,title=Probe Name,description=the name of probe must be unique"`
ProbeChannels []string `yaml:"channels" json:"channels,omitempty" jsonschema:"title=Probe Channels,description=the channels of probe message need to send to"`
ProbeTimeout time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" jsonschema:"type=string,format=duration,title=Probe Timeout,description=the timeout of probe"`
ProbeTimeInterval time.Duration `yaml:"interval,omitempty" json:"interval,omitempty" jsonschema:"type=string,format=duration,title=Probe Interval,description=the interval of probe"`
Labels prometheus.Labels `yaml:"labels,omitempty" json:"labels,omitempty" jsonschema:"title=Probe LabelMap,description=the labels of probe"`
global.StatusChangeThresholdSettings `yaml:",inline" json:",inline"`
global.NotificationStrategySettings `yaml:"alert" json:"alert" jsonschema:"title=Probe Alert,description=the alert strategy of probe"`
ProbeFunc ProbeFuncType `yaml:"-" json:"-"`
ProbeResult *probe.Result `yaml:"-" json:"-"`
metrics *metrics `yaml:"-" json:"-"`
}

func (d *DefaultProbe) LabelMap() metric.LabelMap {
func (d *DefaultProbe) LabelMap() prometheus.Labels {
return d.Labels
}

Expand Down
2 changes: 1 addition & 1 deletion probe/base/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type metrics struct {
}

// newMetrics create the metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
TotalCnt: metric.NewGauge(namespace, subsystem, name, "total",
Expand Down
2 changes: 1 addition & 1 deletion probe/http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type metrics struct {
}

// newMetrics create the HTTP metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
StatusCode: metric.NewCounter(namespace, subsystem, name, "status_code",
Expand Down
2 changes: 1 addition & 1 deletion probe/ping/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type metrics struct {
}

// newMetrics create the metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
PacketsSent: metric.NewCounter(namespace, subsystem, name, "sent",
Expand Down
4 changes: 2 additions & 2 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"time"

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

// Prober Interface
type Prober interface {
LabelMap() metric.LabelMap
LabelMap() prometheus.Labels
Kind() string
Name() string
Channels() []string
Expand Down
2 changes: 1 addition & 1 deletion probe/shell/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type metrics struct {
}

// newMetrics create the shell metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
ExitCode: metric.NewCounter(namespace, subsystem, name, "exit_code",
Expand Down
2 changes: 1 addition & 1 deletion probe/ssh/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type metrics struct {
}

// newMetrics create the shell metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
ExitCode: metric.NewCounter(namespace, subsystem, name, "exit_code",
Expand Down
2 changes: 1 addition & 1 deletion probe/tls/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type metrics struct {
}

// newMetrics create the HTTP metrics
func newMetrics(subsystem, name string, constLabels metric.LabelMap) *metrics {
func newMetrics(subsystem, name string, constLabels prometheus.Labels) *metrics {
namespace := global.GetEaseProbe().Name
return &metrics{
EarliestCertExpiry: metric.NewGauge(namespace, subsystem, name, "earliest_cert_expiry",
Expand Down
Loading