From b896ca403bdc832eb37ba8c06f02fe2bd8c69521 Mon Sep 17 00:00:00 2001 From: Tobias Giese Date: Mon, 12 Jul 2021 08:54:13 +0200 Subject: [PATCH] implement mc.ObserveRequestIgnoreNotFound --- pkg/cloud/services/compute/instance.go | 12 ++++++------ pkg/metrics/metrics.go | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/cloud/services/compute/instance.go b/pkg/cloud/services/compute/instance.go index e0b278c33d..72a0b24bf6 100644 --- a/pkg/cloud/services/compute/instance.go +++ b/pkg/cloud/services/compute/instance.go @@ -707,12 +707,12 @@ func (s *Service) deleteAttachInterface(eventObject runtime.Object, instanceID, mc := metrics.NewMetricPrometheusContext("router_interface", "delete") err = attachinterfaces.Delete(s.computeClient, instanceID, portID).ExtractErr() - if err != nil { + if mc.ObserveRequestIgnoreNotFound(err) != nil { if capoerrors.IsNotFound(err) { return nil } record.Warnf(eventObject, "FailedDeleteAttachInterface", "Failed to delete attach interface: instance %s, port %s: %v", instance.ID, port.ID, err) - return mc.ObserveRequest(err) + return err } record.Eventf(eventObject, "SuccessfulDeleteAttachInterface", "Deleted attach interface: instance %s, port %s", instance.ID, port.ID) @@ -769,11 +769,11 @@ func (s *Service) getPort(portID string) (port *ports.Port, err error) { } mc := metrics.NewMetricPrometheusContext("port", "get") port, err = ports.Get(s.networkClient, portID).Extract() - if err != nil { + if mc.ObserveRequestIgnoreNotFound(err) != nil { if capoerrors.IsNotFound(err) { return nil, nil } - return nil, fmt.Errorf("get port %q detail failed: %v", portID, mc.ObserveRequest(err)) + return nil, fmt.Errorf("get port %q detail failed: %v", portID, err) } return port, nil } @@ -819,11 +819,11 @@ func (s *Service) GetInstance(resourceID string) (instance *infrav1.Instance, er } mc := metrics.NewMetricPrometheusContext("server", "get") server, err := servers.Get(s.computeClient, resourceID).Extract() - if err != nil { + if mc.ObserveRequestIgnoreNotFound(err) != nil { if capoerrors.IsNotFound(err) { return nil, nil } - return nil, fmt.Errorf("get server %q detail failed: %v", resourceID, mc.ObserveRequest(err)) + return nil, fmt.Errorf("get server %q detail failed: %v", resourceID, err) } i, err := serverToInstance(server) if err != nil { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 7882b99e59..190ea4ebba 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -21,6 +21,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors" "sigs.k8s.io/controller-runtime/pkg/metrics" ) @@ -50,6 +51,14 @@ func (mc *MetricPrometheusContext) ObserveRequest(err error) error { return mc.Observe(apiRequestPrometheusMetrics, err) } +// ObserveRequestIgnoreNotFound records the request latency and counts the errors if it's not IsNotFound. +func (mc *MetricPrometheusContext) ObserveRequestIgnoreNotFound(err error) error { + if capoerrors.IsNotFound(err) { + return mc.Observe(apiRequestPrometheusMetrics, nil) + } + return mc.Observe(apiRequestPrometheusMetrics, err) +} + // Observe records the request latency and counts the errors. func (mc *MetricPrometheusContext) Observe(om *OpenstackPrometheusMetrics, err error) error { if om == nil {