Skip to content

Commit 4ae7de6

Browse files
authored
Enable/Disable Features (#1051)
1 parent e04179f commit 4ae7de6

File tree

10 files changed

+100
-27
lines changed

10 files changed

+100
-27
lines changed

internal/collector/otel_collector_plugin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ func (oc *Collector) checkForNewReceivers(nginxConfigContext *model.NginxConfigC
418418
if tcplogReceiversFound {
419419
reloadCollector = true
420420
}
421+
} else {
422+
slog.Debug("NAP logs feature disabled", "enabled_features", oc.config.Features)
421423
}
422424

423425
return reloadCollector

internal/command/command_plugin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {
104104

105105
func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Message) {
106106
if resource, ok := msg.Data.(*mpi.Resource); ok {
107-
if !cp.commandService.IsConnected() && cp.config.IsFeatureEnabled(pkgConfig.FeatureConnection) {
107+
if !cp.commandService.IsConnected() {
108108
cp.createConnection(ctx, resource)
109109
} else {
110110
statusErr := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
@@ -237,8 +237,8 @@ func (cp *CommandPlugin) handleAPIActionRequest(ctx context.Context, message *mp
237237
} else {
238238
slog.WarnContext(
239239
ctx,
240-
"API Action Request feature disabled. Unable to process API action request",
241-
"request", message,
240+
"API action feature disabled. Unable to process API action request",
241+
"request", message, "enabled_features", cp.config.Features,
242242
)
243243

244244
err := cp.commandService.SendDataPlaneResponse(ctx, &mpi.DataPlaneResponse{
@@ -263,7 +263,7 @@ func (cp *CommandPlugin) handleConfigApplyRequest(newCtx context.Context, messag
263263
slog.WarnContext(
264264
newCtx,
265265
"Configuration feature disabled. Unable to process config apply request",
266-
"request", message,
266+
"request", message, "enabled_features", cp.config.Features,
267267
)
268268

269269
err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{
@@ -288,7 +288,7 @@ func (cp *CommandPlugin) handleConfigUploadRequest(newCtx context.Context, messa
288288
slog.WarnContext(
289289
newCtx,
290290
"Configuration feature disabled. Unable to process config upload request",
291-
"request", message,
291+
"request", message, "enabled_features", cp.config.Features,
292292
)
293293

294294
err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{

internal/command/command_plugin_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ func TestCommandPlugin_monitorSubscribeChannel(t *testing.T) {
204204
request: "APIActionRequest",
205205
configFeatures: []string{
206206
pkg.FeatureConfiguration,
207-
pkg.FeatureConnection,
208207
pkg.FeatureMetrics,
209208
pkg.FeatureFileWatcher,
210209
pkg.FeatureAPIAction,
@@ -278,7 +277,6 @@ func TestCommandPlugin_FeatureDisabled(t *testing.T) {
278277
expectedLog: "Configuration feature disabled. Unable to process config upload request",
279278
request: "UploadRequest",
280279
configFeatures: []string{
281-
pkg.FeatureConnection,
282280
pkg.FeatureMetrics,
283281
pkg.FeatureFileWatcher,
284282
},
@@ -293,7 +291,6 @@ func TestCommandPlugin_FeatureDisabled(t *testing.T) {
293291
expectedLog: "Configuration feature disabled. Unable to process config apply request",
294292
request: "ApplyRequest",
295293
configFeatures: []string{
296-
pkg.FeatureConnection,
297294
pkg.FeatureMetrics,
298295
pkg.FeatureFileWatcher,
299296
},

internal/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ func ResolveConfig() (*Config, error) {
123123
checkCollectorConfiguration(collector, config)
124124

125125
slog.Debug("Agent config", "config", config)
126-
slog.Info("Enabled features", "features", config.Features)
127126
slog.Info("Excluded files from being watched for file changes", "exclude_files",
128127
config.Watchers.FileWatcher.ExcludeFiles)
129128

internal/config/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const (
7878
func DefaultFeatures() []string {
7979
return []string{
8080
pkg.FeatureConfiguration,
81-
pkg.FeatureConnection,
81+
pkg.FeatureCertificates,
8282
pkg.FeatureMetrics,
8383
pkg.FeatureFileWatcher,
8484
}

internal/plugin/plugin_manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"context"
1010
"log/slog"
1111

12+
pkg "github.com/nginx/agent/v3/pkg/config"
13+
1214
"github.com/nginx/agent/v3/internal/collector"
1315
"github.com/nginx/agent/v3/internal/command"
1416
"github.com/nginx/agent/v3/internal/file"
@@ -58,6 +60,12 @@ func addCommandAndFilePlugins(ctx context.Context, plugins []bus.Plugin, agentCo
5860
}
5961

6062
func addCollectorPlugin(ctx context.Context, agentConfig *config.Config, plugins []bus.Plugin) []bus.Plugin {
63+
if !agentConfig.IsFeatureEnabled(pkg.FeatureMetrics) {
64+
slog.WarnContext(ctx, "Metrics feature disabled, no metrics will be collected",
65+
"enabled_features", agentConfig.Features)
66+
67+
return plugins
68+
}
6169
if agentConfig.IsACollectorExporterConfigured() {
6270
oTelCollector, err := collector.New(agentConfig)
6371
if err == nil {

internal/plugin/plugin_manager_test.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"context"
1010
"testing"
1111

12+
pkg "github.com/nginx/agent/v3/pkg/config"
13+
1214
"github.com/nginx/agent/v3/internal/collector"
1315
"github.com/nginx/agent/v3/internal/command"
1416
"github.com/nginx/agent/v3/internal/file"
@@ -35,7 +37,8 @@ func TestLoadPlugins(t *testing.T) {
3537
&resource.Resource{},
3638
&watcher.Watcher{},
3739
},
38-
}, {
40+
},
41+
{
3942
name: "Test 2: Load file and command plugins",
4043
input: &config.Config{
4144
Command: &config.Command{
@@ -53,21 +56,82 @@ func TestLoadPlugins(t *testing.T) {
5356
&file.FilePlugin{},
5457
&watcher.Watcher{},
5558
},
56-
}, {
59+
},
60+
{
5761
name: "Test 3: Load metrics collector plugin",
5862
input: &config.Config{
5963
Collector: &config.Collector{
6064
Exporters: config.Exporters{
6165
Debug: &config.DebugExporter{},
6266
},
6367
},
68+
Features: config.DefaultFeatures(),
6469
},
6570
expected: []bus.Plugin{
6671
&resource.Resource{},
6772
&collector.Collector{},
6873
&watcher.Watcher{},
6974
},
7075
},
76+
{
77+
name: "Test 4: Metrics collector plugin, feature disabled",
78+
input: &config.Config{
79+
Command: &config.Command{
80+
Server: &config.ServerConfig{
81+
Host: "127.0.0.1",
82+
Port: 443,
83+
Type: config.Grpc,
84+
},
85+
},
86+
Collector: &config.Collector{
87+
Exporters: config.Exporters{
88+
Debug: &config.DebugExporter{},
89+
},
90+
},
91+
Features: []string{
92+
pkg.FeatureConfiguration,
93+
pkg.FeatureFileWatcher,
94+
},
95+
},
96+
expected: []bus.Plugin{
97+
&resource.Resource{},
98+
&command.CommandPlugin{},
99+
&file.FilePlugin{},
100+
&watcher.Watcher{},
101+
},
102+
},
103+
{
104+
name: "Test 5: All features enabled",
105+
input: &config.Config{
106+
Command: &config.Command{
107+
Server: &config.ServerConfig{
108+
Host: "127.0.0.1",
109+
Port: 443,
110+
Type: config.Grpc,
111+
},
112+
},
113+
Collector: &config.Collector{
114+
Exporters: config.Exporters{
115+
Debug: &config.DebugExporter{},
116+
},
117+
},
118+
Features: []string{
119+
pkg.FeatureConfiguration,
120+
pkg.FeatureMetrics,
121+
pkg.FeatureFileWatcher,
122+
pkg.FeatureCertificates,
123+
pkg.FeatureAPIAction,
124+
pkg.FeatureLogsNap,
125+
},
126+
},
127+
expected: []bus.Plugin{
128+
&resource.Resource{},
129+
&command.CommandPlugin{},
130+
&file.FilePlugin{},
131+
&collector.Collector{},
132+
&watcher.Watcher{},
133+
},
134+
},
71135
}
72136

73137
for _, test := range tests {

internal/watcher/instance/nginx_config_parser.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strconv"
2121
"strings"
2222

23+
pkg "github.com/nginx/agent/v3/pkg/config"
24+
2325
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2426
"github.com/nginx/agent/v3/internal/config"
2527
"github.com/nginx/agent/v3/internal/model"
@@ -148,14 +150,16 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
148150
}
149151
case "ssl_certificate", "proxy_ssl_certificate", "ssl_client_certificate",
150152
"ssl_trusted_certificate":
151-
sslCertFile := ncp.sslCert(ctx, directive.Args[0], rootDir)
152-
if sslCertFile != nil {
153-
if !ncp.isDuplicateFile(nginxConfigContext.Files, sslCertFile) {
153+
if ncp.agentConfig.IsFeatureEnabled(pkg.FeatureCertificates) {
154+
sslCertFile := ncp.sslCert(ctx, directive.Args[0], rootDir)
155+
if sslCertFile != nil && !ncp.isDuplicateFile(nginxConfigContext.Files, sslCertFile) {
154156
slog.DebugContext(ctx, "Adding SSL certificate file", "ssl_cert", sslCertFile)
155157
nginxConfigContext.Files = append(nginxConfigContext.Files, sslCertFile)
156158
}
159+
} else {
160+
slog.DebugContext(ctx, "Certificate feature is disabled, skipping cert",
161+
"enabled_features", ncp.agentConfig.Features)
157162
}
158-
159163
case "app_protect_security_log":
160164
if len(directive.Args) > 1 {
161165
syslogArg := directive.Args[1]

internal/watcher/watcher_plugin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func (w *Watcher) Init(ctx context.Context, messagePipe bus.MessagePipeInterface
102102

103103
if w.agentConfig.IsFeatureEnabled(pkgConfig.FeatureFileWatcher) {
104104
go w.fileWatcherService.Watch(watcherContext, w.fileUpdatesChannel)
105+
} else {
106+
slog.DebugContext(watcherContext, "File watcher feature is disabled",
107+
"enabled_features", w.agentConfig.Features)
105108
}
106109

107110
go w.monitorWatchers(watcherContext)

pkg/config/features.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
package config
77

88
const (
9-
FeatureCertificates = "certificates"
10-
FeatureConfiguration = "configuration"
11-
FeatureConnection = "connection"
12-
FeatureMetrics = "metrics"
13-
FeatureMetricsContainer = "metrics-container"
14-
FeatureMetricsHost = "metrics-host"
15-
FeatureMetricsInstance = "metrics-instance"
16-
FeatureFileWatcher = "file-watcher"
17-
FeatureAgentAPI = "agent-api"
18-
FeatureAPIAction = "api-action"
19-
FeatureLogsNap = "logs-nap"
9+
FeatureCertificates = "certificates"
10+
FeatureConfiguration = "configuration"
11+
FeatureMetrics = "metrics"
12+
FeatureFileWatcher = "file-watcher"
13+
FeatureAPIAction = "api-action"
14+
// FeatureLogsNap experimental feature
15+
FeatureLogsNap = "logs-nap"
2016
)

0 commit comments

Comments
 (0)