Skip to content

Commit ed33c6a

Browse files
committed
[skip ci] adding Log configuration option to MPI
1 parent b55f22b commit ed33c6a

File tree

14 files changed

+1421
-236
lines changed

14 files changed

+1421
-236
lines changed

api/grpc/mpi/v1/command.pb.go

Lines changed: 499 additions & 233 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/command.pb.validate.go

Lines changed: 469 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/command.proto

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ message UpdateDataPlaneStatusRequest {
113113
// Respond to a UpdateDataPlaneStatusRequest - intentionally empty
114114
message UpdateDataPlaneStatusResponse {}
115115

116+
message UpdateNginxAgentConfigurationRequest {
117+
// Meta-information associated with a message
118+
mpi.v1.MessageMeta message_meta = 1;
119+
// the NGINX Agent configuration to update
120+
AgentConfig agent_config = 2;
121+
}
122+
123+
message UpdateNginxAgentConfigurationResponse {
124+
// The success or failure of the UpdateNginxAgentConfigurationRequest
125+
mpi.v1.CommandResponse response = 1;
126+
}
127+
116128
message InstanceHealth {
117129
// Health status enum
118130
enum InstanceHealthStatus {
@@ -171,6 +183,8 @@ message ManagementPlaneRequest {
171183
APIActionRequest action_request = 7;
172184
// triggers a DataPlaneResponse with a command_response for a particular correlation_id
173185
CommandStatusRequest command_status_request = 8;
186+
// triggers an UpdateNginxAgentConfiguration rpc, returning an UpdateNginxAgentConfigurationResponse
187+
UpdateNginxAgentConfigurationRequest update_nginx_agent_configuration_request = 9;
174188
}
175189
}
176190

@@ -387,6 +401,26 @@ message AgentConfig {
387401
string message_buffer_size = 6;
388402
// Auxiliary Command server settings
389403
AuxiliaryCommandServer auxiliary_command = 7;
404+
// Log settings
405+
Log log = 8;
406+
}
407+
408+
// The log settings associated with NGINX Agent
409+
message Log {
410+
LogLevel log_level = 1;
411+
enum LogLevel {
412+
// Unspecified log level
413+
LOG_LEVEL_UNSPECIFIED = 0;
414+
// Error log level
415+
LOG_LEVEL_ERROR = 1;
416+
// Warning log level
417+
LOG_LEVEL_WARN = 2;
418+
// Info log level
419+
LOG_LEVEL_INFO = 3;
420+
// Debug log level
421+
LOG_LEVEL_DEBUG = 4;
422+
}
423+
string log_path = 2;
390424
}
391425

392426
// The command server settings, associated with messaging from an external source

docs/proto/protos.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
- [InstanceHealth](#mpi-v1-InstanceHealth)
6868
- [InstanceMeta](#mpi-v1-InstanceMeta)
6969
- [InstanceRuntime](#mpi-v1-InstanceRuntime)
70+
- [Log](#mpi-v1-Log)
7071
- [ManagementPlaneRequest](#mpi-v1-ManagementPlaneRequest)
7172
- [MetricsServer](#mpi-v1-MetricsServer)
7273
- [NGINXAppProtectRuntimeInfo](#mpi-v1-NGINXAppProtectRuntimeInfo)
@@ -81,10 +82,13 @@
8182
- [UpdateDataPlaneStatusRequest](#mpi-v1-UpdateDataPlaneStatusRequest)
8283
- [UpdateDataPlaneStatusResponse](#mpi-v1-UpdateDataPlaneStatusResponse)
8384
- [UpdateHTTPUpstreamServers](#mpi-v1-UpdateHTTPUpstreamServers)
85+
- [UpdateNginxAgentConfigurationRequest](#mpi-v1-UpdateNginxAgentConfigurationRequest)
86+
- [UpdateNginxAgentConfigurationResponse](#mpi-v1-UpdateNginxAgentConfigurationResponse)
8487
- [UpdateStreamServers](#mpi-v1-UpdateStreamServers)
8588

8689
- [InstanceHealth.InstanceHealthStatus](#mpi-v1-InstanceHealth-InstanceHealthStatus)
8790
- [InstanceMeta.InstanceType](#mpi-v1-InstanceMeta-InstanceType)
91+
- [Log.LogLevel](#mpi-v1-Log-LogLevel)
8892

8993
- [CommandService](#mpi-v1-CommandService)
9094

@@ -718,6 +722,7 @@ This contains a series of NGINX Agent configurations
718722
| features | [string](#string) | repeated | A list of features that the NGINX Agent has |
719723
| message_buffer_size | [string](#string) | | Message buffer size, maximum not acknowledged messages from the subscribe perspective |
720724
| auxiliary_command | [AuxiliaryCommandServer](#mpi-v1-AuxiliaryCommandServer) | | Auxiliary Command server settings |
725+
| log | [Log](#mpi-v1-Log) | | Log settings |
721726

722727

723728

@@ -1049,6 +1054,22 @@ Meta-information relating to the reported instance
10491054

10501055

10511056

1057+
<a name="mpi-v1-Log"></a>
1058+
1059+
### Log
1060+
The log settings associated with NGINX Agent
1061+
1062+
1063+
| Field | Type | Label | Description |
1064+
| ----- | ---- | ----- | ----------- |
1065+
| log_level | [Log.LogLevel](#mpi-v1-Log-LogLevel) | | |
1066+
| log_path | [string](#string) | | |
1067+
1068+
1069+
1070+
1071+
1072+
10521073
<a name="mpi-v1-ManagementPlaneRequest"></a>
10531074

10541075
### ManagementPlaneRequest
@@ -1064,6 +1085,7 @@ A Management Plane request for information, triggers an associated rpc on the Da
10641085
| config_upload_request | [ConfigUploadRequest](#mpi-v1-ConfigUploadRequest) | | triggers a series of rpc UpdateFile(File) for that instances |
10651086
| action_request | [APIActionRequest](#mpi-v1-APIActionRequest) | | triggers a DataPlaneResponse with a command_response for a particular action |
10661087
| command_status_request | [CommandStatusRequest](#mpi-v1-CommandStatusRequest) | | triggers a DataPlaneResponse with a command_response for a particular correlation_id |
1088+
| update_nginx_agent_configuration_request | [UpdateNginxAgentConfigurationRequest](#mpi-v1-UpdateNginxAgentConfigurationRequest) | | triggers an UpdateNginxAgentConfiguration rpc, returning an UpdateNginxAgentConfigurationResponse |
10671089

10681090

10691091

@@ -1271,6 +1293,37 @@ Update HTTP Upstream Servers for an instance
12711293

12721294

12731295

1296+
<a name="mpi-v1-UpdateNginxAgentConfigurationRequest"></a>
1297+
1298+
### UpdateNginxAgentConfigurationRequest
1299+
1300+
1301+
1302+
| Field | Type | Label | Description |
1303+
| ----- | ---- | ----- | ----------- |
1304+
| message_meta | [MessageMeta](#mpi-v1-MessageMeta) | | Meta-information associated with a message |
1305+
| agent_config | [AgentConfig](#mpi-v1-AgentConfig) | | the NGINX Agent configuration to update |
1306+
1307+
1308+
1309+
1310+
1311+
1312+
<a name="mpi-v1-UpdateNginxAgentConfigurationResponse"></a>
1313+
1314+
### UpdateNginxAgentConfigurationResponse
1315+
1316+
1317+
1318+
| Field | Type | Label | Description |
1319+
| ----- | ---- | ----- | ----------- |
1320+
| response | [CommandResponse](#mpi-v1-CommandResponse) | | The success or failure of the UpdateNginxAgentConfigurationRequest |
1321+
1322+
1323+
1324+
1325+
1326+
12741327
<a name="mpi-v1-UpdateStreamServers"></a>
12751328

12761329
### UpdateStreamServers
@@ -1318,6 +1371,21 @@ the types of instances possible
13181371
| INSTANCE_TYPE_NGINX_APP_PROTECT | 5 | NGINX App Protect |
13191372

13201373

1374+
1375+
<a name="mpi-v1-Log-LogLevel"></a>
1376+
1377+
### Log.LogLevel
1378+
1379+
1380+
| Name | Number | Description |
1381+
| ---- | ------ | ----------- |
1382+
| LOG_LEVEL_UNSPECIFIED | 0 | Unspecified log level |
1383+
| LOG_LEVEL_ERROR | 1 | Error log level |
1384+
| LOG_LEVEL_WARN | 2 | Warning log level |
1385+
| LOG_LEVEL_INFO | 3 | Info log level |
1386+
| LOG_LEVEL_DEBUG | 4 | Debug log level |
1387+
1388+
13211389

13221390

13231391

internal/bus/topics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ const (
2727
DataPlaneHealthRequestTopic = "data-plane-health-request"
2828
DataPlaneHealthResponseTopic = "data-plane-health-response"
2929
APIActionRequestTopic = "api-action-request"
30+
AgentConfigUpdateTopic = "agent-config-update"
3031
)

internal/command/command_plugin.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type (
3737
Subscribe(ctx context.Context)
3838
IsConnected() bool
3939
CreateConnection(ctx context.Context, resource *mpi.Resource) (*mpi.CreateConnectionResponse, error)
40+
UpdateAgentConfiguration(ctx context.Context, request *mpi.AgentConfig) error
4041
}
4142

4243
CommandPlugin struct {
@@ -127,6 +128,8 @@ func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {
127128
cp.processDataPlaneHealth(ctxWithMetadata, msg)
128129
case bus.DataPlaneResponseTopic:
129130
cp.processDataPlaneResponse(ctxWithMetadata, msg)
131+
case bus.AgentConfigUpdateTopic:
132+
cp.processAgentConfigUpdate(ctxWithMetadata, msg)
130133
default:
131134
slog.DebugContext(ctxWithMetadata, "Command plugin received unknown topic", "topic", msg.Topic)
132135
}
@@ -140,6 +143,7 @@ func (cp *CommandPlugin) Subscriptions() []string {
140143
bus.InstanceHealthTopic,
141144
bus.DataPlaneHealthResponseTopic,
142145
bus.DataPlaneResponseTopic,
146+
bus.AgentConfigUpdateTopic,
143147
}
144148
}
145149

@@ -180,6 +184,13 @@ func (cp *CommandPlugin) createConnection(ctx context.Context, resource *mpi.Res
180184
Topic: bus.ConnectionCreatedTopic,
181185
Data: createConnectionResponse,
182186
})
187+
188+
// update agent configuration after connection is created, and notify other plugins
189+
_ = cp.commandService.UpdateAgentConfiguration(ctx, createConnectionResponse.AgentConfig)
190+
cp.messagePipe.Process(ctx, &bus.Message{
191+
Topic: bus.AgentConfigUpdateTopic,
192+
Data: createConnectionResponse.AgentConfig,
193+
})
183194
}
184195
}
185196

@@ -262,6 +273,19 @@ func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Me
262273
}
263274
}
264275

276+
func (cp *CommandPlugin) processAgentConfigUpdate(ctx context.Context, msg *bus.Message) {
277+
slog.DebugContext(ctx, "Command plugin received agent config update message", "data", msg.Data)
278+
//
279+
if mpiConf, ok := msg.Data.(*mpi.AgentConfig); ok {
280+
err := cp.commandService.UpdateAgentConfiguration(ctx, mpiConf)
281+
if err != nil {
282+
slog.ErrorContext(ctx, "Unable to update agent configuration", "error", err)
283+
}
284+
} else {
285+
slog.ErrorContext(ctx, "Invalid data for agent config update message", "data", msg.Data)
286+
}
287+
}
288+
265289
//nolint:revive // cognitive complexity is 14
266290
func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) {
267291
for {
@@ -305,6 +329,9 @@ func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) {
305329
}
306330
slog.InfoContext(ctx, "Received management plane action request")
307331
cp.handleAPIActionRequest(newCtx, message)
332+
case *mpi.ManagementPlaneRequest_UpdateNginxAgentConfigurationRequest:
333+
slog.InfoContext(ctx, "Received management plane request - update agent configuration")
334+
cp.handleAgentConfigUpdateRequest(newCtx, message)
308335
default:
309336
slog.DebugContext(newCtx, "Management plane request not implemented yet")
310337
}
@@ -408,6 +435,11 @@ func (cp *CommandPlugin) handleInvalidRequest(ctx context.Context,
408435
}
409436
}
410437

438+
func (cp *CommandPlugin) handleAgentConfigUpdateRequest(ctx context.Context, request *mpi.ManagementPlaneRequest) {
439+
// notify plugins about the agent config update request
440+
cp.Process(ctx, &bus.Message{Topic: bus.AgentConfigUpdateTopic, Data: request})
441+
}
442+
411443
func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mpi.CommandResponse_CommandStatus,
412444
message, err string,
413445
) *mpi.DataPlaneResponse {

internal/command/command_plugin_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ func TestCommandPlugin_Process(t *testing.T) {
151151
Data: commandPlugin.conn,
152152
})
153153
require.Equal(t, 1, fakeCommandService.UpdateClientCallCount())
154+
155+
commandPlugin.Process(ctx, &bus.Message{
156+
Topic: bus.AgentConfigUpdateTopic,
157+
Data: mpi.AgentConfig{
158+
Log: &mpi.Log{
159+
LogLevel: mpi.Log_LOG_LEVEL_DEBUG,
160+
LogPath: "somewhere",
161+
},
162+
},
163+
})
164+
require.Equal(t, 1, fakeCommandService.UpdateAgentConfigurationCallCount())
154165
}
155166

156167
func TestCommandPlugin_monitorSubscribeChannel(t *testing.T) {

internal/command/command_service.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ func (cs *CommandService) SendDataPlaneResponse(ctx context.Context, response *m
182182
)
183183
}
184184

185+
func (cs *CommandService) UpdateAgentConfiguration(ctx context.Context, mpiConfig *mpi.AgentConfig) error {
186+
if !cs.isConnected.Load() {
187+
return errors.New("command service client not connected yet")
188+
}
189+
slog.DebugContext(ctx, "Updating agent configuration", "config", mpiConfig)
190+
191+
if mpiConfig.Log != nil {
192+
slog.DebugContext(ctx, "Updating log configuration", "log", mpiConfig.Log)
193+
logConf := config.Log{
194+
Level: config.MapConfigLogLevelToSlogLevel(mpiConfig.Log.LogLevel),
195+
Path: mpiConfig.Log.LogPath,
196+
}
197+
cs.agentConfig.Log = &logConf
198+
199+
// Reinitialize logger with new configuration
200+
slogger := logger.New(
201+
cs.agentConfig.Log.Path,
202+
cs.agentConfig.Log.Level,
203+
)
204+
slog.SetDefault(slogger)
205+
}
206+
207+
return nil
208+
}
209+
210+
// Subscribe to the Management Plane for incoming commands.
185211
func (cs *CommandService) Subscribe(ctx context.Context) {
186212
commonSettings := &config.BackOff{
187213
InitialInterval: cs.agentConfig.Client.Backoff.InitialInterval,

internal/command/command_service_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,39 @@ func TestCommandService_SendDataPlaneResponse_configApplyRequest(t *testing.T) {
377377
wg.Wait()
378378
}
379379

380+
func TestCommandService_UpdateAgentConfiguration(t *testing.T) {
381+
ctx := context.Background()
382+
commandServiceClient := &v1fakes.FakeCommandServiceClient{}
383+
384+
initialConfig := types.AgentConfig()
385+
initialConfig.Log.Level = "INFO"
386+
initialConfig.Log.Path = ""
387+
388+
commandService := NewCommandService(
389+
commandServiceClient,
390+
initialConfig,
391+
make(chan *mpi.ManagementPlaneRequest),
392+
)
393+
commandService.isConnected.Store(true)
394+
395+
originalLogger := slog.Default()
396+
397+
updatedConfig := &mpi.AgentConfig{
398+
Log: &mpi.Log{
399+
LogLevel: mpi.Log_LOG_LEVEL_DEBUG,
400+
LogPath: "/etc/nginx-agent",
401+
},
402+
}
403+
404+
err := commandService.UpdateAgentConfiguration(ctx, updatedConfig)
405+
require.NoError(t, err)
406+
require.Equal(t, "DEBUG", commandService.agentConfig.Log.Level)
407+
require.Equal(t, "/etc/nginx-agent", commandService.agentConfig.Log.Path)
408+
409+
updatedLogger := slog.Default()
410+
require.NotEqual(t, originalLogger, updatedLogger)
411+
}
412+
380413
func TestCommandService_isValidRequest(t *testing.T) {
381414
ctx := context.Background()
382415
commandServiceClient := &v1fakes.FakeCommandServiceClient{}

0 commit comments

Comments
 (0)