Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions expfmt/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func extractSamples(f *dto.MetricFamily, o *DecodeOptions) (model.Vector, error)
return extractSummary(o, f), nil
case dto.MetricType_UNTYPED:
return extractUntyped(o, f), nil
case dto.MetricType_HISTOGRAM:
case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
return extractHistogram(o, f), nil
}
return nil, fmt.Errorf("expfmt.extractSamples: unknown metric family type %v", f.GetType())
Expand Down Expand Up @@ -403,9 +403,13 @@ func extractHistogram(o *DecodeOptions, f *dto.MetricFamily) model.Vector {
infSeen = true
}

v := q.GetCumulativeCountFloat()
if v <= 0 {
v = float64(q.GetCumulativeCount())
}
samples = append(samples, &model.Sample{
Metric: model.Metric(lset),
Value: model.SampleValue(q.GetCumulativeCount()),
Value: model.SampleValue(v),
Timestamp: timestamp,
})
}
Expand All @@ -428,9 +432,13 @@ func extractHistogram(o *DecodeOptions, f *dto.MetricFamily) model.Vector {
}
lset[model.MetricNameLabel] = model.LabelValue(f.GetName() + "_count")

v := m.Histogram.GetSampleCountFloat()
if v <= 0 {
v = float64(m.Histogram.GetSampleCount())
}
count := &model.Sample{
Metric: model.Metric(lset),
Value: model.SampleValue(m.Histogram.GetSampleCount()),
Value: model.SampleValue(v),
Timestamp: timestamp,
}
samples = append(samples, count)
Expand Down
19 changes: 18 additions & 1 deletion expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
n, err = w.WriteString(" unknown\n")
case dto.MetricType_HISTOGRAM:
n, err = w.WriteString(" histogram\n")
case dto.MetricType_GAUGE_HISTOGRAM:
n, err = w.WriteString(" gaugehistogram\n")
default:
return written, fmt.Errorf("unknown metric type %s", metricType.String())
}
Expand Down Expand Up @@ -325,14 +327,20 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
createdTsBytesWritten, err = writeOpenMetricsCreated(w, compliantName, "", metric, "", 0, metric.Summary.GetCreatedTimestamp())
n += createdTsBytesWritten
}
case dto.MetricType_HISTOGRAM:
case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
if metric.Histogram == nil {
return written, fmt.Errorf(
"expected histogram in metric %s %s", compliantName, metric,
)
}
infSeen := false
for _, b := range metric.Histogram.Bucket {
if b.GetCumulativeCountFloat() > 0 {
return written, fmt.Errorf(
"OpenMetrics v1.0 does not support float histogram %s %s",
compliantName, metric,
)
}
n, err = writeOpenMetricsSample(
w, compliantName, "_bucket", metric,
model.BucketLabel, b.GetUpperBound(),
Expand All @@ -354,6 +362,9 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
0, metric.Histogram.GetSampleCount(), true,
nil,
)
// We do not check for a float sample count here
// because we will check for it below (and error
// out if needed).
written += n
if err != nil {
return
Expand All @@ -368,6 +379,12 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
if err != nil {
return
}
if metric.Histogram.GetSampleCountFloat() > 0 {
return written, fmt.Errorf(
"OpenMetrics v1.0 does not support float histogram %s %s",
compliantName, metric,
)
}
n, err = writeOpenMetricsSample(
w, compliantName, "_count", metric, "", 0,
0, metric.Histogram.GetSampleCount(), true,
Expand Down
85 changes: 85 additions & 0 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,50 @@ request_duration_microseconds_count 2693
# UNIT some_measure_seconds seconds
some_measure_seconds_total{labelname="val1",basename="basevalue"} 42.0
some_measure_seconds_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06
`,
},
// 11: Gauge histogram.
{
in: &dto.MetricFamily{
Name: proto.String("name"),
Help: proto.String("doc string"),
Type: dto.MetricType_GAUGE_HISTOGRAM.Enum(),
Metric: []*dto.Metric{
{
Histogram: &dto.Histogram{
SampleCount: proto.Uint64(2693),
SampleSum: proto.Float64(1756047.3),
Bucket: []*dto.Bucket{
{
UpperBound: proto.Float64(100),
CumulativeCount: proto.Uint64(123),
},
{
UpperBound: proto.Float64(120),
CumulativeCount: proto.Uint64(412),
},
{
UpperBound: proto.Float64(144),
CumulativeCount: proto.Uint64(592),
},
{
UpperBound: proto.Float64(172.8),
CumulativeCount: proto.Uint64(1524),
},
},
},
},
},
},
out: `# HELP name doc string
# TYPE name gaugehistogram
name_bucket{le="100.0"} 123
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
`,
},
}
Expand Down Expand Up @@ -903,6 +947,47 @@ func TestOpenMetricsCreateError(t *testing.T) {
},
err: "expected counter in metric",
},
// 2: Float histogram.
{
in: &dto.MetricFamily{
Name: proto.String("name"),
Help: proto.String("doc string"),
Type: dto.MetricType_GAUGE_HISTOGRAM.Enum(),
Metric: []*dto.Metric{
{
Histogram: &dto.Histogram{
// Note that it is enough to fill the float fields even
// if the values are integers.
SampleCountFloat: proto.Float64(2693),
SampleSum: proto.Float64(1756047.3),
Bucket: []*dto.Bucket{
{
UpperBound: proto.Float64(100),
CumulativeCountFloat: proto.Float64(123),
},
{
UpperBound: proto.Float64(120),
CumulativeCountFloat: proto.Float64(412),
},
{
UpperBound: proto.Float64(144),
CumulativeCountFloat: proto.Float64(592),
},
{
UpperBound: proto.Float64(172.8),
CumulativeCountFloat: proto.Float64(1524),
},
{
UpperBound: proto.Float64(math.Inf(+1)),
CumulativeCountFloat: proto.Float64(2693),
},
},
},
},
},
},
err: "OpenMetrics v1.0 does not support float histogram name histogram",
},
}

for i, scenario := range scenarios {
Expand Down
28 changes: 20 additions & 8 deletions expfmt/text_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
n, err = w.WriteString(" summary\n")
case dto.MetricType_UNTYPED:
n, err = w.WriteString(" untyped\n")
case dto.MetricType_HISTOGRAM:
case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
// The classic Prometheus text format has no notion of a gauge
// histogram. We render a gauge histogram in the same way as a
// regular histogram.
n, err = w.WriteString(" histogram\n")
default:
return written, fmt.Errorf("unknown metric type %s", metricType.String())
Expand Down Expand Up @@ -223,18 +226,22 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
w, name, "_count", metric, "", 0,
float64(metric.Summary.GetSampleCount()),
)
case dto.MetricType_HISTOGRAM:
case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM:
if metric.Histogram == nil {
return written, fmt.Errorf(
"expected histogram in metric %s %s", name, metric,
)
}
infSeen := false
for _, b := range metric.Histogram.Bucket {
v := b.GetCumulativeCountFloat()
if v == 0 {
v = float64(b.GetCumulativeCount())
}
n, err = writeSample(
w, name, "_bucket", metric,
model.BucketLabel, b.GetUpperBound(),
float64(b.GetCumulativeCount()),
v,
)
written += n
if err != nil {
Expand All @@ -245,10 +252,14 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
}
}
if !infSeen {
v := metric.Histogram.GetSampleCountFloat()
if v == 0 {
v = float64(metric.Histogram.GetSampleCount())
}
n, err = writeSample(
w, name, "_bucket", metric,
model.BucketLabel, math.Inf(+1),
float64(metric.Histogram.GetSampleCount()),
v,
)
written += n
if err != nil {
Expand All @@ -263,10 +274,11 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err e
if err != nil {
return
}
n, err = writeSample(
w, name, "_count", metric, "", 0,
float64(metric.Histogram.GetSampleCount()),
)
v := metric.Histogram.GetSampleCountFloat()
if v == 0 {
v = float64(metric.Histogram.GetSampleCount())
}
n, err = writeSample(w, name, "_count", metric, "", 0, v)
default:
return written, fmt.Errorf(
"unexpected type in metric %s %s", name, metric,
Expand Down
Loading
Loading