Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add GET gophercloud metrics #932

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/cloud/services/compute/availabilityzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"fmt"

"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"

"sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics"
)

func (s *Service) GetAvailabilityZones() ([]availabilityzones.AvailabilityZone, error) {
mc := metrics.NewMetricPrometheusContext("availability_zone", "list")
allPages, err := availabilityzones.List(s.computeClient).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, fmt.Errorf("error getting availability zone list: %v", err)
}
availabilityZoneList, err := availabilityzones.ExtractAvailabilityZones(allPages)
Expand Down
41 changes: 26 additions & 15 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ func applyServerGroupID(opts servers.CreateOptsBuilder, serverGroupID string) se
}

func (s *Service) getTrunkSupport() (bool, error) {
mc := metrics.NewMetricPrometheusContext("network_extension", "list")
allPages, err := netext.List(s.networkClient).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
}

Expand All @@ -357,8 +358,9 @@ func (s *Service) getSecurityGroups(securityGroupParams []infrav1.SecurityGroupP
}
listOpts.Name = sg.Name
listOpts.ID = sg.UUID
mc := metrics.NewMetricPrometheusContext("security_group", "list")
pages, err := groups.List(s.networkClient, listOpts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}

Expand Down Expand Up @@ -422,11 +424,12 @@ func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]inf
}

func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string) (*ports.Port, error) {
mc := metrics.NewMetricPrometheusContext("port", "list")
allPages, err := ports.List(s.networkClient, ports.ListOpts{
Name: portName,
NetworkID: net.ID,
}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, fmt.Errorf("searching for existing port for server: %v", err)
}
existingPorts, err := ports.ExtractPorts(allPages)
Expand Down Expand Up @@ -491,8 +494,8 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
if len(fixedIPs) > 0 {
createOpts.FixedIPs = fixedIPs
}
mc := metrics.NewMetricPrometheusContext("port", "create")

mc = metrics.NewMetricPrometheusContext("port", "create")
port, err := ports.Create(s.networkClient, portsbinding.CreateOptsExt{
CreateOptsBuilder: createOpts,
HostID: portOpts.HostID,
Expand All @@ -509,11 +512,12 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
}

func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trunkName, portID string) (*trunks.Trunk, error) {
mc := metrics.NewMetricPrometheusContext("trunk", "list")
allPages, err := trunks.List(s.networkClient, trunks.ListOpts{
Name: trunkName,
PortID: portID,
}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, fmt.Errorf("searching for existing trunk for server: %v", err)
}
trunkList, err := trunks.ExtractTrunks(allPages)
Expand All @@ -530,8 +534,8 @@ func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trun
PortID: portID,
Description: names.GetDescription(clusterName),
}
mc := metrics.NewMetricPrometheusContext("trunk", "create")

mc = metrics.NewMetricPrometheusContext("trunk", "create")
trunk, err := trunks.Create(s.networkClient, trunkCreateOpts).Extract()
if mc.ObserveRequest(err) != nil {
record.Warnf(eventObject, "FailedCreateTrunk", "Failed to create trunk %s: %v", trunkName, err)
Expand Down Expand Up @@ -566,8 +570,9 @@ func (s *Service) getImageID(imageName string) (string, error) {
Name: imageName,
}

mc := metrics.NewMetricPrometheusContext("image", "list")
pages, err := images.List(s.imagesClient, opts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return "", err
}

Expand Down Expand Up @@ -608,8 +613,9 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instanceName string
return nil
}

mc := metrics.NewMetricPrometheusContext("server_os_interface", "list")
allInterfaces, err := attachinterfaces.List(s.computeClient, instance.ID).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return err
}
instanceInterfaces, err := attachinterfaces.ExtractInterfaces(allInterfaces)
Expand Down Expand Up @@ -699,9 +705,9 @@ func (s *Service) deleteAttachInterface(eventObject runtime.Object, instanceID,
return nil
}

