Add admission_latencies_milliseconds metrics for backward compatible

This commit is contained in:
danielqsj 2019-01-18 11:50:48 +08:00
parent c183646baf
commit d9c57e7407

View File

@ -153,19 +153,31 @@ func (m *AdmissionMetrics) ObserveWebhook(elapsed time.Duration, rejected bool,
} }
type metricSet struct { type metricSet struct {
latencies *prometheus.HistogramVec latencies *prometheus.HistogramVec
latenciesSummary *prometheus.SummaryVec deprecatedLatencies *prometheus.HistogramVec
latenciesSummary *prometheus.SummaryVec
deprecatedLatenciesSummary *prometheus.SummaryVec
} }
func newMetricSet(name string, labels []string, helpTemplate string, hasSummary bool) *metricSet { func newMetricSet(name string, labels []string, helpTemplate string, hasSummary bool) *metricSet {
var summary *prometheus.SummaryVec var summary, deprecatedSummary *prometheus.SummaryVec
if hasSummary { if hasSummary {
summary = prometheus.NewSummaryVec( summary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{ prometheus.SummaryOpts{
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: fmt.Sprintf("%s_admission_latencies_seconds_summary", name), Name: fmt.Sprintf("%s_admission_latencies_seconds_summary", name),
Help: fmt.Sprintf(helpTemplate, "latency summary"), Help: fmt.Sprintf(helpTemplate, "latency summary in seconds"),
MaxAge: latencySummaryMaxAge,
},
labels,
)
deprecatedSummary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: fmt.Sprintf("%s_admission_latencies_milliseconds_summary", name),
Help: fmt.Sprintf("(Deprecated) "+helpTemplate, "latency summary in milliseconds"),
MaxAge: latencySummaryMaxAge, MaxAge: latencySummaryMaxAge,
}, },
labels, labels,
@ -178,37 +190,61 @@ func newMetricSet(name string, labels []string, helpTemplate string, hasSummary
Namespace: namespace, Namespace: namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: fmt.Sprintf("%s_admission_latencies_seconds", name), Name: fmt.Sprintf("%s_admission_latencies_seconds", name),
Help: fmt.Sprintf(helpTemplate, "latency histogram"), Help: fmt.Sprintf(helpTemplate, "latency histogram in seconds"),
Buckets: latencyBuckets,
},
labels,
),
deprecatedLatencies: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: fmt.Sprintf("%s_admission_latencies_milliseconds", name),
Help: fmt.Sprintf("(Deprecated) "+helpTemplate, "latency histogram in milliseconds"),
Buckets: latencyBuckets, Buckets: latencyBuckets,
}, },
labels, labels,
), ),
latenciesSummary: summary, latenciesSummary: summary,
deprecatedLatenciesSummary: deprecatedSummary,
} }
} }
// MustRegister registers all the prometheus metrics in the metricSet. // MustRegister registers all the prometheus metrics in the metricSet.
func (m *metricSet) mustRegister() { func (m *metricSet) mustRegister() {
prometheus.MustRegister(m.latencies) prometheus.MustRegister(m.latencies)
prometheus.MustRegister(m.deprecatedLatencies)
if m.latenciesSummary != nil { if m.latenciesSummary != nil {
prometheus.MustRegister(m.latenciesSummary) prometheus.MustRegister(m.latenciesSummary)
} }
if m.deprecatedLatenciesSummary != nil {
prometheus.MustRegister(m.deprecatedLatenciesSummary)
}
} }
// Reset resets all the prometheus metrics in the metricSet. // Reset resets all the prometheus metrics in the metricSet.
func (m *metricSet) reset() { func (m *metricSet) reset() {
m.latencies.Reset() m.latencies.Reset()
m.deprecatedLatencies.Reset()
if m.latenciesSummary != nil { if m.latenciesSummary != nil {
m.latenciesSummary.Reset() m.latenciesSummary.Reset()
} }
if m.deprecatedLatenciesSummary != nil {
m.deprecatedLatenciesSummary.Reset()
}
} }
// Observe records an observed admission event to all metrics in the metricSet. // Observe records an observed admission event to all metrics in the metricSet.
func (m *metricSet) observe(elapsed time.Duration, labels ...string) { func (m *metricSet) observe(elapsed time.Duration, labels ...string) {
elapsedSeconds := elapsed.Seconds() elapsedSeconds := elapsed.Seconds()
elapsedMicroseconds := float64(elapsed / time.Microsecond)
m.latencies.WithLabelValues(labels...).Observe(elapsedSeconds) m.latencies.WithLabelValues(labels...).Observe(elapsedSeconds)
m.deprecatedLatencies.WithLabelValues(labels...).Observe(elapsedMicroseconds)
if m.latenciesSummary != nil { if m.latenciesSummary != nil {
m.latenciesSummary.WithLabelValues(labels...).Observe(elapsedSeconds) m.latenciesSummary.WithLabelValues(labels...).Observe(elapsedSeconds)
} }
if m.deprecatedLatenciesSummary != nil {
m.deprecatedLatenciesSummary.WithLabelValues(labels...).Observe(elapsedMicroseconds)
}
} }