diff --git a/sdk/metric/internal/exemplar/value.go b/sdk/metric/internal/exemplar/value.go index 923ddd5c29fc..9daf27dc0067 100644 --- a/sdk/metric/internal/exemplar/value.go +++ b/sdk/metric/internal/exemplar/value.go @@ -3,9 +3,7 @@ package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar" -import ( - "math" -) +import "math" // ValueType identifies the type of value used in exemplar data. type ValueType uint8 @@ -30,21 +28,13 @@ type Value struct { func NewValue[N int64 | float64](value N) Value { switch v := any(value).(type) { case int64: - return newInt64Value(v) + return Value{t: Int64ValueType, val: uint64(v)} case float64: - return newFloat64Value(v) + return Value{t: Float64ValueType, val: math.Float64bits(v)} } return Value{} } -func newInt64Value(val int64) Value { - return Value{t: Int64ValueType, val: uint64(val)} -} - -func newFloat64Value(val float64) Value { - return Value{t: Float64ValueType, val: math.Float64bits(val)} -} - // Type returns the [ValueType] of data held by v. func (v Value) Type() ValueType { return v.t } diff --git a/sdk/metric/internal/exemplar/value_test.go b/sdk/metric/internal/exemplar/value_test.go new file mode 100644 index 000000000000..2747ceef61f1 --- /dev/null +++ b/sdk/metric/internal/exemplar/value_test.go @@ -0,0 +1,23 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package exemplar + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValue(t *testing.T) { + const iVal, fVal = int64(43), float64(0.3) + i, f := NewValue[int64](iVal), NewValue[float64](fVal) + + assert.Equal(t, Int64ValueType, i.Type()) + assert.Equal(t, iVal, i.Int64()) + assert.Equal(t, float64(0), i.Float64()) + + assert.Equal(t, Float64ValueType, f.Type()) + assert.Equal(t, fVal, f.Float64()) + assert.Equal(t, int64(0), f.Int64()) +}