git subrepo clone (merge) https://github.com/kubernetes-incubator/metrics-server.git metrics-server

subrepo:
  subdir:   "metrics-server"
  merged:   "92d8412"
upstream:
  origin:   "https://github.com/kubernetes-incubator/metrics-server.git"
  branch:   "master"
  commit:   "92d8412"
git-subrepo:
  version:  "0.4.0"
  origin:   "???"
  commit:   "???"
diff --git a/metrics-server/pkg/metrics/util.go b/metrics-server/pkg/metrics/util.go
new file mode 100644
index 0000000..537e2e9
--- /dev/null
+++ b/metrics-server/pkg/metrics/util.go
@@ -0,0 +1,58 @@
+// Copyright 2018 The Kubernetes Authors.
+//
+// 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 (
+	"time"
+
+	"github.com/prometheus/client_golang/prometheus"
+)
+
+// BucketsForScrapeDuration calculates a variant of the prometheus default histogram
+// buckets that includes relevant buckets around out scrape timeout.
+func BucketsForScrapeDuration(scrapeTimeout time.Duration) []float64 {
+	// set up some buckets that include our scrape timeout,
+	// so that we can easily pinpoint scrape timeout issues.
+	// The default buckets provide a sane starting point for
+	// the smaller numbers.
+	buckets := append([]float64(nil), prometheus.DefBuckets...)
+	maxBucket := buckets[len(buckets)-1]
+	timeoutSeconds := float64(scrapeTimeout) / float64(time.Second)
+	if timeoutSeconds > maxBucket {
+		// [defaults, (scrapeTimeout + (scrapeTimeout - maxBucket)/ 2), scrapeTimeout, scrapeTimeout*1.5, scrapeTimeout*2]
+		halfwayToScrapeTimeout := maxBucket + (timeoutSeconds-maxBucket)/2
+		buckets = append(buckets, halfwayToScrapeTimeout, timeoutSeconds, timeoutSeconds*1.5, timeoutSeconds*2.0)
+	} else if timeoutSeconds < maxBucket {
+		var i int
+		var bucket float64
+		for i, bucket = range buckets {
+			if bucket > timeoutSeconds {
+				break
+			}
+		}
+
+		if bucket-timeoutSeconds < buckets[0] || (i > 0 && timeoutSeconds-buckets[i-1] < buckets[0]) {
+			// if we're sufficiently close to another bucket, just skip this
+			return buckets
+		}
+
+		// likely that our scrape timeout is close to another bucket, so don't bother injecting more than just our scrape timeout
+		oldRest := append([]float64(nil), buckets[i:]...) // make a copy so we don't overwrite it
+		buckets = append(buckets[:i], timeoutSeconds)
+		buckets = append(buckets, oldRest...)
+	}
+
+	return buckets
+}
diff --git a/metrics-server/pkg/metrics/util_test.go b/metrics-server/pkg/metrics/util_test.go
new file mode 100644
index 0000000..dcac09b
--- /dev/null
+++ b/metrics-server/pkg/metrics/util_test.go
@@ -0,0 +1,80 @@
+// Copyright 2018 The Kubernetes Authors.
+//
+// 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_test
+
+import (
+	"testing"
+	"time"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/prometheus/client_golang/prometheus"
+
+	. "github.com/kubernetes-incubator/metrics-server/pkg/metrics"
+)
+
+func TestMetricsUtil(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "Prometheus Metrics Util Test")
+}
+
+var _ = Describe("Prometheus Bucket Estimator", func() {
+	Context("with a scrape timeout longer than the max default bucket", func() {
+		It("should generate buckets in strictly increasing order", func() {
+			buckets := BucketsForScrapeDuration(15 * time.Second)
+			lastBucket := 0.0
+			for _, bucket := range buckets {
+				Expect(bucket).To(BeNumerically(">", lastBucket))
+				lastBucket = bucket
+			}
+		})
+
+		It("should include some buckets around the scrape timeout", func() {
+			Expect(BucketsForScrapeDuration(15 * time.Second)).To(ContainElement(15.0))
+			Expect(BucketsForScrapeDuration(15 * time.Second)).To(ContainElement(30.0))
+		})
+	})
+	Context("with a scrape timeout shorter than the max default bucket", func() {
+		It("should generate buckets in strictly increasing order", func() {
+			buckets := BucketsForScrapeDuration(5 * time.Second)
+			lastBucket := 0.0
+			for _, bucket := range buckets {
+				Expect(bucket).To(BeNumerically(">", lastBucket))
+				lastBucket = bucket
+			}
+		})
+
+		It("should include a bucket for the scrape timeout", func() {
+			Expect(BucketsForScrapeDuration(5 * time.Second)).To(ContainElement(5.0))
+		})
+	})
+	Context("with a scrape timeout equalt to the max default bucket", func() {
+		maxBucket := prometheus.DefBuckets[len(prometheus.DefBuckets)-1]
+		maxBucketDuration := time.Duration(maxBucket) * time.Second
+
+		It("should generate buckets in strictly increasing order", func() {
+			buckets := BucketsForScrapeDuration(maxBucketDuration)
+			lastBucket := 0.0
+			for _, bucket := range buckets {
+				Expect(bucket).To(BeNumerically(">", lastBucket))
+				lastBucket = bucket
+			}
+		})
+
+		It("should include a bucket for the scrape timeout", func() {
+			Expect(BucketsForScrapeDuration(maxBucketDuration)).To(ContainElement(maxBucket))
+		})
+	})
+})