From f3be3ece79a3a57ceca81ef625615163aac132f8 Mon Sep 17 00:00:00 2001 From: Ashvitha Sridharan Date: Wed, 26 Jul 2023 16:32:53 -0400 Subject: [PATCH] Remove named imports, use errors.New if not fformatting --- agent/hcp/telemetry_provider.go | 27 +++++++++++++------------ agent/hcp/telemetry_provider_test.go | 30 ++++++++++++++-------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/agent/hcp/telemetry_provider.go b/agent/hcp/telemetry_provider.go index 931c851f6205..0a4e34309199 100644 --- a/agent/hcp/telemetry_provider.go +++ b/agent/hcp/telemetry_provider.go @@ -2,18 +2,19 @@ package hcp import ( "context" + "errors" "fmt" "net/url" "regexp" "sync" "time" - goMetrics "github.com/armon/go-metrics" + "github.com/armon/go-metrics" "github.com/hashicorp/go-hclog" "github.com/mitchellh/hashstructure/v2" - hcpclient "github.com/hashicorp/consul/agent/hcp/client" - hcpTelemetry "github.com/hashicorp/consul/agent/hcp/telemetry" + "github.com/hashicorp/consul/agent/hcp/client" + "github.com/hashicorp/consul/agent/hcp/telemetry" ) var ( @@ -22,8 +23,8 @@ var ( ) // Ensure hcpProviderImpl implements telemetry provider interfaces. -var _ hcpTelemetry.ConfigProvider = &hcpProviderImpl{} -var _ hcpTelemetry.EndpointProvider = &hcpProviderImpl{} +var _ telemetry.ConfigProvider = &hcpProviderImpl{} +var _ telemetry.EndpointProvider = &hcpProviderImpl{} // hcpProviderImpl holds telemetry configuration and settings for continuous fetch of new config from HCP. // it updates configuration, if changes are detected. @@ -38,7 +39,7 @@ type hcpProviderImpl struct { // Meanwhile, config is only updated when there are changes (write). rw sync.RWMutex // hcpClient is an authenticated client used to make HTTP requests to HCP. - hcpClient hcpclient.Client + hcpClient client.Client } // dynamicConfig is a set of configurable settings for metrics collection, processing and export. @@ -52,23 +53,23 @@ type dynamicConfig struct { // providerParams is used to initialize a hcpProviderImpl. type providerParams struct { - metricsConfig *hcpclient.MetricsConfig + metricsConfig *client.MetricsConfig refreshInterval time.Duration - hcpClient hcpclient.Client + hcpClient client.Client } // NewHCPProviderImpl initializes and starts a HCP Telemetry provider with provided params. func NewHCPProviderImpl(ctx context.Context, params *providerParams) (*hcpProviderImpl, error) { if params.hcpClient == nil { - return nil, fmt.Errorf("missing HCP client") + return nil, errors.New("missing HCP client") } if params.metricsConfig == nil { - return nil, fmt.Errorf("missing metrics config") + return nil, errors.New("missing metrics config") } if params.refreshInterval <= 0 { - return nil, fmt.Errorf("invalid refresh interval") + return nil, fmt.Errorf("invalid refresh interval: %d", params.refreshInterval) } cfg := &dynamicConfig{ @@ -125,7 +126,7 @@ func (t *hcpProviderImpl) checkUpdate(ctx context.Context) (*dynamicConfig, bool telemetryCfg, err := t.hcpClient.FetchTelemetryConfig(ctx) if err != nil { logger.Error("failed to fetch telemetry config from HCP", "error", err) - goMetrics.IncrCounter(internalMetricRefreshFailure, 1) + metrics.IncrCounter(internalMetricRefreshFailure, 1) return nil, false } @@ -139,7 +140,7 @@ func (t *hcpProviderImpl) checkUpdate(ctx context.Context) (*dynamicConfig, bool newHash, err := calculateHash(newDynamicConfig) if err != nil { logger.Error("failed to calculate hash for new config", "error", err) - goMetrics.IncrCounter(internalMetricRefreshFailure, 1) + metrics.IncrCounter(internalMetricRefreshFailure, 1) return nil, false } diff --git a/agent/hcp/telemetry_provider_test.go b/agent/hcp/telemetry_provider_test.go index aba29d52e700..56153aa885fb 100644 --- a/agent/hcp/telemetry_provider_test.go +++ b/agent/hcp/telemetry_provider_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - hcpclient "github.com/hashicorp/consul/agent/hcp/client" + "github.com/hashicorp/consul/agent/hcp/client" ) const defaultTestRefreshInterval = 100 * time.Millisecond @@ -33,27 +33,27 @@ func TestNewTelemetryConfigProvider(t *testing.T) { }{ "success": { opts: &providerParams{ - hcpClient: hcpclient.NewMockClient(t), - metricsConfig: &hcpclient.MetricsConfig{}, + hcpClient: client.NewMockClient(t), + metricsConfig: &client.MetricsConfig{}, refreshInterval: 1 * time.Second, }, }, "failsWithMissingHCPClient": { opts: &providerParams{ - metricsConfig: &hcpclient.MetricsConfig{}, + metricsConfig: &client.MetricsConfig{}, }, wantErr: "missing HCP client", }, "failsWithMissingMetricsConfig": { opts: &providerParams{ - hcpClient: hcpclient.NewMockClient(t), + hcpClient: client.NewMockClient(t), }, wantErr: "missing metrics config", }, "failsWithInvalidRefreshInterval": { opts: &providerParams{ - hcpClient: hcpclient.NewMockClient(t), - metricsConfig: &hcpclient.MetricsConfig{}, + hcpClient: client.NewMockClient(t), + metricsConfig: &client.MetricsConfig{}, refreshInterval: 0 * time.Second, }, wantErr: "invalid refresh interval", @@ -116,7 +116,7 @@ func TestTelemetryConfigProvider_Success(t *testing.T) { } { t.Run(name, func(t *testing.T) { // Setup client mock to return the expected config. - mockClient := hcpclient.NewMockClient(t) + mockClient := client.NewMockClient(t) mockCfg, err := telemetryConfig(tc.expected) require.NoError(t, err) @@ -152,7 +152,7 @@ func TestTelemetryConfigProvider_Success(t *testing.T) { func TestTelemetryConfigProvider_UpdateFailuresWithMetrics(t *testing.T) { for name, tc := range map[string]struct { expected *testConfig - expect func(*hcpclient.MockClient) + expect func(*client.MockClient) }{ "failsWithHCPClientFailure": { expected: &testConfig{ @@ -162,7 +162,7 @@ func TestTelemetryConfigProvider_UpdateFailuresWithMetrics(t *testing.T) { }, endpoint: "http://test.com/v1/metrics", }, - expect: func(m *hcpclient.MockClient) { + expect: func(m *client.MockClient) { m.EXPECT().FetchTelemetryConfig(mock.Anything).Return(nil, fmt.Errorf("failure")) }, }, @@ -182,7 +182,7 @@ func TestTelemetryConfigProvider_UpdateFailuresWithMetrics(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - mockClient := hcpclient.NewMockClient(t) + mockClient := client.NewMockClient(t) tc.expect(mockClient) opts := &providerParams{ @@ -221,7 +221,7 @@ func TestTelemetryConfigProvider_UpdateFailuresWithMetrics(t *testing.T) { // TODO: Add race test. -func telemetryConfig(testCfg *testConfig) (*hcpclient.TelemetryConfig, error) { +func telemetryConfig(testCfg *testConfig) (*client.TelemetryConfig, error) { filters, err := regexp.Compile(testCfg.filters) if err != nil { return nil, err @@ -231,13 +231,13 @@ func telemetryConfig(testCfg *testConfig) (*hcpclient.TelemetryConfig, error) { if err != nil { return nil, err } - return &hcpclient.TelemetryConfig{ - MetricsConfig: &hcpclient.MetricsConfig{ + return &client.TelemetryConfig{ + MetricsConfig: &client.MetricsConfig{ Endpoint: endpoint, Filters: filters, Labels: testCfg.labels, }, - RefreshConfig: &hcpclient.RefreshConfig{ + RefreshConfig: &client.RefreshConfig{ RefreshInterval: defaultTestRefreshInterval, }, }, nil