Skip to content

Commit

Permalink
[chore] Upgrade golangci and fix lint issues (#36908)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Dec 20, 2024
1 parent c208ea2 commit 5af8c70
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 205 deletions.
8 changes: 2 additions & 6 deletions cmd/opampsupervisor/supervisor/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,8 @@ agent:
Telemetry: DefaultSupervisor().Telemetry,
}

require.NoError(t, os.Setenv("TEST_ENDPOINT", "ws://localhost/v1/opamp"))
require.NoError(t, os.Setenv("TEST_EXECUTABLE_PATH", executablePath))
defer func() {
require.NoError(t, os.Unsetenv("TEST_ENDPOINT"))
require.NoError(t, os.Unsetenv("TEST_EXECUTABLE_PATH"))
}()
t.Setenv("TEST_ENDPOINT", "ws://localhost/v1/opamp")
t.Setenv("TEST_EXECUTABLE_PATH", executablePath)

cfgPath := setupSupervisorConfigFile(t, tmpDir, config)
runSupervisorConfigLoadTest(t, cfgPath, expected, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ func newMetricLogFromRaw(
}
}

func min(l, r int) int {
if l < r {
return l
}
return r
}

func resourceToMetricLabels(labels *KeyValues, resource pcommon.Resource) {
attrs := resource.Attributes()
attrs.Range(func(k string, v pcommon.Value) bool {
Expand Down
7 changes: 0 additions & 7 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ func stringSlicesEqual(expected, actual []string) bool {
return true
}

func min(i, j int) int {
if i < j {
return i
}
return j
}

type dimensionality [][]string

func (d dimensionality) Len() int {
Expand Down
6 changes: 3 additions & 3 deletions exporter/googlecloudpubsubexporter/watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type collector struct {
func (c *collector) earliest(timestamp pcommon.Timestamp) bool {
t := timestamp.AsTime()
if t.Before(c.calculatedTime) {
min := c.processingTime.Add(-c.allowedDrift)
if t.Before(min) {
c.calculatedTime = min
minTime := c.processingTime.Add(-c.allowedDrift)
if t.Before(minTime) {
c.calculatedTime = minTime
return true
}
c.calculatedTime = t
Expand Down
6 changes: 3 additions & 3 deletions exporter/mezmoexporter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package mezmoexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/mezmoexporter"

// truncateString Truncates the given string to a maximum length provided by max.
func truncateString(s string, max int) string {
if len(s) < max {
func truncateString(s string, maxLen int) string {
if len(s) < maxLen {
return s
}

return s[:max]
return s[:maxLen]
}
6 changes: 3 additions & 3 deletions exporter/signalfxexporter/internal/apm/correlations/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ func (cc *Client) Correlate(cor *Correlation, cb CorrelateCB) {
cor.Logger(cc.log).WithFields(log.Fields{"method": http.MethodPut}).Info("Updated dimension")
}
case http.StatusTeapot:
max := &ErrMaxEntries{}
err = json.Unmarshal(body, max)
maxEntry := &ErrMaxEntries{}
err = json.Unmarshal(body, maxEntry)
if err == nil {
err = max
err = maxEntry
}
}
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions exporter/signalfxexporter/internal/apm/tracetracker/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func (t *TimeoutCache) IsFull() bool {
return false
}

func (t *TimeoutCache) SetMaxSize(max int64, now time.Time) {
func (t *TimeoutCache) SetMaxSize(maxSize int64, now time.Time) {
t.Lock()
defer t.Unlock()
t.maxSize = max
t.maxSize = maxSize
t.maxSizeExpiryTS = now.Add(time.Hour * 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (a *ActiveServiceTracker) processEnvironment(res pcommon.Resource, now time
a.hostEnvironmentCache.UpdateOrCreate(&CacheKey{value: environment}, now)
}
// nolint:errorlint
if max, ok := err.(*correlations.ErrMaxEntries); ok && max.MaxEntries > 0 {
a.hostEnvironmentCache.SetMaxSize(max.MaxEntries, now)
if maxEntry, ok := err.(*correlations.ErrMaxEntries); ok && maxEntry.MaxEntries > 0 {
a.hostEnvironmentCache.SetMaxSize(maxEntry.MaxEntries, now)
}
})
}
Expand Down Expand Up @@ -226,8 +226,8 @@ func (a *ActiveServiceTracker) processService(res pcommon.Resource, now time.Tim
a.hostServiceCache.UpdateOrCreate(&CacheKey{value: service}, now)
}
// nolint:errorlint
if max, ok := err.(*correlations.ErrMaxEntries); ok && max.MaxEntries > 0 {
a.hostServiceCache.SetMaxSize(max.MaxEntries, now)
if maxEntry, ok := err.(*correlations.ErrMaxEntries); ok && maxEntry.MaxEntries > 0 {
a.hostServiceCache.SetMaxSize(maxEntry.MaxEntries, now)
}
})
}
Expand Down
13 changes: 2 additions & 11 deletions extension/observer/ecsobserver/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (f *taskFetcher) attachContainerInstance(ctx context.Context, tasks []*task

// DescribeContainerInstance size limit is 100, do it in batch.
for i := 0; i < len(instanceList); i += describeContainerInstanceLimit {
end := minInt(i+describeContainerInstanceLimit, len(instanceList))
end := min(i+describeContainerInstanceLimit, len(instanceList))
if err := f.describeContainerInstances(ctx, instanceList[i:end], ciToEC2); err != nil {
return fmt.Errorf("describe container instanced failed offset=%d: %w", i, err)
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func (f *taskFetcher) getAllServices(ctx context.Context) ([]*ecs.Service, error
// DescribeServices size limit is 10 so we need to do paging on client side.
var services []*ecs.Service
for i := 0; i < len(servicesToDescribe); i += describeServiceLimit {
end := minInt(i+describeServiceLimit, len(servicesToDescribe))
end := min(i+describeServiceLimit, len(servicesToDescribe))
desc := &ecs.DescribeServicesInput{
Cluster: cluster,
Services: servicesToDescribe[i:end],
Expand Down Expand Up @@ -424,12 +424,3 @@ func sortStringPointers(ps []*string) {
ps[i] = aws.String(ss[i])
}
}

func minInt(a, b int) int {
if a < b {
return a
}
return b
}

// Util End
11 changes: 1 addition & 10 deletions extension/observer/ecsobserver/internal/ecsmock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func getPage(p pageInput) (*pageOutput, error) {
return nil, fmt.Errorf("invalid next token %q: %w", token, err)
}
}
end := minInt(p.size, start+p.limit)
end := min(p.size, start+p.limit)
var newNextToken *string
if end < p.size {
newNextToken = aws.String(strconv.Itoa(end))
Expand All @@ -462,12 +462,3 @@ func getArns(items any, arnGetter func(i int) *string) []*string {
}
return arns
}

// 'generic' End

func minInt(a, b int) int {
if a < b {
return a
}
return b
}
30 changes: 17 additions & 13 deletions extension/solarwindsapmsettingsextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package solarwindsapmsettingsextension

import (
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -275,21 +274,26 @@ func TestLoadConfig(t *testing.T) {
}
}

func TestResolveServiceNameBestEffort(t *testing.T) {
func TestResolveServiceNameBestEffortNoEnv(t *testing.T) {
// Without any environment variables
require.Empty(t, resolveServiceNameBestEffort())
// With OTEL_SERVICE_NAME only
require.NoError(t, os.Setenv("OTEL_SERVICE_NAME", "otel_ser1"))
}

// With OTEL_SERVICE_NAME only
func TestResolveServiceNameBestEffortOnlyOtelService(t *testing.T) {
t.Setenv("OTEL_SERVICE_NAME", "otel_ser1")
require.Equal(t, "otel_ser1", resolveServiceNameBestEffort())
require.NoError(t, os.Unsetenv("OTEL_SERVICE_NAME"))
// With AWS_LAMBDA_FUNCTION_NAME only
require.NoError(t, os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "lambda"))
}

// With AWS_LAMBDA_FUNCTION_NAME only
func TestResolveServiceNameBestEffortOnlyAwsLambda(t *testing.T) {
t.Setenv("AWS_LAMBDA_FUNCTION_NAME", "lambda")
require.Equal(t, "lambda", resolveServiceNameBestEffort())
require.NoError(t, os.Unsetenv("AWS_LAMBDA_FUNCTION_NAME"))
// With both
require.NoError(t, os.Setenv("OTEL_SERVICE_NAME", "otel_ser1"))
require.NoError(t, os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "lambda"))
}

// With both
func TestResolveServiceNameBestEffortBoth(t *testing.T) {
t.Setenv("OTEL_SERVICE_NAME", "otel_ser1")
t.Setenv("AWS_LAMBDA_FUNCTION_NAME", "lambda")
require.Equal(t, "otel_ser1", resolveServiceNameBestEffort())
require.NoError(t, os.Unsetenv("AWS_LAMBDA_FUNCTION_NAME"))
require.NoError(t, os.Unsetenv("OTEL_SERVICE_NAME"))
}
14 changes: 7 additions & 7 deletions internal/aws/cwlogs/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,29 @@ func TestValidateLogEventFailed(t *testing.T) {

// eventBatch Tests
func TestLogEventBatch_timestampWithin24Hours(t *testing.T) {
min := time.Date(2017, time.June, 20, 23, 38, 0, 0, time.Local)
max := min.Add(23 * time.Hour)
minDate := time.Date(2017, time.June, 20, 23, 38, 0, 0, time.Local)
maxDate := minDate.Add(23 * time.Hour)
logEventBatch := &eventBatch{
maxTimestampMs: max.UnixNano() / 1e6,
minTimestampMs: min.UnixNano() / 1e6,
maxTimestampMs: maxDate.UnixNano() / 1e6,
minTimestampMs: minDate.UnixNano() / 1e6,
}

// less than the min
target := min.Add(-1 * time.Hour)
target := minDate.Add(-1 * time.Hour)
assert.True(t, logEventBatch.isActive(aws.Int64(target.UnixNano()/1e6)))

target = target.Add(-1 * time.Millisecond)
assert.False(t, logEventBatch.isActive(aws.Int64(target.UnixNano()/1e6)))

// more than the max
target = max.Add(1 * time.Hour)
target = maxDate.Add(1 * time.Hour)
assert.True(t, logEventBatch.isActive(aws.Int64(target.UnixNano()/1e6)))

target = target.Add(1 * time.Millisecond)
assert.False(t, logEventBatch.isActive(aws.Int64(target.UnixNano()/1e6)))

// in between min and max
target = min.Add(2 * time.Hour)
target = minDate.Add(2 * time.Hour)
assert.True(t, logEventBatch.isActive(aws.Int64(target.UnixNano()/1e6)))
}

Expand Down
8 changes: 4 additions & 4 deletions internal/exp/metrics/staleness/staleness.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ type Staleness[T any] struct {
pq PriorityQueue
}

func NewStaleness[T any](max time.Duration, items streams.Map[T]) *Staleness[T] {
func NewStaleness[T any](maxDuration time.Duration, items streams.Map[T]) *Staleness[T] {
return &Staleness[T]{
Max: max,
Max: maxDuration,

items: items,
pq: NewPriorityQueue(),
Expand Down Expand Up @@ -117,13 +117,13 @@ func (stale Tracker) Refresh(ts time.Time, ids ...identity.Stream) {
}
}

func (stale Tracker) Collect(max time.Duration) []identity.Stream {
func (stale Tracker) Collect(maxDuration time.Duration) []identity.Stream {
now := NowFunc()

var ids []identity.Stream
for stale.pq.Len() > 0 {
_, ts := stale.pq.Peek()
if now.Sub(ts) < max {
if now.Sub(ts) < maxDuration {
break
}
id, _ := stale.pq.Pop()
Expand Down
3 changes: 1 addition & 2 deletions internal/exp/metrics/staleness/staleness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
)

func TestStaleness(t *testing.T) {
max := 1 * time.Second
stalenessMap := NewStaleness[int](
max,
1*time.Second,
make(streams.HashMap[int]),
)

Expand Down
Loading

0 comments on commit 5af8c70

Please sign in to comment.