Skip to content

Commit 6e23ea6

Browse files
committed
Make unit an optional argument in MetricFamilyToOpenMetrics func
Signed-off-by: Arianna Vespri <[email protected]>
1 parent ae18ca7 commit 6e23ea6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

expfmt/openmetrics_create.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,25 @@ import (
6363
//
6464
// - The value of Counters is not checked. (OpenMetrics doesn't allow counters
6565
// with a `NaN` value.)
66-
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int, err error) {
66+
67+
type ToOpenMetrics struct {
68+
withUnit bool
69+
// withCreated bool can be added in the future
70+
}
71+
72+
type ToOpenMetricsOption = func(*ToOpenMetrics)
73+
74+
func ToOpenMetricsWithUnit() ToOpenMetricsOption {
75+
return func(om *ToOpenMetrics) {
76+
om.withUnit = true
77+
}
78+
}
79+
80+
func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...ToOpenMetricsOption) (written int, err error) {
81+
toOM := ToOpenMetrics{}
82+
for _, option := range options {
83+
option(&toOM)
84+
}
6785
name := in.GetName()
6886
if name == "" {
6987
return 0, fmt.Errorf("MetricFamily has no name: %s", in)
@@ -154,7 +172,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily) (written int
154172
if err != nil {
155173
return
156174
}
157-
if in.Unit != nil {
175+
if toOM.withUnit && in.Unit != nil {
158176
n, err = w.WriteString("# UNIT ")
159177
written += n
160178
if err != nil {

0 commit comments

Comments
 (0)