Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(linters): Enable import-alias-naming and redundant-import-alias rules for revive #15836

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ linters-settings:
- name: get-return
- name: identical-branches
- name: if-return
- name: import-alias-naming
arguments:
- "^[a-z][a-z0-9_]*[a-z0-9]+$"
- name: import-shadowing
- name: increment-decrement
- name: indent-error-flow
Expand All @@ -281,6 +284,7 @@ linters-settings:
- name: range-val-in-closure
- name: receiver-naming
- name: redefines-builtin-id
- name: redundant-import-alias
- name: string-of-int
- name: struct-tag
- name: superfluous-else
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/influxdata/telegraf/plugins/processors"
"github.com/influxdata/telegraf/plugins/serializers"
_ "github.com/influxdata/telegraf/plugins/serializers/all" // Blank import to have all serializers for testing
promserializer "github.com/influxdata/telegraf/plugins/serializers/prometheus"
serializers_prometheus "github.com/influxdata/telegraf/plugins/serializers/prometheus"
"github.com/influxdata/telegraf/testutil"
)

Expand Down Expand Up @@ -687,7 +687,7 @@ func TestConfig_SerializerInterfaceNewFormat(t *testing.T) {
// Ignore all unexported fields and fields not relevant for functionality
options := []cmp.Option{
cmpopts.IgnoreUnexported(stype),
cmpopts.IgnoreUnexported(reflect.Indirect(reflect.ValueOf(promserializer.MetricTypes{})).Interface()),
cmpopts.IgnoreUnexported(reflect.Indirect(reflect.ValueOf(serializers_prometheus.MetricTypes{})).Interface()),
cmpopts.IgnoreTypes(sync.Mutex{}, regexp.Regexp{}),
cmpopts.IgnoreInterfaces(struct{ telegraf.Logger }{}),
}
Expand Down Expand Up @@ -779,7 +779,7 @@ func TestConfig_SerializerInterfaceOldFormat(t *testing.T) {
// Ignore all unexported fields and fields not relevant for functionality
options := []cmp.Option{
cmpopts.IgnoreUnexported(stype),
cmpopts.IgnoreUnexported(reflect.Indirect(reflect.ValueOf(promserializer.MetricTypes{})).Interface()),
cmpopts.IgnoreUnexported(reflect.Indirect(reflect.ValueOf(serializers_prometheus.MetricTypes{})).Interface()),
cmpopts.IgnoreTypes(sync.Mutex{}, regexp.Regexp{}),
cmpopts.IgnoreInterfaces(struct{ telegraf.Logger }{}),
}
Expand Down
4 changes: 2 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"compress/gzip"
"context"
cryptoRand "crypto/rand"
crypto_rand "crypto/rand"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -94,7 +94,7 @@ func ReadLines(filename string) ([]string, error) {
// RandomString returns a random string of alphanumeric characters
func RandomString(n int) (string, error) {
var bytes = make([]byte, n)
_, err := cryptoRand.Read(bytes)
_, err := crypto_rand.Read(bytes)
if err != nil {
return "", err
}
Expand Down
26 changes: 13 additions & 13 deletions plugins/aggregators/histogram/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/influxdata/telegraf"
telegrafConfig "github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/aggregators"
)

Expand All @@ -29,18 +29,18 @@ const bucketNegInf = "-Inf"

// HistogramAggregator is aggregator with histogram configs and particular histograms for defined metrics
type HistogramAggregator struct {
Configs []config `toml:"config"`
ResetBuckets bool `toml:"reset"`
Cumulative bool `toml:"cumulative"`
ExpirationInterval telegrafConfig.Duration `toml:"expiration_interval"`
PushOnlyOnUpdate bool `toml:"push_only_on_update"`
Configs []bucketConfig `toml:"config"`
ResetBuckets bool `toml:"reset"`
Cumulative bool `toml:"cumulative"`
ExpirationInterval config.Duration `toml:"expiration_interval"`
PushOnlyOnUpdate bool `toml:"push_only_on_update"`

buckets bucketsByMetrics
cache map[uint64]metricHistogramCollection
}

// config is the config, which contains name, field of metric and histogram buckets.
type config struct {
// bucketConfig is the config, which contains name, field of metric and histogram buckets.
type bucketConfig struct {
Metric string `toml:"measurement_name"`
Fields []string `toml:"fields"`
Buckets buckets `toml:"buckets"`
Expand Down Expand Up @@ -239,25 +239,25 @@ func (h *HistogramAggregator) getBuckets(metric string, field string) []float64
return buckets
}

for _, config := range h.Configs {
if config.Metric == metric {
if !isBucketExists(field, config) {
for _, cfg := range h.Configs {
if cfg.Metric == metric {
if !isBucketExists(field, cfg) {
continue
}

if _, ok := h.buckets[metric]; !ok {
h.buckets[metric] = make(bucketsByFields)
}

h.buckets[metric][field] = sortBuckets(config.Buckets)
h.buckets[metric][field] = sortBuckets(cfg.Buckets)
}
}

return h.buckets[metric][field]
}

// isBucketExists checks if buckets exists for the passed field
func isBucketExists(field string, cfg config) bool {
func isBucketExists(field string, cfg bucketConfig) bool {
if len(cfg.Fields) == 0 {
return true
}
Expand Down
40 changes: 20 additions & 20 deletions plugins/aggregators/histogram/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
telegrafConfig "github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
)
Expand All @@ -17,16 +17,16 @@ type fields map[string]interface{}
type tags map[string]string

// NewTestHistogram creates new test histogram aggregation with specified config
func NewTestHistogram(cfg []config, reset bool, cumulative bool, pushOnlyOnUpdate bool) telegraf.Aggregator {
func NewTestHistogram(cfg []bucketConfig, reset bool, cumulative bool, pushOnlyOnUpdate bool) telegraf.Aggregator {
return NewTestHistogramWithExpirationInterval(cfg, reset, cumulative, pushOnlyOnUpdate, 0)
}

func NewTestHistogramWithExpirationInterval(
cfg []config,
cfg []bucketConfig,
reset bool,
cumulative bool,
pushOnlyOnUpdate bool,
expirationInterval telegrafConfig.Duration,
expirationInterval config.Duration,
) telegraf.Aggregator {
htm := NewHistogramAggregator()
htm.Configs = cfg
Expand Down Expand Up @@ -85,8 +85,8 @@ func BenchmarkApply(b *testing.B) {

// TestHistogram tests metrics for one period and for one field
func TestHistogram(t *testing.T) {
var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, false, true, false)

acc := &testutil.Accumulator{}
Expand All @@ -107,8 +107,8 @@ func TestHistogram(t *testing.T) {

// TestHistogram tests metrics for one period, for one field and push only on histogram update
func TestHistogramPushOnUpdate(t *testing.T) {
var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, false, true, true)

acc := &testutil.Accumulator{}
Expand Down Expand Up @@ -143,8 +143,8 @@ func TestHistogramPushOnUpdate(t *testing.T) {

// TestHistogramNonCumulative tests metrics for one period and for one field
func TestHistogramNonCumulative(t *testing.T) {
var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, false, false, false)

acc := &testutil.Accumulator{}
Expand All @@ -165,8 +165,8 @@ func TestHistogramNonCumulative(t *testing.T) {

// TestHistogramWithReset tests metrics for one period and for one field, with reset between metrics adding
func TestHistogramWithReset(t *testing.T) {
var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, true, true, false)

acc := &testutil.Accumulator{}
Expand All @@ -187,7 +187,7 @@ func TestHistogramWithReset(t *testing.T) {

// TestHistogramWithAllFields tests two metrics for one period and for all fields
func TestHistogramWithAllFields(t *testing.T) {
cfg := []config{
cfg := []bucketConfig{
{Metric: "first_metric_name", Buckets: []float64{0.0, 15.5, 20.0, 30.0, 40.0}},
{Metric: "second_metric_name", Buckets: []float64{0.0, 4.0, 10.0, 23.0, 30.0}},
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestHistogramWithAllFields(t *testing.T) {

// TestHistogramWithAllFieldsNonCumulative tests two metrics for one period and for all fields
func TestHistogramWithAllFieldsNonCumulative(t *testing.T) {
cfg := []config{
cfg := []bucketConfig{
{Metric: "first_metric_name", Buckets: []float64{0.0, 15.5, 20.0, 30.0, 40.0}},
{Metric: "second_metric_name", Buckets: []float64{0.0, 4.0, 10.0, 23.0, 30.0}},
}
Expand Down Expand Up @@ -370,8 +370,8 @@ func TestHistogramWithAllFieldsNonCumulative(t *testing.T) {
// TestHistogramWithTwoPeriodsAndAllFields tests two metrics getting added with a push/reset in between (simulates
// getting added in different periods) for all fields
func TestHistogramWithTwoPeriodsAndAllFields(t *testing.T) {
var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, false, true, false)

acc := &testutil.Accumulator{}
Expand Down Expand Up @@ -415,8 +415,8 @@ func TestWrongBucketsOrder(t *testing.T) {
}
}()

var cfg []config
cfg = append(cfg, config{Metric: "first_metric_name", Buckets: []float64{0.0, 90.0, 20.0, 30.0, 40.0}})
var cfg []bucketConfig
cfg = append(cfg, bucketConfig{Metric: "first_metric_name", Buckets: []float64{0.0, 90.0, 20.0, 30.0, 40.0}})
histogram := NewTestHistogram(cfg, false, true, false)
histogram.Add(firstMetric2)
}
Expand All @@ -431,11 +431,11 @@ func TestHistogramMetricExpiration(t *testing.T) {
timeNow = time.Now
}()

cfg := []config{
cfg := []bucketConfig{
{Metric: "first_metric_name", Fields: []string{"a"}, Buckets: []float64{0.0, 10.0, 20.0, 30.0, 40.0}},
{Metric: "second_metric_name", Buckets: []float64{0.0, 4.0, 10.0, 23.0, 30.0}},
}
histogram := NewTestHistogramWithExpirationInterval(cfg, false, true, false, telegrafConfig.Duration(30))
histogram := NewTestHistogramWithExpirationInterval(cfg, false, true, false, config.Duration(30))

acc := &testutil.Accumulator{}

Expand Down
3 changes: 2 additions & 1 deletion plugins/common/auth/basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package auth

import (
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestBasicAuth_VerifyWithCredentials(t *testing.T) {
Expand Down
42 changes: 21 additions & 21 deletions plugins/common/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package aws
import (
"context"

awsV2 "github.com/aws/aws-sdk-go-v2/aws"
configV2 "github.com/aws/aws-sdk-go-v2/config"
credentialsV2 "github.com/aws/aws-sdk-go-v2/credentials"
stscredsV2 "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
)

Expand All @@ -24,61 +24,61 @@ type CredentialConfig struct {
WebIdentityTokenFile string `toml:"web_identity_token_file"`
}

func (c *CredentialConfig) Credentials() (awsV2.Config, error) {
func (c *CredentialConfig) Credentials() (aws.Config, error) {
if c.RoleARN != "" {
return c.configWithAssumeCredentials()
}
return c.configWithRootCredentials()
}

func (c *CredentialConfig) configWithRootCredentials() (awsV2.Config, error) {
options := []func(*configV2.LoadOptions) error{
configV2.WithRegion(c.Region),
func (c *CredentialConfig) configWithRootCredentials() (aws.Config, error) {
options := []func(*config.LoadOptions) error{
config.WithRegion(c.Region),
}

if c.Profile != "" {
options = append(options, configV2.WithSharedConfigProfile(c.Profile))
options = append(options, config.WithSharedConfigProfile(c.Profile))
}
if c.Filename != "" {
options = append(options, configV2.WithSharedCredentialsFiles([]string{c.Filename}))
options = append(options, config.WithSharedCredentialsFiles([]string{c.Filename}))
}

if c.AccessKey != "" || c.SecretKey != "" {
provider := credentialsV2.NewStaticCredentialsProvider(c.AccessKey, c.SecretKey, c.Token)
options = append(options, configV2.WithCredentialsProvider(provider))
provider := credentials.NewStaticCredentialsProvider(c.AccessKey, c.SecretKey, c.Token)
options = append(options, config.WithCredentialsProvider(provider))
}

return configV2.LoadDefaultConfig(context.Background(), options...)
return config.LoadDefaultConfig(context.Background(), options...)
}

func (c *CredentialConfig) configWithAssumeCredentials() (awsV2.Config, error) {
func (c *CredentialConfig) configWithAssumeCredentials() (aws.Config, error) {
// To generate credentials using assumeRole, we need to create AWS STS client with the default AWS endpoint,
defaultConfig, err := c.configWithRootCredentials()
if err != nil {
return awsV2.Config{}, err
return aws.Config{}, err
}

var provider awsV2.CredentialsProvider
var provider aws.CredentialsProvider
stsService := sts.NewFromConfig(defaultConfig)
if c.WebIdentityTokenFile != "" {
provider = stscredsV2.NewWebIdentityRoleProvider(
provider = stscreds.NewWebIdentityRoleProvider(
stsService,
c.RoleARN,
stscredsV2.IdentityTokenFile(c.WebIdentityTokenFile),
func(opts *stscredsV2.WebIdentityRoleOptions) {
stscreds.IdentityTokenFile(c.WebIdentityTokenFile),
func(opts *stscreds.WebIdentityRoleOptions) {
if c.RoleSessionName != "" {
opts.RoleSessionName = c.RoleSessionName
}
},
)
} else {
provider = stscredsV2.NewAssumeRoleProvider(stsService, c.RoleARN, func(opts *stscredsV2.AssumeRoleOptions) {
provider = stscreds.NewAssumeRoleProvider(stsService, c.RoleARN, func(opts *stscreds.AssumeRoleOptions) {
if c.RoleSessionName != "" {
opts.RoleSessionName = c.RoleSessionName
}
})
}

defaultConfig.Credentials = awsV2.NewCredentialsCache(provider)
defaultConfig.Credentials = aws.NewCredentialsCache(provider)
return defaultConfig, nil
}
4 changes: 2 additions & 2 deletions plugins/common/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/cookie"
oauthConfig "github.com/influxdata/telegraf/plugins/common/oauth"
"github.com/influxdata/telegraf/plugins/common/oauth"
"github.com/influxdata/telegraf/plugins/common/proxy"
"github.com/influxdata/telegraf/plugins/common/tls"
)
Expand All @@ -27,7 +27,7 @@ type HTTPClientConfig struct {

proxy.HTTPProxy
tls.ClientConfig
oauthConfig.OAuth2Config
oauth.OAuth2Config
cookie.CookieAuthConfig
}

Expand Down
Loading
Loading