...

Source file src/github.com/linkerd/linkerd2/multicluster/service-mirror/metrics.go

Documentation: github.com/linkerd/linkerd2/multicluster/service-mirror

     1  package servicemirror
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  	"github.com/prometheus/client_golang/prometheus/promauto"
     6  	logging "github.com/sirupsen/logrus"
     7  )
     8  
     9  const (
    10  	gatewayClusterName   = "target_cluster_name"
    11  	eventTypeLabelName   = "event_type"
    12  	probeSuccessfulLabel = "probe_successful"
    13  )
    14  
    15  // ProbeMetricVecs stores metrics about about gateways collected by probe
    16  // workers.
    17  type ProbeMetricVecs struct {
    18  	gatewayEnabled *prometheus.GaugeVec
    19  	alive          *prometheus.GaugeVec
    20  	latency        *prometheus.GaugeVec
    21  	latencies      *prometheus.HistogramVec
    22  	enqueues       *prometheus.CounterVec
    23  	dequeues       *prometheus.CounterVec
    24  	probes         *prometheus.CounterVec
    25  }
    26  
    27  // ProbeMetrics stores metrics about about a specific gateway collected by a
    28  // probe worker.
    29  type ProbeMetrics struct {
    30  	gatewayEnabled prometheus.Gauge
    31  	alive          prometheus.Gauge
    32  	latency        prometheus.Gauge
    33  	latencies      prometheus.Observer
    34  	probes         *prometheus.CounterVec
    35  	unregister     func()
    36  }
    37  
    38  var endpointRepairCounter *prometheus.CounterVec
    39  
    40  func init() {
    41  	endpointRepairCounter = promauto.NewCounterVec(
    42  		prometheus.CounterOpts{
    43  			Name: "service_mirror_endpoint_repairs",
    44  			Help: "Increments when the service mirror controller attempts to repair mirror endpoints",
    45  		},
    46  		[]string{gatewayClusterName},
    47  	)
    48  }
    49  
    50  // NewProbeMetricVecs creates a new ProbeMetricVecs.
    51  func NewProbeMetricVecs() ProbeMetricVecs {
    52  	labelNames := []string{gatewayClusterName}
    53  
    54  	probes := promauto.NewCounterVec(
    55  		prometheus.CounterOpts{
    56  			Name: "gateway_probes",
    57  			Help: "A counter for the number of actual performed probes to a gateway",
    58  		},
    59  		[]string{gatewayClusterName, probeSuccessfulLabel},
    60  	)
    61  
    62  	enqueues := promauto.NewCounterVec(
    63  		prometheus.CounterOpts{
    64  			Name: "probe_manager_event_enqueues",
    65  			Help: "A counter for the number of enqueued events to the probe manager",
    66  		},
    67  		[]string{eventTypeLabelName},
    68  	)
    69  
    70  	dequeues := promauto.NewCounterVec(
    71  		prometheus.CounterOpts{
    72  			Name: "probe_manager_event_dequeues",
    73  			Help: "A counter for the number of dequeued events to the probe manager",
    74  		},
    75  		[]string{eventTypeLabelName},
    76  	)
    77  
    78  	alive := promauto.NewGaugeVec(
    79  		prometheus.GaugeOpts{
    80  			Name: "gateway_alive",
    81  			Help: "A gauge which is 1 if the gateway is alive and 0 if it is not.",
    82  		},
    83  		labelNames,
    84  	)
    85  
    86  	gatewayEnabled :=
    87  		promauto.NewGaugeVec(
    88  			prometheus.GaugeOpts{
    89  				Name: "gateway_enabled",
    90  				Help: "A gauge which is 1 if the gateway is enabled, and 0 if it is not",
    91  			},
    92  			labelNames,
    93  		)
    94  
    95  	latency := promauto.NewGaugeVec(
    96  		prometheus.GaugeOpts{
    97  			Name: "gateway_latency",
    98  			Help: "A gauge which is the latency of the last probe to the gateway.",
    99  		},
   100  		labelNames,
   101  	)
   102  
   103  	latencies := promauto.NewHistogramVec(
   104  		prometheus.HistogramOpts{
   105  			Name: "gateway_probe_latency_ms",
   106  			Help: "A histogram of latencies to a gateway in a target cluster.",
   107  			Buckets: []float64{
   108  				1, 2, 3, 4, 5,
   109  				10, 20, 30, 40, 50,
   110  				100, 200, 300, 400, 500,
   111  				1000, 2000, 3000, 4000, 5000,
   112  				10000, 20000, 30000, 40000, 50000,
   113  			},
   114  		},
   115  		labelNames)
   116  
   117  	return ProbeMetricVecs{
   118  		alive:          alive,
   119  		gatewayEnabled: gatewayEnabled,
   120  		latency:        latency,
   121  		latencies:      latencies,
   122  		enqueues:       enqueues,
   123  		dequeues:       dequeues,
   124  		probes:         probes,
   125  	}
   126  }
   127  
   128  // NewWorkerMetrics creates a new ProbeMetrics by scoping to a specific target
   129  // cluster.
   130  func (mv ProbeMetricVecs) NewWorkerMetrics(remoteClusterName string) (*ProbeMetrics, error) {
   131  
   132  	labels := prometheus.Labels{
   133  		gatewayClusterName: remoteClusterName,
   134  	}
   135  
   136  	curriedProbes, err := mv.probes.CurryWith(labels)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	gatewayEnabled := mv.gatewayEnabled.With(labels)
   141  	gatewayEnabled.Set(0)
   142  	return &ProbeMetrics{
   143  		gatewayEnabled: gatewayEnabled,
   144  		alive:          mv.alive.With(labels),
   145  		latency:        mv.latency.With(labels),
   146  		latencies:      mv.latencies.With(labels),
   147  		probes:         curriedProbes,
   148  		unregister: func() {
   149  			mv.unregister(remoteClusterName)
   150  		},
   151  	}, nil
   152  }
   153  
   154  func (mv ProbeMetricVecs) unregister(remoteClusterName string) {
   155  	labels := prometheus.Labels{
   156  		gatewayClusterName: remoteClusterName,
   157  	}
   158  
   159  	if !mv.gatewayEnabled.Delete(labels) {
   160  		logging.Warnf("unable to delete gateway_enabled metric with labels %s", labels)
   161  	}
   162  
   163  	if !mv.alive.Delete(labels) {
   164  		logging.Warnf("unable to delete gateway_alive metric with labels %s", labels)
   165  	}
   166  	if !mv.latencies.Delete(labels) {
   167  		logging.Warnf("unable to delete gateway_probe_latency_ms metric with labels %s", labels)
   168  	}
   169  }
   170  

View as plain text