1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

Functionality moved from internal/attribute to attribute (#6580)

Resolve #6525

## Description

This pull request addresses the issue of moving internal functionality
from the `internal/attribute` package to the `attribute `package within
the open-telemetry/opentelemetry-go repository. The goal is to ensure
that none of this functionality is added to the public API of the
attribute or any other public package while improving the naming,
layout, and following Go idioms.

## Changes made

1. Moved Functions to `attribute/internal`

- Created a new internal package within the `attribute` package:
`attribute/internal`.
- Moved `attribute.go` and `attribute_test.go` into the
`attribute/internal`
- Moved the following functions from `internal/attribute` to
`attribute/internal`

2 . Changes in .golangci.yml

- Removed line 67 and 68 from .golangci.yml

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Rabel Mervin
2025-04-03 18:42:30 +05:30
committed by GitHub
parent f713339685
commit 5bf8691520
4 changed files with 2 additions and 4 deletions

View File

@@ -0,0 +1,96 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
/*
Package attribute provide several helper functions for some commonly used
logic of processing attributes.
*/
package attribute // import "go.opentelemetry.io/otel/attribute/internal"
import (
"reflect"
)
// BoolSliceValue converts a bool slice into an array with same elements as slice.
func BoolSliceValue(v []bool) interface{} {
var zero bool
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// Int64SliceValue converts an int64 slice into an array with same elements as slice.
func Int64SliceValue(v []int64) interface{} {
var zero int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// Float64SliceValue converts a float64 slice into an array with same elements as slice.
func Float64SliceValue(v []float64) interface{} {
var zero float64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// StringSliceValue converts a string slice into an array with same elements as slice.
func StringSliceValue(v []string) interface{} {
var zero string
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}
// AsBoolSlice converts a bool array into a slice into with same elements as array.
func AsBoolSlice(v interface{}) []bool {
rv := reflect.ValueOf(v)
if rv.Type().Kind() != reflect.Array {
return nil
}
cpy := make([]bool, rv.Len())
if len(cpy) > 0 {
_ = reflect.Copy(reflect.ValueOf(cpy), rv)
}
return cpy
}
// AsInt64Slice converts an int64 array into a slice into with same elements as array.
func AsInt64Slice(v interface{}) []int64 {
rv := reflect.ValueOf(v)
if rv.Type().Kind() != reflect.Array {
return nil
}
cpy := make([]int64, rv.Len())
if len(cpy) > 0 {
_ = reflect.Copy(reflect.ValueOf(cpy), rv)
}
return cpy
}
// AsFloat64Slice converts a float64 array into a slice into with same elements as array.
func AsFloat64Slice(v interface{}) []float64 {
rv := reflect.ValueOf(v)
if rv.Type().Kind() != reflect.Array {
return nil
}
cpy := make([]float64, rv.Len())
if len(cpy) > 0 {
_ = reflect.Copy(reflect.ValueOf(cpy), rv)
}
return cpy
}
// AsStringSlice converts a string array into a slice into with same elements as array.
func AsStringSlice(v interface{}) []string {
rv := reflect.ValueOf(v)
if rv.Type().Kind() != reflect.Array {
return nil
}
cpy := make([]string, rv.Len())
if len(cpy) > 0 {
_ = reflect.Copy(reflect.ValueOf(cpy), rv)
}
return cpy
}

View File

@@ -0,0 +1,145 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attribute
import (
"reflect"
"testing"
)
var wrapFloat64SliceValue = func(v interface{}) interface{} {
if vi, ok := v.([]float64); ok {
return Float64SliceValue(vi)
}
return nil
}
var wrapInt64SliceValue = func(v interface{}) interface{} {
if vi, ok := v.([]int64); ok {
return Int64SliceValue(vi)
}
return nil
}
var wrapBoolSliceValue = func(v interface{}) interface{} {
if vi, ok := v.([]bool); ok {
return BoolSliceValue(vi)
}
return nil
}
var wrapStringSliceValue = func(v interface{}) interface{} {
if vi, ok := v.([]string); ok {
return StringSliceValue(vi)
}
return nil
}
var (
wrapAsBoolSlice = func(v interface{}) interface{} { return AsBoolSlice(v) }
wrapAsInt64Slice = func(v interface{}) interface{} { return AsInt64Slice(v) }
wrapAsFloat64Slice = func(v interface{}) interface{} { return AsFloat64Slice(v) }
wrapAsStringSlice = func(v interface{}) interface{} { return AsStringSlice(v) }
)
func TestSliceValue(t *testing.T) {
type args struct {
v interface{}
}
tests := []struct {
name string
args args
want interface{}
fn func(interface{}) interface{}
}{
{
name: "Float64SliceValue() two items",
args: args{v: []float64{1, 2.3}}, want: [2]float64{1, 2.3}, fn: wrapFloat64SliceValue,
},
{
name: "Int64SliceValue() two items",
args: args{[]int64{1, 2}}, want: [2]int64{1, 2}, fn: wrapInt64SliceValue,
},
{
name: "BoolSliceValue() two items",
args: args{v: []bool{true, false}}, want: [2]bool{true, false}, fn: wrapBoolSliceValue,
},
{
name: "StringSliceValue() two items",
args: args{[]string{"123", "2"}}, want: [2]string{"123", "2"}, fn: wrapStringSliceValue,
},
{
name: "AsBoolSlice() two items",
args: args{[2]bool{true, false}}, want: []bool{true, false}, fn: wrapAsBoolSlice,
},
{
name: "AsInt64Slice() two items",
args: args{[2]int64{1, 3}}, want: []int64{1, 3}, fn: wrapAsInt64Slice,
},
{
name: "AsFloat64Slice() two items",
args: args{[2]float64{1.2, 3.1}}, want: []float64{1.2, 3.1}, fn: wrapAsFloat64Slice,
},
{
name: "AsStringSlice() two items",
args: args{[2]string{"1234", "12"}}, want: []string{"1234", "12"}, fn: wrapAsStringSlice,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fn(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
// sync is a global used to ensure the benchmark are not optimized away.
var sync any
func BenchmarkBoolSliceValue(b *testing.B) {
b.ReportAllocs()
s := []bool{true, false, true, false}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = BoolSliceValue(s)
}
}
func BenchmarkInt64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []int64{1, 2, 3, 4}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = Int64SliceValue(s)
}
}
func BenchmarkFloat64SliceValue(b *testing.B) {
b.ReportAllocs()
s := []float64{1.2, 3.4, 5.6, 7.8}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = Float64SliceValue(s)
}
}
func BenchmarkStringSliceValue(b *testing.B) {
b.ReportAllocs()
s := []string{"a", "b", "c", "d"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
sync = StringSliceValue(s)
}
}
func BenchmarkAsFloat64Slice(b *testing.B) {
b.ReportAllocs()
var in interface{} = [2]float64{1, 2.3}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sync = AsFloat64Slice(in)
}
}

View File

@@ -9,8 +9,8 @@ import (
"reflect"
"strconv"
attribute "go.opentelemetry.io/otel/attribute/internal"
"go.opentelemetry.io/otel/internal"
"go.opentelemetry.io/otel/internal/attribute"
)
//go:generate stringer -type=Type