-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
apps: add prometheus metrics for rollout #14796
Merged
openshift-merge-robot
merged 1 commit into
openshift:master
from
mfojtik:deployment-condition-metrics
Sep 19, 2017
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 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,3 @@ | ||
// Package prometheus contains code for injecting apps related metrics | ||
// into the prometheus registry running in the openshift master | ||
package prometheus |
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,162 @@ | ||
package prometheus | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"k8s.io/apimachinery/pkg/labels" | ||
kcorelisters "k8s.io/kubernetes/pkg/client/listers/core/v1" | ||
|
||
"github.com/openshift/origin/pkg/apps/util" | ||
) | ||
|
||
const ( | ||
completeRolloutCount = "complete_rollouts_total" | ||
activeRolloutDurationSeconds = "active_rollouts_duration_seconds" | ||
lastFailedRolloutTime = "last_failed_rollout_time" | ||
|
||
availablePhase = "available" | ||
failedPhase = "failed" | ||
cancelledPhase = "cancelled" | ||
) | ||
|
||
var ( | ||
nameToQuery = func(name string) string { | ||
return strings.Join([]string{"openshift_apps_deploymentconfigs", name}, "_") | ||
} | ||
|
||
completeRolloutCountDesc = prometheus.NewDesc( | ||
nameToQuery(completeRolloutCount), | ||
"Counts total complete rollouts", | ||
[]string{"phase"}, nil, | ||
) | ||
|
||
lastFailedRolloutTimeDesc = prometheus.NewDesc( | ||
nameToQuery(lastFailedRolloutTime), | ||
"Tracks the time of last failure rollout per deployment config", | ||
[]string{"namespace", "name", "generation"}, nil, | ||
) | ||
|
||
activeRolloutDurationSecondsDesc = prometheus.NewDesc( | ||
nameToQuery(activeRolloutDurationSeconds), | ||
"Tracks the active rollout duration in seconds", | ||
[]string{"namespace", "name", "phase", "generation"}, nil, | ||
) | ||
|
||
apps = appsCollector{} | ||
registered = false | ||
) | ||
|
||
type appsCollector struct { | ||
lister kcorelisters.ReplicationControllerLister | ||
} | ||
|
||
func IntializeMetricsCollector(rcLister kcorelisters.ReplicationControllerLister) { | ||
apps.lister = rcLister | ||
if !registered { | ||
prometheus.MustRegister(&apps) | ||
registered = true | ||
} | ||
glog.V(4).Info("apps metrics registered with prometheus") | ||
} | ||
|
||
func (c *appsCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- completeRolloutCountDesc | ||
ch <- activeRolloutDurationSecondsDesc | ||
} | ||
|
||
type failedRollout struct { | ||
timestamp float64 | ||
generation int64 | ||
} | ||
|
||
// Collect implements the prometheus.Collector interface. | ||
func (c *appsCollector) Collect(ch chan<- prometheus.Metric) { | ||
result, err := c.lister.List(labels.Everything()) | ||
if err != nil { | ||
glog.V(4).Infof("Collecting metrics for apps failed: %v", err) | ||
return | ||
} | ||
|
||
var available, failed, cancelled float64 | ||
|
||
latestFailedRollouts := map[string]failedRollout{} | ||
|
||
for _, d := range result { | ||
dcName := util.DeploymentConfigNameFor(d) | ||
if len(dcName) == 0 { | ||
continue | ||
} | ||
|
||
if util.IsTerminatedDeployment(d) { | ||
if util.IsDeploymentCancelled(d) { | ||
cancelled++ | ||
continue | ||
} | ||
if util.IsFailedDeployment(d) { | ||
failed++ | ||
|
||
// Track the latest failed rollout per deployment config | ||
shouldUpdate := false | ||
if r, exists := latestFailedRollouts[d.Namespace+"/"+dcName]; exists { | ||
if d.Status.ObservedGeneration > r.generation { | ||
shouldUpdate = true | ||
} | ||
} | ||
if shouldUpdate { | ||
latestFailedRollouts[d.Namespace+"/"+dcName] = failedRollout{ | ||
timestamp: float64(d.CreationTimestamp.Unix()), | ||
generation: d.Status.ObservedGeneration, | ||
} | ||
} | ||
continue | ||
} | ||
if util.IsCompleteDeployment(d) { | ||
available++ | ||
continue | ||
} | ||
} | ||
|
||
// TODO: Figure out under what circumstances the phase is not set. | ||
phase := strings.ToLower(string(util.DeploymentStatusFor(d))) | ||
if len(phase) == 0 { | ||
phase = "unknown" | ||
} | ||
|
||
// Record duration in seconds for active rollouts | ||
// TODO: possible time screw? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. time "skew". Time "screw" would probably be very different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. classic @mfojtik comment |
||
durationSeconds := time.Now().Unix() - d.CreationTimestamp.Unix() | ||
ch <- prometheus.MustNewConstMetric( | ||
activeRolloutDurationSecondsDesc, | ||
prometheus.CounterValue, | ||
float64(durationSeconds), | ||
[]string{ | ||
d.Namespace, | ||
dcName, | ||
phase, | ||
fmt.Sprintf("%d", d.Status.ObservedGeneration), | ||
}...) | ||
} | ||
|
||
// Record latest failed rollouts | ||
for dc, r := range latestFailedRollouts { | ||
parts := strings.Split(dc, "/") | ||
ch <- prometheus.MustNewConstMetric( | ||
lastFailedRolloutTimeDesc, | ||
prometheus.GaugeValue, | ||
r.timestamp, | ||
[]string{ | ||
parts[0], | ||
parts[1], | ||
fmt.Sprintf("%d", r.generation), | ||
}...) | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, available, []string{availablePhase}...) | ||
ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, failed, []string{failedPhase}...) | ||
ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, cancelled, []string{cancelledPhase}...) | ||
} |
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.
Why do we need the extra list here? We already do one inside the main sync loop of the controller. Can't you use those results?
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 guess prometheus collector schedule the list, if I pass the main controller loop rcList there might be a race? or I will have to read lock it? not sure if it is worth, since this list is done via cache imho.
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.
Discussed IRL, this is fine.