Skip to content

Commit

Permalink
Add TestCollectExemplars
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed May 6, 2024
1 parent ce98ed4 commit 667b58d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
12 changes: 5 additions & 7 deletions sdk/metric/internal/aggregate/exemplar.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ func collectExemplars[N int64 | float64](out *[]metricdata.Exemplar[N], f func(*
(*out)[i].SpanID = e.SpanID
(*out)[i].TraceID = e.TraceID

val := (*out)[i].Value
valPtr := any(&val)
switch v := valPtr.(type) {
case *int64:
*v = e.Value.Int64()
case *float64:
*v = e.Value.Float64()
switch e.Value.Type() {
case exemplar.Int64ValueType:
(*out)[i].Value = N(e.Value.Int64())
case exemplar.Float64ValueType:
(*out)[i].Value = N(e.Value.Float64())
}
}
}
49 changes: 49 additions & 0 deletions sdk/metric/internal/aggregate/exemplar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package aggregate

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

func TestCollectExemplars(t *testing.T) {
t.Run("Int64", testCollectExemplars[int64]())
t.Run("Float64", testCollectExemplars[float64]())
}

func testCollectExemplars[N int64 | float64]() func(t *testing.T) {
return func(t *testing.T) {
now := time.Now()
alice := attribute.String("user", "Alice")
value := N(1)
spanID := [8]byte{0x1}
traceID := [16]byte{0x1}

out := new([]metricdata.Exemplar[N])
collectExemplars(out, func(in *[]exemplar.Exemplar) {
*in = reset(*in, 1, 1)
(*in)[0] = exemplar.Exemplar{
FilteredAttributes: []attribute.KeyValue{alice},
Time: now,
Value: exemplar.NewValue(value),
SpanID: spanID[:],
TraceID: traceID[:],
}
})

assert.Equal(t, []metricdata.Exemplar[N]{{
FilteredAttributes: []attribute.KeyValue{alice},
Time: now,
Value: value,
SpanID: spanID[:],
TraceID: traceID[:],
}}, *out)
}
}

0 comments on commit 667b58d

Please sign in to comment.