@@ -31,7 +31,7 @@ import (
31
31
32
32
type encoderOption struct {
33
33
withCreatedLines bool
34
- withUnit bool
34
+ withUnit bool
35
35
}
36
36
37
37
type EncoderOption func (* encoderOption )
@@ -52,24 +52,17 @@ func WithCreatedLines() EncoderOption {
52
52
}
53
53
}
54
54
55
- type ToOpenMetrics struct {
56
- withUnit bool
57
- // withCreated bool can be added in the future.
55
+ // WithUnit is an EncoderOption enabling a set unit to be written to the output
56
+ // and to be added to the metric name, if it's not there already, as a suffix.
57
+ // Without opting in this way, the unit will not be added to the metric name and,
58
+ // on top of that, the unit will not be passed onto the output, even if it
59
+ // were declared in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
60
+ func WithUnit () EncoderOption {
61
+ return func (t * encoderOption ) {
62
+ t .withUnit = true
63
+ }
58
64
}
59
65
60
- // type ToOpenMetricsOption = func(*ToOpenMetrics)
61
-
62
- // // ToOpenMetricsWithUnit is meant to be called as an optional argument in the MetricFamilyToOpenMetrics
63
- // // function. It enables a set unit to be written to the output and to be added to the metric name, if
64
- // // it's not there already, as a suffix. Without opting in this way, the unit will not be added to the
65
- // // metric name and, on top of that, the unit will not be passed onto the output, even if it were declared
66
- // // in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
67
- // func ToOpenMetricsWithUnit() ToOpenMetricsOption {
68
- // return func(om *ToOpenMetrics) {
69
- // om.withUnit = true
70
- // }
71
- // }
72
-
73
66
// MetricFamilyToOpenMetrics converts a MetricFamily proto message into the
74
67
// OpenMetrics text format and writes the resulting lines to 'out'. It returns
75
68
// the number of bytes written and any error encountered. The output will have
@@ -113,11 +106,11 @@ type ToOpenMetrics struct {
113
106
// However, in order to accommodate any potential scenario where such a change in the
114
107
// metric name is not desirable, the users are here given the choice of either explicitly
115
108
// opt in, in case they wish for the unit to be included in the output AND in the metric name
116
- // as a suffix (see the description of the ToOpenMetricsWithUnit function below ),
109
+ // as a suffix (see the description of the WithUnit function above ),
117
110
// or not to opt in, in case they don't want for any of that to happen.
118
111
//
119
- // - No support for the following (optional) features: `_created`,
120
- // info type, stateset type, gaugehistogram type.
112
+ // - No support for the following (optional) features: info type,
113
+ // stateset type, gaugehistogram type.
121
114
//
122
115
// - The size of exemplar labels is not checked (i.e. it's possible to create
123
116
// exemplars that are larger than allowed by the OpenMetrics specification).
@@ -126,9 +119,9 @@ type ToOpenMetrics struct {
126
119
// with a `NaN` value.)
127
120
func MetricFamilyToOpenMetrics (out io.Writer , in * dto.MetricFamily , options ... EncoderOption ) (written int , err error ) {
128
121
toOM := encoderOption {}
129
- // for _, option := range options {
130
- // option(&toOM)
131
- // }
122
+ for _ , option := range options {
123
+ option (& toOM )
124
+ }
132
125
133
126
name := in .GetName ()
134
127
if name == "" {
@@ -245,13 +238,15 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
245
238
if err != nil {
246
239
return
247
240
}
248
- var createdTsBytesWritten int // err = w.WriteByte('\n')
241
+ err = w .WriteByte ('\n' )
249
242
written ++
250
243
if err != nil {
251
244
return
252
245
}
253
246
}
254
247
248
+ var createdTsBytesWritten int
249
+
255
250
// Finally the samples, one line for each.
256
251
for _ , metric := range in .Metric {
257
252
switch metricType {
@@ -270,7 +265,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
270
265
metric .Counter .Exemplar ,
271
266
)
272
267
if toOM .withCreatedLines && metric .Counter .CreatedTimestamp != nil {
273
- createdTsBytesWritten , err = writeOpenMetricsCreated (w , name , "_total" , metric , "" , 0 , metric .Counter .GetCreatedTimestamp ())
268
+ createdTsBytesWritten , err = writeOpenMetricsCreated (w , compliantName , "_total" , metric , "" , 0 , metric .Counter .GetCreatedTimestamp ())
274
269
n += createdTsBytesWritten
275
270
}
276
271
case dto .MetricType_GAUGE :
@@ -328,7 +323,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
328
323
nil ,
329
324
)
330
325
if toOM .withCreatedLines && metric .Summary .CreatedTimestamp != nil {
331
- createdTsBytesWritten , err = writeOpenMetricsCreated (w , name , "" , metric , "" , 0 , metric .Summary .GetCreatedTimestamp ())
326
+ createdTsBytesWritten , err = writeOpenMetricsCreated (w , compliantName , "" , metric , "" , 0 , metric .Summary .GetCreatedTimestamp ())
332
327
n += createdTsBytesWritten
333
328
}
334
329
case dto .MetricType_HISTOGRAM :
@@ -380,7 +375,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
380
375
nil ,
381
376
)
382
377
if toOM .withCreatedLines && metric .Histogram .CreatedTimestamp != nil {
383
- createdTsBytesWritten , err = writeOpenMetricsCreated (w , name , "" , metric , "" , 0 , metric .Histogram .GetCreatedTimestamp ())
378
+ createdTsBytesWritten , err = writeOpenMetricsCreated (w , compliantName , "" , metric , "" , 0 , metric .Histogram .GetCreatedTimestamp ())
384
379
n += createdTsBytesWritten
385
380
}
386
381
default :
0 commit comments