Repository: prometheus/prometheus
File: discovery/metrics_refresh.go (Go)

Code before:
	durationVec     *prometheus.SummaryVec
	durationHistVec *prometheus.HistogramVec

	metricRegisterer MetricRegisterer
}

var _ RefreshMetricsManager = (*RefreshMetricsVecs)(nil)

func NewRefreshMetrics(reg prometheus.Registerer) RefreshMetricsManager {
	m := &RefreshMetricsVecs{
		failuresVec: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Name: "prometheus_sd_refresh_failures_total",
				Help: "Number of refresh failures for the given SD mechanism.",
			},
			[]string{"mechanism"}),
		durationVec: prometheus.NewSummaryVec(
			prometheus.SummaryOpts{
				Name:       "prometheus_sd_refresh_duration_seconds",
				Help:       "The duration of a refresh in seconds for the given SD mechanism.",
				Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
			},
			[]string{"mechanism"}),
		durationHistVec: prometheus.NewHistogramVec(
			prometheus.HistogramOpts{
				Name:                            "prometheus_sd_refresh_histogram_duration_seconds",
				Help:                            "The duration of a refresh for the given SD mechanism.",
				Buckets:                         []float64{.01, .1, 1, 10},
				NativeHistogramBucketFactor:     1.1,
				NativeHistogramMaxBucketNumber:  100,
				NativeHistogramMinResetDuration: 1 * time.Hour,
			},
			[]string{"mechanism"}),
	}

	// The reason we register metric vectors instead of metrics is so that
	// the metrics are not visible until they are recorded.
	m.metricRegisterer = NewMetricRegisterer(reg, []prometheus.Collector{
		m.failuresVec,
		m.durationVec,
		m.durationHistVec,
	})

	return m
}

// Instantiate returns metrics out of metric vectors.
func (m *RefreshMetricsVecs) Instantiate(mech string) *RefreshMetrics {
	return &RefreshMetrics{
		Failures:          m.failuresVec.WithLabelValues(mech),
		Duration:          m.durationVec.WithLabelValues(mech),

Review comment (suggestion):
Let's follow exactly the naming scheme proposed in #17322:

```suggestion
				Name:                            "prometheus_sd_refresh_duration_histogram_seconds",
```

Code after:
	durationVec     *prometheus.SummaryVec
	durationHistVec *prometheus.HistogramVec

	metricRegisterer MetricRegisterer
}

var _ RefreshMetricsManager = (*RefreshMetricsVecs)(nil)

func NewRefreshMetrics(reg prometheus.Registerer) RefreshMetricsManager {
	m := &RefreshMetricsVecs{
		failuresVec: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Name: "prometheus_sd_refresh_failures_total",
				Help: "Number of refresh failures for the given SD mechanism.",
			},
			[]string{"mechanism", "config"}),
		durationVec: prometheus.NewSummaryVec(
			prometheus.SummaryOpts{
				Name:       "prometheus_sd_refresh_duration_seconds",
				Help:       "The duration of a refresh in seconds for the given SD mechanism.",
				Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
			},
			[]string{"mechanism", "config"}),
		durationHistVec: prometheus.NewHistogramVec(
			prometheus.HistogramOpts{
				Name:                            "prometheus_sd_refresh_duration_histogram_seconds",
				Help:                            "The duration of a refresh for the given SD mechanism.",
				Buckets:                         []float64{.01, .1, 1, 10},
				NativeHistogramBucketFactor:     1.1,
				NativeHistogramMaxBucketNumber:  100,
				NativeHistogramMinResetDuration: 1 * time.Hour,
			},
			[]string{"mechanism"}),
	}

	// The reason we register metric vectors instead of metrics is so that
	// the metrics are not visible until they are recorded.
	m.metricRegisterer = NewMetricRegisterer(reg, []prometheus.Collector{
		m.failuresVec,
		m.durationVec,
		m.durationHistVec,
	})

	return m
}

// Instantiate returns metrics out of metric vectors for a given mechanism and config.
func (m *RefreshMetricsVecs) Instantiate(mech, config string) *RefreshMetrics {
	return &RefreshMetrics{
		Failures:          m.failuresVec.WithLabelValues(mech, config),
		Duration:          m.durationVec.WithLabelValues(mech, config),