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

[occm] API requests & Loadbalancer reconciliation metrics #1178

Merged
merged 3 commits into from
Sep 24, 2020
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/onsi/gomega v1.9.0
github.com/pborman/uuid v1.2.0
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/prometheus/client_golang v1.7.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudprovider/.import-restrictions
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"k8s.io/api",
"k8s.io/client-go",
"k8s.io/cloud-provider",
"k8s.io/component-base",
"k8s.io/klog",
"k8s.io/utils"
],
Expand Down
122 changes: 122 additions & 0 deletions pkg/cloudprovider/providers/openstack/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to move this to a generic location so that other component can benefit it later on

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you everyone for reviewing and approving this PR.
Refactoring / moving this to a generic location is handled within #1236.


import (
"sync"
"time"

"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)

type openstackMetrics struct {
duration *metrics.HistogramVec
total *metrics.CounterVec
errors *metrics.CounterVec
}

var (
reconcileMetrics = &openstackMetrics{
duration: metrics.NewHistogramVec(
&metrics.HistogramOpts{
Name: "cloudprovider_openstack_reconcile_duration_seconds",
Help: "Time taken by various parts of OpenStack cloud controller manager reconciliation loops",
Buckets: []float64{0.01, 0.05, 0.1, 0.5, 1.0, 2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 22.5, 25.0, 27.5, 30.0, 50.0, 75.0, 100.0, 1000.0},
}, []string{"operation"}),
total: metrics.NewCounterVec(
&metrics.CounterOpts{
Name: "cloudprovider_openstack_reconcile_total",
Help: "Total number of OpenStack cloud controller manager reconciliations",
}, []string{"operation"}),
errors: metrics.NewCounterVec(
&metrics.CounterOpts{
Name: "cloudprovider_openstack_reconcile_errors_total",
Help: "Total number of OpenStack cloud controller manager reconciliation errors",
}, []string{"operation"}),
}
requestMetrics = &openstackMetrics{
duration: metrics.NewHistogramVec(
&metrics.HistogramOpts{
Name: "openstack_api_request_duration_seconds",
Help: "Latency of an OpenStack API call",
}, []string{"request"}),
total: metrics.NewCounterVec(
&metrics.CounterOpts{
Name: "openstack_api_requests_total",
Help: "Total number of OpenStack API calls",
}, []string{"request"}),
errors: metrics.NewCounterVec(
&metrics.CounterOpts{
Name: "openstack_api_request_errors_total",
Help: "Total number of errors for an OpenStack API call",
}, []string{"request"}),
}
)

// MetricContext indicates the context for OpenStack metrics.
type MetricContext struct {
start time.Time
attributes []string
}

// NewMetricContext creates a new MetricContext.
func NewMetricContext(resource string, request string) *MetricContext {
return &MetricContext{
start: time.Now(),
attributes: []string{resource + "_" + request},
}
}

// ObserveReconcile records reconciliation duration,
// frequency and number of errors.
func (mc *MetricContext) ObserveReconcile(err error) error {
reconcileMetrics.duration.WithLabelValues(mc.attributes...).Observe(
time.Since(mc.start).Seconds())
reconcileMetrics.total.WithLabelValues(mc.attributes...).Inc()
if err != nil {
reconcileMetrics.errors.WithLabelValues(mc.attributes...).Inc()
}
return err
}

// ObserveRequest records the request latency and counts the errors.
func (mc *MetricContext) ObserveRequest(err error) error {
requestMetrics.duration.WithLabelValues(mc.attributes...).Observe(
time.Since(mc.start).Seconds())
requestMetrics.total.WithLabelValues(mc.attributes...).Inc()
if err != nil {
requestMetrics.errors.WithLabelValues(mc.attributes...).Inc()
}
return err
}

var registerMetrics sync.Once

// RegisterMetrics registers OpenStack metrics.
func RegisterMetrics() {
registerMetrics.Do(func() {
legacyregistry.MustRegister(
reconcileMetrics.duration,
reconcileMetrics.total,
reconcileMetrics.errors,
requestMetrics.duration,
requestMetrics.total,
requestMetrics.errors,
)
})
}
23 changes: 15 additions & 8 deletions pkg/cloudprovider/providers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
netutil "k8s.io/apimachinery/pkg/util/net"
certutil "k8s.io/client-go/util/cert"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
"k8s.io/cloud-provider-openstack/pkg/version"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -290,7 +291,7 @@ func (l Logger) Printf(format string, args ...interface{}) {
}

func init() {
RegisterMetrics()
metrics.RegisterMetrics()

cloudprovider.RegisterCloudProvider(ProviderName, func(config io.Reader) (cloudprovider.Interface, error) {
cfg, err := ReadConfig(config)
Expand Down Expand Up @@ -627,8 +628,9 @@ func (os *OpenStack) GetNodeNameByID(instanceID string) (types.NodeName, error)
return nodeName, err
}

mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(client, instanceID).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
return nodeName, err
}
nodeName = mapServerToNodeName(server)
Expand All @@ -644,6 +646,7 @@ func mapServerToNodeName(server *servers.Server) types.NodeName {
}

func foreachServer(client *gophercloud.ServiceClient, opts servers.ListOptsBuilder, handler func(*servers.Server) (bool, error)) error {
mc := metrics.NewMetricContext("server", "list")
pager := servers.List(client, opts)

err := pager.EachPage(func(page pagination.Page) (bool, error) {
Expand All @@ -659,19 +662,20 @@ func foreachServer(client *gophercloud.ServiceClient, opts servers.ListOptsBuild
}
return true, nil
})
return err
return mc.ObserveRequest(err)
}

func getServerByName(client *gophercloud.ServiceClient, name types.NodeName) (*ServerAttributesExt, error) {
opts := servers.ListOpts{
Name: fmt.Sprintf("^%s$", regexp.QuoteMeta(mapNodeNameToServerName(name))),
}

pager := servers.List(client, opts)

var s []ServerAttributesExt
serverList := make([]ServerAttributesExt, 0, 1)

mc := metrics.NewMetricContext("server", "list")
pager := servers.List(client, opts)

err := pager.EachPage(func(page pagination.Page) (bool, error) {
if err := servers.ExtractServersInto(page, &s); err != nil {
return false, err
Expand All @@ -682,7 +686,7 @@ func getServerByName(client *gophercloud.ServiceClient, name types.NodeName) (*S
}
return true, nil
})
if err != nil {
if mc.ObserveRequest(err) != nil {
return nil, err
}

Expand Down Expand Up @@ -854,6 +858,7 @@ func getAddressByName(client *gophercloud.ServiceClient, name types.NodeName, ne
func getAttachedInterfacesByID(client *gophercloud.ServiceClient, serviceID string) ([]attachinterfaces.Interface, error) {
var interfaces []attachinterfaces.Interface

mc := metrics.NewMetricContext("server_os_interface", "list")
pager := attachinterfaces.List(client, serviceID)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
s, err := attachinterfaces.ExtractInterfaces(page)
Expand All @@ -863,7 +868,7 @@ func getAttachedInterfacesByID(client *gophercloud.ServiceClient, serviceID stri
interfaces = append(interfaces, s...)
return true, nil
})
if err != nil {
if mc.ObserveRequest(err) != nil {
return interfaces, err
}

Expand Down Expand Up @@ -956,7 +961,9 @@ func (os *OpenStack) GetZoneByProviderID(ctx context.Context, providerID string)
}

var serverWithAttributesExt ServerAttributesExt
if err := servers.Get(compute, instanceID).ExtractInto(&serverWithAttributesExt); err != nil {
mc := metrics.NewMetricContext("server", "get")
err = servers.Get(compute, instanceID).ExtractInto(&serverWithAttributesExt)
if mc.ObserveRequest(err) != nil {
return cloudprovider.Zone{}, err
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/cloudprovider/providers/openstack/openstack_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
)
Expand Down Expand Up @@ -111,9 +112,10 @@ func (i *Instances) NodeAddressesByProviderID(ctx context.Context, providerID st
return []v1.NodeAddress{}, err
}

mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(i.compute, instanceID).Extract()

if err != nil {
if mc.ObserveRequest(err) != nil {
return []v1.NodeAddress{}, err
}

Expand Down Expand Up @@ -143,8 +145,9 @@ func (i *Instances) InstanceExistsByProviderID(ctx context.Context, providerID s
return false, err
}

mc := metrics.NewMetricContext("server", "get")
_, err = servers.Get(i.compute, instanceID).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
if errors.IsNotFound(err) {
return false, nil
}
Expand All @@ -168,8 +171,9 @@ func (i *Instances) InstanceShutdownByProviderID(ctx context.Context, providerID
return false, err
}

mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(i.compute, instanceID).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
return false, err
}

Expand Down Expand Up @@ -249,9 +253,10 @@ func (i *Instances) InstanceTypeByProviderID(ctx context.Context, providerID str
return "", err
}

mc := metrics.NewMetricContext("server", "get")
server, err := servers.Get(i.compute, instanceID).Extract()

if err != nil {
if mc.ObserveRequest(err) != nil {
return "", err
}

Expand Down
Loading