Skip to content

Commit 8e4a2ec

Browse files
authored
Merge pull request #2470 from mrueg/better-align
chore: Memory align structs
2 parents 518a39a + 0a86b24 commit 8e4a2ec

File tree

11 files changed

+102
-98
lines changed

11 files changed

+102
-98
lines changed

internal/discovery/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ type kindPlural struct {
3737

3838
// CRDiscoverer provides a cache of the collected GVKs, along with helper utilities.
3939
type CRDiscoverer struct {
40-
// m is a mutex to protect the cache.
41-
m sync.RWMutex
42-
// Map is a cache of the collected GVKs.
43-
Map map[string]map[string][]kindPlural
44-
// ShouldUpdate is a flag that indicates whether the cache was updated.
45-
WasUpdated bool
4640
// CRDsAddEventsCounter tracks the number of times that the CRD informer triggered the "add" event.
4741
CRDsAddEventsCounter prometheus.Counter
4842
// CRDsDeleteEventsCounter tracks the number of times that the CRD informer triggered the "remove" event.
4943
CRDsDeleteEventsCounter prometheus.Counter
5044
// CRDsCacheCountGauge tracks the net amount of CRDs affecting the cache at this point.
5145
CRDsCacheCountGauge prometheus.Gauge
46+
// Map is a cache of the collected GVKs.
47+
Map map[string]map[string][]kindPlural
48+
// m is a mutex to protect the cache.
49+
m sync.RWMutex
50+
// ShouldUpdate is a flag that indicates whether the cache was updated.
51+
WasUpdated bool
5252
}
5353

5454
// SafeRead executes the given function while holding a read lock.

internal/store/builder.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,24 @@ var _ ksmtypes.BuilderInterface = &Builder{}
6565
// Builder helps to build store. It follows the builder pattern
6666
// (https://en.wikipedia.org/wiki/Builder_pattern).
6767
type Builder struct {
68-
kubeClient clientset.Interface
69-
customResourceClients map[string]interface{}
70-
namespaces options.NamespaceList
71-
// namespaceFilter is inside fieldSelectorFilter
72-
fieldSelectorFilter string
68+
kubeClient clientset.Interface
7369
ctx context.Context
74-
enabledResources []string
7570
familyGeneratorFilter generator.FamilyGeneratorFilter
71+
customResourceClients map[string]interface{}
7672
listWatchMetrics *watch.ListWatchMetrics
7773
shardingMetrics *sharding.Metrics
78-
shard int32
79-
totalShards int
8074
buildStoresFunc ksmtypes.BuildStoresFunc
8175
buildCustomResourceStoresFunc ksmtypes.BuildCustomResourceStoresFunc
8276
allowAnnotationsList map[string][]string
8377
allowLabelsList map[string][]string
84-
useAPIServerCache bool
8578
utilOptions *options.Options
79+
// namespaceFilter is inside fieldSelectorFilter
80+
fieldSelectorFilter string
81+
namespaces options.NamespaceList
82+
enabledResources []string
83+
totalShards int
84+
shard int32
85+
useAPIServerCache bool
8686
}
8787

8888
// NewBuilder returns a new builder.

internal/store/pod.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,14 +1349,14 @@ func createPodStatusPhaseFamilyGenerator() generator.FamilyGenerator {
13491349
}
13501350

13511351
phases := []struct {
1352-
v bool
13531352
n string
1353+
v bool
13541354
}{
1355-
{phase == v1.PodPending, string(v1.PodPending)},
1356-
{phase == v1.PodSucceeded, string(v1.PodSucceeded)},
1357-
{phase == v1.PodFailed, string(v1.PodFailed)},
1358-
{phase == v1.PodUnknown, string(v1.PodUnknown)},
1359-
{phase == v1.PodRunning, string(v1.PodRunning)},
1355+
{string(v1.PodPending), phase == v1.PodPending},
1356+
{string(v1.PodSucceeded), phase == v1.PodSucceeded},
1357+
{string(v1.PodFailed), phase == v1.PodFailed},
1358+
{string(v1.PodUnknown), phase == v1.PodUnknown},
1359+
{string(v1.PodRunning), phase == v1.PodRunning},
13601360
}
13611361

13621362
ms := make([]*metric.Metric, len(phases))
@@ -1475,12 +1475,12 @@ func createPodStatusQosClassFamilyGenerator() generator.FamilyGenerator {
14751475
}
14761476

14771477
qosClasses := []struct {
1478-
v bool
14791478
n string
1479+
v bool
14801480
}{
1481-
{class == v1.PodQOSBestEffort, string(v1.PodQOSBestEffort)},
1482-
{class == v1.PodQOSBurstable, string(v1.PodQOSBurstable)},
1483-
{class == v1.PodQOSGuaranteed, string(v1.PodQOSGuaranteed)},
1481+
{string(v1.PodQOSBestEffort), class == v1.PodQOSBestEffort},
1482+
{string(v1.PodQOSBurstable), class == v1.PodQOSBurstable},
1483+
{string(v1.PodQOSGuaranteed), class == v1.PodQOSGuaranteed},
14841484
}
14851485

14861486
ms := make([]*metric.Metric, len(qosClasses))

internal/store/testutils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import (
3131

3232
type generateMetricsTestCase struct {
3333
Obj interface{}
34+
Func func(interface{}) []metric.FamilyInterface
35+
Want string
3436
MetricNames []string
3537
AllowAnnotationsList []string
3638
AllowLabelsList []string
37-
Want string
3839
Headers []string
39-
Func func(interface{}) []metric.FamilyInterface
4040
}
4141

4242
func (testCase *generateMetricsTestCase) run() error {

pkg/customresourcestate/config.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type MetricsSpec struct {
4949

5050
// Resource configures a custom resource for metric generation.
5151
type Resource struct {
52+
53+
// Labels are added to all metrics. If the same key is used in a metric, the value from the metric will overwrite the value here.
54+
Labels `yaml:",inline" json:",inline"`
55+
5256
// MetricNamePrefix defines a prefix for all metrics of the resource.
5357
// If set to "", no prefix will be added.
5458
// Example: If set to "foo", MetricNamePrefix will be "foo_<metric>".
@@ -57,16 +61,13 @@ type Resource struct {
5761
// GroupVersionKind of the custom resource to be monitored.
5862
GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"`
5963

60-
// Labels are added to all metrics. If the same key is used in a metric, the value from the metric will overwrite the value here.
61-
Labels `yaml:",inline" json:",inline"`
64+
// ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize.
65+
ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"`
6266

6367
// Metrics are the custom resource fields to be collected.
6468
Metrics []Generator `yaml:"metrics" json:"metrics"`
6569
// ErrorLogV defines the verbosity threshold for errors logged for this resource.
6670
ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV"`
67-
68-
// ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize.
69-
ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"`
7071
}
7172

7273
// GetMetricNamePrefix returns the prefix to use for metrics.
@@ -132,25 +133,22 @@ func (l Labels) Merge(other Labels) Labels {
132133

133134
// Generator describes a unique metric name.
134135
type Generator struct {
135-
// Name of the metric. Subject to prefixing based on the configuration of the Resource.
136-
Name string `yaml:"name" json:"name"`
137-
// Help text for the metric.
138-
Help string `yaml:"help" json:"help"`
139136
// Each targets a value or values from the resource.
140137
Each Metric `yaml:"each" json:"each"`
141138

142139
// Labels are added to all metrics. Labels from Each will overwrite these if using the same key.
143140
Labels `yaml:",inline" json:",inline"` // json will inline because it is already tagged
141+
// Name of the metric. Subject to prefixing based on the configuration of the Resource.
142+
Name string `yaml:"name" json:"name"`
143+
// Help text for the metric.
144+
Help string `yaml:"help" json:"help"`
144145
// ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting.
145146
ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV"`
146147
}
147148

148149
// Metric defines a metric to expose.
149150
// +union
150151
type Metric struct {
151-
// Type defines the type of the metric.
152-
// +unionDiscriminator
153-
Type metric.Type `yaml:"type" json:"type"`
154152

155153
// Gauge defines a gauge metric.
156154
// +optional
@@ -161,6 +159,9 @@ type Metric struct {
161159
// Info defines an info metric.
162160
// +optional
163161
Info *MetricInfo `yaml:"info" json:"info"`
162+
// Type defines the type of the metric.
163+
// +unionDiscriminator
164+
Type metric.Type `yaml:"type" json:"type"`
164165
}
165166

166167
// ConfigDecoder is for use with FromConfig.

pkg/customresourcestate/config_metrics_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ type MetricMeta struct {
2727
// MetricGauge targets a Path that may be a single value, array, or object. Arrays and objects will generate a metric per element.
2828
// Ref: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#gauge
2929
type MetricGauge struct {
30-
MetricMeta `yaml:",inline" json:",inline"`
30+
// LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
31+
LabelFromKey string `yaml:"labelFromKey" json:"labelFromKey"`
32+
MetricMeta `yaml:",inline" json:",inline"`
3133

3234
// ValueFrom is the path to a numeric field under Path that will be the metric value.
3335
ValueFrom []string `yaml:"valueFrom" json:"valueFrom"`
34-
// LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
35-
LabelFromKey string `yaml:"labelFromKey" json:"labelFromKey"`
3636
// NilIsZero indicates that if a value is nil it will be treated as zero value.
3737
NilIsZero bool `yaml:"nilIsZero" json:"nilIsZero"`
3838
}
3939

4040
// MetricInfo is a metric which is used to expose textual information.
4141
// Ref: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#info
4242
type MetricInfo struct {
43-
MetricMeta `yaml:",inline" json:",inline"`
4443
// LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
4544
LabelFromKey string `yaml:"labelFromKey" json:"labelFromKey"`
45+
MetricMeta `yaml:",inline" json:",inline"`
4646
}
4747

4848
// MetricStateSet is a metric which represent a series of related boolean values, also called a bitset.

pkg/customresourcestate/registry_factory.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ type compiledEach compiledMetric
124124

125125
type compiledCommon struct {
126126
labelFromPath map[string]valuePath
127-
path valuePath
128127
t metric.Type
128+
path valuePath
129129
}
130130

131131
func (c compiledCommon) Path() valuePath {
@@ -211,9 +211,9 @@ func newCompiledMetric(m Metric) (compiledMetric, error) {
211211

212212
type compiledGauge struct {
213213
compiledCommon
214+
labelFromKey string
214215
ValueFrom valuePath
215216
NilIsZero bool
216-
labelFromKey string
217217
}
218218

219219
func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error) {
@@ -376,9 +376,9 @@ func (c *compiledInfo) values(v interface{}) (result []eachValue, err []error) {
376376

377377
type compiledStateSet struct {
378378
compiledCommon
379+
LabelName string
379380
ValueFrom valuePath
380381
List []string
381-
LabelName string
382382
}
383383

384384
func (c *compiledStateSet) Values(v interface{}) (result []eachValue, errs []error) {
@@ -495,11 +495,11 @@ func (e eachValue) ToMetric() *metric.Metric {
495495
}
496496

497497
type compiledFamily struct {
498-
Name string
499-
Help string
500498
Each compiledEach
501499
Labels map[string]string
502500
LabelFromPath map[string]valuePath
501+
Name string
502+
Help string
503503
ErrorLogV klog.Level
504504
}
505505

@@ -547,8 +547,8 @@ func addPathLabels(obj interface{}, labels map[string]valuePath, result map[stri
547547
}
548548

549549
type pathOp struct {
550-
part string
551550
op func(interface{}) interface{}
551+
part string
552552
}
553553

554554
type valuePath []pathOp

pkg/metric_generator/generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030
// DeprecatedVersion is defined only if the metric for which this options applies is,
3131
// in fact, deprecated.
3232
type FamilyGenerator struct {
33+
GenerateFunc func(obj interface{}) *metric.Family
3334
Name string
3435
Help string
3536
Type metric.Type
36-
OptIn bool
3737
DeprecatedVersion string
3838
StabilityLevel basemetrics.StabilityLevel
39-
GenerateFunc func(obj interface{}) *metric.Family
39+
OptIn bool
4040
}
4141

4242
// NewFamilyGeneratorWithStability creates new FamilyGenerator instances with metric

pkg/metrics_store/metrics_store.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@ import (
2929
// interface. Instead of storing entire Kubernetes objects, it stores metrics
3030
// generated based on those objects.
3131
type MetricsStore struct {
32-
// Protects metrics
33-
mutex sync.RWMutex
3432
// metrics is a map indexed by Kubernetes object id, containing a slice of
3533
// metric families, containing a slice of metrics. We need to keep metrics
3634
// grouped by metric families in order to zip families with their help text in
3735
// MetricsStore.WriteAll().
3836
metrics map[types.UID][][]byte
37+
38+
// generateMetricsFunc generates metrics based on a given Kubernetes object
39+
// and returns them grouped by metric family.
40+
generateMetricsFunc func(interface{}) []metric.FamilyInterface
3941
// headers contains the header (TYPE and HELP) of each metric family. It is
4042
// later on zipped with with their corresponding metric families in
4143
// MetricStore.WriteAll().
4244
headers []string
4345

44-
// generateMetricsFunc generates metrics based on a given Kubernetes object
45-
// and returns them grouped by metric family.
46-
generateMetricsFunc func(interface{}) []metric.FamilyInterface
46+
// Protects metrics
47+
mutex sync.RWMutex
4748
}
4849

4950
// NewMetricsStore returns a new MetricsStore

pkg/metricshandler/metrics_handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ import (
4444
// MetricsHandler is a http.Handler that exposes the main kube-state-metrics
4545
// /metrics endpoint. It allows concurrent reconfiguration at runtime.
4646
type MetricsHandler struct {
47-
opts *options.Options
48-
kubeClient kubernetes.Interface
49-
storeBuilder ksmtypes.BuilderInterface
50-
enableGZIPEncoding bool
47+
kubeClient kubernetes.Interface
48+
storeBuilder ksmtypes.BuilderInterface
49+
opts *options.Options
5150

5251
cancel func()
5352

5453
// mtx protects metricsWriters, curShard, and curTotalShards
55-
mtx *sync.RWMutex
56-
metricsWriters metricsstore.MetricsWriterList
57-
curShard int32
58-
curTotalShards int
54+
mtx *sync.RWMutex
55+
metricsWriters metricsstore.MetricsWriterList
56+
curTotalShards int
57+
curShard int32
58+
enableGZIPEncoding bool
5959
}
6060

6161
// New creates and returns a new MetricsHandler with the given options.

0 commit comments

Comments
 (0)