2023-10-23 13:55:36 +00:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
clientGoRequestMetrics = &clientGoRequestMetricAdapter{}
|
|
|
|
clientGoWorkloadMetrics = &clientGoWorkqueueMetricsProvider{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
clientGoRequestMetrics.RegisterWithK8sGoClient()
|
|
|
|
clientGoWorkloadMetrics.RegisterWithK8sGoClient()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metrics to be used with a discovery manager.
|
|
|
|
type Metrics struct {
|
2023-11-15 17:13:40 +00:00
|
|
|
FailedConfigs prometheus.Gauge
|
2023-10-23 13:55:36 +00:00
|
|
|
DiscoveredTargets *prometheus.GaugeVec
|
2023-11-15 17:13:40 +00:00
|
|
|
ReceivedUpdates prometheus.Counter
|
|
|
|
DelayedUpdates prometheus.Counter
|
|
|
|
SentUpdates prometheus.Counter
|
2023-10-23 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
func NewManagerMetrics(registerer prometheus.Registerer, sdManagerName string) (*Metrics, error) {
|
2023-10-23 13:55:36 +00:00
|
|
|
m := &Metrics{}
|
|
|
|
|
2023-11-15 17:13:40 +00:00
|
|
|
m.FailedConfigs = prometheus.NewGauge(
|
2023-10-23 13:55:36 +00:00
|
|
|
prometheus.GaugeOpts{
|
2023-11-15 17:13:40 +00:00
|
|
|
Name: "prometheus_sd_failed_configs",
|
|
|
|
Help: "Current number of service discovery configurations that failed to load.",
|
|
|
|
ConstLabels: prometheus.Labels{"name": sdManagerName},
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
m.DiscoveredTargets = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
2023-11-15 17:13:40 +00:00
|
|
|
Name: "prometheus_sd_discovered_targets",
|
|
|
|
Help: "Current number of discovered targets.",
|
|
|
|
ConstLabels: prometheus.Labels{"name": sdManagerName},
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
2023-11-15 17:13:40 +00:00
|
|
|
[]string{"config"},
|
2023-10-23 13:55:36 +00:00
|
|
|
)
|
|
|
|
|
2023-11-15 17:13:40 +00:00
|
|
|
m.ReceivedUpdates = prometheus.NewCounter(
|
2023-10-23 13:55:36 +00:00
|
|
|
prometheus.CounterOpts{
|
2023-11-15 17:13:40 +00:00
|
|
|
Name: "prometheus_sd_received_updates_total",
|
|
|
|
Help: "Total number of update events received from the SD providers.",
|
|
|
|
ConstLabels: prometheus.Labels{"name": sdManagerName},
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-11-15 17:13:40 +00:00
|
|
|
m.DelayedUpdates = prometheus.NewCounter(
|
2023-10-23 13:55:36 +00:00
|
|
|
prometheus.CounterOpts{
|
2023-11-15 17:13:40 +00:00
|
|
|
Name: "prometheus_sd_updates_delayed_total",
|
|
|
|
Help: "Total number of update events that couldn't be sent immediately.",
|
|
|
|
ConstLabels: prometheus.Labels{"name": sdManagerName},
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-11-15 17:13:40 +00:00
|
|
|
m.SentUpdates = prometheus.NewCounter(
|
2023-10-23 13:55:36 +00:00
|
|
|
prometheus.CounterOpts{
|
2023-11-15 17:13:40 +00:00
|
|
|
Name: "prometheus_sd_updates_total",
|
|
|
|
Help: "Total number of update events sent to the SD consumers.",
|
|
|
|
ConstLabels: prometheus.Labels{"name": sdManagerName},
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-11-15 17:13:40 +00:00
|
|
|
metrics := []prometheus.Collector{
|
|
|
|
m.FailedConfigs,
|
|
|
|
m.DiscoveredTargets,
|
|
|
|
m.ReceivedUpdates,
|
|
|
|
m.DelayedUpdates,
|
|
|
|
m.SentUpdates,
|
|
|
|
}
|
2023-10-23 13:55:36 +00:00
|
|
|
|
|
|
|
for _, collector := range metrics {
|
|
|
|
err := registerer.Register(collector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to register discovery manager metrics: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
2024-04-05 14:19:07 +00:00
|
|
|
|
|
|
|
// Unregister unregisters all metrics.
|
|
|
|
func (m *Metrics) Unregister(registerer prometheus.Registerer) {
|
|
|
|
registerer.Unregister(m.FailedConfigs)
|
|
|
|
registerer.Unregister(m.DiscoveredTargets)
|
|
|
|
registerer.Unregister(m.ReceivedUpdates)
|
|
|
|
registerer.Unregister(m.DelayedUpdates)
|
|
|
|
registerer.Unregister(m.SentUpdates)
|
|
|
|
}
|