Skip to content

Commit

Permalink
Update agent config defaults and format (#959)
Browse files Browse the repository at this point in the history
* update config defaults and format
  • Loading branch information
aphralG authored Jan 15, 2025
1 parent 6f74a21 commit b6e143c
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 163 deletions.
6 changes: 3 additions & 3 deletions internal/backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
// Information from https://pkg.go.dev/github.com/cenkalti/backoff/v4#section-readme
func WaitUntil(
ctx context.Context,
backoffSettings *config.CommonSettings,
backoffSettings *config.BackOff,
operation backoff.Operation,
) error {
eb := backoff.NewExponentialBackOff()
Expand All @@ -68,7 +68,7 @@ func WaitUntil(
// nolint: ireturn
func WaitUntilWithData[T any](
ctx context.Context,
backoffSettings *config.CommonSettings,
backoffSettings *config.BackOff,
operation backoff.OperationWithData[T],
) (T, error) {
backoffWithContext := Context(ctx, backoffSettings)
Expand All @@ -77,7 +77,7 @@ func WaitUntilWithData[T any](
}

// nolint: ireturn
func Context(ctx context.Context, backoffSettings *config.CommonSettings) backoff.BackOffContext {
func Context(ctx context.Context, backoffSettings *config.BackOff) backoff.BackOffContext {
eb := backoff.NewExponentialBackOff()
eb.InitialInterval = backoffSettings.InitialInterval
eb.MaxInterval = backoffSettings.MaxInterval
Expand Down
20 changes: 11 additions & 9 deletions internal/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ func TestWaitUntil(t *testing.T) {

for _, test := range tests {
invocations = 0
settings := &config.CommonSettings{
InitialInterval: test.initialInterval,
MaxInterval: test.maxInterval,
MaxElapsedTime: test.maxElapsedTime,
RandomizationFactor: config.DefBackoffRandomizationFactor,
Multiplier: config.DefBackoffMultiplier,
settings := &config.Client{
Backoff: &config.BackOff{
InitialInterval: test.initialInterval,
MaxInterval: test.maxInterval,
MaxElapsedTime: test.maxElapsedTime,
RandomizationFactor: config.DefBackoffRandomizationFactor,
Multiplier: config.DefBackoffMultiplier,
},
}
result := WaitUntil(test.context, settings, test.operation)
result := WaitUntil(test.context, settings.Backoff, test.operation)

if test.expectedError {
assert.Errorf(t, result, test.name)
Expand Down Expand Up @@ -164,7 +166,7 @@ func TestWaitUntilWithData(t *testing.T) {
}

for _, test := range tests {
settings := &config.CommonSettings{
settings := &config.BackOff{
InitialInterval: test.initialInterval,
MaxInterval: test.maxInterval,
MaxElapsedTime: test.maxElapsedTime,
Expand All @@ -185,7 +187,7 @@ func TestWaitUntilWithData(t *testing.T) {
}

func TestContext(t *testing.T) {
settings := &config.CommonSettings{
settings := &config.BackOff{
InitialInterval: 10 * time.Millisecond,
MaxInterval: 10 * time.Millisecond,
MaxElapsedTime: 10 * time.Millisecond,
Expand Down
4 changes: 2 additions & 2 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func (oc *Collector) Close(ctx context.Context) error {
oc.service.Shutdown()
oc.cancel()

settings := oc.config.Common
settings := oc.config.Client.Backoff
settings.MaxElapsedTime = maxTimeToWaitForShutdown
err := backoff.WaitUntil(ctx, oc.config.Common, func() error {
err := backoff.WaitUntil(ctx, settings, func() error {
if oc.service.GetState() == otelcol.StateClosed {
return nil
}
Expand Down
34 changes: 16 additions & 18 deletions internal/command/command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"log/slog"
"sync"
"sync/atomic"
"time"

"github.com/cenkalti/backoff/v4"

Expand All @@ -29,7 +28,6 @@ import (
var _ commandService = (*CommandService)(nil)

const (
retryInterval = 5 * time.Second
createConnectionMaxElapsedTime = 0
)

Expand Down Expand Up @@ -95,7 +93,7 @@ func (cs *CommandService) UpdateDataPlaneStatus(
Resource: resource,
}

backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
defer backoffCancel()

sendDataPlaneStatus := func() (*mpi.UpdateDataPlaneStatusResponse, error) {
Expand All @@ -119,7 +117,7 @@ func (cs *CommandService) UpdateDataPlaneStatus(

response, err := backoff.RetryWithData(
sendDataPlaneStatus,
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
)
if err != nil {
return err
Expand All @@ -145,12 +143,12 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea
InstanceHealths: instanceHealths,
}

backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
defer backoffCancel()

response, err := backoff.RetryWithData(
cs.dataPlaneHealthCallback(ctx, request),
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
)
if err != nil {
return err
Expand All @@ -164,12 +162,12 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea
func (cs *CommandService) SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error {
slog.DebugContext(ctx, "Sending data plane response", "response", response)

backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Common.MaxElapsedTime)
backOffCtx, backoffCancel := context.WithTimeout(ctx, cs.agentConfig.Client.Backoff.MaxElapsedTime)
defer backoffCancel()

return backoff.Retry(
cs.sendDataPlaneResponseCallback(ctx, response),
backoffHelpers.Context(backOffCtx, cs.agentConfig.Common),
backoffHelpers.Context(backOffCtx, cs.agentConfig.Client.Backoff),
)
}

Expand All @@ -184,12 +182,12 @@ func (cs *CommandService) CancelSubscription(ctx context.Context) {
}

func (cs *CommandService) subscribe(ctx context.Context) {
commonSettings := &config.CommonSettings{
InitialInterval: cs.agentConfig.Common.InitialInterval,
MaxInterval: cs.agentConfig.Common.MaxInterval,
commonSettings := &config.BackOff{
InitialInterval: cs.agentConfig.Client.Backoff.InitialInterval,
MaxInterval: cs.agentConfig.Client.Backoff.MaxInterval,
MaxElapsedTime: createConnectionMaxElapsedTime,
RandomizationFactor: cs.agentConfig.Common.RandomizationFactor,
Multiplier: cs.agentConfig.Common.Multiplier,
RandomizationFactor: cs.agentConfig.Client.Backoff.RandomizationFactor,
Multiplier: cs.agentConfig.Client.Backoff.Multiplier,
}

for {
Expand Down Expand Up @@ -223,12 +221,12 @@ func (cs *CommandService) CreateConnection(
Resource: resource,
}

commonSettings := &config.CommonSettings{
InitialInterval: cs.agentConfig.Common.InitialInterval,
MaxInterval: cs.agentConfig.Common.MaxInterval,
commonSettings := &config.BackOff{
InitialInterval: cs.agentConfig.Client.Backoff.InitialInterval,
MaxInterval: cs.agentConfig.Client.Backoff.MaxInterval,
MaxElapsedTime: createConnectionMaxElapsedTime,
RandomizationFactor: cs.agentConfig.Common.RandomizationFactor,
Multiplier: cs.agentConfig.Common.Multiplier,
RandomizationFactor: cs.agentConfig.Client.Backoff.RandomizationFactor,
Multiplier: cs.agentConfig.Client.Backoff.Multiplier,
}

slog.DebugContext(ctx, "Sending create connection request", "request", request)
Expand Down
128 changes: 91 additions & 37 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"path/filepath"
"slices"
"strings"
"time"

selfsignedcerts "github.com/nginx/agent/v3/pkg/tls"
uuidLibrary "github.com/nginx/agent/v3/pkg/uuid"
Expand Down Expand Up @@ -101,7 +100,6 @@ func ResolveConfig() (*Config, error) {
AllowedDirectories: allowedDirs,
Collector: collector,
Command: resolveCommand(),
Common: resolveCommon(),
Watchers: resolveWatchers(),
Features: viperInstance.GetStringSlice(FeaturesKey),
}
Expand Down Expand Up @@ -155,7 +153,6 @@ func registerFlags() {
"collection or error monitoring",
)

fs.Duration(ClientTimeoutKey, time.Minute, "Client timeout")
fs.StringSlice(AllowedDirectoriesKey,
DefaultAllowedDirectories(),
"A comma-separated list of paths that you want to grant NGINX Agent read/write access to")
Expand All @@ -178,24 +175,6 @@ func registerFlags() {
"How often the NGINX Agent will check for file changes.",
)

fs.Int(
ClientMaxMessageSizeKey,
DefMaxMessageSize,
"The value used, if not 0, for both max_message_send_size and max_message_receive_size",
)

fs.Int(
ClientMaxMessageReceiveSizeKey,
DefMaxMessageRecieveSize,
"Updates the client grpc setting MaxRecvMsgSize with the specific value in MB.",
)

fs.Int(
ClientMaxMessageSendSizeKey,
DefMaxMessageSendSize,
"Updates the client grpc setting MaxSendMsgSize with the specific value in MB.",
)

fs.StringSlice(
FeaturesKey,
DefaultFeatures(),
Expand All @@ -204,6 +183,7 @@ func registerFlags() {

registerCommandFlags(fs)
registerCollectorFlags(fs)
registerClientFlags(fs)

fs.SetNormalizeFunc(normalizeFunc)

Expand All @@ -218,6 +198,76 @@ func registerFlags() {
})
}

func registerClientFlags(fs *flag.FlagSet) {
// HTTP Flags
fs.Duration(
ClientHTTPTimeoutKey,
DefHTTPTimeout,
"The client HTTP Timeout, value in seconds")

// Backoff Flags
fs.Duration(
ClientBackoffInitialIntervalKey,
DefBackoffInitialInterval,
"The client backoff initial interval, value in seconds")

fs.Duration(
ClientBackoffMaxIntervalKey,
DefBackoffMaxInterval,
"The client backoff max interval, value in seconds")

fs.Duration(
ClientBackoffMaxElapsedTimeKey,
DefBackoffMaxElapsedTime,
"The client backoff max elapsed time, value in seconds")

fs.Float64(
ClientBackoffRandomizationFactorKey,
DefBackoffRandomizationFactor,
"The client backoff randomization factor, value float")

fs.Float64(
ClientBackoffMultiplierKey,
DefBackoffMultiplier,
"The client backoff multiplier, value float")

// GRPC Flags
fs.Duration(
ClientKeepAliveTimeoutKey,
DefGRPCKeepAliveTimeout,
"Updates the client grpc setting, KeepAlive Timeout with the specific value in seconds.",
)

fs.Duration(
ClientKeepAliveTimeKey,
DefGRPCKeepAliveTime,
"Updates the client grpc setting, KeepAlive Time with the specific value in seconds.",
)

fs.Bool(
ClientKeepAlivePermitWithoutStreamKey,
DefGRPCKeepAlivePermitWithoutStream,
"Update the client grpc setting, KeepAlive PermitWithoutStream value")

fs.Int(
ClientGRPCMaxMessageSizeKey,
DefMaxMessageSize,
"The value used, if not 0, for both max_message_send_size and max_message_receive_size",
)

fs.Int(
ClientGRPCMaxMessageReceiveSizeKey,
DefMaxMessageRecieveSize,
"Updates the client grpc setting MaxRecvMsgSize with the specific value in MB.",
)

fs.Int(
ClientGRPCMaxMessageSendSizeKey,
DefMaxMessageSendSize,
"Updates the client grpc setting MaxSendMsgSize with the specific value in MB.",
)
}

func registerCommandFlags(fs *flag.FlagSet) {
fs.String(
CommandServerHostKey,
Expand Down Expand Up @@ -418,12 +468,26 @@ func resolveDataPlaneConfig() *DataPlaneConfig {

func resolveClient() *Client {
return &Client{
Timeout: viperInstance.GetDuration(ClientTimeoutKey),
Time: viperInstance.GetDuration(ClientTimeKey),
PermitWithoutStream: viperInstance.GetBool(ClientPermitWithoutStreamKey),
MaxMessageSize: viperInstance.GetInt(ClientMaxMessageSizeKey),
MaxMessageRecieveSize: viperInstance.GetInt(ClientMaxMessageReceiveSizeKey),
MaxMessageSendSize: viperInstance.GetInt(ClientMaxMessageSendSizeKey),
HTTP: &HTTP{
Timeout: viperInstance.GetDuration(ClientHTTPTimeoutKey),
},
Grpc: &GRPC{
KeepAlive: &KeepAlive{
Timeout: viperInstance.GetDuration(ClientKeepAliveTimeoutKey),
Time: viperInstance.GetDuration(ClientKeepAliveTimeKey),
PermitWithoutStream: viperInstance.GetBool(ClientKeepAlivePermitWithoutStreamKey),
},
MaxMessageSize: viperInstance.GetInt(ClientGRPCMaxMessageSizeKey),
MaxMessageReceiveSize: viperInstance.GetInt(ClientGRPCMaxMessageReceiveSizeKey),
MaxMessageSendSize: viperInstance.GetInt(ClientGRPCMaxMessageSendSizeKey),
},
Backoff: &BackOff{
InitialInterval: viperInstance.GetDuration(ClientBackoffInitialIntervalKey),
MaxInterval: viperInstance.GetDuration(ClientBackoffMaxIntervalKey),
MaxElapsedTime: viperInstance.GetDuration(ClientBackoffMaxElapsedTimeKey),
RandomizationFactor: viperInstance.GetFloat64(ClientBackoffRandomizationFactorKey),
Multiplier: viperInstance.GetFloat64(ClientBackoffMultiplierKey),
},
}
}

Expand Down Expand Up @@ -686,16 +750,6 @@ func arePrometheusExportTLSSettingsSet() bool {
viperInstance.IsSet(CollectorPrometheusExporterTLSServerNameKey)
}

func resolveCommon() *CommonSettings {
return &CommonSettings{
InitialInterval: DefBackoffInitialInterval,
MaxInterval: DefBackoffMaxInterval,
MaxElapsedTime: DefBackoffMaxElapsedTime,
RandomizationFactor: DefBackoffRandomizationFactor,
Multiplier: DefBackoffMultiplier,
}
}

func resolveWatchers() *Watchers {
return &Watchers{
InstanceWatcher: InstanceWatcher{
Expand Down
Loading

0 comments on commit b6e143c

Please sign in to comment.