Skip to content

Commit

Permalink
Remove named imports, use errors.New if not fformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Jul 31, 2023
1 parent a58baa7 commit f3be3ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
27 changes: 14 additions & 13 deletions agent/hcp/telemetry_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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{
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
30 changes: 15 additions & 15 deletions agent/hcp/telemetry_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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"))
},
},
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f3be3ec

Please sign in to comment.