-
Notifications
You must be signed in to change notification settings - Fork 620
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
k8s-ci-robot
merged 3 commits into
kubernetes:master
from
mercedes-benz:cloudprovider-metrics
Sep 24, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
122 changes: 122 additions & 0 deletions
122
pkg/cloudprovider/providers/openstack/metrics/metrics.go
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,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 | ||
|
||
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, | ||
) | ||
}) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.