1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

refactor: modernize code (#7850)

Enable the
[`modernize`](https://golangci-lint.run/docs/linters/configuration/#modernize)
linter in the golangci-lint configuration and fix the reported issues by
running `golangci-lint run --enable-only modernize --fix`. The
`modernize` linter is internally the same as [gopls
modernize](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize).

Similar to #7089.
This commit is contained in:
Oleksandr Redko
2026-01-30 19:15:17 +02:00
committed by GitHub
parent fd5d030c0a
commit 3264bf171b
25 changed files with 101 additions and 138 deletions
+4
View File
@@ -16,6 +16,7 @@ linters:
- govet - govet
- ineffassign - ineffassign
- misspell - misspell
- modernize
- perfsprint - perfsprint
- revive - revive
- staticcheck - staticcheck
@@ -111,6 +112,9 @@ linters:
locale: US locale: US
ignore-rules: ignore-rules:
- cancelled - cancelled
modernize:
disable:
- omitzero
perfsprint: perfsprint:
int-conversion: true int-conversion: true
err-error: true err-error: true
+3 -3
View File
@@ -53,12 +53,12 @@ func TestHashKVsEquality(t *testing.T) {
result = append(result, testcase{hashKVs(nil), nil}) result = append(result, testcase{hashKVs(nil), nil})
for _, key := range keys { for _, key := range keys {
for i := 0; i < len(keyVals); i++ { for i := range keyVals {
kvs := []KeyValue{keyVals[i](key)} kvs := []KeyValue{keyVals[i](key)}
hash := hashKVs(kvs) hash := hashKVs(kvs)
result = append(result, testcase{hash, kvs}) result = append(result, testcase{hash, kvs})
for j := 0; j < len(keyVals); j++ { for j := range keyVals {
kvs := []KeyValue{ kvs := []KeyValue{
keyVals[i](key), keyVals[i](key),
keyVals[j](key), keyVals[j](key),
@@ -66,7 +66,7 @@ func TestHashKVsEquality(t *testing.T) {
hash := hashKVs(kvs) hash := hashKVs(kvs)
result = append(result, testcase{hash, kvs}) result = append(result, testcase{hash, kvs})
for k := 0; k < len(keyVals); k++ { for k := range keyVals {
kvs := []KeyValue{ kvs := []KeyValue{
keyVals[i](key), keyVals[i](key),
keyVals[j](key), keyVals[j](key),
+4 -8
View File
@@ -13,32 +13,28 @@ import (
// BoolSliceValue converts a bool slice into an array with same elements as slice. // BoolSliceValue converts a bool slice into an array with same elements as slice.
func BoolSliceValue(v []bool) any { func BoolSliceValue(v []bool) any {
var zero bool cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[bool]())).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v)) reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface() return cp.Interface()
} }
// Int64SliceValue converts an int64 slice into an array with same elements as slice. // Int64SliceValue converts an int64 slice into an array with same elements as slice.
func Int64SliceValue(v []int64) any { func Int64SliceValue(v []int64) any {
var zero int64 cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]())).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v)) reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface() return cp.Interface()
} }
// Float64SliceValue converts a float64 slice into an array with same elements as slice. // Float64SliceValue converts a float64 slice into an array with same elements as slice.
func Float64SliceValue(v []float64) any { func Float64SliceValue(v []float64) any {
var zero float64 cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[float64]())).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v)) reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface() return cp.Interface()
} }
// StringSliceValue converts a string slice into an array with same elements as slice. // StringSliceValue converts a string slice into an array with same elements as slice.
func StringSliceValue(v []string) any { func StringSliceValue(v []string) any {
var zero string cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[string]())).Elem()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v)) reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface() return cp.Interface()
} }
+9 -10
View File
@@ -101,8 +101,8 @@ var sync any
func BenchmarkBoolSliceValue(b *testing.B) { func BenchmarkBoolSliceValue(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
s := []bool{true, false, true, false} s := []bool{true, false, true, false}
b.ResetTimer()
for n := 0; n < b.N; n++ { for b.Loop() {
sync = BoolSliceValue(s) sync = BoolSliceValue(s)
} }
} }
@@ -110,8 +110,8 @@ func BenchmarkBoolSliceValue(b *testing.B) {
func BenchmarkInt64SliceValue(b *testing.B) { func BenchmarkInt64SliceValue(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
s := []int64{1, 2, 3, 4} s := []int64{1, 2, 3, 4}
b.ResetTimer()
for n := 0; n < b.N; n++ { for b.Loop() {
sync = Int64SliceValue(s) sync = Int64SliceValue(s)
} }
} }
@@ -119,8 +119,8 @@ func BenchmarkInt64SliceValue(b *testing.B) {
func BenchmarkFloat64SliceValue(b *testing.B) { func BenchmarkFloat64SliceValue(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
s := []float64{1.2, 3.4, 5.6, 7.8} s := []float64{1.2, 3.4, 5.6, 7.8}
b.ResetTimer()
for n := 0; n < b.N; n++ { for b.Loop() {
sync = Float64SliceValue(s) sync = Float64SliceValue(s)
} }
} }
@@ -128,8 +128,8 @@ func BenchmarkFloat64SliceValue(b *testing.B) {
func BenchmarkStringSliceValue(b *testing.B) { func BenchmarkStringSliceValue(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
s := []string{"a", "b", "c", "d"} s := []string{"a", "b", "c", "d"}
b.ResetTimer()
for n := 0; n < b.N; n++ { for b.Loop() {
sync = StringSliceValue(s) sync = StringSliceValue(s)
} }
} }
@@ -137,9 +137,8 @@ func BenchmarkStringSliceValue(b *testing.B) {
func BenchmarkAsFloat64Slice(b *testing.B) { func BenchmarkAsFloat64Slice(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
var in any = [2]float64{1, 2.3} var in any = [2]float64{1, 2.3}
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
sync = AsFloat64Slice(in) sync = AsFloat64Slice(in)
} }
} }
+12 -12
View File
@@ -143,9 +143,9 @@ func BenchmarkUint64KB(b *testing.B) {
func BenchmarkUint64(b *testing.B) { func BenchmarkUint64(b *testing.B) {
h := New() h := New()
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; b.Loop(); i++ {
h = h.Uint64(uint64(i)) h = h.Uint64(uint64(i))
} }
} }
@@ -153,45 +153,45 @@ func BenchmarkUint64(b *testing.B) {
func BenchmarkString(b *testing.B) { func BenchmarkString(b *testing.B) {
h := New() h := New()
str := "benchmark_string_value" str := "benchmark_string_value"
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
h = h.String(str) h = h.String(str)
} }
} }
func BenchmarkBool(b *testing.B) { func BenchmarkBool(b *testing.B) {
h := New() h := New()
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; b.Loop(); i++ {
h = h.Bool(i%2 == 0) h = h.Bool(i%2 == 0)
} }
} }
func BenchmarkFloat64(b *testing.B) { func BenchmarkFloat64(b *testing.B) {
h := New() h := New()
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; b.Loop(); i++ {
h = h.Float64(float64(i) * 3.14159) h = h.Float64(float64(i) * 3.14159)
} }
} }
func BenchmarkInt64(b *testing.B) { func BenchmarkInt64(b *testing.B) {
h := New() h := New()
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; b.Loop(); i++ {
h = h.Int64(int64(i)) h = h.Int64(int64(i))
} }
} }
func BenchmarkSum64(b *testing.B) { func BenchmarkSum64(b *testing.B) {
h := New().String("key").Uint64(42).Bool(true) h := New().String("key").Uint64(42).Bool(true)
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
_ = h.Sum64() _ = h.Sum64()
} }
} }
+1 -1
View File
@@ -58,7 +58,7 @@ func isComparable[T comparable](t T) T { return t }
var ( var (
// keyValueType is used in computeDistinctReflect. // keyValueType is used in computeDistinctReflect.
keyValueType = reflect.TypeOf(KeyValue{}) keyValueType = reflect.TypeFor[KeyValue]()
// emptyHash is the hash of an empty set. // emptyHash is the hash of an empty set.
emptyHash = xxhash.New().Sum64() emptyHash = xxhash.New().Sum64()
+3 -3
View File
@@ -314,7 +314,7 @@ func TestLookup(t *testing.T) {
} }
func TestZeroSetExportedMethodsNoPanic(t *testing.T) { func TestZeroSetExportedMethodsNoPanic(t *testing.T) {
rType := reflect.TypeOf((*attribute.Set)(nil)) rType := reflect.TypeFor[*attribute.Set]()
rVal := reflect.ValueOf(&attribute.Set{}) rVal := reflect.ValueOf(&attribute.Set{})
for n := 0; n < rType.NumMethod(); n++ { for n := 0; n < rType.NumMethod(); n++ {
mType := rType.Method(n) mType := rType.Method(n)
@@ -552,12 +552,12 @@ func generateStringAttrsWithSize(keyLen, valueLen int) []attribute.KeyValue {
valueBase := "" valueBase := ""
// Build key base string // Build key base string
for i := 0; i < keyLen; i++ { for i := range keyLen {
keyBase += string(rune('a' + i%26)) keyBase += string(rune('a' + i%26))
} }
// Build value base string // Build value base string
for i := 0; i < valueLen; i++ { for i := range valueLen {
valueBase += string(rune('0' + i%10)) valueBase += string(rune('0' + i%10))
} }
+1 -2
View File
@@ -66,8 +66,7 @@ func IntValue(v int) Value {
// IntSliceValue creates an INTSLICE Value. // IntSliceValue creates an INTSLICE Value.
func IntSliceValue(v []int) Value { func IntSliceValue(v []int) Value {
var int64Val int64 cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]()))
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val)))
for i, val := range v { for i, val := range v {
cp.Elem().Index(i).SetInt(int64(val)) cp.Elem().Index(i).SetInt(int64(val))
} }
+2 -2
View File
@@ -317,7 +317,7 @@ func parseMember(member string) (Member, error) {
keyValue, properties, found := strings.Cut(member, propertyDelimiter) keyValue, properties, found := strings.Cut(member, propertyDelimiter)
if found { if found {
// Parse the member properties. // Parse the member properties.
for _, pStr := range strings.Split(properties, propertyDelimiter) { for pStr := range strings.SplitSeq(properties, propertyDelimiter) {
p, err := parseProperty(pStr) p, err := parseProperty(pStr)
if err != nil { if err != nil {
return newInvalidMember(), err return newInvalidMember(), err
@@ -480,7 +480,7 @@ func Parse(bStr string) (Baggage, error) {
} }
b := make(baggage.List) b := make(baggage.List)
for _, memberStr := range strings.Split(bStr, listDelimiter) { for memberStr := range strings.SplitSeq(bStr, listDelimiter) {
m, err := parseMember(memberStr) m, err := parseMember(memberStr)
if err != nil { if err != nil {
return Baggage{}, err return Baggage{}, err
+6 -8
View File
@@ -1148,9 +1148,8 @@ func BenchmarkNew(b *testing.B) {
mem4, _ := NewMemberRaw("key4", "val4") mem4, _ := NewMemberRaw("key4", "val4")
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
benchBaggage, _ = New(mem1, mem2, mem3, mem4) benchBaggage, _ = New(mem1, mem2, mem3, mem4)
} }
} }
@@ -1160,7 +1159,7 @@ var benchMember Member
func BenchmarkNewMemberRaw(b *testing.B) { func BenchmarkNewMemberRaw(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
benchMember, _ = NewMemberRaw("key", "value") benchMember, _ = NewMemberRaw("key", "value")
} }
} }
@@ -1168,7 +1167,7 @@ func BenchmarkNewMemberRaw(b *testing.B) {
func BenchmarkParse(b *testing.B) { func BenchmarkParse(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
benchBaggage, _ = Parse( benchBaggage, _ = Parse(
"userId=alice,serverNode = DF28 , isProduction = false,hasProp=stuff;propKey;propWValue=value, invalidUtf8=pr%ffo%ffp%fcValue", "userId=alice,serverNode = DF28 , isProduction = false,hasProp=stuff;propKey;propWValue=value, invalidUtf8=pr%ffo%ffp%fcValue",
) )
@@ -1192,9 +1191,8 @@ func BenchmarkString(b *testing.B) {
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
_ = bg.String() _ = bg.String()
} }
} }
@@ -1229,8 +1227,8 @@ func BenchmarkMemberString(b *testing.B) {
member, err := NewMember(alphabet, alphabet, props...) member, err := NewMember(alphabet, alphabet, props...)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
_ = member.String() _ = member.String()
} }
} }
+1 -1
View File
@@ -437,7 +437,7 @@ func loadInsecureFromEnvEndpoint(envEndpoint []string) resolver[bool] {
func convHeaders(s string) (map[string]string, error) { func convHeaders(s string) (map[string]string, error) {
out := make(map[string]string) out := make(map[string]string)
var err error var err error
for _, header := range strings.Split(s, ",") { for header := range strings.SplitSeq(s, ",") {
rawKey, rawVal, found := strings.Cut(header, "=") rawKey, rawVal, found := strings.Cut(header, "=")
if !found { if !found {
err = errors.Join(err, fmt.Errorf("invalid header: %s", header)) err = errors.Join(err, fmt.Errorf("invalid header: %s", header))
+1 -1
View File
@@ -599,7 +599,7 @@ func insecureFromScheme(prev setting[bool], scheme string) setting[bool] {
func convHeaders(s string) (map[string]string, error) { func convHeaders(s string) (map[string]string, error) {
out := make(map[string]string) out := make(map[string]string)
var err error var err error
for _, header := range strings.Split(s, ",") { for header := range strings.SplitSeq(s, ",") {
rawKey, rawVal, found := strings.Cut(header, "=") rawKey, rawVal, found := strings.Cut(header, "=")
if !found { if !found {
err = errors.Join(err, fmt.Errorf("invalid header: %s", header)) err = errors.Join(err, fmt.Errorf("invalid header: %s", header))
+2 -2
View File
@@ -2749,11 +2749,11 @@ func TestExporterSelfInstrumentationConcurrency(t *testing.T) {
const numOperations = 100 const numOperations = 100
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < numGoroutines; i++ { for i := range numGoroutines {
wg.Add(1) wg.Add(1)
go func(id int) { go func(id int) {
defer wg.Done() defer wg.Done()
for j := 0; j < numOperations; j++ { for j := range numOperations {
counter.Add(ctx, 1, otelmetric.WithAttributes(attribute.Int("goroutine", id))) counter.Add(ctx, 1, otelmetric.WithAttributes(attribute.Int("goroutine", id)))
// Occasionally trigger collection // Occasionally trigger collection
+2 -2
View File
@@ -13,8 +13,8 @@ func BenchmarkStartEndSpanNoSDK(b *testing.B) {
ResetForTest(b) ResetForTest(b)
t := TracerProvider().Tracer("Benchmark StartEndSpan") t := TracerProvider().Tracer("Benchmark StartEndSpan")
ctx := b.Context() ctx := b.Context()
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
_, span := t.Start(ctx, "/foo") _, span := t.Start(ctx, "/foo")
span.End() span.End()
} }
+14 -14
View File
@@ -157,7 +157,7 @@ func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption)
cfg := metric.NewInt64CounterConfig(options...) cfg := metric.NewInt64CounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*siCounter)(nil)), kind: reflect.TypeFor[*siCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -183,7 +183,7 @@ func (m *meter) Int64UpDownCounter(
cfg := metric.NewInt64UpDownCounterConfig(options...) cfg := metric.NewInt64UpDownCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*siUpDownCounter)(nil)), kind: reflect.TypeFor[*siUpDownCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -206,7 +206,7 @@ func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOpti
cfg := metric.NewInt64HistogramConfig(options...) cfg := metric.NewInt64HistogramConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*siHistogram)(nil)), kind: reflect.TypeFor[*siHistogram](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -229,7 +229,7 @@ func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (met
cfg := metric.NewInt64GaugeConfig(options...) cfg := metric.NewInt64GaugeConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*siGauge)(nil)), kind: reflect.TypeFor[*siGauge](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -255,7 +255,7 @@ func (m *meter) Int64ObservableCounter(
cfg := metric.NewInt64ObservableCounterConfig(options...) cfg := metric.NewInt64ObservableCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*aiCounter)(nil)), kind: reflect.TypeFor[*aiCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -281,7 +281,7 @@ func (m *meter) Int64ObservableUpDownCounter(
cfg := metric.NewInt64ObservableUpDownCounterConfig(options...) cfg := metric.NewInt64ObservableUpDownCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*aiUpDownCounter)(nil)), kind: reflect.TypeFor[*aiUpDownCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -307,7 +307,7 @@ func (m *meter) Int64ObservableGauge(
cfg := metric.NewInt64ObservableGaugeConfig(options...) cfg := metric.NewInt64ObservableGaugeConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*aiGauge)(nil)), kind: reflect.TypeFor[*aiGauge](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -330,7 +330,7 @@ func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOpti
cfg := metric.NewFloat64CounterConfig(options...) cfg := metric.NewFloat64CounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*sfCounter)(nil)), kind: reflect.TypeFor[*sfCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -356,7 +356,7 @@ func (m *meter) Float64UpDownCounter(
cfg := metric.NewFloat64UpDownCounterConfig(options...) cfg := metric.NewFloat64UpDownCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*sfUpDownCounter)(nil)), kind: reflect.TypeFor[*sfUpDownCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -382,7 +382,7 @@ func (m *meter) Float64Histogram(
cfg := metric.NewFloat64HistogramConfig(options...) cfg := metric.NewFloat64HistogramConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*sfHistogram)(nil)), kind: reflect.TypeFor[*sfHistogram](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -405,7 +405,7 @@ func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption)
cfg := metric.NewFloat64GaugeConfig(options...) cfg := metric.NewFloat64GaugeConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*sfGauge)(nil)), kind: reflect.TypeFor[*sfGauge](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -431,7 +431,7 @@ func (m *meter) Float64ObservableCounter(
cfg := metric.NewFloat64ObservableCounterConfig(options...) cfg := metric.NewFloat64ObservableCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*afCounter)(nil)), kind: reflect.TypeFor[*afCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -457,7 +457,7 @@ func (m *meter) Float64ObservableUpDownCounter(
cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...) cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*afUpDownCounter)(nil)), kind: reflect.TypeFor[*afUpDownCounter](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
@@ -483,7 +483,7 @@ func (m *meter) Float64ObservableGauge(
cfg := metric.NewFloat64ObservableGaugeConfig(options...) cfg := metric.NewFloat64ObservableGaugeConfig(options...)
id := instID{ id := instID{
name: name, name: name,
kind: reflect.TypeOf((*afGauge)(nil)), kind: reflect.TypeFor[*afGauge](),
description: cfg.Description(), description: cfg.Description(),
unit: cfg.Unit(), unit: cfg.Unit(),
} }
+2 -2
View File
@@ -17,11 +17,11 @@ func TestImplementationNoPanics(t *testing.T) {
// methods added to it than the No-Op implementation implements them. // methods added to it than the No-Op implementation implements them.
t.Run("LoggerProvider", assertAllExportedMethodNoPanic( t.Run("LoggerProvider", assertAllExportedMethodNoPanic(
reflect.ValueOf(LoggerProvider{}), reflect.ValueOf(LoggerProvider{}),
reflect.TypeOf((*log.LoggerProvider)(nil)).Elem(), reflect.TypeFor[log.LoggerProvider](),
)) ))
t.Run("Logger", assertAllExportedMethodNoPanic( t.Run("Logger", assertAllExportedMethodNoPanic(
reflect.ValueOf(Logger{}), reflect.ValueOf(Logger{}),
reflect.TypeOf((*log.Logger)(nil)).Elem(), reflect.TypeFor[log.Logger](),
)) ))
} }
+20 -20
View File
@@ -17,83 +17,83 @@ func TestImplementationNoPanics(t *testing.T) {
// methods added to it than the No-Op implementation implements them. // methods added to it than the No-Op implementation implements them.
t.Run("MeterProvider", assertAllExportedMethodNoPanic( t.Run("MeterProvider", assertAllExportedMethodNoPanic(
reflect.ValueOf(MeterProvider{}), reflect.ValueOf(MeterProvider{}),
reflect.TypeOf((*metric.MeterProvider)(nil)).Elem(), reflect.TypeFor[metric.MeterProvider](),
)) ))
t.Run("Meter", assertAllExportedMethodNoPanic( t.Run("Meter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Meter{}), reflect.ValueOf(Meter{}),
reflect.TypeOf((*metric.Meter)(nil)).Elem(), reflect.TypeFor[metric.Meter](),
)) ))
t.Run("Observer", assertAllExportedMethodNoPanic( t.Run("Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Observer{}), reflect.ValueOf(Observer{}),
reflect.TypeOf((*metric.Observer)(nil)).Elem(), reflect.TypeFor[metric.Observer](),
)) ))
t.Run("Registration", assertAllExportedMethodNoPanic( t.Run("Registration", assertAllExportedMethodNoPanic(
reflect.ValueOf(Registration{}), reflect.ValueOf(Registration{}),
reflect.TypeOf((*metric.Registration)(nil)).Elem(), reflect.TypeFor[metric.Registration](),
)) ))
t.Run("Int64Counter", assertAllExportedMethodNoPanic( t.Run("Int64Counter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Counter{}), reflect.ValueOf(Int64Counter{}),
reflect.TypeOf((*metric.Int64Counter)(nil)).Elem(), reflect.TypeFor[metric.Int64Counter](),
)) ))
t.Run("Float64Counter", assertAllExportedMethodNoPanic( t.Run("Float64Counter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Counter{}), reflect.ValueOf(Float64Counter{}),
reflect.TypeOf((*metric.Float64Counter)(nil)).Elem(), reflect.TypeFor[metric.Float64Counter](),
)) ))
t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic( t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64UpDownCounter{}), reflect.ValueOf(Int64UpDownCounter{}),
reflect.TypeOf((*metric.Int64UpDownCounter)(nil)).Elem(), reflect.TypeFor[metric.Int64UpDownCounter](),
)) ))
t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic( t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64UpDownCounter{}), reflect.ValueOf(Float64UpDownCounter{}),
reflect.TypeOf((*metric.Float64UpDownCounter)(nil)).Elem(), reflect.TypeFor[metric.Float64UpDownCounter](),
)) ))
t.Run("Int64Histogram", assertAllExportedMethodNoPanic( t.Run("Int64Histogram", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Histogram{}), reflect.ValueOf(Int64Histogram{}),
reflect.TypeOf((*metric.Int64Histogram)(nil)).Elem(), reflect.TypeFor[metric.Int64Histogram](),
)) ))
t.Run("Float64Histogram", assertAllExportedMethodNoPanic( t.Run("Float64Histogram", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Histogram{}), reflect.ValueOf(Float64Histogram{}),
reflect.TypeOf((*metric.Float64Histogram)(nil)).Elem(), reflect.TypeFor[metric.Float64Histogram](),
)) ))
t.Run("Int64Gauge", assertAllExportedMethodNoPanic( t.Run("Int64Gauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Gauge{}), reflect.ValueOf(Int64Gauge{}),
reflect.TypeOf((*metric.Int64Gauge)(nil)).Elem(), reflect.TypeFor[metric.Int64Gauge](),
)) ))
t.Run("Float64Gauge", assertAllExportedMethodNoPanic( t.Run("Float64Gauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Gauge{}), reflect.ValueOf(Float64Gauge{}),
reflect.TypeOf((*metric.Float64Gauge)(nil)).Elem(), reflect.TypeFor[metric.Float64Gauge](),
)) ))
t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic( t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableCounter{}), reflect.ValueOf(Int64ObservableCounter{}),
reflect.TypeOf((*metric.Int64ObservableCounter)(nil)).Elem(), reflect.TypeFor[metric.Int64ObservableCounter](),
)) ))
t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic( t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableCounter{}), reflect.ValueOf(Float64ObservableCounter{}),
reflect.TypeOf((*metric.Float64ObservableCounter)(nil)).Elem(), reflect.TypeFor[metric.Float64ObservableCounter](),
)) ))
t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic( t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableGauge{}), reflect.ValueOf(Int64ObservableGauge{}),
reflect.TypeOf((*metric.Int64ObservableGauge)(nil)).Elem(), reflect.TypeFor[metric.Int64ObservableGauge](),
)) ))
t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic( t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableGauge{}), reflect.ValueOf(Float64ObservableGauge{}),
reflect.TypeOf((*metric.Float64ObservableGauge)(nil)).Elem(), reflect.TypeFor[metric.Float64ObservableGauge](),
)) ))
t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic( t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableUpDownCounter{}), reflect.ValueOf(Int64ObservableUpDownCounter{}),
reflect.TypeOf((*metric.Int64ObservableUpDownCounter)(nil)).Elem(), reflect.TypeFor[metric.Int64ObservableUpDownCounter](),
)) ))
t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic( t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableUpDownCounter{}), reflect.ValueOf(Float64ObservableUpDownCounter{}),
reflect.TypeOf((*metric.Float64ObservableUpDownCounter)(nil)).Elem(), reflect.TypeFor[metric.Float64ObservableUpDownCounter](),
)) ))
t.Run("Int64Observer", assertAllExportedMethodNoPanic( t.Run("Int64Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Observer{}), reflect.ValueOf(Int64Observer{}),
reflect.TypeOf((*metric.Int64Observer)(nil)).Elem(), reflect.TypeFor[metric.Int64Observer](),
)) ))
t.Run("Float64Observer", assertAllExportedMethodNoPanic( t.Run("Float64Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Observer{}), reflect.ValueOf(Float64Observer{}),
reflect.TypeOf((*metric.Float64Observer)(nil)).Elem(), reflect.TypeFor[metric.Float64Observer](),
)) ))
} }
+6 -26
View File
@@ -495,15 +495,7 @@ func (r *Record) applyValueLimitsAndDedup(val log.Value) log.Value {
sl := val.AsSlice() sl := val.AsSlice()
// First check if any limits need to be applied. // First check if any limits need to be applied.
needsChange := false if slices.ContainsFunc(sl, r.needsValueLimitsOrDedup) {
for _, v := range sl {
if r.needsValueLimitsOrDedup(v) {
needsChange = true
break
}
}
if needsChange {
// Create a new slice to avoid modifying the original. // Create a new slice to avoid modifying the original.
newSl := make([]log.Value, len(sl)) newSl := make([]log.Value, len(sl))
for i, item := range sl { for i, item := range sl {
@@ -564,10 +556,8 @@ func (r *Record) needsValueLimitsOrDedup(val log.Value) bool {
case log.KindString: case log.KindString:
return r.attributeValueLengthLimit >= 0 && len(val.AsString()) > r.attributeValueLengthLimit return r.attributeValueLengthLimit >= 0 && len(val.AsString()) > r.attributeValueLengthLimit
case log.KindSlice: case log.KindSlice:
for _, v := range val.AsSlice() { if slices.ContainsFunc(val.AsSlice(), r.needsValueLimitsOrDedup) {
if r.needsValueLimitsOrDedup(v) { return true
return true
}
} }
case log.KindMap: case log.KindMap:
kvs := val.AsMap() kvs := val.AsMap()
@@ -603,15 +593,7 @@ func (r *Record) dedupeBodyCollections(val log.Value) log.Value {
sl := val.AsSlice() sl := val.AsSlice()
// Check if any nested values need deduplication. // Check if any nested values need deduplication.
needsChange := false if slices.ContainsFunc(sl, r.needsBodyDedup) {
for _, item := range sl {
if r.needsBodyDedup(item) {
needsChange = true
break
}
}
if needsChange {
// Create a new slice to avoid modifying the original. // Create a new slice to avoid modifying the original.
newSl := make([]log.Value, len(sl)) newSl := make([]log.Value, len(sl))
for i, item := range sl { for i, item := range sl {
@@ -654,10 +636,8 @@ func (r *Record) dedupeBodyCollections(val log.Value) log.Value {
func (r *Record) needsBodyDedup(val log.Value) bool { func (r *Record) needsBodyDedup(val log.Value) bool {
switch val.Kind() { switch val.Kind() {
case log.KindSlice: case log.KindSlice:
for _, item := range val.AsSlice() { if slices.ContainsFunc(val.AsSlice(), r.needsBodyDedup) {
if r.needsBodyDedup(item) { return true
return true
}
} }
case log.KindMap: case log.KindMap:
kvs := val.AsMap() kvs := val.AsMap()
+1 -1
View File
@@ -328,7 +328,7 @@ func createMetricDataTestProducerForManual() testSDKProducer {
// Create multiple scopes for comprehensive test data // Create multiple scopes for comprehensive test data
var scopeMetrics []metricdata.ScopeMetrics var scopeMetrics []metricdata.ScopeMetrics
for i := 0; i < 20; i++ { // 20 scopes with 4 metrics each = 80 total metrics for i := range 20 { // 20 scopes with 4 metrics each = 80 total metrics
scopeMetrics = append(scopeMetrics, createScopeMetrics(i)) scopeMetrics = append(scopeMetrics, createScopeMetrics(i))
} }
+1 -1
View File
@@ -773,7 +773,7 @@ func createMetricDataTestProducer() testSDKProducer {
// Create multiple scopes for comprehensive test data // Create multiple scopes for comprehensive test data
var scopeMetrics []metricdata.ScopeMetrics var scopeMetrics []metricdata.ScopeMetrics
for i := 0; i < 20; i++ { // 20 scopes with 4 metrics each = 80 total metrics for i := range 20 { // 20 scopes with 4 metrics each = 80 total metrics
scopeMetrics = append(scopeMetrics, createScopeMetrics(i)) scopeMetrics = append(scopeMetrics, createScopeMetrics(i))
} }
+1 -2
View File
@@ -60,8 +60,7 @@ func (r *hostIDReaderDarwin) read() (string, error) {
return "", err return "", err
} }
lines := strings.Split(result, "\n") for line := range strings.SplitSeq(result, "\n") {
for _, line := range lines {
if strings.Contains(line, "IOPlatformUUID") { if strings.Contains(line, "IOPlatformUUID") {
parts := strings.Split(line, " = ") parts := strings.Split(line, " = ")
if len(parts) == 2 { if len(parts) == 2 {
-12
View File
@@ -123,8 +123,6 @@ PROP3='Final value'`)
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
result := resource.ParseOSReleaseFile(tc.OSRelease) result := resource.ParseOSReleaseFile(tc.OSRelease)
require.Equal(t, tc.Parsed, result) require.Equal(t, tc.Parsed, result)
@@ -152,8 +150,6 @@ func TestSkip(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
result := resource.Skip(tc.Line) result := resource.Skip(tc.Line)
require.Equal(t, tc.Expected, result) require.Equal(t, tc.Expected, result)
@@ -179,8 +175,6 @@ func TestParse(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
key, value, ok := resource.Parse(tc.Line) key, value, ok := resource.Parse(tc.Line)
require.Equal(t, tc.ExpectedKey, key) require.Equal(t, tc.ExpectedKey, key)
@@ -219,8 +213,6 @@ func TestUnquote(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
result := resource.Unquote(tc.Text) result := resource.Unquote(tc.Text)
require.Equal(t, tc.Expected, result) require.Equal(t, tc.Expected, result)
@@ -243,8 +235,6 @@ func TestUnescape(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
result := resource.Unescape(tc.Text) result := resource.Unescape(tc.Text)
require.Equal(t, tc.Expected, result) require.Equal(t, tc.Expected, result)
@@ -285,8 +275,6 @@ func TestBuildOSRelease(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
result := resource.BuildOSRelease(tc.Values) result := resource.BuildOSRelease(tc.Values)
require.Equal(t, tc.Expected, result) require.Equal(t, tc.Expected, result)
+1 -1
View File
@@ -23,7 +23,7 @@ func TestErrorType(t *testing.T) {
builtinErr := errors.New("something went wrong") builtinErr := errors.New("something went wrong")
var nilErr error var nilErr error
wantCustomType := reflect.TypeOf(customErr) wantCustomType := reflect.TypeFor[CustomError]()
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name()) wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
tests := []struct { tests := []struct {
+1 -1
View File
@@ -23,7 +23,7 @@ func TestErrorType(t *testing.T) {
builtinErr := errors.New("something went wrong") builtinErr := errors.New("something went wrong")
var nilErr error var nilErr error
wantCustomType := reflect.TypeOf(customErr) wantCustomType := reflect.TypeFor[CustomError]()
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name()) wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name())
tests := []struct { tests := []struct {
+3 -3
View File
@@ -17,15 +17,15 @@ func TestImplementationNoPanics(t *testing.T) {
// methods added to it than the No-Op implementation implements them. // methods added to it than the No-Op implementation implements them.
t.Run("TracerProvider", assertAllExportedMethodNoPanic( t.Run("TracerProvider", assertAllExportedMethodNoPanic(
reflect.ValueOf(TracerProvider{}), reflect.ValueOf(TracerProvider{}),
reflect.TypeOf((*trace.TracerProvider)(nil)).Elem(), reflect.TypeFor[trace.TracerProvider](),
)) ))
t.Run("Tracer", assertAllExportedMethodNoPanic( t.Run("Tracer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Tracer{}), reflect.ValueOf(Tracer{}),
reflect.TypeOf((*trace.Tracer)(nil)).Elem(), reflect.TypeFor[trace.Tracer](),
)) ))
t.Run("Span", assertAllExportedMethodNoPanic( t.Run("Span", assertAllExportedMethodNoPanic(
reflect.ValueOf(Span{}), reflect.ValueOf(Span{}),
reflect.TypeOf((*trace.Span)(nil)).Elem(), reflect.TypeFor[trace.Span](),
)) ))
} }