From 971ce92e9e709e04fe5e8d3ecfa148cc204d63d5 Mon Sep 17 00:00:00 2001 From: Xu Dongyan Date: Tue, 25 Jul 2023 17:59:25 +0800 Subject: [PATCH] [fix] all probers of a probing type should have the same labels --- metric/prometheus.go | 47 ++++++++++++++++++++++++++++++--------- metric/prometheus_test.go | 18 +++++++-------- probe/base/base.go | 35 +++++++++++++++-------------- probe/base/metrics.go | 2 +- probe/host/basic.go | 4 ++-- probe/host/cpu.go | 36 +++++++++++++++--------------- probe/host/disk.go | 16 ++++++------- probe/host/load.go | 12 +++++----- probe/host/mem.go | 16 ++++++------- probe/http/http.go | 38 ++++++++++++++++--------------- probe/http/metrics.go | 2 +- probe/ping/metrics.go | 2 +- probe/ping/ping.go | 29 ++++++++++++------------ probe/shell/metrics.go | 2 +- probe/ssh/metrics.go | 2 +- probe/tls/metrics.go | 2 +- probe/tls/tls.go | 9 ++++---- 17 files changed, 151 insertions(+), 121 deletions(-) diff --git a/metric/prometheus.go b/metric/prometheus.go index c014f013..9f8d8109 100644 --- a/metric/prometheus.go +++ b/metric/prometheus.go @@ -33,7 +33,13 @@ type MetricsType interface { *prometheus.CounterVec | *prometheus.GaugeVec | *prometheus.HistogramVec | *prometheus.SummaryVec } +type Label struct { + Name string `yaml:"name" json:"name" jsonschema:"required,title=Label Name,description=the name of label must be unique"` + Value string `yaml:"value" json:"value" jsonschema:"required,title=Label Value,description=the value of label"` +} + var ( + registries = make([]*prometheus.Registry, 0) counterMap = make(map[string]*prometheus.CounterVec) gaugeMap = make(map[string]*prometheus.GaugeVec) histogramMap = make(map[string]*prometheus.HistogramVec) @@ -57,7 +63,7 @@ func Gauge(key string) *prometheus.GaugeVec { // NewCounter create the counter metric func NewCounter(namespace, subsystem, name, metric string, - help string, labels []string, constLabels map[string]string) *prometheus.CounterVec { + help string, labels []string, constLabels []Label) *prometheus.CounterVec { metricName, err := getAndValid(namespace, subsystem, name, metric, labels) if err != nil { @@ -72,27 +78,28 @@ func NewCounter(namespace, subsystem, name, metric string, counterMap[metricName] = prometheus.NewCounterVec( prometheus.CounterOpts{ - Name: metricName, - Help: help, - ConstLabels: constLabels, + Name: metricName, + Help: help, }, - labels, + mergeLabels(labels, constLabels), ) - prometheus.MustRegister(counterMap[metricName]) + // registries[len(registries)-1].MustRegister(m) + prometheus.MustRegister(counterMap[metricName]) log.Infof("[%s] Counter <%s> is created!", module, metricName) return counterMap[metricName] } // NewGauge create the gauge metric func NewGauge(namespace, subsystem, name, metric string, - help string, labels []string, constLabels map[string]string) *prometheus.GaugeVec { + help string, labels []string, constLabels []Label) *prometheus.GaugeVec { metricName, err := getAndValid(namespace, subsystem, name, metric, labels) if err != nil { log.Errorf("[%s] %v", module, err) return nil } + if m, find := gaugeMap[metricName]; find { log.Debugf("[%s] Gauge <%s> already created!", module, metricName) return m @@ -100,18 +107,29 @@ func NewGauge(namespace, subsystem, name, metric string, gaugeMap[metricName] = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: metricName, - Help: help, - ConstLabels: constLabels, + Name: metricName, + Help: help, }, - labels, + mergeLabels(labels, constLabels), ) + prometheus.MustRegister(gaugeMap[metricName]) log.Infof("[%s] Gauge <%s> is created!", module, metricName) return gaugeMap[metricName] } +func mergeLabels(labels []string, constLabels []Label) []string { + l := make([]string, 0, len(labels)+len(constLabels)) + l = append(l, labels...) + + for _, v := range constLabels { + l = append(l, v.Name) + } + + return l +} + func getAndValid(namespace, subsystem, name, metric string, labels []string) (string, error) { metricName := GetName(namespace, subsystem, name, metric) if ValidMetricName(metricName) == false { @@ -181,3 +199,10 @@ func RemoveInvalidChars(name string) string { } return string(result) } + +func AddConstLabels(labels prometheus.Labels, constLabels []Label) prometheus.Labels { + for _, v := range constLabels { + labels[v.Name] = v.Value + } + return labels +} diff --git a/metric/prometheus_test.go b/metric/prometheus_test.go index 3db72b01..f563238c 100644 --- a/metric/prometheus_test.go +++ b/metric/prometheus_test.go @@ -65,12 +65,12 @@ func TestGetName(t *testing.T) { func TestNewMetrics(t *testing.T) { NewCounter("namespace", "subsystem", "counter", "metric", - "help", []string{"label1", "label2"}, make(map[string]string)) + "help", []string{"label1", "label2"}, make([]Label, 0)) assert.NotNil(t, GetName("namespace_subsystem_counter_metric")) assert.NotNil(t, Counter("namespace_subsystem_counter_metric")) NewGauge("namespace", "subsystem", "gauge", "metric", - "help", []string{"label1", "label2"}, make(map[string]string)) + "help", []string{"label1", "label2"}, make([]Label, 0)) assert.NotNil(t, GetName("namespace_subsystem_gauge_metric")) assert.NotNil(t, Gauge("namespace_subsystem_gauge_metric")) } @@ -110,15 +110,15 @@ func TestName(t *testing.T) { func TestDuplicateName(t *testing.T) { counter1 := NewCounter("namespace", "subsystem", "counter", "metric", - "help", []string{}, make(map[string]string)) + "help", []string{}, make([]Label, 0)) counter2 := NewCounter("namespace", "subsystem", "counter", "metric", - "help", []string{}, make(map[string]string)) + "help", []string{}, make([]Label, 0)) assert.Equal(t, counter1, counter2) gauge1 := NewGauge("namespace", "subsystem", "gauge", "metric", - "help", []string{}, make(map[string]string)) + "help", []string{}, make([]Label, 0)) gauge2 := NewGauge("namespace", "subsystem", "gauge", "metric", - "help", []string{}, make(map[string]string)) + "help", []string{}, make([]Label, 0)) assert.Equal(t, gauge1, gauge2) } @@ -126,18 +126,18 @@ func TestInvalidName(t *testing.T) { //label errors counter := NewCounter("namespace", "subsystem", "counter", "metric", - "help", []string{"label-1", "label:2"}, make(map[string]string)) + "help", []string{"label-1", "label:2"}, make([]Label, 0)) assert.Nil(t, counter) gauge := NewGauge("namespace", "subsystem", "gauge", "metric", - "help", []string{"label-1", "label:2"}, make(map[string]string)) + "help", []string{"label-1", "label:2"}, make([]Label, 0)) assert.Nil(t, gauge) monkey.Patch(ValidMetricName, func(name string) bool { return false }) counter = NewCounter("namespace", "subsystem", "counter", "metric", - "help", []string{}, make(map[string]string)) + "help", []string{}, make([]Label, 0)) assert.Nil(t, counter) monkey.UnpatchAll() diff --git a/probe/base/base.go b/probe/base/base.go index d5cbe36f..307df8a3 100644 --- a/probe/base/base.go +++ b/probe/base/base.go @@ -20,6 +20,7 @@ package base import ( "fmt" + "github.com/megaease/easeprobe/metric" "math" "net" "net/url" @@ -45,13 +46,13 @@ 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 map[string]string `yaml:"labels,omitempty" json:"labels,omitempty" jsonschema:"title=Probe Labels,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 []metric.Label `yaml:"labels,omitempty" json:"labels,omitempty" jsonschema:"title=Probe Labels,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:"-"` @@ -232,37 +233,37 @@ func (d *DefaultProbe) ExportMetrics() { } // Add endpoint label according to ProbeKind(tcp/http/ping/host/...) - d.metrics.TotalCnt.With(prometheus.Labels{ + d.metrics.TotalCnt.With(metric.AddConstLabels(prometheus.Labels{ "name": d.ProbeName, "status": d.ProbeResult.Status.String(), "endpoint": d.ProbeResult.Endpoint, - }).Set(float64(cnt)) + }, d.Labels)).Set(float64(cnt)) - d.metrics.TotalTime.With(prometheus.Labels{ + d.metrics.TotalTime.With(metric.AddConstLabels(prometheus.Labels{ "name": d.ProbeName, "status": d.ProbeResult.Status.String(), "endpoint": d.ProbeResult.Endpoint, - }).Set(float64(time.Seconds())) + }, d.Labels)).Set(float64(time.Seconds())) - d.metrics.Duration.With(prometheus.Labels{ + d.metrics.Duration.With(metric.AddConstLabels(prometheus.Labels{ "name": d.ProbeName, "status": d.ProbeResult.Status.String(), "endpoint": d.ProbeResult.Endpoint, - }).Set(float64(d.ProbeResult.RoundTripTime.Milliseconds())) + }, d.Labels)).Set(float64(d.ProbeResult.RoundTripTime.Milliseconds())) status := ServiceUp // up if d.ProbeResult.Status != probe.StatusUp { status = ServiceDown // down } - d.metrics.Status.With(prometheus.Labels{ + d.metrics.Status.With(metric.AddConstLabels(prometheus.Labels{ "name": d.ProbeName, "endpoint": d.ProbeResult.Endpoint, - }).Set(float64(status)) + }, d.Labels)).Set(float64(status)) - d.metrics.SLA.With(prometheus.Labels{ + d.metrics.SLA.With(metric.AddConstLabels(prometheus.Labels{ "name": d.ProbeName, "endpoint": d.ProbeResult.Endpoint, - }).Set(float64(d.ProbeResult.SLAPercent())) + }, d.Labels)).Set(float64(d.ProbeResult.SLAPercent())) } // DownTimeCalculation calculate the down time diff --git a/probe/base/metrics.go b/probe/base/metrics.go index 18e025d9..80ebc63f 100644 --- a/probe/base/metrics.go +++ b/probe/base/metrics.go @@ -33,7 +33,7 @@ type metrics struct { } // newMetrics create the metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ TotalCnt: metric.NewGauge(namespace, subsystem, name, "total", diff --git a/probe/host/basic.go b/probe/host/basic.go index e4ce5606..2e8bab26 100644 --- a/probe/host/basic.go +++ b/probe/host/basic.go @@ -93,8 +93,8 @@ func (b *Basic) CreateMetrics(subsystem, name string) { // ExportMetrics export the cpu metrics func (b *Basic) ExportMetrics(name string) { - b.metrics.With(prometheus.Labels{ + b.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "cpu_core", - }).Set(float64(b.Core)) + }, b.Labels)).Set(float64(b.Core)) } diff --git a/probe/host/cpu.go b/probe/host/cpu.go index ed3f8cab..2ea1a5c0 100644 --- a/probe/host/cpu.go +++ b/probe/host/cpu.go @@ -118,48 +118,48 @@ func (c *CPU) CreateMetrics(subsystem, name string) { // ExportMetrics export the cpu metrics func (c *CPU) ExportMetrics(name string) { // CPU metrics - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "usage", - }).Set(100 - c.Idle) + }, c.Labels)).Set(100 - c.Idle) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "idle", - }).Set(c.Idle) + }, c.Labels)).Set(c.Idle) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "user", - }).Set(c.User) + }, c.Labels)).Set(c.User) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "sys", - }).Set(c.Sys) + }, c.Labels)).Set(c.Sys) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "nice", - }).Set(c.Nice) + }, c.Labels)).Set(c.Nice) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "wait", - }).Set(c.Wait) + }, c.Labels)).Set(c.Wait) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "hard", - }).Set(c.Hard) + }, c.Labels)).Set(c.Hard) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "soft", - }).Set(c.Soft) + }, c.Labels)).Set(c.Soft) - c.metrics.With(prometheus.Labels{ + c.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "steal", - }).Set(c.Steal) + }, c.Labels)).Set(c.Steal) } diff --git a/probe/host/disk.go b/probe/host/disk.go index 26e0686b..993c0a2b 100644 --- a/probe/host/disk.go +++ b/probe/host/disk.go @@ -128,28 +128,28 @@ func (d *Disks) CreateMetrics(subsystem, name string) { // ExportMetrics export the disk metrics func (d *Disks) ExportMetrics(name string) { for _, disk := range d.Usage { - d.metrics.With(prometheus.Labels{ + d.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "disk": disk.Tag, "state": "used", - }).Set(float64(disk.Used)) + }, d.Labels)).Set(float64(disk.Used)) - d.metrics.With(prometheus.Labels{ + d.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "disk": disk.Tag, "state": "available", - }).Set(float64(disk.Total - disk.Used)) + }, d.Labels)).Set(float64(disk.Total - disk.Used)) - d.metrics.With(prometheus.Labels{ + d.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "disk": disk.Tag, "state": "total", - }).Set(float64(disk.Total)) + }, d.Labels)).Set(float64(disk.Total)) - d.metrics.With(prometheus.Labels{ + d.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "disk": disk.Tag, "state": "usage", - }).Set(disk.Usage) + }, d.Labels)).Set(disk.Usage) } } diff --git a/probe/host/load.go b/probe/host/load.go index f4498c43..0e60cbd8 100644 --- a/probe/host/load.go +++ b/probe/host/load.go @@ -135,18 +135,18 @@ func (l *Load) CreateMetrics(subsystem, name string) { // ExportMetrics export the load average metrics func (l *Load) ExportMetrics(name string) { - l.metrics.With(prometheus.Labels{ + l.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "m1", - }).Set(l.Metrics["m1"]) + }, l.Labels)).Set(l.Metrics["m1"]) - l.metrics.With(prometheus.Labels{ + l.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "m5", - }).Set(l.Metrics["m5"]) + }, l.Labels)).Set(l.Metrics["m5"]) - l.metrics.With(prometheus.Labels{ + l.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "m15", - }).Set(l.Metrics["m15"]) + }, l.Labels)).Set(l.Metrics["m15"]) } diff --git a/probe/host/mem.go b/probe/host/mem.go index db02b8d6..4a859ba1 100644 --- a/probe/host/mem.go +++ b/probe/host/mem.go @@ -104,23 +104,23 @@ func (m *Mem) CreateMetrics(subsystem, name string) { // ExportMetrics export the memory metrics func (m *Mem) ExportMetrics(name string) { - m.metrics.With(prometheus.Labels{ + m.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "used", - }).Set(float64(m.Used)) + }, m.Labels)).Set(float64(m.Used)) - m.metrics.With(prometheus.Labels{ + m.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "available", - }).Set(float64(m.Total - m.Used)) + }, m.Labels)).Set(float64(m.Total - m.Used)) - m.metrics.With(prometheus.Labels{ + m.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "total", - }).Set(float64(m.Total)) + }, m.Labels)).Set(float64(m.Total)) - m.metrics.With(prometheus.Labels{ + m.metrics.With(metric.AddConstLabels(prometheus.Labels{ "host": name, "state": "usage", - }).Set(m.Usage) + }, m.Labels)).Set(m.Usage) } diff --git a/probe/http/http.go b/probe/http/http.go index bc878f16..761a5145 100644 --- a/probe/http/http.go +++ b/probe/http/http.go @@ -22,6 +22,7 @@ import ( "bytes" "context" "fmt" + "github.com/megaease/easeprobe/metric" "io" "net" "net/http" @@ -209,6 +210,7 @@ func (h *HTTP) DoProbe() (bool, string) { resp, err := h.client.Do(req) h.traceStats.Done() + prometheus.NewRegistry() h.ExportMetrics(resp) if err != nil { @@ -278,57 +280,57 @@ func (h *HTTP) ExportMetrics(resp *http.Response) { code = resp.StatusCode len = int(resp.ContentLength) } - h.metrics.StatusCode.With(prometheus.Labels{ + h.metrics.StatusCode.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Inc() + }, h.Labels)).Inc() - h.metrics.ContentLen.With(prometheus.Labels{ + h.metrics.ContentLen.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(float64(len)) + }, h.Labels)).Set(float64(len)) - h.metrics.DNSDuration.With(prometheus.Labels{ + h.metrics.DNSDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.dnsTook)) + }, h.Labels)).Set(toMS(h.traceStats.dnsTook)) - h.metrics.ConnectDuration.With(prometheus.Labels{ + h.metrics.ConnectDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.connTook)) + }, h.Labels)).Set(toMS(h.traceStats.connTook)) - h.metrics.TLSDuration.With(prometheus.Labels{ + h.metrics.TLSDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.tlsTook)) + }, h.Labels)).Set(toMS(h.traceStats.tlsTook)) - h.metrics.SendDuration.With(prometheus.Labels{ + h.metrics.SendDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.sendTook)) + }, h.Labels)).Set(toMS(h.traceStats.sendTook)) - h.metrics.WaitDuration.With(prometheus.Labels{ + h.metrics.WaitDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.waitTook)) + }, h.Labels)).Set(toMS(h.traceStats.waitTook)) - h.metrics.TransferDuration.With(prometheus.Labels{ + h.metrics.TransferDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.transferTook)) + }, h.Labels)).Set(toMS(h.traceStats.transferTook)) - h.metrics.TotalDuration.With(prometheus.Labels{ + h.metrics.TotalDuration.With(metric.AddConstLabels(prometheus.Labels{ "name": h.ProbeName, "status": fmt.Sprintf("%d", code), "endpoint": h.ProbeResult.Endpoint, - }).Set(toMS(h.traceStats.totalTook)) + }, h.Labels)).Set(toMS(h.traceStats.totalTook)) } diff --git a/probe/http/metrics.go b/probe/http/metrics.go index 923c7f0e..74d0434a 100644 --- a/probe/http/metrics.go +++ b/probe/http/metrics.go @@ -37,7 +37,7 @@ type metrics struct { } // newMetrics create the HTTP metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ StatusCode: metric.NewCounter(namespace, subsystem, name, "status_code", diff --git a/probe/ping/metrics.go b/probe/ping/metrics.go index 16013b1f..3c1d4c88 100644 --- a/probe/ping/metrics.go +++ b/probe/ping/metrics.go @@ -34,7 +34,7 @@ type metrics struct { } // newMetrics create the metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ PacketsSent: metric.NewCounter(namespace, subsystem, name, "sent", diff --git a/probe/ping/ping.go b/probe/ping/ping.go index d402dc01..664d9ac6 100644 --- a/probe/ping/ping.go +++ b/probe/ping/ping.go @@ -20,6 +20,7 @@ package ping import ( "fmt" + "github.com/megaease/easeprobe/metric" "reflect" "github.com/megaease/easeprobe/global" @@ -126,38 +127,38 @@ func (p *Ping) DoProbe() (bool, string) { // ExportMetrics export Ping metrics func (p *Ping) ExportMetrics(stats *ping.Statistics) { - p.metrics.PacketsSent.With(prometheus.Labels{ + p.metrics.PacketsSent.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Add(float64(stats.PacketsSent)) + }, p.Labels)).Add(float64(stats.PacketsSent)) - p.metrics.PacketsRecv.With(prometheus.Labels{ + p.metrics.PacketsRecv.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Add(float64(stats.PacketsRecv)) + }, p.Labels)).Add(float64(stats.PacketsRecv)) - p.metrics.PacketLoss.With(prometheus.Labels{ + p.metrics.PacketLoss.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Set(stats.PacketLoss) + }, p.Labels)).Add(stats.PacketLoss) - p.metrics.MaxRtt.With(prometheus.Labels{ + p.metrics.AvgRtt.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Set(float64(stats.MaxRtt.Milliseconds())) + }, p.Labels)).Add(float64(stats.MaxRtt.Milliseconds())) - p.metrics.MinRtt.With(prometheus.Labels{ + p.metrics.MinRtt.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Set(float64(stats.MinRtt.Milliseconds())) + }, p.Labels)).Set(float64(stats.MinRtt.Milliseconds())) - p.metrics.AvgRtt.With(prometheus.Labels{ + p.metrics.AvgRtt.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Set(float64(stats.AvgRtt.Milliseconds())) + }, p.Labels)).Set(float64(stats.AvgRtt.Milliseconds())) - p.metrics.StdDevRtt.With(prometheus.Labels{ + p.metrics.StdDevRtt.With(metric.AddConstLabels(prometheus.Labels{ "name": p.ProbeName, "endpoint": p.ProbeResult.Endpoint, - }).Set(float64(stats.StdDevRtt.Milliseconds())) + }, p.Labels)).Set(float64(stats.StdDevRtt.Milliseconds())) } diff --git a/probe/shell/metrics.go b/probe/shell/metrics.go index 5edcd5fc..2a354a6f 100644 --- a/probe/shell/metrics.go +++ b/probe/shell/metrics.go @@ -30,7 +30,7 @@ type metrics struct { } // newMetrics create the shell metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ ExitCode: metric.NewCounter(namespace, subsystem, name, "exit_code", diff --git a/probe/ssh/metrics.go b/probe/ssh/metrics.go index ad1eeb6f..be06b30a 100644 --- a/probe/ssh/metrics.go +++ b/probe/ssh/metrics.go @@ -30,7 +30,7 @@ type metrics struct { } // newMetrics create the shell metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ ExitCode: metric.NewCounter(namespace, subsystem, name, "exit_code", diff --git a/probe/tls/metrics.go b/probe/tls/metrics.go index 5f0d1da8..fee56d42 100644 --- a/probe/tls/metrics.go +++ b/probe/tls/metrics.go @@ -33,7 +33,7 @@ type metrics struct { } // newMetrics create the HTTP metrics -func newMetrics(subsystem, name string, constLabels map[string]string) *metrics { +func newMetrics(subsystem, name string, constLabels []metric.Label) *metrics { namespace := global.GetEaseProbe().Name return &metrics{ EarliestCertExpiry: metric.NewGauge(namespace, subsystem, name, "earliest_cert_expiry", diff --git a/probe/tls/tls.go b/probe/tls/tls.go index 54beeec5..5fb0f5c9 100644 --- a/probe/tls/tls.go +++ b/probe/tls/tls.go @@ -23,6 +23,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" + "github.com/megaease/easeprobe/metric" "net" "os" "strings" @@ -137,12 +138,12 @@ func (t *TLS) DoProbe() (bool, string) { state := tconn.ConnectionState() - t.metrics.EarliestCertExpiry.With(prometheus.Labels{ + t.metrics.EarliestCertExpiry.With(metric.AddConstLabels(prometheus.Labels{ "endpoint": t.ProbeResult.Endpoint, - }).Set(float64(getEarliestCertExpiry(&state).Unix())) - t.metrics.LastChainExpiryTimestampSeconds.With(prometheus.Labels{ + }, t.Labels)).Set(float64(getEarliestCertExpiry(&state).Unix())) + t.metrics.EarliestCertExpiry.With(metric.AddConstLabels(prometheus.Labels{ "endpoint": t.ProbeResult.Endpoint, - }).Set(float64(getLastChainExpiry(&state).Unix())) + }, t.Labels)).Set(float64(getLastChainExpiry(&state).Unix())) return true, "TLS Endpoint Verified Successfully!" }