-
Notifications
You must be signed in to change notification settings - Fork 40.1k
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
Add an integration test that checks for the metrics we expect to be exported from the master #6941
Merged
+161
−32
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,126 @@ | ||
// +build integration,!no-etcd | ||
|
||
/* | ||
Copyright 2015 Google Inc. All rights reserved. | ||
|
||
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 integration | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" | ||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" | ||
"github.com/golang/glog" | ||
"github.com/golang/protobuf/proto" | ||
prometheuspb "github.com/prometheus/client_model/go" | ||
) | ||
|
||
const scrapeRequestHeader = "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=compact-text" | ||
|
||
func init() { | ||
requireEtcd() | ||
} | ||
|
||
func scrapeMetrics(s *httptest.Server) ([]*prometheuspb.MetricFamily, error) { | ||
req, err := http.NewRequest("GET", s.URL+"/metrics", nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to create http request: %v", err) | ||
} | ||
// Ask the prometheus exporter for its text protocol buffer format, since it's | ||
// much easier to parse than its plain-text format. Don't use the serialized | ||
// proto representation since it uses a non-standard varint delimiter between | ||
// metric families. | ||
req.Header.Add("Accept", scrapeRequestHeader) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to contact metrics endpoint of master: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("Non-200 response trying to scrape metrics from master: %v", resp) | ||
} | ||
|
||
// Each line in the response body should contain all the data for a single metric. | ||
var metrics []*prometheuspb.MetricFamily | ||
scanner := bufio.NewScanner(resp.Body) | ||
for scanner.Scan() { | ||
var metric prometheuspb.MetricFamily | ||
if err := proto.UnmarshalText(scanner.Text(), &metric); err != nil { | ||
return nil, fmt.Errorf("Failed to unmarshal line of metrics response: %v", err) | ||
} | ||
glog.Infof("Got metric %q", metric.GetName()) | ||
metrics = append(metrics, &metric) | ||
} | ||
return metrics, nil | ||
} | ||
|
||
func checkForExpectedMetrics(t *testing.T, metrics []*prometheuspb.MetricFamily, expectedMetrics []string) { | ||
foundMetrics := make(map[string]bool) | ||
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. This might be simpler as:
Then you get rid of one of the loops and the map variable name makes a bit more sense. |
||
for _, metric := range metrics { | ||
foundMetrics[metric.GetName()] = true | ||
} | ||
for _, expected := range expectedMetrics { | ||
if _, found := foundMetrics[expected]; !found { | ||
t.Errorf("Master metrics did not include expected metric %q", expected) | ||
} | ||
} | ||
} | ||
|
||
func TestMasterProcessMetrics(t *testing.T) { | ||
_, s := runAMaster(t) | ||
defer s.Close() | ||
|
||
metrics, err := scrapeMetrics(s) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
checkForExpectedMetrics(t, metrics, []string{ | ||
"process_start_time_seconds", | ||
"process_cpu_seconds_total", | ||
"process_goroutines", | ||
"process_open_fds", | ||
"process_resident_memory_bytes", | ||
}) | ||
} | ||
|
||
func TestApiserverMetrics(t *testing.T) { | ||
_, s := runAMaster(t) | ||
defer s.Close() | ||
|
||
// Make a request to the apiserver to ensure there's at least one data point | ||
// for the metrics we're expecting -- otherwise, they won't be exported. | ||
client := client.NewOrDie(&client.Config{Host: s.URL, Version: testapi.Version()}) | ||
if _, err := client.Pods(api.NamespaceDefault).List(labels.Everything()); err != nil { | ||
t.Fatalf("unexpected error getting pods: %v", err) | ||
} | ||
|
||
metrics, err := scrapeMetrics(s) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
checkForExpectedMetrics(t, metrics, []string{ | ||
"apiserver_request_count", | ||
"apiserver_request_latencies", | ||
}) | ||
} |
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
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.
Typically with helper functions in test cod you don't pass in the testing.T reference. Instead, just have the helper return an error and the caller can fail the test by checking the error result.
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.
While that's true internally, it doesn't seem to be the case in this code base, and it's much more convenient do it this way because it allows us to spit out a separate error for every metric that's missing, rather than just the first one.
I've changed scrapeMetrics() to return an error instead, but really prefer checkForExpectedMetrics like this.