Browse Source

support int exemplar value type

When the exemplar type is an int, it incorrectly gets converted to a 0
when DoubleValue() is called on the exemplar. This adds a check to
ensure that the value is converted properly based on the type.

Signed-off-by: Charlie Le <charlie_le@apple.com>
pull/15217/head
Charlie Le 4 weeks ago
parent
commit
d87f7440ca
  1. 10
      storage/remote/otlptranslator/prometheusremotewrite/helper.go
  2. 35
      storage/remote/otlptranslator/prometheusremotewrite/helper_test.go

10
storage/remote/otlptranslator/prometheusremotewrite/helper.go

@ -351,9 +351,17 @@ func getPromExemplars[T exemplarType](ctx context.Context, everyN *everyNTimes,
exemplarRunes := 0
promExemplar := prompb.Exemplar{
Value: exemplar.DoubleValue(),
Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()),
}
switch exemplar.ValueType() {
case pmetric.ExemplarValueTypeInt:
promExemplar.Value = float64(exemplar.IntValue())
case pmetric.ExemplarValueTypeDouble:
promExemplar.Value = exemplar.DoubleValue()
default:
return nil, fmt.Errorf("unsupported exemplar value type: %v", exemplar.ValueType())
}
if traceID := exemplar.TraceID(); !traceID.IsEmpty() {
val := hex.EncodeToString(traceID[:])
exemplarRunes += utf8.RuneCountInString(traceIDKey) + utf8.RuneCountInString(val)

35
storage/remote/otlptranslator/prometheusremotewrite/helper_test.go

@ -406,3 +406,38 @@ func TestPrometheusConverter_AddHistogramDataPoints(t *testing.T) {
})
}
}
func TestGetPromExemplars(t *testing.T) {
ctx := context.Background()
everyN := &everyNTimes{n: 1}
t.Run("Exemplars with int value", func(t *testing.T) {
pt := pmetric.NewNumberDataPoint()
exemplar := pt.Exemplars().AppendEmpty()
exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano()))
exemplar.SetIntValue(42)
exemplars, err := getPromExemplars(ctx, everyN, pt)
assert.NoError(t, err)
assert.Len(t, exemplars, 1)
assert.Equal(t, float64(42), exemplars[0].Value)
})
t.Run("Exemplars with double value", func(t *testing.T) {
pt := pmetric.NewNumberDataPoint()
exemplar := pt.Exemplars().AppendEmpty()
exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano()))
exemplar.SetDoubleValue(69.420)
exemplars, err := getPromExemplars(ctx, everyN, pt)
assert.NoError(t, err)
assert.Len(t, exemplars, 1)
assert.Equal(t, 69.420, exemplars[0].Value)
})
t.Run("Exemplars with unsupported value type", func(t *testing.T) {
pt := pmetric.NewNumberDataPoint()
exemplar := pt.Exemplars().AppendEmpty()
exemplar.SetTimestamp(pcommon.Timestamp(time.Now().UnixNano()))
_, err := getPromExemplars(ctx, everyN, pt)
assert.Error(t, err)
})
}

Loading…
Cancel
Save