diff --git a/expfmt/openmetrics_create.go b/expfmt/openmetrics_create.go index 1af167f1..cb165267 100644 --- a/expfmt/openmetrics_create.go +++ b/expfmt/openmetrics_create.go @@ -309,6 +309,10 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E "expected histogram in metric %s %s", compliantName, metric, ) } + sumSuffix, countSuffix := "_sum", "_count" + if metricType == dto.MetricType_GAUGE_HISTOGRAM { + sumSuffix, countSuffix = "_gsum", "_gcount" + } infSeen := false for _, b := range metric.Histogram.Bucket { if b.GetCumulativeCountFloat() > 0 { @@ -347,7 +351,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E } } n, err = writeOpenMetricsSample( - w, compliantName, "_sum", metric, "", 0, + w, compliantName, sumSuffix, metric, "", 0, metric.Histogram.GetSampleSum(), 0, false, nil, ) @@ -362,11 +366,11 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E ) } n, err = writeOpenMetricsSample( - w, compliantName, "_count", metric, "", 0, + w, compliantName, countSuffix, metric, "", 0, 0, metric.Histogram.GetSampleCount(), true, nil, ) - if toOM.withCreatedLines && metric.Histogram.CreatedTimestamp != nil { + if metricType == dto.MetricType_HISTOGRAM && toOM.withCreatedLines && metric.Histogram.CreatedTimestamp != nil { createdTsBytesWritten, err = writeOpenMetricsCreated(w, compliantName, "", metric, "", 0, metric.Histogram.GetCreatedTimestamp()) n += createdTsBytesWritten } diff --git a/expfmt/openmetrics_create_test.go b/expfmt/openmetrics_create_test.go index 39fc40cc..d823899e 100644 --- a/expfmt/openmetrics_create_test.go +++ b/expfmt/openmetrics_create_test.go @@ -718,6 +718,7 @@ some_measure_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06 }, // 17: Gauge histogram. { + options: []EncoderOption{WithCreatedLines()}, in: &dto.MetricFamily{ Name: proto.String("name"), Help: proto.String("doc string"), @@ -725,8 +726,9 @@ some_measure_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06 Metric: []*dto.Metric{ { Histogram: &dto.Histogram{ - SampleCount: proto.Uint64(2693), - SampleSum: proto.Float64(1756047.3), + SampleCount: proto.Uint64(2693), + SampleSum: proto.Float64(1756047.3), + CreatedTimestamp: openMetricsTimestamp, Bucket: []*dto.Bucket{ { UpperBound: proto.Float64(100), @@ -756,8 +758,8 @@ name_bucket{le="120.0"} 412 name_bucket{le="144.0"} 592 name_bucket{le="172.8"} 1524 name_bucket{le="+Inf"} 2693 -name_sum 1.7560473e+06 -name_count 2693 +name_gsum 1.7560473e+06 +name_gcount 2693 `, }, // 18: Colons are valid in legacy metric names but not in legacy label names.