Skip to content

Commit e64a6a2

Browse files
committed
suggestions from code review
1 parent 74f530b commit e64a6a2

File tree

7 files changed

+102
-145
lines changed

7 files changed

+102
-145
lines changed

extension/datadogfleetautomationextension/config.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package datadogfleetautomationextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/datadogfleetautomationextension"
55

66
import (
7-
"errors"
87
"fmt"
98
"strings"
109
"time"
@@ -17,24 +16,22 @@ import (
1716

1817
var (
1918
// ErrUnsetAPIKey is returned when the API key is not set.
20-
ErrUnsetAPIKey = datadogconfig.ErrUnsetAPIKey
19+
errUnsetAPIKey = datadogconfig.ErrUnsetAPIKey
2120
// ErrEmptyEndpoint is returned when endpoint is empty
22-
ErrEmptyEndpoint = datadogconfig.ErrEmptyEndpoint
21+
errEmptyEndpoint = datadogconfig.ErrEmptyEndpoint
2322
// ErrAPIKeyFormat is returned if API key contains invalid characters
24-
ErrAPIKeyFormat = datadogconfig.ErrAPIKeyFormat
23+
errAPIKeyFormat = datadogconfig.ErrAPIKeyFormat
2524
// NonHexRegex is a regex of characters that are always invalid in a Datadog API Key
26-
NonHexRegex = datadogconfig.NonHexRegex
25+
nonHexRegex = datadogconfig.NonHexRegex
2726
)
2827

2928
var _ component.Config = (*Config)(nil)
3029

3130
const (
32-
// DefaultSite is the default site for the Datadog API.
33-
DefaultSite = datadogconfig.DefaultSite
34-
// NonHexChars is a regex of characters that are always invalid in a Datadog API key.
35-
NonHexChars = datadogconfig.NonHexChars
36-
// DefaultReporterPeriod is the default amount of time between sending fleet automation payloads to Datadog.
37-
DefaultReporterPeriod = 20 * time.Minute
31+
// defaultSite is the default site for the Datadog API.
32+
defaultSite = datadogconfig.DefaultSite
33+
// defaultReporterPeriod is the default amount of time between sending fleet automation payloads to Datadog.
34+
defaultReporterPeriod = 20 * time.Minute
3835
)
3936

4037
// Config contains the information necessary for enabling the Datadog Fleet
@@ -44,8 +41,6 @@ type Config struct {
4441
API APIConfig `mapstructure:"api"`
4542
// If Hostname is empty extension will use available system APIs and cloud provider endpoints.
4643
Hostname string `mapstructure:"hostname"`
47-
// ReporterPeriod sets the amount of time between sending fleet automation payloads to Datadog.
48-
ReporterPeriod time.Duration `mapstructure:"reporter_period"`
4944
}
5045

5146
// APIConfig contains the information necessary for configuring the Datadog API.
@@ -54,17 +49,14 @@ type APIConfig = datadogconfig.APIConfig
5449
// Validate ensures that the configuration is valid.
5550
func (c *Config) Validate() error {
5651
if c.API.Site == "" {
57-
return ErrEmptyEndpoint
52+
return errEmptyEndpoint
5853
}
5954
if c.API.Key == "" {
60-
return ErrUnsetAPIKey
55+
return errUnsetAPIKey
6156
}
62-
invalidAPIKeyChars := NonHexRegex.FindAllString(string(c.API.Key), -1)
57+
invalidAPIKeyChars := nonHexRegex.FindAllString(string(c.API.Key), -1)
6358
if len(invalidAPIKeyChars) > 0 {
64-
return fmt.Errorf("%w: invalid characters: %s", ErrAPIKeyFormat, strings.Join(invalidAPIKeyChars, ", "))
65-
}
66-
if c.ReporterPeriod < 5*time.Minute {
67-
return errors.New("reporter_period must be 5 minutes or higher")
59+
return fmt.Errorf("%w: invalid characters: %s", errAPIKeyFormat, strings.Join(invalidAPIKeyChars, ", "))
6860
}
6961
return nil
7062
}

extension/datadogfleetautomationextension/config_test.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package datadogfleetautomationextension // import "github.com/open-telemetry/ope
66
import (
77
"fmt"
88
"testing"
9-
"time"
109

1110
"github.com/stretchr/testify/assert"
1211
)
@@ -21,10 +20,9 @@ func TestConfig_Validate(t *testing.T) {
2120
name: "Valid configuration",
2221
config: Config{
2322
API: APIConfig{
24-
Site: DefaultSite,
23+
Site: defaultSite,
2524
Key: "1234567890abcdef1234567890abcdef",
2625
},
27-
ReporterPeriod: DefaultReporterPeriod,
2826
},
2927
wantErr: nil,
3028
},
@@ -35,42 +33,28 @@ func TestConfig_Validate(t *testing.T) {
3533
Site: "",
3634
Key: "1234567890abcdef1234567890abcdef",
3735
},
38-
ReporterPeriod: DefaultReporterPeriod,
3936
},
40-
wantErr: ErrEmptyEndpoint,
37+
wantErr: errEmptyEndpoint,
4138
},
4239
{
4340
name: "Unset API key",
4441
config: Config{
4542
API: APIConfig{
46-
Site: DefaultSite,
43+
Site: defaultSite,
4744
Key: "",
4845
},
49-
ReporterPeriod: DefaultReporterPeriod,
5046
},
51-
wantErr: ErrUnsetAPIKey,
47+
wantErr: errUnsetAPIKey,
5248
},
5349
{
5450
name: "Invalid API key characters",
5551
config: Config{
5652
API: APIConfig{
57-
Site: DefaultSite,
53+
Site: defaultSite,
5854
Key: "1234567890abcdef1234567890abcdeg",
5955
},
60-
ReporterPeriod: DefaultReporterPeriod,
6156
},
62-
wantErr: fmt.Errorf("%w: invalid characters: %s", ErrAPIKeyFormat, "g"),
63-
},
64-
{
65-
name: "too small reporter period",
66-
config: Config{
67-
API: APIConfig{
68-
Site: DefaultSite,
69-
Key: "1234567890abcdef1234567890abcdef",
70-
},
71-
ReporterPeriod: 2 * time.Minute,
72-
},
73-
wantErr: fmt.Errorf("reporter_period must be 5 minutes or higher"),
57+
wantErr: fmt.Errorf("%w: invalid characters: %s", errAPIKeyFormat, "g"),
7458
},
7559
}
7660

