pkg/metrics_store: Accept Writer instead of returning metrics on scrape
This commit is contained in:
parent
581c49d593
commit
d1444f8061
12
main.go
12
main.go
|
|
@ -227,7 +227,7 @@ func serveMetrics(collectors []*kcollectors.Collector, host string, port int, en
|
||||||
}
|
}
|
||||||
|
|
||||||
type metricHandler struct {
|
type metricHandler struct {
|
||||||
c []*kcollectors.Collector
|
collectors []*kcollectors.Collector
|
||||||
enableGZIPEncoding bool
|
enableGZIPEncoding bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -251,14 +251,8 @@ func (m *metricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range m.c {
|
for _, c := range m.collectors {
|
||||||
for _, m := range c.Collect() {
|
c.Collect(w)
|
||||||
_, err := fmt.Fprint(writer, m)
|
|
||||||
if err != nil {
|
|
||||||
// TODO: Handle panic
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case we gziped the response, we have to close the writer.
|
// In case we gziped the response, we have to close the writer.
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,12 @@ limitations under the License.
|
||||||
|
|
||||||
package collectors
|
package collectors
|
||||||
|
|
||||||
import ()
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type Store interface {
|
type Store interface {
|
||||||
GetAll() []string
|
WriteAll(io.Writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collector represents a kube-state-metrics metric collector. It is stripped
|
// 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.
|
// Collect returns all metrics of the underlying store of the collector.
|
||||||
func (c *Collector) Collect() []string {
|
func (c *Collector) Collect(w io.Writer) {
|
||||||
return c.Store.GetAll()
|
c.Store.WriteAll(w)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package metricsstore
|
package metricsstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"k8s.io/kube-state-metrics/pkg/metrics"
|
"k8s.io/kube-state-metrics/pkg/metrics"
|
||||||
|
|
@ -10,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
helpPrefix = "# HELP "
|
helpPrefix = []byte("# HELP ")
|
||||||
)
|
)
|
||||||
|
|
||||||
// MetricsStore implements the k8s.io/kubernetes/client-go/tools/cache.Store
|
// MetricsStore implements the k8s.io/kubernetes/client-go/tools/cache.Store
|
||||||
|
|
@ -117,22 +118,19 @@ func (s *MetricsStore) Resync() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll returns all metrics of the store, zipped with the help text of each
|
// WriteAll writes all metrics of the store into the given writer, zipped with the
|
||||||
// metric family.
|
// help text of each metric family.
|
||||||
func (s *MetricsStore) GetAll() []string {
|
func (s *MetricsStore) WriteAll(w io.Writer) {
|
||||||
metrics := []string{}
|
|
||||||
|
|
||||||
s.mutex.RLock()
|
s.mutex.RLock()
|
||||||
defer s.mutex.RUnlock()
|
defer s.mutex.RUnlock()
|
||||||
|
|
||||||
for i, help := range s.helpTexts {
|
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 _, metricsPerObject := range s.metrics {
|
||||||
for _, metric := range metricsPerObject[i] {
|
for _, metric := range metricsPerObject[i] {
|
||||||
metrics = append(metrics, string(*metric))
|
w.Write([]byte(*metric))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return metrics
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
// Expect 4 lines, HELP + 2 metrics + newline
|
||||||
if len(metrics) != 3 {
|
if len(strings.Split(m, "\n")) != 4 {
|
||||||
t.Fatalf("expected 2 metrics but got %v", len(metrics))
|
t.Fatalf("expected 2 metrics but got %v", len(strings.Split(m, "\n")))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, id := range serviceIDS {
|
for _, id := range serviceIDS {
|
||||||
found := false
|
if !strings.Contains(m, fmt.Sprintf("uid=\"%v\"", id)) {
|
||||||
for _, m := range metrics {
|
|
||||||
if strings.Contains(m, fmt.Sprintf("uid=\"%v\"", id)) {
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
t.Fatalf("expected to find metric with uid %v", id)
|
t.Fatalf("expected to find metric with uid %v", id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,11 @@ func TestAsLibrary(t *testing.T) {
|
||||||
// Wait for informers to sync
|
// Wait for informers to sync
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
m := c.Collect()
|
w := strings.Builder{}
|
||||||
|
c.Collect(&w)
|
||||||
|
m := w.String()
|
||||||
|
|
||||||
if len(m) != 2 {
|
if !strings.Contains(m, service.ObjectMeta.Name) {
|
||||||
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) {
|
|
||||||
t.Fatal("expected string to contain service name")
|
t.Fatal("expected string to contain service name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue