forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor exemplars to not use generic argument (open-telemetry#5285)
* Refactor exemplars to not use generic argument * Update internal/aggregate * Update metric SDK * Test exemplar value type * Add TestCollectExemplars * Fix lint --------- Co-authored-by: Sam Xie <[email protected]>
- Loading branch information
Showing
24 changed files
with
334 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" | ||
|
||
import ( | ||
"sync" | ||
|
||
"go.opentelemetry.io/otel/sdk/metric/internal/exemplar" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
) | ||
|
||
var exemplarPool = sync.Pool{ | ||
New: func() any { return new([]exemplar.Exemplar) }, | ||
} | ||
|
||
func collectExemplars[N int64 | float64](out *[]metricdata.Exemplar[N], f func(*[]exemplar.Exemplar)) { | ||
dest := exemplarPool.Get().(*[]exemplar.Exemplar) | ||
defer func() { | ||
*dest = (*dest)[:0] | ||
exemplarPool.Put(dest) | ||
}() | ||
|
||
*dest = reset(*dest, len(*out), cap(*out)) | ||
|
||
f(dest) | ||
|
||
*out = reset(*out, len(*dest), cap(*dest)) | ||
for i, e := range *dest { | ||
(*out)[i].FilteredAttributes = e.FilteredAttributes | ||
(*out)[i].Time = e.Time | ||
(*out)[i].SpanID = e.SpanID | ||
(*out)[i].TraceID = e.TraceID | ||
|
||
switch e.Value.Type() { | ||
case exemplar.Int64ValueType: | ||
(*out)[i].Value = N(e.Value.Int64()) | ||
case exemplar.Float64ValueType: | ||
(*out)[i].Value = N(e.Value.Float64()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.