From 50630c4c5f3b528ab7538e6683f446d57eda7714 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 6 Dec 2024 16:03:25 -0300 Subject: [PATCH] [exporter/prometheusremotewrite] respect integer exemplar values (#36688) Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com> --- ...theusremotewriteexporter_exemplar-int.yaml | 27 +++++++++++++++++++ .../prometheusremotewrite/helper.go | 15 ++++++++--- .../prometheusremotewrite/helper_test.go | 11 ++++++++ .../prometheusremotewrite/testutils_test.go | 9 +++++-- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 .chloggen/exporter_prometheusremotewriteexporter_exemplar-int.yaml diff --git a/.chloggen/exporter_prometheusremotewriteexporter_exemplar-int.yaml b/.chloggen/exporter_prometheusremotewriteexporter_exemplar-int.yaml new file mode 100644 index 000000000000..40ef4e29a8a0 --- /dev/null +++ b/.chloggen/exporter_prometheusremotewriteexporter_exemplar-int.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: exporter/prometheusremotewrite + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix exemplar handling when the exemplar is an integer value. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36657] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: Send metrics with exemplars as integer values now are correctly handled. + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/pkg/translator/prometheusremotewrite/helper.go b/pkg/translator/prometheusremotewrite/helper.go index 252973b96dc2..939f3ae5e5e3 100644 --- a/pkg/translator/prometheusremotewrite/helper.go +++ b/pkg/translator/prometheusremotewrite/helper.go @@ -301,9 +301,18 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar { exemplar := pt.Exemplars().At(i) exemplarRunes := 0 - promExemplar := prompb.Exemplar{ - Value: exemplar.DoubleValue(), - Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), + var promExemplar prompb.Exemplar + switch exemplar.ValueType() { + case pmetric.ExemplarValueTypeInt: + promExemplar = prompb.Exemplar{ + Value: float64(exemplar.IntValue()), + Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), + } + case pmetric.ExemplarValueTypeDouble: + promExemplar = prompb.Exemplar{ + Value: exemplar.DoubleValue(), + Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()), + } } if traceID := exemplar.TraceID(); !traceID.IsEmpty() { val := hex.EncodeToString(traceID[:]) diff --git a/pkg/translator/prometheusremotewrite/helper_test.go b/pkg/translator/prometheusremotewrite/helper_test.go index 12c0dcd978be..631ff7c831b1 100644 --- a/pkg/translator/prometheusremotewrite/helper_test.go +++ b/pkg/translator/prometheusremotewrite/helper_test.go @@ -479,6 +479,17 @@ func Test_getPromExemplars(t *testing.T) { }, }, }, + { + "with_exemplars_int_value", + getHistogramDataPointWithExemplars(t, tnow, intVal2, traceIDValue1, spanIDValue1, label11, value11), + []prompb.Exemplar{ + { + Value: float64(intVal2), + Timestamp: timestamp.FromTime(tnow), + Labels: []prompb.Label{getLabel(prometheustranslator.ExemplarTraceIDKey, traceIDValue1), getLabel(prometheustranslator.ExemplarSpanIDKey, spanIDValue1), getLabel(label11, value11)}, + }, + }, + }, { "too_many_runes_drops_labels", getHistogramDataPointWithExemplars(t, tnow, floatVal1, "", "", keyWith129Runes, ""), diff --git a/pkg/translator/prometheusremotewrite/testutils_test.go b/pkg/translator/prometheusremotewrite/testutils_test.go index 49ef7a735081..42a8dc48f9e7 100644 --- a/pkg/translator/prometheusremotewrite/testutils_test.go +++ b/pkg/translator/prometheusremotewrite/testutils_test.go @@ -184,11 +184,16 @@ func getTimeSeriesWithSamplesAndExemplars(labels []prompb.Label, samples []promp } } -func getHistogramDataPointWithExemplars(t *testing.T, time time.Time, value float64, traceID string, spanID string, attributeKey string, attributeValue string) pmetric.HistogramDataPoint { +func getHistogramDataPointWithExemplars[V int64 | float64](t *testing.T, time time.Time, value V, traceID string, spanID string, attributeKey string, attributeValue string) pmetric.HistogramDataPoint { h := pmetric.NewHistogramDataPoint() e := h.Exemplars().AppendEmpty() - e.SetDoubleValue(value) + switch v := (any)(value).(type) { + case int64: + e.SetIntValue(v) + case float64: + e.SetDoubleValue(v) + } e.SetTimestamp(pcommon.NewTimestampFromTime(time)) if attributeKey != "" || attributeValue != "" { e.FilteredAttributes().PutStr(attributeKey, attributeValue)