Skip to content

Commit 67f184c

Browse files
committed
fixup! fixup! fixup! fix: fallback to gauge for protofmt-based negotiations
1 parent bfa611d commit 67f184c

File tree

4 files changed

+14
-54
lines changed

4 files changed

+14
-54
lines changed

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 m.Type {
155+
switch metric.Type(strings.ToLower(string(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: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,11 @@ 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-
// TypeN represents the type of the metric as an integer.
44-
type TypeN int
45-
4643
// String returns the string representation of the metric type.
47-
func (i TypeN) String() string {
48-
return string(TypeNMap[i])
49-
}
50-
51-
// NString returns the string representation of the metric type as an integer.
52-
func (i TypeN) NString() string {
53-
return strconv.Itoa(int(i))
44+
func (t Type) String() string {
45+
return string(t)
5446
}
5547

56-
// Supported metric types.
57-
const (
58-
// GaugeN defines an OpenMetrics gauge.
59-
GaugeN TypeN = iota
60-
61-
// InfoN defines an OpenMetrics info.
62-
InfoN
63-
64-
// StateSetN defines an OpenMetrics stateset.
65-
StateSetN
66-
67-
// CounterN defines an OpenMetrics counter.
68-
CounterN
69-
)
70-
7148
// Supported metric types.
7249
var (
7350

@@ -82,22 +59,6 @@ var (
8259

8360
// Counter defines an OpenMetrics counter.
8461
Counter Type = "counter"
85-
86-
// TypeNMap is a map of MetricTypeN to MetricType.
87-
TypeNMap = map[TypeN]Type{
88-
GaugeN: Gauge,
89-
InfoN: Info,
90-
StateSetN: StateSet,
91-
CounterN: Counter,
92-
}
93-
94-
// TypeMap is a map of MetricType to MetricTypeN.
95-
TypeMap = map[Type]TypeN{
96-
Gauge: GaugeN,
97-
Info: InfoN,
98-
StateSet: StateSetN,
99-
Counter: CounterN,
100-
}
10162
)
10263

10364
// Metric represents a single time series.

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(metric.TypeMap[g.Type].NString())
93+
header.WriteString(g.Type.String())
9494

9595
return header.String()
9696
}

pkg/metrics_store/metrics_writer.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,16 @@ func SanitizeHeaders(contentType string, writers MetricsWriterList) MetricsWrite
106106
} else if strings.HasPrefix(header, "# HELP") {
107107
lastHeader = header
108108

109-
// If the requested content type was proto-based, replace the type with "gauge", as "info" and "statesets" are not recognized by Prometheus' protobuf machinery,
110-
// else replace them by their respective string representations.
111-
if strings.HasPrefix(contentType, expfmt.ProtoType) &&
112-
(strings.HasSuffix(header, metric.InfoN.NString()) || strings.HasSuffix(header, metric.StateSetN.NString())) {
113-
writer.stores[0].headers[i] = header[:len(header)-1] + string(metric.Gauge)
114-
}
115-
116-
// Replace all remaining type enums with their string representations.
117-
n := int(header[len(header)-1]) - '0'
118-
if n >= 0 && n < len(metric.TypeNMap) {
119-
writer.stores[0].headers[i] = header[:len(header)-1] + string(metric.TypeNMap[metric.TypeN(n)])
109+
// If the requested content type was proto-based, replace "info" and "statesets" with "gauge", as they are not recognized by Prometheus' protobuf machinery.
110+
if strings.HasPrefix(contentType, expfmt.ProtoType) {
111+
infoTypeString := metric.Info.String()
112+
stateSetTypeString := metric.StateSet.String()
113+
if strings.HasSuffix(header, infoTypeString) {
114+
writer.stores[0].headers[i] = header[:len(header)-len(infoTypeString)] + string(metric.Gauge)
115+
}
116+
if strings.HasSuffix(header, stateSetTypeString) {
117+
writer.stores[0].headers[i] = header[:len(header)-len(stateSetTypeString)] + string(metric.Gauge)
118+
}
120119
}
121120
}
122121
}

0 commit comments

Comments
 (0)