From 88a6229fc40b2c4de1f53596176b514f9b0af492 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Tue, 13 Jul 2021 15:11:26 +0200 Subject: [PATCH] protobufparse: add exemplar support for counters Signed-off-by: beorn7 --- pkg/textparse/protobufparse.go | 33 +++++++++++++++++++++++++---- pkg/textparse/protobufparse_test.go | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/textparse/protobufparse.go b/pkg/textparse/protobufparse.go index e87f786a5..265acdf87 100644 --- a/pkg/textparse/protobufparse.go +++ b/pkg/textparse/protobufparse.go @@ -188,10 +188,35 @@ func (p *ProtobufParser) Metric(l *labels.Labels) string { return p.metricBytes.String() } -// Exemplar always returns false because exemplars aren't supported yet by the -// protobuf format. -func (p *ProtobufParser) Exemplar(l *exemplar.Exemplar) bool { - return false +// Exemplar writes the exemplar of the current sample into the passed +// exemplar. It returns if an exemplar exists or not. +func (p *ProtobufParser) Exemplar(ex *exemplar.Exemplar) bool { + m := p.mf.GetMetric()[p.metricPos] + var exProto *dto.Exemplar + switch p.mf.GetType() { + case dto.MetricType_COUNTER: + exProto = m.GetCounter().GetExemplar() + case dto.MetricType_HISTOGRAM: + // TODO(beorn7): Have to pick bucket. Forward iterator in case of sparse histogram. + return false + default: + return false + } + if exProto == nil { + return false + } + ex.Value = exProto.GetValue() + if ts := exProto.GetTimestamp(); ts != nil { + ex.HasTs = true + ex.Ts = ts.GetSeconds()*1000 + int64(ts.GetNanos()/1_000_000) + } + for _, lp := range exProto.GetLabel() { + ex.Labels = append(ex.Labels, labels.Label{ + Name: lp.GetName(), + Value: lp.GetValue(), + }) + } + return true } // Next advances the parser to the next "sample" (emulating the behavior of a diff --git a/pkg/textparse/protobufparse_test.go b/pkg/textparse/protobufparse_test.go index e8c786011..96beba06c 100644 --- a/pkg/textparse/protobufparse_test.go +++ b/pkg/textparse/protobufparse_test.go @@ -299,6 +299,7 @@ metric: < lset: labels.FromStrings( "__name__", "go_memstats_alloc_bytes_total", ), + e: &exemplar.Exemplar{Labels: labels.FromStrings("dummyID", "42"), Value: 12, HasTs: true, Ts: 1625851151233}, }, { m: "something_untyped",