Skip to content

Commit e355c48

Browse files
committed
Remove duplicated MetricsWriter implementation
Simplify the implementation of the MetricsWriter to avoid code duplication between single and multi stores scenarios. Signed-off-by: Damien Grisonnet <[email protected]>
1 parent db20a99 commit e355c48

File tree

9 files changed

+22
-44
lines changed

9 files changed

+22
-44
lines changed

internal/store/builder.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,20 @@ func (b *Builder) WithAllowLabels(labels map[string][]string) error {
233233
// Build initializes and registers all enabled stores.
234234
// It returns metrics writers which can be used to write out
235235
// metrics from the stores.
236-
func (b *Builder) Build() []metricsstore.MetricsWriter {
236+
func (b *Builder) Build() metricsstore.MetricsWriterList {
237237
if b.familyGeneratorFilter == nil {
238238
panic("familyGeneratorFilter should not be nil")
239239
}
240240

241-
var metricsWriters []metricsstore.MetricsWriter
241+
var metricsWriters metricsstore.MetricsWriterList
242242
var activeStoreNames []string
243243

244244
for _, c := range b.enabledResources {
245245
constructor, ok := availableStores[c]
246246
if ok {
247247
stores := cacheStoresToMetricStores(constructor(b))
248248
activeStoreNames = append(activeStoreNames, c)
249-
if len(stores) == 1 {
250-
metricsWriters = append(metricsWriters, stores[0])
251-
} else {
252-
metricsWriters = append(metricsWriters, metricsstore.NewMultiStoreMetricsWriter(stores))
253-
}
249+
metricsWriters = append(metricsWriters, metricsstore.NewMetricsWriter(stores...))
254250
}
255251
}
256252

pkg/builder/builder.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package builder
1919
import (
2020
"context"
2121

22-
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
23-
2422
"github.com/prometheus/client_golang/prometheus"
2523
vpaclientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned"
2624
clientset "k8s.io/client-go/kubernetes"
@@ -29,6 +27,7 @@ import (
2927
internalstore "k8s.io/kube-state-metrics/v2/internal/store"
3028
ksmtypes "k8s.io/kube-state-metrics/v2/pkg/builder/types"
3129
"k8s.io/kube-state-metrics/v2/pkg/customresource"
30+
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
3231
metricsstore "k8s.io/kube-state-metrics/v2/pkg/metrics_store"
3332
"k8s.io/kube-state-metrics/v2/pkg/options"
3433
)
@@ -135,7 +134,7 @@ func (b *Builder) WithCustomResourceStoreFactories(fs ...customresource.Registry
135134

136135
// Build initializes and registers all enabled stores.
137136
// Returns metric writers.
138-
func (b *Builder) Build() []metricsstore.MetricsWriter {
137+
func (b *Builder) Build() metricsstore.MetricsWriterList {
139138
return b.internal.Build()
140139
}
141140

pkg/builder/types/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type BuilderInterface interface {
5151
DefaultGenerateStoresFunc() BuildStoresFunc
5252
DefaultGenerateCustomResourceStoresFunc() BuildCustomResourceStoresFunc
5353
WithCustomResourceStoreFactories(fs ...customresource.RegistryFactory)
54-
Build() []metricsstore.MetricsWriter
54+
Build() metricsstore.MetricsWriterList
5555
BuildStores() [][]cache.Store
5656
}
5757

pkg/metrics_store/metrics_store.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package metricsstore
1818

1919
import (
20-
"io"
2120
"sync"
2221

2322
"k8s.io/apimachinery/pkg/api/meta"
@@ -144,18 +143,3 @@ func (s *MetricsStore) Replace(list []interface{}, _ string) error {
144143
func (s *MetricsStore) Resync() error {
145144
return nil
146145
}
147-
148-
// WriteAll writes all metrics of the store into the given writer, zipped with the
149-
// help text of each metric family.
150-
func (s *MetricsStore) WriteAll(w io.Writer) {
151-
s.mutex.RLock()
152-
defer s.mutex.RUnlock()
153-
154-
for i, help := range s.headers {
155-
w.Write([]byte(help))
156-
w.Write([]byte{'\n'})
157-
for _, metricFamilies := range s.metrics {
158-
w.Write(metricFamilies[i])
159-
}
160-
}
161-
}

pkg/metrics_store/metrics_store_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func TestObjectsSameNameDifferentNamespaces(t *testing.T) {
8181
}
8282

8383
w := strings.Builder{}
84-
ms.WriteAll(&w)
84+
mw := NewMetricsWriter(ms)
85+
mw.WriteAll(&w)
8586
m := w.String()
8687

8788
for _, id := range serviceIDS {

pkg/metrics_store/metrics_writer.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,23 @@ package metricsstore
1818

1919
import "io"
2020

21-
// MetricsWriter is the interface that wraps the WriteAll method.
22-
// WriteAll writes out bytes to the underlying writer.
23-
type MetricsWriter interface {
24-
WriteAll(w io.Writer)
25-
}
21+
// MetricsWriterList represent a list of MetricsWriter
22+
type MetricsWriterList []*MetricsWriter
2623

27-
// MultiStoreMetricsWriter is a struct that holds multiple MetricsStore(s) and
24+
// MetricsWriter is a struct that holds multiple MetricsStore(s) and
2825
// implements the MetricsWriter interface.
2926
// It should be used with stores which have the same metric headers.
3027
//
31-
// MultiStoreMetricsWriter writes out metrics from the underlying stores so that
28+
// MetricsWriter writes out metrics from the underlying stores so that
3229
// metrics with the same name coming from different stores end up grouped together.
3330
// It also ensures that the metric headers are only written out once.
34-
type MultiStoreMetricsWriter struct {
31+
type MetricsWriter struct {
3532
stores []*MetricsStore
3633
}
3734

38-
// NewMultiStoreMetricsWriter creates a new MultiStoreMetricsWriter.
39-
func NewMultiStoreMetricsWriter(stores []*MetricsStore) MetricsWriter {
40-
return &MultiStoreMetricsWriter{
35+
// NewMetricsWriter creates a new MetricsWriter.
36+
func NewMetricsWriter(stores ...*MetricsStore) *MetricsWriter {
37+
return &MetricsWriter{
4138
stores: stores,
4239
}
4340
}
@@ -46,7 +43,7 @@ func NewMultiStoreMetricsWriter(stores []*MetricsStore) MetricsWriter {
4643
//
4744
// WriteAll writes metrics so that the ones with the same name
4845
// are grouped together when written out.
49-
func (m MultiStoreMetricsWriter) WriteAll(w io.Writer) {
46+
func (m MetricsWriter) WriteAll(w io.Writer) {
5047
if len(m.stores) == 0 {
5148
return
5249
}

pkg/metrics_store/metrics_writer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestWriteAllWithSingleStore(t *testing.T) {
8383
}
8484
}
8585

86-
multiNsWriter := metricsstore.NewMultiStoreMetricsWriter([]*metricsstore.MetricsStore{store})
86+
multiNsWriter := metricsstore.NewMetricsWriter(store)
8787
w := strings.Builder{}
8888
multiNsWriter.WriteAll(&w)
8989
result := w.String()
@@ -192,7 +192,7 @@ func TestWriteAllWithMultipleStores(t *testing.T) {
192192
}
193193
}
194194

195-
multiNsWriter := metricsstore.NewMultiStoreMetricsWriter([]*metricsstore.MetricsStore{s1, s2})
195+
multiNsWriter := metricsstore.NewMetricsWriter(s1, s2)
196196
w := strings.Builder{}
197197
multiNsWriter.WriteAll(&w)
198198
result := w.String()

pkg/metricshandler/metrics_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type MetricsHandler struct {
5151

5252
// mtx protects metricsWriters, curShard, and curTotalShards
5353
mtx *sync.RWMutex
54-
metricsWriters []metricsstore.MetricsWriter
54+
metricsWriters metricsstore.MetricsWriterList
5555
curShard int32
5656
curTotalShards int
5757
}

tests/lib/lib_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ func TestAsLibrary(t *testing.T) {
5555
time.Sleep(time.Second)
5656

5757
w := strings.Builder{}
58-
c.WriteAll(&w)
58+
mw := metricsstore.NewMetricsWriter(c)
59+
mw.WriteAll(&w)
5960
m := w.String()
6061

6162
if !strings.Contains(m, service.ObjectMeta.Name) {

0 commit comments

Comments
 (0)