mc := metrics.NewMetricPrometheusContext("router_interface", "delete")
mc := metrics.NewMetricPrometheusContext("server_os_interface", "delete")
err = attachinterfaces.Delete(s.computeClient, instanceID, portID).ExtractErr()
if mc.ObserveRequest(err) != nil {
if mc.ObserveRequestIgnoreNotFound(err) != nil {
if capoerrors.IsNotFound(err) {
return nil
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -725,8 +731,9 @@ func (s *Service) deleteTrunk(eventObject runtime.Object, portID string) error {
listOpts := trunks.ListOpts{
PortID: port.ID,
}
mc := metrics.NewMetricPrometheusContext("trunk", "list")
trunkList, err := trunks.List(s.networkClient, listOpts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return err
}
trunkInfo, err := trunks.ExtractTrunks(trunkList)
Expand All @@ -738,7 +745,8 @@ func (s *Service) deleteTrunk(eventObject runtime.Object, portID string) error {
}

err = util.PollImmediate(retryIntervalTrunkDelete, timeoutTrunkDelete, func() (bool, error) {
if err := trunks.Delete(s.networkClient, trunkInfo[0].ID).ExtractErr(); err != nil {
mc := metrics.NewMetricPrometheusContext("trunk", "delete")
if err := trunks.Delete(s.networkClient, trunkInfo[0].ID).ExtractErr(); mc.ObserveRequest(err) != nil {
if capoerrors.IsRetryable(err) {
return false, nil
}
Expand All @@ -759,8 +767,9 @@ func (s *Service) getPort(portID string) (port *ports.Port, err error) {
if portID == "" {
return nil, fmt.Errorf("portID should be specified to get detail")
}
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) {
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
Expand Down Expand Up @@ -808,8 +817,9 @@ func (s *Service) GetInstance(resourceID string) (instance *infrav1.Instance, er
if resourceID == "" {
return nil, fmt.Errorf("resourceId should be specified to get detail")
}
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) {
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
Expand All @@ -835,8 +845,9 @@ func (s *Service) InstanceExists(name string) (instance *infrav1.Instance, err e
listOpts = servers.ListOpts{}
}

mc := metrics.NewMetricPrometheusContext("server", "list")
allPages, err := servers.List(s.computeClient, listOpts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, fmt.Errorf("get server list: %v", err)
}
serverList, err := servers.ExtractServers(allPages)
Expand Down
21 changes: 14 additions & 7 deletions pkg/cloud/services/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ func getLoadBalancerName(clusterName string) string {
}

func (s *Service) checkIfLbExists(name string) (*loadbalancers.LoadBalancer, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer", "list")
allPages, err := loadbalancers.List(s.loadbalancerClient, loadbalancers.ListOpts{Name: name}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
lbList, err := loadbalancers.ExtractLoadBalancers(allPages)
Expand All @@ -437,8 +438,9 @@ func (s *Service) checkIfLbExists(name string) (*loadbalancers.LoadBalancer, err
}

func (s *Service) checkIfListenerExists(name string) (*listeners.Listener, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer_listener", "list")
allPages, err := listeners.List(s.loadbalancerClient, listeners.ListOpts{Name: name}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
listenerList, err := listeners.ExtractListeners(allPages)
Expand All @@ -452,8 +454,9 @@ func (s *Service) checkIfListenerExists(name string) (*listeners.Listener, error
}

func (s *Service) checkIfPoolExists(name string) (*pools.Pool, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer_pool", "list")
allPages, err := pools.List(s.loadbalancerClient, pools.ListOpts{Name: name}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
poolList, err := pools.ExtractPools(allPages)
Expand All @@ -467,8 +470,9 @@ func (s *Service) checkIfPoolExists(name string) (*pools.Pool, error) {
}

func (s *Service) checkIfMonitorExists(name string) (*monitors.Monitor, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer_healthmonitor", "list")
allPages, err := monitors.List(s.loadbalancerClient, monitors.ListOpts{Name: name}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
monitorList, err := monitors.ExtractMonitors(allPages)
Expand All @@ -482,8 +486,9 @@ func (s *Service) checkIfMonitorExists(name string) (*monitors.Monitor, error) {
}

func (s *Service) checkIfLbMemberExists(poolID, name string) (*pools.Member, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer_pool", "list")
allPages, err := pools.ListMembers(s.loadbalancerClient, poolID, pools.ListMembersOpts{Name: name}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
lbMemberList, err := pools.ExtractMembers(allPages)
Expand All @@ -507,8 +512,9 @@ var backoff = wait.Backoff{
func (s *Service) waitForLoadBalancerActive(id string) error {
s.logger.Info("Waiting for load balancer", "id", id, "targetStatus", "ACTIVE")
return wait.ExponentialBackoff(backoff, func() (bool, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer", "get")
lb, err := loadbalancers.Get(s.loadbalancerClient, id).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
}
return lb.ProvisioningStatus == "ACTIVE", nil
Expand All @@ -518,8 +524,9 @@ func (s *Service) waitForLoadBalancerActive(id string) error {
func (s *Service) waitForListener(id, target string) error {
s.logger.Info("Waiting for load balancer listener", "id", id, "targetStatus", target)
return wait.ExponentialBackoff(backoff, func() (bool, error) {
mc := metrics.NewMetricPrometheusContext("loadbalancer_listener", "get")
_, err := listeners.Get(s.loadbalancerClient, id).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
}
// The listener resource has no Status attribute, so a successful Get is the best we can do
Expand Down
9 changes: 6 additions & 3 deletions pkg/cloud/services/networking/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust
}

func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
allPages, err := floatingips.List(s.client, floatingips.ListOpts{FloatingIP: ip}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
fpList, err := floatingips.ExtractFloatingIPs(allPages)
Expand All @@ -86,8 +87,9 @@ func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, e
}

func (s *Service) GetFloatingIPByPortID(portID string) (*floatingips.FloatingIP, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "list")
allPages, err := floatingips.List(s.client, floatingips.ListOpts{PortID: portID}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}
fpList, err := floatingips.ExtractFloatingIPs(allPages)
Expand Down Expand Up @@ -186,8 +188,9 @@ func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackClus
func (s *Service) waitForFloatingIP(id, target string) error {
s.logger.Info("Waiting for floating IP", "id", id, "targetStatus", target)
return wait.ExponentialBackoff(backoff, func() (bool, error) {
mc := metrics.NewMetricPrometheusContext("floating_ip", "get")
fip, err := floatingips.Get(s.client, id).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
}
return fip.Status == target, nil
Expand Down
18 changes: 12 additions & 6 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (s *Service) ReconcileExternalNetwork(openStackCluster *infrav1.OpenStackCl
External: &iTrue,
}

mc := metrics.NewMetricPrometheusContext("network", "list")
allPages, err := networks.List(s.client, listOpts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return err
}
networkList, err := networks.ExtractNetworks(allPages)
Expand Down Expand Up @@ -180,11 +181,12 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl
subnetName := getSubnetName(clusterName)
s.logger.Info("Reconciling subnet", "name", subnetName)

mc := metrics.NewMetricPrometheusContext("subnet", "list")
allPages, err := subnets.List(s.client, subnets.ListOpts{
NetworkID: openStackCluster.Status.Network.ID,
CIDR: openStackCluster.Spec.NodeCIDR,
}).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return err
}

Expand Down Expand Up @@ -255,8 +257,9 @@ func (s *Service) getNetworkByID(networkID string) (networks.Network, error) {
ID: networkID,
}

mc := metrics.NewMetricPrometheusContext("network", "list")
allPages, err := networks.List(s.client, opts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return networks.Network{}, err
}

Expand All @@ -279,8 +282,9 @@ func (s *Service) getNetworkByName(networkName string) (networks.Network, error)
Name: networkName,
}

mc := metrics.NewMetricPrometheusContext("network", "list")
allPages, err := networks.List(s.client, opts).AllPages()
if err != nil {
if mc.ObserveRequest(err) != nil {
return networks.Network{}, err
}

Expand All @@ -303,11 +307,12 @@ func (s *Service) GetNetworksByFilter(opts networks.ListOptsBuilder) ([]networks
if opts == nil {
return nil, fmt.Errorf("no Filters were passed")
}
mc := metrics.NewMetricPrometheusContext("network", "list")
pager := networks.List(s.client, opts)
var nets []networks.Network
err := pager.EachPage(func(page pagination.Page) (bool, error) {
networkList, err := networks.ExtractNetworks(page)
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
} else if len(networkList) == 0 {
return false, fmt.Errorf("no networks could be found with the filters provided")
Expand Down Expand Up @@ -339,11 +344,12 @@ func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Su
if opts == nil {
return []subnets.Subnet{}, fmt.Errorf("no Filters were passed")
}
mc := metrics.NewMetricPrometheusContext("subnet", "list")
pager := subnets.List(s.client, opts)
var snets []subnets.Subnet
err := pager.EachPage(func(page pagination.Page) (bool, error) {
subnetList, err := subnets.ExtractSubnets(page)
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
} else if len(subnetList) == 0 {
return false, fmt.Errorf("no subnets could be found with the filters provided")
Expand Down
Loading