Skip to content

Commit

Permalink
implement mc.ObserveRequestIgnoreNotFound
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasgiese committed Jul 12, 2021
1 parent baafaef commit b896ca4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b896ca4

Please sign in to comment.