extension/datadogfleetautomationextension/factory.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ func createDefaultConfig() component.Config {
3030
return &Config{
3131
ClientConfig: confighttp.NewDefaultClientConfig(),
3232
API: APIConfig{
33-
Site: DefaultSite,
33+
Site: defaultSite,
3434
},
35-
ReporterPeriod: DefaultReporterPeriod,
3635
}
3736
}
3837

extension/datadogfleetautomationextension/factory_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {
2020
expectedConfig := &Config{
2121
ClientConfig: confighttp.NewDefaultClientConfig(),
2222
API: APIConfig{
23-
Site: DefaultSite,
23+
Site: defaultSite,
2424
},
25-
ReporterPeriod: DefaultReporterPeriod,
2625
}
2726
cfg := createDefaultConfig()
2827
assert.Equal(t, expectedConfig, cfg)
@@ -50,9 +49,8 @@ func TestFactory_NewFactory(t *testing.T) {
5049
expectedConfig := &Config{
5150
ClientConfig: confighttp.NewDefaultClientConfig(),
5251
API: APIConfig{
53-
Site: DefaultSite,
52+
Site: defaultSite,
5453
},
55-
ReporterPeriod: DefaultReporterPeriod,
5654
}
5755
assert.Equal(t, expectedConfig, defaultConfig)
5856

extension/datadogfleetautomationextension/fleetautomationextension.go

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"net/http"
1010
"strings"
11-
"sync"
1211
"time"
1312

1413
coreconfig "github.com/DataDog/datadog-agent/comp/core/config"
@@ -37,12 +36,12 @@ import (
3736
)
3837

3938
type (
40-
// APIKeyValidator is a function that validates the API key, provided to newExtension for mocking
41-
APIKeyValidator func(context.Context, string, *zap.Logger, *datadog.APIClient) error
42-
// SourceProviderGetter is a function that returns a source.Provider, provided to newExtension for mocking
43-
SourceProviderGetter func(component.TelemetrySettings, string, time.Duration) (source.Provider, error)
44-
// ForwarderGetter is a function that returns a defaultforwarder.Forwarder, provided to newExtension for mocking
45-
ForwarderGetter func(coreconfig.Component, corelog.Component) defaultforwarder.Forwarder
39+
// apiKeyValidator is a function that validates the API key, provided to newExtension for mocking
40+
apiKeyValidator func(context.Context, string, *zap.Logger, *datadog.APIClient) error
41+
// sourceProviderGetter is a function that returns a source.Provider, provided to newExtension for mocking
42+
sourceProviderGetter func(component.TelemetrySettings, string, time.Duration) (source.Provider, error)
43+
// forwarderGetter is a function that returns a defaultforwarder.Forwarder, provided to newExtension for mocking
44+
forwarderGetter func(coreconfig.Component, corelog.Component) defaultforwarder.Forwarder
4645
)
4746

4847
// defaultForwarderInterface is wrapper for methods in datadog-agent DefaultForwarder struct
@@ -69,14 +68,12 @@ type fleetAutomationExtension struct {
6968
collectorConfigStringMap map[string]any
7069
ticker *time.Ticker
7170
done chan bool
72-
mu sync.RWMutex
7371
uuid uuid.UUID
7472

75-
buildInfo payload.CustomBuildInfo
76-
moduleInfo service.ModuleInfos
77-
ModuleInfoJSON *payload.ModuleInfoJSON
78-
activeComponentsJSON *payload.ActiveComponentsJSON
79-
version string
73+
buildInfo payload.CustomBuildInfo
74+
moduleInfo service.ModuleInfos
75+
ModuleInfoJSON *payload.ModuleInfoJSON
76+
version string
8077

8178
forwarder defaultForwarderInterface
8279
compressor *compression.Compressor
@@ -111,14 +108,15 @@ var (
111108
// This method is called during the startup process by the Collector's Service right after
112109
// calling Start.
113110
func (e *fleetAutomationExtension) NotifyConfig(ctx context.Context, conf *confmap.Conf) error {
114-
e.mu.Lock()
115-
defer e.mu.Unlock()
116111

117112
e.collectorConfig = conf
118113
e.telemetry.Logger.Info("Received new collector configuration")
119114
e.collectorConfigStringMap = e.collectorConfig.ToStringMap()
120115

121116
e.updateHostname(ctx)
117+
if e.hostnameSource == "unset" {
118+
return fmt.Errorf("collector hostname is unset, please set hostname manually in config")
119+
}
122120

123121
// create agent metadata payload. most fields are not relevant to OSS collector.
124122
e.agentMetadataPayload = payload.PrepareAgentMetadataPayload(
@@ -165,7 +163,6 @@ func (e *fleetAutomationExtension) NotifyConfig(ctx context.Context, conf *confm
165163
e.otelCollectorPayload,
166164
)
167165
if err != nil {
168-
e.telemetry.Logger.Error("Failed to prepare and send fleet automation payloads", zap.Error(err))
169166
return err
170167
}
171168

@@ -174,12 +171,9 @@ func (e *fleetAutomationExtension) NotifyConfig(ctx context.Context, conf *confm
174171

175172
// Start starts the extension via the component interface.
176173
func (e *fleetAutomationExtension) Start(_ context.Context, host component.Host) error {
177-
if e.forwarder != nil {
178-
err := e.forwarder.Start()
179-
if err != nil {
180-
e.telemetry.Logger.Error("Failed to start forwarder", zap.Error(err))
181-
return err
182-
}
174+
err := e.forwarder.Start()
175+
if err != nil {
176+
return err
183177
}
184178

185179
// Store the host for component status tracking
@@ -194,7 +188,6 @@ func (e *fleetAutomationExtension) Start(_ context.Context, host component.Host)
194188
// Create and start HTTP server
195189
e.httpServer = httpserver.NewServer(e.telemetry.Logger, e.serializer, e.forwarder)
196190
if e.httpServer == nil {
197-
e.telemetry.Logger.Error("Failed to create HTTP server")
198191
return fmt.Errorf("failed to create HTTP server")
199192
}
200193
e.httpServer.Start(func(w http.ResponseWriter, r *http.Request) {
@@ -265,8 +258,6 @@ func (e *fleetAutomationExtension) processComponentStatusEvents() {
265258

266259
// updateComponentStatus updates the componentStatus map with the latest status
267260
func (e *fleetAutomationExtension) updateComponentStatus(esp *eventSourcePair) {
268-
e.mu.Lock()
269-
defer e.mu.Unlock()
270261

271262
if e.componentStatus == nil {
272263
e.componentStatus = make(map[string]any)
@@ -351,9 +342,9 @@ func newExtension(
351342
ctx context.Context,
352343
config *Config,
353344
settings extension.Settings,
354-
apiKeyValidator APIKeyValidator,
355-
sourceProviderGetter SourceProviderGetter,
356-
forwarderGetter ForwarderGetter,
345+
apiKeyValidator apiKeyValidator,
346+
sourceProviderGetter sourceProviderGetter,
347+
forwarderGetter forwarderGetter,
357348
) (*fleetAutomationExtension, error) {
358349
// API Key validation
359350
// TODO: consider moving common logic to pkg/datadog or internal/datadog
@@ -412,7 +403,7 @@ func newExtension(
412403
serializer: serializer,
413404
buildInfo: buildInfo,
414405
version: version,
415-
ticker: time.NewTicker(config.ReporterPeriod),
406+
ticker: time.NewTicker(defaultReporterPeriod),
416407
done: make(chan bool),
417408
hostnameProvider: sp,
418409
hostnameSource: hostnameSource,

0 commit comments

Comments
 (0)