-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add new handler for metrics #3436
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
Merged
bharathi-tenneti
merged 1 commit into
operator-framework:master
from
bharathi-tenneti:metrics_handler
Jul 16, 2020
Merged
Changes from all commits
Commits
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 hidden or 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,7 @@ | ||
| entries: | ||
| - description: > | ||
| Add new handler for metrics which can be registered with controller-runtime registry, | ||
| and wraps existing EnqueueResuestForObject, for both Ansible and Helm controllers. | ||
|
|
||
| kind: "addition" | ||
| breaking: false |
This file contains hidden or 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 hidden or 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,81 @@ | ||
| // Copyright 2020 The Operator-SDK Authors | ||
|
bharathi-tenneti marked this conversation as resolved.
|
||
| // | ||
| // 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 handler | ||
|
|
||
| import ( | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/util/workqueue" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
|
|
||
| "github.com/operator-framework/operator-sdk/pkg/handler/internal/metrics" | ||
| ) | ||
|
|
||
|
bharathi-tenneti marked this conversation as resolved.
|
||
| // InstrumentedEnqueueRequestForObject wraps controller-runtime handler for "EnqueueRequestForObject", and | ||
| // sets up primary resource metrics on event handlers. The main objective of this handler is to set | ||
| // prometheues metrics when create/update/delete events occur. These metrics contain below information on resource. | ||
| // | ||
| // resource_created_at_seconds{"name", "namespace", "group", "version", "kind"} | ||
| // | ||
| // '&handler.InstrumentedEnqueueRequestForObject{}' is used to call the handler. | ||
| type InstrumentedEnqueueRequestForObject struct { | ||
| handler.EnqueueRequestForObject | ||
| } | ||
|
|
||
| // Create implements EventHandler, and creates the metrics. | ||
| func (h InstrumentedEnqueueRequestForObject) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
| setResourceMetric(e.Meta, e.Object) | ||
| h.EnqueueRequestForObject.Create(e, q) | ||
| } | ||
|
|
||
| // Update implements EventHandler, and updates the metrics. | ||
| func (h InstrumentedEnqueueRequestForObject) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
| setResourceMetric(e.MetaOld, e.ObjectOld) | ||
| setResourceMetric(e.MetaNew, e.ObjectNew) | ||
|
|
||
| h.EnqueueRequestForObject.Update(e, q) | ||
| } | ||
|
|
||
| // Delete implements EventHandler, and deletes metrics. | ||
| func (h InstrumentedEnqueueRequestForObject) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) { | ||
| deleteResourceMetric(e.Meta, e.Object) | ||
| h.EnqueueRequestForObject.Delete(e, q) | ||
| } | ||
|
|
||
| func setResourceMetric(metadata metav1.Object, obj runtime.Object) { | ||
| if metadata != nil && obj != nil { | ||
| labels := getResourceLabels(metadata, obj) | ||
| m, _ := metrics.ResourceCreatedAt.GetMetricWith(labels) | ||
| m.Set(float64(metadata.GetCreationTimestamp().UTC().Unix())) | ||
| } | ||
| } | ||
|
|
||
| func deleteResourceMetric(metadata metav1.Object, obj runtime.Object) { | ||
| if metadata != nil && obj != nil { | ||
| labels := getResourceLabels(metadata, obj) | ||
| _ = metrics.ResourceCreatedAt.Delete(labels) | ||
| } | ||
| } | ||
|
|
||
| func getResourceLabels(metadata metav1.Object, obj runtime.Object) map[string]string { | ||
| return map[string]string{ | ||
| "name": metadata.GetName(), | ||
| "namespace": metadata.GetNamespace(), | ||
| "group": obj.GetObjectKind().GroupVersionKind().Group, | ||
| "version": obj.GetObjectKind().GroupVersionKind().Version, | ||
| "kind": obj.GetObjectKind().GroupVersionKind().Kind, | ||
| } | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| // Copyright 2020 The Operator-SDK Authors | ||
|
bharathi-tenneti marked this conversation as resolved.
|
||
| // | ||
| // 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 ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
| ) | ||
|
|
||
| // ResourceCreatedAt creates new prometheus metrics for primary resource, | ||
| // with information {"name", "namespace", "group", "version", "kind"} | ||
|
bharathi-tenneti marked this conversation as resolved.
|
||
| var ResourceCreatedAt = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "resource_created_at_seconds", | ||
| Help: "Timestamp at which a resource was created", | ||
| }, []string{"name", "namespace", "group", "version", "kind"}) | ||
|
|
||
| func init() { | ||
| metrics.Registry.MustRegister( | ||
| ResourceCreatedAt, | ||
| ) | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.