-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactory metrics code so that additional code can consume it (#1236)
- Loading branch information
Showing
11 changed files
with
197 additions
and
129 deletions.
There are no files selected for viewing
122 changes: 0 additions & 122 deletions
122
pkg/cloudprovider/providers/openstack/metrics/metrics.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters