-
Notifications
You must be signed in to change notification settings - Fork 123
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
Bug 1827585: Add controller that watches leader changes and capture disk metrics #313
Merged
openshift-merge-robot
merged 2 commits into
openshift:master
from
mfojtik:metrics-observe-controller
Apr 24, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,67 @@ | ||
package metriccontroller | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
prometheusapi "github.com/prometheus/client_golang/api" | ||
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/client-go/transport" | ||
) | ||
|
||
func getPrometheusClient(ctx context.Context, secretClient coreclientv1.SecretsGetter) (prometheusv1.API, error) { | ||
secrets, err := secretClient.Secrets("openshift-monitoring").List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bearerToken := "" | ||
for _, s := range secrets.Items { | ||
if s.Type != corev1.SecretTypeServiceAccountToken || | ||
!strings.HasPrefix(s.Name, "prometheus-k8s") { | ||
continue | ||
} | ||
bearerToken = string(s.Data[corev1.ServiceAccountTokenKey]) | ||
break | ||
} | ||
if len(bearerToken) == 0 { | ||
return nil, fmt.Errorf("unable to retrieve prometheus-k8 bearer token") | ||
} | ||
|
||
serviceCABytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
roots := x509.NewCertPool() | ||
roots.AppendCertsFromPEM(serviceCABytes) | ||
|
||
client, err := prometheusapi.NewClient(prometheusapi.Config{ | ||
Address: "https://" + net.JoinHostPort("thanos-querier.openshift-monitoring.svc", "9091"), | ||
RoundTripper: transport.NewBearerAuthRoundTripper( | ||
bearerToken, | ||
&http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}).DialContext, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
TLSClientConfig: &tls.Config{ | ||
RootCAs: roots, | ||
}, | ||
}, | ||
), | ||
}) | ||
|
||
return prometheusv1.NewAPI(client), nil | ||
} |
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,100 @@ | ||
package metriccontroller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
"github.com/prometheus/common/model" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/klog" | ||
|
||
configclientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" | ||
"github.com/openshift/library-go/pkg/controller/factory" | ||
"github.com/openshift/library-go/pkg/operator/events" | ||
operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
) | ||
|
||
type FSyncController struct { | ||
secretClient corev1client.SecretsGetter | ||
infraClient configclientv1.InfrastructuresGetter | ||
} | ||
|
||
func NewFSyncController(operatorClient operatorv1helpers.OperatorClient, infraClient configclientv1.InfrastructuresGetter, secretGetter corev1client.SecretsGetter, recorder events.Recorder) factory.Controller { | ||
c := &FSyncController{ | ||
secretClient: secretGetter, | ||
infraClient: infraClient, | ||
} | ||
return factory.New().ResyncEvery(1*time.Minute).WithSync(c.sync).ToController("FSyncController", recorder) | ||
} | ||
|
||
func (c *FSyncController) sync(ctx context.Context, syncCtx factory.SyncContext) error { | ||
client, err := getPrometheusClient(ctx, c.secretClient) | ||
if errors.IsNotFound(err) { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// First check how many leader changes we see in last 15 minutes | ||
etcdLeaderChangesResult, _, err := client.Query(ctx, "increase((max by (job) (etcd_server_leader_changes_seen_total) or 0*absent(etcd_server_leader_changes_seen_total))[5m:1m])", time.Now()) | ||
if err != nil { | ||
return err | ||
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. we don't want to degrade if we cannot query. |
||
} | ||
leaderChanges := etcdLeaderChangesResult.(model.Vector)[0].Value | ||
klog.Infof("Etcd leader changes increase in last 5m: %s", leaderChanges) | ||
|
||
// Do nothing if there are no leader changes | ||
if leaderChanges == 0.0 { | ||
return nil | ||
} | ||
|
||
// Capture etcd disk metrics as we detected excessive etcd leader changes | ||
etcdWalFsyncResult, _, err := client.QueryRange(ctx, "histogram_quantile(0.99, rate(etcd_disk_wal_fsync_duration_seconds_bucket[5m]))", prometheusv1.Range{ | ||
Start: time.Now().Add(-1 * time.Hour), | ||
End: time.Now(), | ||
Step: 1 * time.Second, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
matrix, ok := etcdWalFsyncResult.(model.Matrix) | ||
if !ok { | ||
return fmt.Errorf("unexpected type, expected Matrix, got %T", matrix) | ||
} | ||
|
||
values := []string{} | ||
for _, s := range matrix { | ||
if _, ok := s.Metric["pod"]; !ok { | ||
klog.Warningf("No pod label found in metric: %+v", s.Metric) | ||
continue | ||
} | ||
values = append(values, fmt.Sprintf("%s=%s", s.Metric["pod"], s.Values[0].Value)) | ||
} | ||
|
||
infra, err := c.infraClient.Infrastructures().Get(ctx, "cluster", metav1.GetOptions{}) | ||
if errors.IsNotFound(err) { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
var platformType string | ||
if infra.Status.PlatformStatus != nil { | ||
platformType = string(infra.Status.PlatformStatus.Type) | ||
} | ||
|
||
// Send warning event if excessive leader changes are detected. The event will include fsync disk metrics. | ||
syncCtx.Recorder().Warningf("EtcdLeaderChangeMetrics", "Detected %s leader changes in last 5 minutes on %q disk metrics are: %s", leaderChanges, platformType, strings.Join(values, ",")) | ||
|
||
// TODO: Consider Degraded condition here. | ||
|
||
return nil | ||
} |
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
156 changes: 156 additions & 0 deletions
156
vendor/github.com/prometheus/client_golang/api/client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
does returning an error from this method mean the operator goes degrade? if so, this should not error.
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.
it does not in this case.