diff --git a/.golangci.yml b/.golangci.yml index 1b1b2aff9..d48722875 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - govet - ineffassign - misspell + - modernize - perfsprint - revive - staticcheck @@ -111,6 +112,9 @@ linters: locale: US ignore-rules: - cancelled + modernize: + disable: + - omitzero perfsprint: int-conversion: true err-error: true diff --git a/attribute/hash_test.go b/attribute/hash_test.go index 0d98c8fb1..4b3b382e0 100644 --- a/attribute/hash_test.go +++ b/attribute/hash_test.go @@ -53,12 +53,12 @@ func TestHashKVsEquality(t *testing.T) { result = append(result, testcase{hashKVs(nil), nil}) for _, key := range keys { - for i := 0; i < len(keyVals); i++ { + for i := range keyVals { kvs := []KeyValue{keyVals[i](key)} hash := hashKVs(kvs) result = append(result, testcase{hash, kvs}) - for j := 0; j < len(keyVals); j++ { + for j := range keyVals { kvs := []KeyValue{ keyVals[i](key), keyVals[j](key), @@ -66,7 +66,7 @@ func TestHashKVsEquality(t *testing.T) { hash := hashKVs(kvs) result = append(result, testcase{hash, kvs}) - for k := 0; k < len(keyVals); k++ { + for k := range keyVals { kvs := []KeyValue{ keyVals[i](key), keyVals[j](key), diff --git a/attribute/internal/attribute.go b/attribute/internal/attribute.go index 087550430..7f5eae877 100644 --- a/attribute/internal/attribute.go +++ b/attribute/internal/attribute.go @@ -13,32 +13,28 @@ import ( // BoolSliceValue converts a bool slice into an array with same elements as slice. func BoolSliceValue(v []bool) any { - var zero bool - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem() + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[bool]())).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) any { - var zero int64 - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem() + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]())).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) any { - var zero float64 - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem() + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[float64]())).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) any { - var zero string - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem() + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[string]())).Elem() reflect.Copy(cp, reflect.ValueOf(v)) return cp.Interface() } diff --git a/attribute/internal/attribute_test.go b/attribute/internal/attribute_test.go index dd710aeaa..e0ebb0643 100644 --- a/attribute/internal/attribute_test.go +++ b/attribute/internal/attribute_test.go @@ -101,8 +101,8 @@ 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++ { + + for b.Loop() { sync = BoolSliceValue(s) } } @@ -110,8 +110,8 @@ func BenchmarkBoolSliceValue(b *testing.B) { func BenchmarkInt64SliceValue(b *testing.B) { b.ReportAllocs() s := []int64{1, 2, 3, 4} - b.ResetTimer() - for n := 0; n < b.N; n++ { + + for b.Loop() { sync = Int64SliceValue(s) } } @@ -119,8 +119,8 @@ func BenchmarkInt64SliceValue(b *testing.B) { 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++ { + + for b.Loop() { sync = Float64SliceValue(s) } } @@ -128,8 +128,8 @@ func BenchmarkFloat64SliceValue(b *testing.B) { func BenchmarkStringSliceValue(b *testing.B) { b.ReportAllocs() s := []string{"a", "b", "c", "d"} - b.ResetTimer() - for n := 0; n < b.N; n++ { + + for b.Loop() { sync = StringSliceValue(s) } } @@ -137,9 +137,8 @@ func BenchmarkStringSliceValue(b *testing.B) { func BenchmarkAsFloat64Slice(b *testing.B) { b.ReportAllocs() var in any = [2]float64{1, 2.3} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { sync = AsFloat64Slice(in) } } diff --git a/attribute/internal/xxhash/xxhash_test.go b/attribute/internal/xxhash/xxhash_test.go index a8f19fed0..bca1a378e 100644 --- a/attribute/internal/xxhash/xxhash_test.go +++ b/attribute/internal/xxhash/xxhash_test.go @@ -143,9 +143,9 @@ func BenchmarkUint64KB(b *testing.B) { func BenchmarkUint64(b *testing.B) { h := New() - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { h = h.Uint64(uint64(i)) } } @@ -153,45 +153,45 @@ func BenchmarkUint64(b *testing.B) { func BenchmarkString(b *testing.B) { h := New() str := "benchmark_string_value" - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { h = h.String(str) } } func BenchmarkBool(b *testing.B) { h := New() - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { h = h.Bool(i%2 == 0) } } func BenchmarkFloat64(b *testing.B) { h := New() - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { h = h.Float64(float64(i) * 3.14159) } } func BenchmarkInt64(b *testing.B) { h := New() - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { h = h.Int64(int64(i)) } } func BenchmarkSum64(b *testing.B) { h := New().String("key").Uint64(42).Bool(true) - b.ResetTimer() + b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { _ = h.Sum64() } } diff --git a/attribute/set.go b/attribute/set.go index 911d557ee..6572c98b1 100644 --- a/attribute/set.go +++ b/attribute/set.go @@ -58,7 +58,7 @@ func isComparable[T comparable](t T) T { return t } var ( // keyValueType is used in computeDistinctReflect. - keyValueType = reflect.TypeOf(KeyValue{}) + keyValueType = reflect.TypeFor[KeyValue]() // emptyHash is the hash of an empty set. emptyHash = xxhash.New().Sum64() diff --git a/attribute/set_test.go b/attribute/set_test.go index 098458310..2c631f761 100644 --- a/attribute/set_test.go +++ b/attribute/set_test.go @@ -314,7 +314,7 @@ func TestLookup(t *testing.T) { } func TestZeroSetExportedMethodsNoPanic(t *testing.T) { - rType := reflect.TypeOf((*attribute.Set)(nil)) + rType := reflect.TypeFor[*attribute.Set]() rVal := reflect.ValueOf(&attribute.Set{}) for n := 0; n < rType.NumMethod(); n++ { mType := rType.Method(n) @@ -552,12 +552,12 @@ func generateStringAttrsWithSize(keyLen, valueLen int) []attribute.KeyValue { valueBase := "" // Build key base string - for i := 0; i < keyLen; i++ { + for i := range keyLen { keyBase += string(rune('a' + i%26)) } // Build value base string - for i := 0; i < valueLen; i++ { + for i := range valueLen { valueBase += string(rune('0' + i%10)) } diff --git a/attribute/value.go b/attribute/value.go index 653c33a86..5931e7129 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -66,8 +66,7 @@ func IntValue(v int) Value { // IntSliceValue creates an INTSLICE Value. func IntSliceValue(v []int) Value { - var int64Val int64 - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val))) + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeFor[int64]())) for i, val := range v { cp.Elem().Index(i).SetInt(int64(val)) } diff --git a/baggage/baggage.go b/baggage/baggage.go index 78e98c4c0..c4093e49a 100644 --- a/baggage/baggage.go +++ b/baggage/baggage.go @@ -317,7 +317,7 @@ func parseMember(member string) (Member, error) { keyValue, properties, found := strings.Cut(member, propertyDelimiter) if found { // Parse the member properties. - for _, pStr := range strings.Split(properties, propertyDelimiter) { + for pStr := range strings.SplitSeq(properties, propertyDelimiter) { p, err := parseProperty(pStr) if err != nil { return newInvalidMember(), err @@ -480,7 +480,7 @@ func Parse(bStr string) (Baggage, error) { } b := make(baggage.List) - for _, memberStr := range strings.Split(bStr, listDelimiter) { + for memberStr := range strings.SplitSeq(bStr, listDelimiter) { m, err := parseMember(memberStr) if err != nil { return Baggage{}, err diff --git a/baggage/baggage_test.go b/baggage/baggage_test.go index 749bd328f..3ef59734c 100644 --- a/baggage/baggage_test.go +++ b/baggage/baggage_test.go @@ -1148,9 +1148,8 @@ func BenchmarkNew(b *testing.B) { mem4, _ := NewMemberRaw("key4", "val4") b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { benchBaggage, _ = New(mem1, mem2, mem3, mem4) } } @@ -1160,7 +1159,7 @@ var benchMember Member func BenchmarkNewMemberRaw(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { benchMember, _ = NewMemberRaw("key", "value") } } @@ -1168,7 +1167,7 @@ func BenchmarkNewMemberRaw(b *testing.B) { func BenchmarkParse(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { benchBaggage, _ = Parse( "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) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _ = bg.String() } } @@ -1229,8 +1227,8 @@ func BenchmarkMemberString(b *testing.B) { member, err := NewMember(alphabet, alphabet, props...) require.NoError(b, err) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { _ = member.String() } } diff --git a/exporters/otlp/otlplog/otlploggrpc/config.go b/exporters/otlp/otlplog/otlploggrpc/config.go index 3fda9fcb0..f2a3c552d 100644 --- a/exporters/otlp/otlplog/otlploggrpc/config.go +++ b/exporters/otlp/otlplog/otlploggrpc/config.go @@ -437,7 +437,7 @@ func loadInsecureFromEnvEndpoint(envEndpoint []string) resolver[bool] { func convHeaders(s string) (map[string]string, error) { out := make(map[string]string) var err error - for _, header := range strings.Split(s, ",") { + for header := range strings.SplitSeq(s, ",") { rawKey, rawVal, found := strings.Cut(header, "=") if !found { err = errors.Join(err, fmt.Errorf("invalid header: %s", header)) diff --git a/exporters/otlp/otlplog/otlploghttp/config.go b/exporters/otlp/otlplog/otlploghttp/config.go index 07be145bd..21e700769 100644 --- a/exporters/otlp/otlplog/otlploghttp/config.go +++ b/exporters/otlp/otlplog/otlploghttp/config.go @@ -599,7 +599,7 @@ func insecureFromScheme(prev setting[bool], scheme string) setting[bool] { func convHeaders(s string) (map[string]string, error) { out := make(map[string]string) var err error - for _, header := range strings.Split(s, ",") { + for header := range strings.SplitSeq(s, ",") { rawKey, rawVal, found := strings.Cut(header, "=") if !found { err = errors.Join(err, fmt.Errorf("invalid header: %s", header)) diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index 5b4d80c58..eda709be5 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -2749,11 +2749,11 @@ func TestExporterSelfInstrumentationConcurrency(t *testing.T) { const numOperations = 100 var wg sync.WaitGroup - for i := 0; i < numGoroutines; i++ { + for i := range numGoroutines { wg.Add(1) go func(id int) { defer wg.Done() - for j := 0; j < numOperations; j++ { + for j := range numOperations { counter.Add(ctx, 1, otelmetric.WithAttributes(attribute.Int("goroutine", id))) // Occasionally trigger collection diff --git a/internal/global/benchmark_test.go b/internal/global/benchmark_test.go index 6706fe729..2401073e3 100644 --- a/internal/global/benchmark_test.go +++ b/internal/global/benchmark_test.go @@ -13,8 +13,8 @@ func BenchmarkStartEndSpanNoSDK(b *testing.B) { ResetForTest(b) t := TracerProvider().Tracer("Benchmark StartEndSpan") ctx := b.Context() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { _, span := t.Start(ctx, "/foo") span.End() } diff --git a/internal/global/meter.go b/internal/global/meter.go index 6db969f73..50043d669 100644 --- a/internal/global/meter.go +++ b/internal/global/meter.go @@ -157,7 +157,7 @@ func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) cfg := metric.NewInt64CounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*siCounter)(nil)), + kind: reflect.TypeFor[*siCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -183,7 +183,7 @@ func (m *meter) Int64UpDownCounter( cfg := metric.NewInt64UpDownCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*siUpDownCounter)(nil)), + kind: reflect.TypeFor[*siUpDownCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -206,7 +206,7 @@ func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOpti cfg := metric.NewInt64HistogramConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*siHistogram)(nil)), + kind: reflect.TypeFor[*siHistogram](), description: cfg.Description(), unit: cfg.Unit(), } @@ -229,7 +229,7 @@ func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (met cfg := metric.NewInt64GaugeConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*siGauge)(nil)), + kind: reflect.TypeFor[*siGauge](), description: cfg.Description(), unit: cfg.Unit(), } @@ -255,7 +255,7 @@ func (m *meter) Int64ObservableCounter( cfg := metric.NewInt64ObservableCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*aiCounter)(nil)), + kind: reflect.TypeFor[*aiCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -281,7 +281,7 @@ func (m *meter) Int64ObservableUpDownCounter( cfg := metric.NewInt64ObservableUpDownCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*aiUpDownCounter)(nil)), + kind: reflect.TypeFor[*aiUpDownCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -307,7 +307,7 @@ func (m *meter) Int64ObservableGauge( cfg := metric.NewInt64ObservableGaugeConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*aiGauge)(nil)), + kind: reflect.TypeFor[*aiGauge](), description: cfg.Description(), unit: cfg.Unit(), } @@ -330,7 +330,7 @@ func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOpti cfg := metric.NewFloat64CounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*sfCounter)(nil)), + kind: reflect.TypeFor[*sfCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -356,7 +356,7 @@ func (m *meter) Float64UpDownCounter( cfg := metric.NewFloat64UpDownCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*sfUpDownCounter)(nil)), + kind: reflect.TypeFor[*sfUpDownCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -382,7 +382,7 @@ func (m *meter) Float64Histogram( cfg := metric.NewFloat64HistogramConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*sfHistogram)(nil)), + kind: reflect.TypeFor[*sfHistogram](), description: cfg.Description(), unit: cfg.Unit(), } @@ -405,7 +405,7 @@ func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) cfg := metric.NewFloat64GaugeConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*sfGauge)(nil)), + kind: reflect.TypeFor[*sfGauge](), description: cfg.Description(), unit: cfg.Unit(), } @@ -431,7 +431,7 @@ func (m *meter) Float64ObservableCounter( cfg := metric.NewFloat64ObservableCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*afCounter)(nil)), + kind: reflect.TypeFor[*afCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -457,7 +457,7 @@ func (m *meter) Float64ObservableUpDownCounter( cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*afUpDownCounter)(nil)), + kind: reflect.TypeFor[*afUpDownCounter](), description: cfg.Description(), unit: cfg.Unit(), } @@ -483,7 +483,7 @@ func (m *meter) Float64ObservableGauge( cfg := metric.NewFloat64ObservableGaugeConfig(options...) id := instID{ name: name, - kind: reflect.TypeOf((*afGauge)(nil)), + kind: reflect.TypeFor[*afGauge](), description: cfg.Description(), unit: cfg.Unit(), } diff --git a/log/noop/noop_test.go b/log/noop/noop_test.go index ffffb56b9..b8437e75d 100644 --- a/log/noop/noop_test.go +++ b/log/noop/noop_test.go @@ -17,11 +17,11 @@ func TestImplementationNoPanics(t *testing.T) { // methods added to it than the No-Op implementation implements them. t.Run("LoggerProvider", assertAllExportedMethodNoPanic( reflect.ValueOf(LoggerProvider{}), - reflect.TypeOf((*log.LoggerProvider)(nil)).Elem(), + reflect.TypeFor[log.LoggerProvider](), )) t.Run("Logger", assertAllExportedMethodNoPanic( reflect.ValueOf(Logger{}), - reflect.TypeOf((*log.Logger)(nil)).Elem(), + reflect.TypeFor[log.Logger](), )) } diff --git a/metric/noop/noop_test.go b/metric/noop/noop_test.go index 0264b2362..007642f58 100644 --- a/metric/noop/noop_test.go +++ b/metric/noop/noop_test.go @@ -17,83 +17,83 @@ func TestImplementationNoPanics(t *testing.T) { // methods added to it than the No-Op implementation implements them. t.Run("MeterProvider", assertAllExportedMethodNoPanic( reflect.ValueOf(MeterProvider{}), - reflect.TypeOf((*metric.MeterProvider)(nil)).Elem(), + reflect.TypeFor[metric.MeterProvider](), )) t.Run("Meter", assertAllExportedMethodNoPanic( reflect.ValueOf(Meter{}), - reflect.TypeOf((*metric.Meter)(nil)).Elem(), + reflect.TypeFor[metric.Meter](), )) t.Run("Observer", assertAllExportedMethodNoPanic( reflect.ValueOf(Observer{}), - reflect.TypeOf((*metric.Observer)(nil)).Elem(), + reflect.TypeFor[metric.Observer](), )) t.Run("Registration", assertAllExportedMethodNoPanic( reflect.ValueOf(Registration{}), - reflect.TypeOf((*metric.Registration)(nil)).Elem(), + reflect.TypeFor[metric.Registration](), )) t.Run("Int64Counter", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64Counter{}), - reflect.TypeOf((*metric.Int64Counter)(nil)).Elem(), + reflect.TypeFor[metric.Int64Counter](), )) t.Run("Float64Counter", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64Counter{}), - reflect.TypeOf((*metric.Float64Counter)(nil)).Elem(), + reflect.TypeFor[metric.Float64Counter](), )) t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64UpDownCounter{}), - reflect.TypeOf((*metric.Int64UpDownCounter)(nil)).Elem(), + reflect.TypeFor[metric.Int64UpDownCounter](), )) t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64UpDownCounter{}), - reflect.TypeOf((*metric.Float64UpDownCounter)(nil)).Elem(), + reflect.TypeFor[metric.Float64UpDownCounter](), )) t.Run("Int64Histogram", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64Histogram{}), - reflect.TypeOf((*metric.Int64Histogram)(nil)).Elem(), + reflect.TypeFor[metric.Int64Histogram](), )) t.Run("Float64Histogram", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64Histogram{}), - reflect.TypeOf((*metric.Float64Histogram)(nil)).Elem(), + reflect.TypeFor[metric.Float64Histogram](), )) t.Run("Int64Gauge", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64Gauge{}), - reflect.TypeOf((*metric.Int64Gauge)(nil)).Elem(), + reflect.TypeFor[metric.Int64Gauge](), )) t.Run("Float64Gauge", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64Gauge{}), - reflect.TypeOf((*metric.Float64Gauge)(nil)).Elem(), + reflect.TypeFor[metric.Float64Gauge](), )) t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64ObservableCounter{}), - reflect.TypeOf((*metric.Int64ObservableCounter)(nil)).Elem(), + reflect.TypeFor[metric.Int64ObservableCounter](), )) t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64ObservableCounter{}), - reflect.TypeOf((*metric.Float64ObservableCounter)(nil)).Elem(), + reflect.TypeFor[metric.Float64ObservableCounter](), )) t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64ObservableGauge{}), - reflect.TypeOf((*metric.Int64ObservableGauge)(nil)).Elem(), + reflect.TypeFor[metric.Int64ObservableGauge](), )) t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64ObservableGauge{}), - reflect.TypeOf((*metric.Float64ObservableGauge)(nil)).Elem(), + reflect.TypeFor[metric.Float64ObservableGauge](), )) t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64ObservableUpDownCounter{}), - reflect.TypeOf((*metric.Int64ObservableUpDownCounter)(nil)).Elem(), + reflect.TypeFor[metric.Int64ObservableUpDownCounter](), )) t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64ObservableUpDownCounter{}), - reflect.TypeOf((*metric.Float64ObservableUpDownCounter)(nil)).Elem(), + reflect.TypeFor[metric.Float64ObservableUpDownCounter](), )) t.Run("Int64Observer", assertAllExportedMethodNoPanic( reflect.ValueOf(Int64Observer{}), - reflect.TypeOf((*metric.Int64Observer)(nil)).Elem(), + reflect.TypeFor[metric.Int64Observer](), )) t.Run("Float64Observer", assertAllExportedMethodNoPanic( reflect.ValueOf(Float64Observer{}), - reflect.TypeOf((*metric.Float64Observer)(nil)).Elem(), + reflect.TypeFor[metric.Float64Observer](), )) } diff --git a/sdk/log/record.go b/sdk/log/record.go index 83d1812ef..66ed1adb3 100644 --- a/sdk/log/record.go +++ b/sdk/log/record.go @@ -495,15 +495,7 @@ func (r *Record) applyValueLimitsAndDedup(val log.Value) log.Value { sl := val.AsSlice() // First check if any limits need to be applied. - needsChange := false - for _, v := range sl { - if r.needsValueLimitsOrDedup(v) { - needsChange = true - break - } - } - - if needsChange { + if slices.ContainsFunc(sl, r.needsValueLimitsOrDedup) { // Create a new slice to avoid modifying the original. newSl := make([]log.Value, len(sl)) for i, item := range sl { @@ -564,10 +556,8 @@ func (r *Record) needsValueLimitsOrDedup(val log.Value) bool { case log.KindString: return r.attributeValueLengthLimit >= 0 && len(val.AsString()) > r.attributeValueLengthLimit case log.KindSlice: - for _, v := range val.AsSlice() { - if r.needsValueLimitsOrDedup(v) { - return true - } + if slices.ContainsFunc(val.AsSlice(), r.needsValueLimitsOrDedup) { + return true } case log.KindMap: kvs := val.AsMap() @@ -603,15 +593,7 @@ func (r *Record) dedupeBodyCollections(val log.Value) log.Value { sl := val.AsSlice() // Check if any nested values need deduplication. - needsChange := false - for _, item := range sl { - if r.needsBodyDedup(item) { - needsChange = true - break - } - } - - if needsChange { + if slices.ContainsFunc(sl, r.needsBodyDedup) { // Create a new slice to avoid modifying the original. newSl := make([]log.Value, len(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 { switch val.Kind() { case log.KindSlice: - for _, item := range val.AsSlice() { - if r.needsBodyDedup(item) { - return true - } + if slices.ContainsFunc(val.AsSlice(), r.needsBodyDedup) { + return true } case log.KindMap: kvs := val.AsMap() diff --git a/sdk/metric/manual_reader_test.go b/sdk/metric/manual_reader_test.go index 3680ddfa1..fa19d2944 100644 --- a/sdk/metric/manual_reader_test.go +++ b/sdk/metric/manual_reader_test.go @@ -328,7 +328,7 @@ func createMetricDataTestProducerForManual() testSDKProducer { // Create multiple scopes for comprehensive test data 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)) } diff --git a/sdk/metric/periodic_reader_test.go b/sdk/metric/periodic_reader_test.go index 781a324c6..946bf4fb7 100644 --- a/sdk/metric/periodic_reader_test.go +++ b/sdk/metric/periodic_reader_test.go @@ -773,7 +773,7 @@ func createMetricDataTestProducer() testSDKProducer { // Create multiple scopes for comprehensive test data 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)) } diff --git a/sdk/resource/host_id.go b/sdk/resource/host_id.go index f8addf040..023621ba7 100644 --- a/sdk/resource/host_id.go +++ b/sdk/resource/host_id.go @@ -60,8 +60,7 @@ func (r *hostIDReaderDarwin) read() (string, error) { return "", err } - lines := strings.Split(result, "\n") - for _, line := range lines { + for line := range strings.SplitSeq(result, "\n") { if strings.Contains(line, "IOPlatformUUID") { parts := strings.Split(line, " = ") if len(parts) == 2 { diff --git a/sdk/resource/os_release_unix_test.go b/sdk/resource/os_release_unix_test.go index a4c1c52c7..2a59143dd 100644 --- a/sdk/resource/os_release_unix_test.go +++ b/sdk/resource/os_release_unix_test.go @@ -123,8 +123,6 @@ PROP3='Final value'`) } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { result := resource.ParseOSReleaseFile(tc.OSRelease) require.Equal(t, tc.Parsed, result) @@ -152,8 +150,6 @@ func TestSkip(t *testing.T) { } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { result := resource.Skip(tc.Line) require.Equal(t, tc.Expected, result) @@ -179,8 +175,6 @@ func TestParse(t *testing.T) { } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { key, value, ok := resource.Parse(tc.Line) require.Equal(t, tc.ExpectedKey, key) @@ -219,8 +213,6 @@ func TestUnquote(t *testing.T) { } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { result := resource.Unquote(tc.Text) require.Equal(t, tc.Expected, result) @@ -243,8 +235,6 @@ func TestUnescape(t *testing.T) { } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { result := resource.Unescape(tc.Text) require.Equal(t, tc.Expected, result) @@ -285,8 +275,6 @@ func TestBuildOSRelease(t *testing.T) { } for _, tc := range tt { - tc := tc - t.Run(tc.Name, func(t *testing.T) { result := resource.BuildOSRelease(tc.Values) require.Equal(t, tc.Expected, result) diff --git a/semconv/v1.34.0/error_type_test.go b/semconv/v1.34.0/error_type_test.go index 744c29f77..bd37f6fe4 100644 --- a/semconv/v1.34.0/error_type_test.go +++ b/semconv/v1.34.0/error_type_test.go @@ -23,7 +23,7 @@ func TestErrorType(t *testing.T) { builtinErr := errors.New("something went wrong") var nilErr error - wantCustomType := reflect.TypeOf(customErr) + wantCustomType := reflect.TypeFor[CustomError]() wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name()) tests := []struct { diff --git a/semconv/v1.36.0/error_type_test.go b/semconv/v1.36.0/error_type_test.go index 71c039e74..06565b0f0 100644 --- a/semconv/v1.36.0/error_type_test.go +++ b/semconv/v1.36.0/error_type_test.go @@ -23,7 +23,7 @@ func TestErrorType(t *testing.T) { builtinErr := errors.New("something went wrong") var nilErr error - wantCustomType := reflect.TypeOf(customErr) + wantCustomType := reflect.TypeFor[CustomError]() wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name()) tests := []struct { diff --git a/trace/noop/noop_test.go b/trace/noop/noop_test.go index 0c1ba8d8c..99d4589b1 100644 --- a/trace/noop/noop_test.go +++ b/trace/noop/noop_test.go @@ -17,15 +17,15 @@ func TestImplementationNoPanics(t *testing.T) { // methods added to it than the No-Op implementation implements them. t.Run("TracerProvider", assertAllExportedMethodNoPanic( reflect.ValueOf(TracerProvider{}), - reflect.TypeOf((*trace.TracerProvider)(nil)).Elem(), + reflect.TypeFor[trace.TracerProvider](), )) t.Run("Tracer", assertAllExportedMethodNoPanic( reflect.ValueOf(Tracer{}), - reflect.TypeOf((*trace.Tracer)(nil)).Elem(), + reflect.TypeFor[trace.Tracer](), )) t.Run("Span", assertAllExportedMethodNoPanic( reflect.ValueOf(Span{}), - reflect.TypeOf((*trace.Span)(nil)).Elem(), + reflect.TypeFor[trace.Span](), )) }