pkg/metrics_store: Accept Writer instead of returning metrics on scrape

This commit is contained in:
Max Leonard Inden 2018-11-05 17:15:44 +00:00
parent 581c49d593
commit d1444f8061
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
5 changed files with 28 additions and 40 deletions

12
main.go
View File

@ -227,7 +227,7 @@ func serveMetrics(collectors []*kcollectors.Collector, host string, port int, en
}
type metricHandler struct {
c []*kcollectors.Collector
collectors []*kcollectors.Collector
enableGZIPEncoding bool
}
@ -251,14 +251,8 @@ func (m *metricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
for _, c := range m.c {
for _, m := range c.Collect() {
_, err := fmt.Fprint(writer, m)
if err != nil {
// TODO: Handle panic
panic(err)
}
}
for _, c := range m.collectors {
c.Collect(w)
}
// In case we gziped the response, we have to close the writer.

View File

@ -16,10 +16,12 @@ limitations under the License.
package collectors
import ()
import (
"io"
)
type Store interface {
GetAll() []string
WriteAll(io.Writer)
}
// Collector represents a kube-state-metrics metric collector. It is stripped
@ -33,6 +35,6 @@ func NewCollector(s Store) *Collector {
}
// Collect returns all metrics of the underlying store of the collector.
func (c *Collector) Collect() []string {
return c.Store.GetAll()
func (c *Collector) Collect(w io.Writer) {
c.Store.WriteAll(w)
}

View File

@ -1,6 +1,7 @@
package metricsstore
import (
"io"
"sync"
"k8s.io/kube-state-metrics/pkg/metrics"
@ -10,7 +11,7 @@ import (
)
var (
helpPrefix = "# HELP "
helpPrefix = []byte("# HELP ")
)
// MetricsStore implements the k8s.io/kubernetes/client-go/tools/cache.Store
@ -117,22 +118,19 @@ func (s *MetricsStore) Resync() error {
return nil
}
// GetAll returns all metrics of the store, zipped with the help text of each
// metric family.
func (s *MetricsStore) GetAll() []string {
metrics := []string{}
// WriteAll writes all metrics of the store into the given writer, zipped with the
// help text of each metric family.
func (s *MetricsStore) WriteAll(w io.Writer) {
s.mutex.RLock()
defer s.mutex.RUnlock()
for i, help := range s.helpTexts {
metrics = append(metrics, helpPrefix+help+"\n")
w.Write(append(helpPrefix, []byte(help)...))
w.Write([]byte{'\n'})
for _, metricsPerObject := range s.metrics {
for _, metric := range metricsPerObject[i] {
metrics = append(metrics, string(*metric))
w.Write([]byte(*metric))
}
}
}
return metrics
}

View File

@ -44,22 +44,18 @@ func TestObjectsSameNameDifferentNamespaces(t *testing.T) {
}
}
metrics := ms.GetAll()
w := strings.Builder{}
ms.WriteAll(&w)
m := w.String()
// Expect 3 lines, HELP + 2 metrics
if len(metrics) != 3 {
t.Fatalf("expected 2 metrics but got %v", len(metrics))
// Expect 4 lines, HELP + 2 metrics + newline
if len(strings.Split(m, "\n")) != 4 {
t.Fatalf("expected 2 metrics but got %v", len(strings.Split(m, "\n")))
}
for _, id := range serviceIDS {
found := false
for _, m := range metrics {
if strings.Contains(m, fmt.Sprintf("uid=\"%v\"", id)) {
found = true
}
}
if !strings.Contains(m, fmt.Sprintf("uid=\"%v\"", id)) {
if !found {
t.Fatalf("expected to find metric with uid %v", id)
}
}

View File

@ -38,13 +38,11 @@ func TestAsLibrary(t *testing.T) {
// Wait for informers to sync
time.Sleep(time.Second)
m := c.Collect()
w := strings.Builder{}
c.Collect(&w)
m := w.String()
if len(m) != 2 {
t.Fatalf("expected HELP line and one metric to be returned but got %v", len(m))
}
if !strings.Contains(string(m[1]), service.ObjectMeta.Name) {
if !strings.Contains(m, service.ObjectMeta.Name) {
t.Fatal("expected string to contain service name")
}
}