Skip to content

Commit e0c3e83

Browse files
committed
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fix: fallback to gauge for protofmt-based negotiations
1 parent b6496c7 commit e0c3e83

File tree

7 files changed

+16
-28
lines changed

7 files changed

+16
-28
lines changed

pkg/customresourcestate/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func configOverrides(config *Metrics) {
222222
for j := range config.Spec.Resources[i].Metrics {
223223

224224
// Override the metric type to lowercase, so the internals have a single source of truth for metric type definitions.
225+
// This is done as a convenience measure for users, so they don't have to remember the exact casing.
225226
config.Spec.Resources[i].Metrics[j].Each.Type = metric.Type(strings.ToLower(string(config.Spec.Resources[i].Metrics[j].Each.Type)))
226227
}
227228
}

pkg/customresourcestate/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var testData string
3232
func Test_Metrics_deserialization(t *testing.T) {
3333
var m Metrics
3434
assert.NoError(t, yaml.NewDecoder(strings.NewReader(testData)).Decode(&m))
35+
configOverrides(&m)
3536
assert.Equal(t, "active_count", m.Spec.Resources[0].Metrics[0].Name)
3637

3738
t.Run("can create resource factory", func(t *testing.T) {

pkg/customresourcestate/registry_factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type compiledMetric interface {
152152

153153
// newCompiledMetric returns a compiledMetric depending on the given metric type.
154154
func newCompiledMetric(m Metric) (compiledMetric, error) {
155-
switch metric.Type(strings.ToLower(string(m.Type))) {
155+
switch m.Type {
156156
case metric.Gauge:
157157
if m.Gauge == nil {
158158
return nil, errors.New("expected each.gauge to not be nil")

pkg/metric/metric.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ var (
4040
// Type represents the type of the metric. See https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#metric-types.
4141
type Type string
4242

43-
// String returns the string representation of the metric type.
44-
func (t Type) String() string {
45-
return string(t)
46-
}
47-
4843
// Supported metric types.
4944
var (
5045

pkg/metric_generator/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (g *FamilyGenerator) generateHeader() string {
9090
header.WriteString("# TYPE ")
9191
header.WriteString(g.Name)
9292
header.WriteByte(' ')
93-
header.WriteString(g.Type.String())
93+
header.WriteString(string(g.Type))
9494

9595
return header.String()
9696
}

pkg/metrics_store/metrics_writer.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,8 @@ func SanitizeHeaders(contentType string, writers MetricsWriterList) MetricsWrite
9292
var lastHeader string
9393
for _, writer := range writers {
9494
if len(writer.stores) > 0 {
95-
for i, header := range writer.stores[0].headers {
96-
97-
// Nothing to sanitize.
98-
if len(header) == 0 {
99-
continue
100-
}
95+
for i := 0; i < len(writer.stores[0].headers); {
96+
header := writer.stores[0].headers[i]
10197

10298
// Removes duplicate headers from the given MetricsWriterList for the same family (generated through CRS).
10399
// These are expected to be consecutive since G** resolution generates groups of similar metrics with same headers before moving onto the next G** spec in the CRS configuration.
@@ -106,8 +102,8 @@ func SanitizeHeaders(contentType string, writers MetricsWriterList) MetricsWrite
106102

107103
// If the requested content type was proto-based, replace "info" and "statesets" with "gauge", as they are not recognized by Prometheus' protobuf machinery.
108104
if strings.HasPrefix(contentType, expfmt.ProtoType) {
109-
infoTypeString := metric.Info.String()
110-
stateSetTypeString := metric.StateSet.String()
105+
infoTypeString := string(metric.Info)
106+
stateSetTypeString := string(metric.StateSet)
111107
if strings.HasSuffix(header, infoTypeString) {
112108
header = header[:len(header)-len(infoTypeString)] + string(metric.Gauge)
113109
writer.stores[0].headers[i] = header
@@ -121,22 +117,17 @@ func SanitizeHeaders(contentType string, writers MetricsWriterList) MetricsWrite
121117

122118
// Nullify duplicate headers after the sanitization to not miss out on any new candidates.
123119
if header == lastHeader {
124-
writer.stores[0].headers[i] = ""
120+
writer.stores[0].headers = append(writer.stores[0].headers[:i], writer.stores[0].headers[i+1:]...)
121+
122+
// Do not increment the index, as the next header is now at the current index.
123+
continue
125124
}
126125

127126
// Update the last header.
128127
lastHeader = header
129-
}
130-
}
131-
}
132128

133-
// Remove all empty headers.
134-
for _, writer := range writers {
135-
if len(writer.stores) > 0 {
136-
for i := len(writer.stores[0].headers) - 1; i >= 0; i-- {
137-
if writer.stores[0].headers[i] == "" {
138-
writer.stores[0].headers = append(writer.stores[0].headers[:i], writer.stores[0].headers[i+1:]...)
139-
}
129+
// Move to the next header.
130+
i++
140131
}
141132
}
142133
}

pkg/metricshandler/metrics_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func (m *MetricsHandler) Run(ctx context.Context) error {
178178
return ctx.Err()
179179
}
180180

181-
// ServeHTTP implements the http.Handler interface. It writes all generated
182-
// metrics to the response body.
181+
// ServeHTTP implements the http.Handler interface. It writes all generated metrics to the response body.
182+
// Note that all operations defined within this procedure are performed at every request.
183183
func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
184184
m.mtx.RLock()
185185
defer m.mtx.RUnlock()

0 commit comments

Comments
 (0)