Skip to content

Commit

Permalink
Test exemplar value type
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed May 3, 2024
1 parent d967178 commit 8cc78bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
16 changes: 3 additions & 13 deletions sdk/metric/internal/exemplar/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }

Expand Down
27 changes: 27 additions & 0 deletions sdk/metric/internal/exemplar/value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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, bad := NewValue[int64](iVal), NewValue[float64](fVal), Value{}

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

assert.Equal(t, UnknownValueType, bad.Type())
assert.Equal(t, float64(0), bad.Float64())
assert.Equal(t, int64(0), bad.Int64())
}

0 comments on commit 8cc78bb

Please sign in to comment.