Skip to content

Commit

Permalink
refactory metrics code so that additional code can consume it (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
jichenjc authored Jan 29, 2021
1 parent da85a2c commit c700948
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 129 deletions.
122 changes: 0 additions & 122 deletions pkg/cloudprovider/providers/openstack/metrics/metrics.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/cloudprovider/providers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +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/metrics"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
"k8s.io/cloud-provider-openstack/pkg/version"
"k8s.io/klog/v2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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/metrics"
"k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
cloudprovider "k8s.io/cloud-provider"
klog "k8s.io/klog/v2"

"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/metrics"
cpoutil "k8s.io/cloud-provider-openstack/pkg/util"
cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
netsets "k8s.io/cloud-provider-openstack/pkg/util/net/sets"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/providers/openstack/openstack_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

"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/metrics"
"k8s.io/klog/v2"
)

Expand Down
65 changes: 65 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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

import (
"time"

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

type OpenstackMetrics struct {
Duration *metrics.HistogramVec
Total *metrics.CounterVec
Errors *metrics.CounterVec
}

// MetricContext indicates the context for OpenStack metrics.
type MetricContext struct {
Start time.Time
Attributes []string
Metrics *OpenstackMetrics
}

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

// ObserveRequest records the request latency and counts the errors.
func (mc *MetricContext) Observe(om *OpenstackMetrics, err error) error {
if om == nil {
// mc.RequestMetrics not set, ignore this request
return nil
}

om.Duration.WithLabelValues(mc.Attributes...).Observe(
time.Since(mc.Start).Seconds())
om.Total.WithLabelValues(mc.Attributes...).Inc()
if err != nil {
om.Errors.WithLabelValues(mc.Attributes...).Inc()
}
return err
}

func RegisterMetrics() {
doRegisterAPIMetrics()
doRegisterOccmMetrics()
}
62 changes: 62 additions & 0 deletions pkg/metrics/metrics_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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

import (
"sync"

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

var (
APIRequestMetrics = &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"}),
}
)

// ObserveRequest records the request latency and counts the errors.
func (mc *MetricContext) ObserveRequest(err error) error {
return mc.Observe(APIRequestMetrics, err)
}

var registerAPIMetrics sync.Once

// RegisterMetrics registers OpenStack metrics.
func doRegisterAPIMetrics() {
registerAPIMetrics.Do(func() {
legacyregistry.MustRegister(
APIRequestMetrics.Duration,
APIRequestMetrics.Total,
APIRequestMetrics.Errors,
)
})
}
63 changes: 63 additions & 0 deletions pkg/metrics/metrics_reconcile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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

import (
"sync"

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

var (
occmReconcileMetrics = &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"}),
}
)

// ObserveReconcile records the request reconciliation duration
func (mc *MetricContext) ObserveReconcile(err error) error {
return mc.Observe(occmReconcileMetrics, err)
}

var registerOccmMetrics sync.Once

// RegisterMetrics registers OpenStack metrics.
func doRegisterOccmMetrics() {
registerOccmMetrics.Do(func() {
legacyregistry.MustRegister(
occmReconcileMetrics.Duration,
occmReconcileMetrics.Total,
occmReconcileMetrics.Errors,
)
})
}
2 changes: 1 addition & 1 deletion pkg/util/openstack/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/keymanager/v1/secrets"
"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/metrics"
)

// EnsureSecret creates a secret if it doesn't exist.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/openstack/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/gophercloud/gophercloud/pagination"
version "github.com/hashicorp/go-version"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/metrics"
klog "k8s.io/klog/v2"

cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/openstack/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package openstack
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"k8s.io/cloud-provider-openstack/pkg/cloudprovider/providers/openstack/metrics"
"k8s.io/cloud-provider-openstack/pkg/metrics"
)

// GetFloatingIPs returns all the filtered floating IPs
Expand Down

0 comments on commit c700948

Please sign in to comment.