@@ -35,8 +35,8 @@ type ToOpenMetrics struct {
35
35
type ToOpenMetricsOption = func (* ToOpenMetrics )
36
36
37
37
// ToOpenMetricsWithUnit is meant to be called as an optional argument in the MetricFamilyToOpenMetrics
38
- // function. It enables a set unit to be written to the output and to be added to the metric name, ( if
39
- // it's not there already) , as a suffix. Without opting in this way, the unit will not be added to the
38
+ // function. It enables a set unit to be written to the output and to be added to the metric name, if
39
+ // it's not there already, as a suffix. Without opting in this way, the unit will not be added to the
40
40
// metric name and, on top of that, the unit will not be passed onto the output, even if it were declared
41
41
// in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
42
42
func ToOpenMetricsWithUnit () ToOpenMetricsOption {
@@ -114,15 +114,15 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
114
114
}
115
115
116
116
var (
117
- n int
118
- metricType = in .GetType ()
119
- shortName = name
117
+ n int
118
+ metricType = in .GetType ()
119
+ compliantName = name
120
120
)
121
- if metricType == dto .MetricType_COUNTER && strings .HasSuffix (shortName , "_total" ) {
122
- shortName = name [:len (name )- 6 ]
121
+ if metricType == dto .MetricType_COUNTER && strings .HasSuffix (compliantName , "_total" ) {
122
+ compliantName = name [:len (name )- 6 ]
123
123
}
124
- if toOM .withUnit && in .Unit != nil && ! strings .HasSuffix (shortName , fmt .Sprintf ("_%s" , * in .Unit )) {
125
- shortName = shortName + fmt .Sprintf ("_%s" , * in .Unit )
124
+ if toOM .withUnit && in .Unit != nil && ! strings .HasSuffix (compliantName , fmt .Sprintf ("_%s" , * in .Unit )) {
125
+ compliantName = compliantName + fmt .Sprintf ("_%s" , * in .Unit )
126
126
}
127
127
128
128
// Comments, first HELP, then TYPE.
@@ -132,7 +132,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
132
132
if err != nil {
133
133
return
134
134
}
135
- n , err = w .WriteString (shortName )
135
+ n , err = w .WriteString (compliantName )
136
136
written += n
137
137
if err != nil {
138
138
return
@@ -158,7 +158,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
158
158
if err != nil {
159
159
return
160
160
}
161
- n , err = w .WriteString (shortName )
161
+ n , err = w .WriteString (compliantName )
162
162
written += n
163
163
if err != nil {
164
164
return
@@ -191,7 +191,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
191
191
if err != nil {
192
192
return
193
193
}
194
- n , err = w .WriteString (shortName )
194
+ n , err = w .WriteString (compliantName )
195
195
written += n
196
196
if err != nil {
197
197
return
@@ -218,50 +218,50 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
218
218
for _ , metric := range in .Metric {
219
219
switch metricType {
220
220
case dto .MetricType_COUNTER :
221
+ if strings .HasSuffix (name , "_total" ) {
222
+ compliantName = compliantName + "_total"
223
+ }
221
224
if metric .Counter == nil {
222
225
return written , fmt .Errorf (
223
- "expected counter in metric %s %s" , name , metric ,
226
+ "expected counter in metric %s %s" , compliantName , metric ,
224
227
)
225
228
}
226
- // Note that we have ensured above that either the name
227
- // ends on `_total` or that the rendered type is
228
- // `unknown`. Therefore, no `_total` must be added here.
229
229
n , err = writeOpenMetricsSample (
230
- w , name , "" , metric , "" , 0 ,
230
+ w , compliantName , "" , metric , "" , 0 ,
231
231
metric .Counter .GetValue (), 0 , false ,
232
232
metric .Counter .Exemplar ,
233
233
)
234
234
case dto .MetricType_GAUGE :
235
235
if metric .Gauge == nil {
236
236
return written , fmt .Errorf (
237
- "expected gauge in metric %s %s" , name , metric ,
237
+ "expected gauge in metric %s %s" , compliantName , metric ,
238
238
)
239
239
}
240
240
n , err = writeOpenMetricsSample (
241
- w , name , "" , metric , "" , 0 ,
241
+ w , compliantName , "" , metric , "" , 0 ,
242
242
metric .Gauge .GetValue (), 0 , false ,
243
243
nil ,
244
244
)
245
245
case dto .MetricType_UNTYPED :
246
246
if metric .Untyped == nil {
247
247
return written , fmt .Errorf (
248
- "expected untyped in metric %s %s" , name , metric ,
248
+ "expected untyped in metric %s %s" , compliantName , metric ,
249
249
)
250
250
}
251
251
n , err = writeOpenMetricsSample (
252
- w , name , "" , metric , "" , 0 ,
252
+ w , compliantName , "" , metric , "" , 0 ,
253
253
metric .Untyped .GetValue (), 0 , false ,
254
254
nil ,
255
255
)
256
256
case dto .MetricType_SUMMARY :
257
257
if metric .Summary == nil {
258
258
return written , fmt .Errorf (
259
- "expected summary in metric %s %s" , name , metric ,
259
+ "expected summary in metric %s %s" , compliantName , metric ,
260
260
)
261
261
}
262
262
for _ , q := range metric .Summary .Quantile {
263
263
n , err = writeOpenMetricsSample (
264
- w , name , "" , metric ,
264
+ w , compliantName , "" , metric ,
265
265
model .QuantileLabel , q .GetQuantile (),
266
266
q .GetValue (), 0 , false ,
267
267
nil ,
@@ -272,7 +272,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
272
272
}
273
273
}
274
274
n , err = writeOpenMetricsSample (
275
- w , name , "_sum" , metric , "" , 0 ,
275
+ w , compliantName , "_sum" , metric , "" , 0 ,
276
276
metric .Summary .GetSampleSum (), 0 , false ,
277
277
nil ,
278
278
)
@@ -281,20 +281,20 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
281
281
return
282
282
}
283
283
n , err = writeOpenMetricsSample (
284
- w , name , "_count" , metric , "" , 0 ,
284
+ w , compliantName , "_count" , metric , "" , 0 ,
285
285
0 , metric .Summary .GetSampleCount (), true ,
286
286
nil ,
287
287
)
288
288
case dto .MetricType_HISTOGRAM :
289
289
if metric .Histogram == nil {
290
290
return written , fmt .Errorf (
291
- "expected histogram in metric %s %s" , name , metric ,
291
+ "expected histogram in metric %s %s" , compliantName , metric ,
292
292
)
293
293
}
294
294
infSeen := false
295
295
for _ , b := range metric .Histogram .Bucket {
296
296
n , err = writeOpenMetricsSample (
297
- w , name , "_bucket" , metric ,
297
+ w , compliantName , "_bucket" , metric ,
298
298
model .BucketLabel , b .GetUpperBound (),
299
299
0 , b .GetCumulativeCount (), true ,
300
300
b .Exemplar ,
@@ -309,7 +309,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
309
309
}
310
310
if ! infSeen {
311
311
n , err = writeOpenMetricsSample (
312
- w , name , "_bucket" , metric ,
312
+ w , compliantName , "_bucket" , metric ,
313
313
model .BucketLabel , math .Inf (+ 1 ),
314
314
0 , metric .Histogram .GetSampleCount (), true ,
315
315
nil ,
@@ -320,7 +320,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
320
320
}
321
321
}
322
322
n , err = writeOpenMetricsSample (
323
- w , name , "_sum" , metric , "" , 0 ,
323
+ w , compliantName , "_sum" , metric , "" , 0 ,
324
324
metric .Histogram .GetSampleSum (), 0 , false ,
325
325
nil ,
326
326
)
@@ -329,13 +329,13 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...T
329
329
return
330
330
}
331
331
n , err = writeOpenMetricsSample (
332
- w , name , "_count" , metric , "" , 0 ,
332
+ w , compliantName , "_count" , metric , "" , 0 ,
333
333
0 , metric .Histogram .GetSampleCount (), true ,
334
334
nil ,
335
335
)
336
336
default :
337
337
return written , fmt .Errorf (
338
- "unexpected type in metric %s %s" , name , metric ,
338
+ "unexpected type in metric %s %s" , compliantName , metric ,
339
339
)
340
340
}
341
341
written += n
0 commit comments