change kubelet probe metrics to counter type

This commit is contained in:
danielqsj 2019-04-03 17:31:31 +08:00
parent 997d5182d0
commit 295d672d54
3 changed files with 70 additions and 14 deletions

View File

@ -32,12 +32,29 @@ import (
"k8s.io/kubernetes/pkg/kubelet/util/format" "k8s.io/kubernetes/pkg/kubelet/util/format"
) )
// ProberResults stores the results of a probe as prometheus metrics. // ProberResults stores the cumulative number of a probe by result as prometheus metrics.
var ProberResults = prometheus.NewGaugeVec( var ProberResults = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "prober",
Name: "probe_total",
Help: "Cumulative number of a liveness or readiness probe for a container by result.",
},
[]string{"probe_type",
"result",
"container",
"pod",
"namespace",
"pod_uid"},
)
// DeprecatedProberResults stores the results of a probe as prometheus metrics.
// This metrics is deprecated, will be removed in a future release.
// Please convert to the metrics of counter type above.
var DeprecatedProberResults = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Subsystem: "prober", Subsystem: "prober",
Name: "probe_result", Name: "probe_result",
Help: "The result of a liveness or readiness probe for a container.", Help: "(Deprecated) The result of a liveness or readiness probe for a container.",
}, },
[]string{"probe_type", []string{"probe_type",
"container_name", "container_name",
@ -130,6 +147,10 @@ type probeType int
const ( const (
liveness probeType = iota liveness probeType = iota
readiness readiness
probeResultSuccessful string = "successful"
probeResultFailed string = "failed"
probeResultUnknown string = "unknown"
) )
// For debugging. // For debugging.

View File

@ -69,7 +69,10 @@ type worker struct {
// proberResultsMetricLabels holds the labels attached to this worker // proberResultsMetricLabels holds the labels attached to this worker
// for the ProberResults metric. // for the ProberResults metric.
proberResultsMetricLabels prometheus.Labels proberResultsMetricLabels prometheus.Labels
proberResultsSuccessfulMetricLabels prometheus.Labels
proberResultsFailedMetricLabels prometheus.Labels
proberResultsUnknownMetricLabels prometheus.Labels
} }
// Creates and starts a new probe worker. // Creates and starts a new probe worker.
@ -98,16 +101,27 @@ func newWorker(
w.initialValue = results.Success w.initialValue = results.Success
} }
w.proberResultsMetricLabels = prometheus.Labels{ basicMetricLabels := prometheus.Labels{
"probe_type": w.probeType.String(), "probe_type": w.probeType.String(),
"container_name": w.container.Name, "container": w.container.Name,
"container": w.container.Name, "pod": w.pod.Name,
"pod_name": w.pod.Name, "namespace": w.pod.Namespace,
"pod": w.pod.Name, "pod_uid": string(w.pod.UID),
"namespace": w.pod.Namespace,
"pod_uid": string(w.pod.UID),
} }
w.proberResultsMetricLabels = deepCopyPrometheusLabels(basicMetricLabels)
w.proberResultsMetricLabels["container_name"] = w.container.Name
w.proberResultsMetricLabels["pod_name"] = w.pod.Name
w.proberResultsSuccessfulMetricLabels = deepCopyPrometheusLabels(basicMetricLabels)
w.proberResultsSuccessfulMetricLabels["result"] = probeResultSuccessful
w.proberResultsFailedMetricLabels = deepCopyPrometheusLabels(basicMetricLabels)
w.proberResultsFailedMetricLabels["result"] = probeResultFailed
w.proberResultsUnknownMetricLabels = deepCopyPrometheusLabels(basicMetricLabels)
w.proberResultsUnknownMetricLabels["result"] = probeResultUnknown
return w return w
} }
@ -129,7 +143,10 @@ func (w *worker) run() {
} }
w.probeManager.removeWorker(w.pod.UID, w.container.Name, w.probeType) w.probeManager.removeWorker(w.pod.UID, w.container.Name, w.probeType)
ProberResults.Delete(w.proberResultsMetricLabels) ProberResults.Delete(w.proberResultsSuccessfulMetricLabels)
ProberResults.Delete(w.proberResultsFailedMetricLabels)
ProberResults.Delete(w.proberResultsUnknownMetricLabels)
DeprecatedProberResults.Delete(w.proberResultsMetricLabels)
}() }()
probeLoop: probeLoop:
@ -220,6 +237,15 @@ func (w *worker) doProbe() (keepGoing bool) {
return true return true
} }
switch result {
case results.Success:
ProberResults.With(w.proberResultsSuccessfulMetricLabels).Inc()
case results.Failure:
ProberResults.With(w.proberResultsFailedMetricLabels).Inc()
default:
ProberResults.With(w.proberResultsUnknownMetricLabels).Inc()
}
if w.lastResult == result { if w.lastResult == result {
w.resultRun++ w.resultRun++
} else { } else {
@ -234,7 +260,7 @@ func (w *worker) doProbe() (keepGoing bool) {
} }
w.resultsManager.Set(w.containerID, result, w.pod) w.resultsManager.Set(w.containerID, result, w.pod)
ProberResults.With(w.proberResultsMetricLabels).Set(result.ToPrometheusType()) DeprecatedProberResults.With(w.proberResultsMetricLabels).Set(result.ToPrometheusType())
if w.probeType == liveness && result == results.Failure { if w.probeType == liveness && result == results.Failure {
// The container fails a liveness check, it will need to be restarted. // The container fails a liveness check, it will need to be restarted.
@ -247,3 +273,11 @@ func (w *worker) doProbe() (keepGoing bool) {
return true return true
} }
func deepCopyPrometheusLabels(m prometheus.Labels) prometheus.Labels {
ret := make(prometheus.Labels, len(m))
for k, v := range m {
ret[k] = v
}
return ret
}

View File

@ -321,6 +321,7 @@ func (s *Server) InstallDefaultHandlers() {
// prober metrics are exposed under a different endpoint // prober metrics are exposed under a different endpoint
p := prometheus.NewRegistry() p := prometheus.NewRegistry()
p.MustRegister(prober.ProberResults) p.MustRegister(prober.ProberResults)
p.MustRegister(prober.DeprecatedProberResults)
s.restfulCont.Handle(proberMetricsPath, s.restfulCont.Handle(proberMetricsPath,
promhttp.HandlerFor(p, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}), promhttp.HandlerFor(p, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}),
) )