mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 15:05:27 +00:00
add context to metrics in apiserver admission webhook
This commit is contained in:
parent
8a8282044c
commit
b3aeaa4ed7
@ -54,7 +54,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ObserverFunc is a func that emits metrics.
|
// ObserverFunc is a func that emits metrics.
|
||||||
type ObserverFunc func(elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string)
|
type ObserverFunc func(ctx context.Context, elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
stepValidate = "validate"
|
stepValidate = "validate"
|
||||||
@ -96,7 +96,7 @@ func (p pluginHandlerWithMetrics) Admit(ctx context.Context, a admission.Attribu
|
|||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
err := mutatingHandler.Admit(ctx, a, o)
|
err := mutatingHandler.Admit(ctx, a, o)
|
||||||
p.observer(time.Since(start), err != nil, a, stepAdmit, p.extraLabels...)
|
p.observer(ctx, time.Since(start), err != nil, a, stepAdmit, p.extraLabels...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ func (p pluginHandlerWithMetrics) Validate(ctx context.Context, a admission.Attr
|
|||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
err := validatingHandler.Validate(ctx, a, o)
|
err := validatingHandler.Validate(ctx, a, o)
|
||||||
p.observer(time.Since(start), err != nil, a, stepValidate, p.extraLabels...)
|
p.observer(ctx, time.Since(start), err != nil, a, stepValidate, p.extraLabels...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,28 +163,28 @@ func (m *AdmissionMetrics) reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ObserveAdmissionStep records admission related metrics for a admission step, identified by step type.
|
// ObserveAdmissionStep records admission related metrics for a admission step, identified by step type.
|
||||||
func (m *AdmissionMetrics) ObserveAdmissionStep(elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
func (m *AdmissionMetrics) ObserveAdmissionStep(ctx context.Context, elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
||||||
m.step.observe(elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
m.step.observe(ctx, elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObserveAdmissionController records admission related metrics for a built-in admission controller, identified by it's plugin handler name.
|
// ObserveAdmissionController records admission related metrics for a built-in admission controller, identified by it's plugin handler name.
|
||||||
func (m *AdmissionMetrics) ObserveAdmissionController(elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
func (m *AdmissionMetrics) ObserveAdmissionController(ctx context.Context, elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
||||||
m.controller.observe(elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
m.controller.observe(ctx, elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObserveWebhook records admission related metrics for a admission webhook.
|
// ObserveWebhook records admission related metrics for a admission webhook.
|
||||||
func (m *AdmissionMetrics) ObserveWebhook(elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
func (m *AdmissionMetrics) ObserveWebhook(ctx context.Context, elapsed time.Duration, rejected bool, attr admission.Attributes, stepType string, extraLabels ...string) {
|
||||||
m.webhook.observe(elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
m.webhook.observe(ctx, elapsed, append(extraLabels, stepType, string(attr.GetOperation()), strconv.FormatBool(rejected))...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObserveWebhookRejection records admission related metrics for an admission webhook rejection.
|
// ObserveWebhookRejection records admission related metrics for an admission webhook rejection.
|
||||||
func (m *AdmissionMetrics) ObserveWebhookRejection(name, stepType, operation string, errorType WebhookRejectionErrorType, rejectionCode int) {
|
func (m *AdmissionMetrics) ObserveWebhookRejection(ctx context.Context, name, stepType, operation string, errorType WebhookRejectionErrorType, rejectionCode int) {
|
||||||
// We truncate codes greater than 600 to keep the cardinality bounded.
|
// We truncate codes greater than 600 to keep the cardinality bounded.
|
||||||
// This should be rarely done by a malfunctioning webhook server.
|
// This should be rarely done by a malfunctioning webhook server.
|
||||||
if rejectionCode > 600 {
|
if rejectionCode > 600 {
|
||||||
rejectionCode = 600
|
rejectionCode = 600
|
||||||
}
|
}
|
||||||
m.webhookRejection.WithLabelValues(name, stepType, operation, string(errorType), strconv.Itoa(rejectionCode)).Inc()
|
m.webhookRejection.WithContext(ctx).WithLabelValues(name, stepType, operation, string(errorType), strconv.Itoa(rejectionCode)).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
type metricSet struct {
|
type metricSet struct {
|
||||||
@ -242,10 +242,10 @@ func (m *metricSet) 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(ctx context.Context, elapsed time.Duration, labels ...string) {
|
||||||
elapsedSeconds := elapsed.Seconds()
|
elapsedSeconds := elapsed.Seconds()
|
||||||
m.latencies.WithLabelValues(labels...).Observe(elapsedSeconds)
|
m.latencies.WithContext(ctx).WithLabelValues(labels...).Observe(elapsedSeconds)
|
||||||
if m.latenciesSummary != nil {
|
if m.latenciesSummary != nil {
|
||||||
m.latenciesSummary.WithLabelValues(labels...).Observe(elapsedSeconds)
|
m.latenciesSummary.WithContext(ctx).WithLabelValues(labels...).Observe(elapsedSeconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func TestObserveAdmissionController(t *testing.T) {
|
|||||||
|
|
||||||
func TestObserveWebhook(t *testing.T) {
|
func TestObserveWebhook(t *testing.T) {
|
||||||
Metrics.reset()
|
Metrics.reset()
|
||||||
Metrics.ObserveWebhook(2*time.Second, false, attr, stepAdmit, "x")
|
Metrics.ObserveWebhook(context.TODO(), 2*time.Second, false, attr, stepAdmit, "x")
|
||||||
wantLabels := map[string]string{
|
wantLabels := map[string]string{
|
||||||
"name": "x",
|
"name": "x",
|
||||||
"operation": string(admission.Create),
|
"operation": string(admission.Create),
|
||||||
@ -91,9 +91,9 @@ func TestObserveWebhook(t *testing.T) {
|
|||||||
|
|
||||||
func TestObserveWebhookRejection(t *testing.T) {
|
func TestObserveWebhookRejection(t *testing.T) {
|
||||||
Metrics.reset()
|
Metrics.reset()
|
||||||
Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 500)
|
Metrics.ObserveWebhookRejection(context.TODO(), "x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 500)
|
||||||
Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 654)
|
Metrics.ObserveWebhookRejection(context.TODO(), "x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 654)
|
||||||
Metrics.ObserveWebhookRejection("x", stepValidate, string(admission.Update), WebhookRejectionCallingWebhookError, 0)
|
Metrics.ObserveWebhookRejection(context.TODO(), "x", stepValidate, string(admission.Update), WebhookRejectionCallingWebhookError, 0)
|
||||||
wantLabels := map[string]string{
|
wantLabels := map[string]string{
|
||||||
"name": "x",
|
"name": "x",
|
||||||
"operation": string(admission.Create),
|
"operation": string(admission.Create),
|
||||||
|
@ -142,17 +142,17 @@ func (a *mutatingDispatcher) Dispatch(ctx context.Context, attr admission.Attrib
|
|||||||
case *webhookutil.ErrCallingWebhook:
|
case *webhookutil.ErrCallingWebhook:
|
||||||
if !ignoreClientCallFailures {
|
if !ignoreClientCallFailures {
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionCallingWebhookError, 0)
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionCallingWebhookError, 0)
|
||||||
}
|
}
|
||||||
case *webhookutil.ErrWebhookRejection:
|
case *webhookutil.ErrWebhookRejection:
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionNoError, int(err.Status.ErrStatus.Code))
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionNoError, int(err.Status.ErrStatus.Code))
|
||||||
default:
|
default:
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionAPIServerInternalError, 0)
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "admit", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionAPIServerInternalError, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
admissionmetrics.Metrics.ObserveWebhook(time.Since(t), rejected, versionedAttr.Attributes, "admit", hook.Name)
|
admissionmetrics.Metrics.ObserveWebhook(ctx, time.Since(t), rejected, versionedAttr.Attributes, "admit", hook.Name)
|
||||||
if changed {
|
if changed {
|
||||||
// Patch had changed the object. Prepare to reinvoke all previous webhooks that are eligible for re-invocation.
|
// Patch had changed the object. Prepare to reinvoke all previous webhooks that are eligible for re-invocation.
|
||||||
webhookReinvokeCtx.RequireReinvokingPreviouslyInvokedPlugins()
|
webhookReinvokeCtx.RequireReinvokingPreviouslyInvokedPlugins()
|
||||||
|
@ -109,17 +109,17 @@ func (d *validatingDispatcher) Dispatch(ctx context.Context, attr admission.Attr
|
|||||||
case *webhookutil.ErrCallingWebhook:
|
case *webhookutil.ErrCallingWebhook:
|
||||||
if !ignoreClientCallFailures {
|
if !ignoreClientCallFailures {
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionCallingWebhookError, 0)
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionCallingWebhookError, 0)
|
||||||
}
|
}
|
||||||
case *webhookutil.ErrWebhookRejection:
|
case *webhookutil.ErrWebhookRejection:
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionNoError, int(err.Status.ErrStatus.Code))
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionNoError, int(err.Status.ErrStatus.Code))
|
||||||
default:
|
default:
|
||||||
rejected = true
|
rejected = true
|
||||||
admissionmetrics.Metrics.ObserveWebhookRejection(hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionAPIServerInternalError, 0)
|
admissionmetrics.Metrics.ObserveWebhookRejection(ctx, hook.Name, "validating", string(versionedAttr.Attributes.GetOperation()), admissionmetrics.WebhookRejectionAPIServerInternalError, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
admissionmetrics.Metrics.ObserveWebhook(time.Since(t), rejected, versionedAttr.Attributes, "validating", hook.Name)
|
admissionmetrics.Metrics.ObserveWebhook(ctx, time.Since(t), rejected, versionedAttr.Attributes, "validating", hook.Name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user