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>
This commit is contained in:
Charlie Le 2024-10-24 11:51:41 -07:00
parent 3cb09acb21
commit d87f7440ca
2 changed files with 44 additions and 1 deletions

View file

@ -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)

View file

@ -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)
})
}