metrics_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "context"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/http/httptest"
  20. "runtime"
  21. "testing"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. clientset "k8s.io/client-go/kubernetes"
  25. restclient "k8s.io/client-go/rest"
  26. "k8s.io/component-base/metrics/testutil"
  27. "k8s.io/kubernetes/test/integration/framework"
  28. )
  29. func scrapeMetrics(s *httptest.Server) (testutil.Metrics, error) {
  30. req, err := http.NewRequest("GET", s.URL+"/metrics", nil)
  31. if err != nil {
  32. return nil, fmt.Errorf("Unable to create http request: %v", err)
  33. }
  34. client := &http.Client{}
  35. resp, err := client.Do(req)
  36. if err != nil {
  37. return nil, fmt.Errorf("Unable to contact metrics endpoint of master: %v", err)
  38. }
  39. defer resp.Body.Close()
  40. if resp.StatusCode != http.StatusOK {
  41. return nil, fmt.Errorf("Non-200 response trying to scrape metrics from master: %v", resp)
  42. }
  43. metrics := testutil.NewMetrics()
  44. data, err := ioutil.ReadAll(resp.Body)
  45. if err != nil {
  46. return nil, fmt.Errorf("Unable to read response: %v", resp)
  47. }
  48. err = testutil.ParseMetrics(string(data), &metrics)
  49. return metrics, err
  50. }
  51. func checkForExpectedMetrics(t *testing.T, metrics testutil.Metrics, expectedMetrics []string) {
  52. for _, expected := range expectedMetrics {
  53. if _, found := metrics[expected]; !found {
  54. t.Errorf("Master metrics did not include expected metric %q", expected)
  55. }
  56. }
  57. }
  58. func TestMasterProcessMetrics(t *testing.T) {
  59. if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
  60. t.Skipf("not supported on GOOS=%s", runtime.GOOS)
  61. }
  62. _, s, closeFn := framework.RunAMaster(nil)
  63. defer closeFn()
  64. metrics, err := scrapeMetrics(s)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. checkForExpectedMetrics(t, metrics, []string{
  69. "process_start_time_seconds",
  70. "process_cpu_seconds_total",
  71. "process_open_fds",
  72. "process_resident_memory_bytes",
  73. })
  74. }
  75. func TestApiserverMetrics(t *testing.T) {
  76. _, s, closeFn := framework.RunAMaster(nil)
  77. defer closeFn()
  78. // Make a request to the apiserver to ensure there's at least one data point
  79. // for the metrics we're expecting -- otherwise, they won't be exported.
  80. client := clientset.NewForConfigOrDie(&restclient.Config{Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
  81. if _, err := client.CoreV1().Pods(metav1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{}); err != nil {
  82. t.Fatalf("unexpected error getting pods: %v", err)
  83. }
  84. metrics, err := scrapeMetrics(s)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. checkForExpectedMetrics(t, metrics, []string{
  89. "apiserver_request_total",
  90. "apiserver_request_duration_seconds_sum",
  91. "etcd_request_duration_seconds_sum",
  92. })
  93. }