Skip to content

Fix Add OTel Receiver After Config Apply & Fix Reload Monitoring Old Log #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f4e1d98
Moved Config parser to data source, stop instance watcher during conf…
aphralG Apr 23, 2025
975834b
Moved Config parser to data source, stop instance watcher during conf…
aphralG Apr 23, 2025
d54b145
change resource plugin to parse config
aphralG Apr 24, 2025
37c27b1
clean up
aphralG Apr 24, 2025
23feefa
move logger creation
aphralG Apr 24, 2025
f743035
added comments for context
aphralG Apr 24, 2025
2458c87
added comments for context
aphralG Apr 24, 2025
5e9d5e1
fix unit tests
aphralG Apr 24, 2025
d6532c3
try fix tests
aphralG Apr 24, 2025
65a1159
try fix tests
aphralG Apr 24, 2025
6698df9
fix test maybe
aphralG Apr 24, 2025
6ef74bc
fix tests
aphralG Apr 24, 2025
c219493
fix tests
aphralG Apr 24, 2025
2425def
fix tests
aphralG Apr 24, 2025
3d27ae0
fix tests
aphralG Apr 24, 2025
7537a1b
fix tests
aphralG Apr 24, 2025
606588b
fix tests
aphralG Apr 24, 2025
30b424f
fix tests
aphralG Apr 25, 2025
94f5438
fix tests
aphralG Apr 25, 2025
63eb2d1
fix tests
aphralG Apr 25, 2025
b50260e
fix tests
aphralG Apr 25, 2025
e6e65ce
fix tests
aphralG Apr 25, 2025
ba6f453
fix tests
aphralG Apr 25, 2025
b77f4e9
fix tests
aphralG Apr 25, 2025
18d3821
add plus file
aphralG Apr 25, 2025
725e59d
add plus conf file
aphralG Apr 25, 2025
78cd6d5
Merge branch 'v3' into dynamically-add-otel-receiver-after-config-apply
aphralG Apr 25, 2025
a7d896e
add plus conf file
aphralG Apr 25, 2025
a6e00f7
add plus conf file
aphralG Apr 25, 2025
1ecca95
add plus conf file
aphralG Apr 25, 2025
467b4b1
add plus conf file
aphralG Apr 25, 2025
59d5450
add plus conf file
aphralG Apr 25, 2025
9a8b38b
undo change
aphralG Apr 25, 2025
aa6c683
undo change
aphralG Apr 25, 2025
757168f
PR feedback
aphralG Apr 29, 2025
a58ddf8
Merge branch 'v3' into dynamically-add-otel-receiver-after-config-apply
aphralG Apr 29, 2025
3d6db7a
clean up
aphralG Apr 29, 2025
a662ad7
Pr feedback
aphralG Apr 29, 2025
7a94c34
pr feedback
aphralG Apr 29, 2025
dedec6b
merge v3
aphralG Apr 29, 2025
0cca25c
fix tests
aphralG Apr 29, 2025
262d353
fix tests
aphralG Apr 29, 2025
e4dc0d2
fix tests
aphralG Apr 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pr feedback
  • Loading branch information
aphralG committed Apr 29, 2025
commit a662ad7c15e848d84a6e80ba48f01d7794a9a972
20 changes: 12 additions & 8 deletions internal/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,20 @@ type ConfigApplySuccess struct {
// Complexity is 11, allowed is 10
// nolint: revive, cyclop
func (ncc *NginxConfigContext) Equal(otherNginxConfigContext *NginxConfigContext) bool {
if ncc.StubStatus.URL != otherNginxConfigContext.StubStatus.URL || ncc.StubStatus.Listen !=
otherNginxConfigContext.StubStatus.Listen || ncc.StubStatus.Location !=
otherNginxConfigContext.StubStatus.Location {
return false
if ncc.StubStatus != nil && otherNginxConfigContext.StubStatus != nil {
if ncc.StubStatus.URL != otherNginxConfigContext.StubStatus.URL || ncc.StubStatus.Listen !=
otherNginxConfigContext.StubStatus.Listen || ncc.StubStatus.Location !=
otherNginxConfigContext.StubStatus.Location {
return false
}
}

if ncc.PlusAPI.URL != otherNginxConfigContext.PlusAPI.URL || ncc.PlusAPI.Listen !=
otherNginxConfigContext.PlusAPI.Listen || ncc.PlusAPI.Location !=
otherNginxConfigContext.PlusAPI.Location {
return false
if ncc.PlusAPI != nil && otherNginxConfigContext.PlusAPI != nil {
if ncc.PlusAPI.URL != otherNginxConfigContext.PlusAPI.URL || ncc.PlusAPI.Listen !=
otherNginxConfigContext.PlusAPI.Listen || ncc.PlusAPI.Location !=
otherNginxConfigContext.PlusAPI.Location {
return false
}
}

if ncc.InstanceID != otherNginxConfigContext.InstanceID {
Expand Down
5 changes: 5 additions & 0 deletions internal/model/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func TestNginxConfigContext_Equal(t *testing.T) {
nginxConfigContextWithDifferentErrorLogs := *nginxConfigContext
nginxConfigContextWithDifferentErrorLogs.ErrorLogs = []*ErrorLog{}

nginxConfigContextWithNilValues := *nginxConfigContext
nginxConfigContextWithNilValues.StubStatus = nil
nginxConfigContextWithNilValues.PlusAPI = nil

assert.True(t, nginxConfigContext.Equal(&nginxConfigContextWithSameValues))
assert.False(t, nginxConfigContext.Equal(&nginxConfigContextWithDifferentStubStatus))
assert.False(t, nginxConfigContext.Equal(&nginxConfigContextWithDifferentPlusAPI))
Expand All @@ -89,4 +93,5 @@ func TestNginxConfigContext_Equal(t *testing.T) {
assert.False(t, nginxConfigContext.Equal(&nginxConfigContextWithDifferentFileHashes))
assert.False(t, nginxConfigContext.Equal(&nginxConfigContextWithDifferentAccessLogs))
assert.False(t, nginxConfigContext.Equal(&nginxConfigContextWithDifferentErrorLogs))
assert.True(t, nginxConfigContext.Equal(&nginxConfigContextWithNilValues))
}
47 changes: 21 additions & 26 deletions internal/watcher/instance/instance_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,33 @@ func (iw *InstanceWatcherService) Watch(
func (iw *InstanceWatcherService) ReparseConfigs(ctx context.Context) {
slog.DebugContext(ctx, "Reparsing all instance configurations")
for _, instance := range iw.instanceCache {
iw.ReparseConfig(ctx, instance.GetInstanceMeta().GetInstanceId(), &model.NginxConfigContext{})
slog.DebugContext(
ctx,
"Reparsing NGINX instance config",
"instance_id", instance.GetInstanceMeta().GetInstanceId(),
)

nginxConfigContext, parseErr := iw.nginxConfigParser.Parse(ctx, instance)
if parseErr != nil {
slog.ErrorContext(
ctx,
"Failed to parse NGINX instance config",
"config_path", instance.GetInstanceRuntime().GetConfigPath(),
"instance_id", instance.GetInstanceMeta().GetInstanceId(),
"error", parseErr,
)

return
}

iw.HandleNginxConfigContextUpdate(ctx, instance.GetInstanceMeta().GetInstanceId(), nginxConfigContext)
}
slog.DebugContext(ctx, "Finished reparsing all instance configurations")
}

func (iw *InstanceWatcherService) ReparseConfig(ctx context.Context, instanceID string,
func (iw *InstanceWatcherService) HandleNginxConfigContextUpdate(ctx context.Context, instanceID string,
nginxConfigContext *model.NginxConfigContext,
) {
var parseErr error
iw.cacheMutex.Lock()
defer iw.cacheMutex.Unlock()

Expand All @@ -145,29 +163,6 @@ func (iw *InstanceWatcherService) ReparseConfig(ctx context.Context, instanceID

if instanceType == mpi.InstanceMeta_INSTANCE_TYPE_NGINX ||
instanceType == mpi.InstanceMeta_INSTANCE_TYPE_NGINX_PLUS {
// If the ReparseConfig was not triggered by a config apply that had changes there is no NginxConfigContext
// passed to this function. So we need to parse the config and create one.
if nginxConfigContext.InstanceID == "" {
slog.DebugContext(
ctx,
"Reparsing NGINX instance config",
"instance_id", instanceID,
)

nginxConfigContext, parseErr = iw.nginxConfigParser.Parse(ctx, instance)
if parseErr != nil {
slog.WarnContext(
ctx,
"Unable to parse NGINX instance config",
"config_path", instance.GetInstanceRuntime().GetConfigPath(),
"instance_id", instanceID,
"error", parseErr,
)

return
}
}

iw.sendNginxConfigContextUpdate(ctx, nginxConfigContext)
iw.nginxConfigCache[nginxConfigContext.InstanceID] = nginxConfigContext
updatesRequired = proto.UpdateNginxInstanceRuntime(instance, nginxConfigContext)
Expand Down
13 changes: 8 additions & 5 deletions internal/watcher/instance/instance_watcher_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ func TestInstanceWatcherService_ReparseConfig(t *testing.T) {
updatedInstance.GetInstanceRuntime().GetNginxRuntimeInfo().AccessLogs = []string{"access2.log"}
updatedInstance.GetInstanceRuntime().GetNginxRuntimeInfo().ErrorLogs = []string{"error.log"}

updateNginxConfigContext.InstanceID = updatedInstance.GetInstanceMeta().GetInstanceId()

tests := []struct {
parseReturns *model.NginxConfigContext
name string
Expand All @@ -275,13 +277,10 @@ func TestInstanceWatcherService_ReparseConfig(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
fakeNginxConfigParser := &instancefakes.FakeNginxConfigParser{}
fakeNginxConfigParser.ParseReturns(test.parseReturns, nil)
instanceUpdatesChannel := make(chan InstanceUpdatesMessage, 1)
nginxConfigContextChannel := make(chan NginxConfigContextMessage, 1)

instanceWatcherService := NewInstanceWatcherService(types.AgentConfig())
instanceWatcherService.nginxConfigParser = fakeNginxConfigParser
instanceWatcherService.instancesChannel = instanceUpdatesChannel
instanceWatcherService.nginxConfigContextChannel = nginxConfigContextChannel

Expand All @@ -293,12 +292,16 @@ func TestInstanceWatcherService_ReparseConfig(t *testing.T) {
instance.GetInstanceMeta().GetInstanceId(): instance,
}

instanceWatcherService.ReparseConfig(ctx, updatedInstance.GetInstanceMeta().GetInstanceId(),
&model.NginxConfigContext{})
instanceWatcherService.HandleNginxConfigContextUpdate(ctx, updatedInstance.
GetInstanceMeta().GetInstanceId(),
updateNginxConfigContext)

nginxConfigContextMessage := <-nginxConfigContextChannel
assert.Equal(t, updateNginxConfigContext, nginxConfigContextMessage.NginxConfigContext)

assert.Equal(tt, updateNginxConfigContext, instanceWatcherService.
nginxConfigCache[updatedInstance.GetInstanceMeta().GetInstanceId()])

instanceUpdatesMessage := <-instanceUpdatesChannel
assert.Len(t, instanceUpdatesMessage.InstanceUpdates.UpdatedInstances, 1)
assert.Equal(tt, updatedInstance, instanceUpdatesMessage.InstanceUpdates.UpdatedInstances[0])
Expand Down
4 changes: 2 additions & 2 deletions internal/watcher/watcher_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type (
instancesChannel chan<- instance.InstanceUpdatesMessage,
nginxConfigContextChannel chan<- instance.NginxConfigContextMessage,
)
ReparseConfig(ctx context.Context, instanceID string, configContext *model.NginxConfigContext)
HandleNginxConfigContextUpdate(ctx context.Context, instanceID string, configContext *model.NginxConfigContext)
ReparseConfigs(ctx context.Context)
SetEnabled(enabled bool)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (w *Watcher) handleConfigApplySuccess(ctx context.Context, msg *bus.Message
slog.DebugContext(ctx, "NginxConfigContext is empty, no need to reparse config")
return
}
w.instanceWatcherService.ReparseConfig(ctx, instanceID, successMessage.ConfigContext)
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, successMessage.ConfigContext)

w.watcherMutex.Lock()
w.instancesWithConfigApplyInProgress = slices.DeleteFunc(
Expand Down
2 changes: 1 addition & 1 deletion internal/watcher/watcher_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestWatcher_Process_ConfigApplySuccessfulTopic(t *testing.T) {

watcherPlugin.Process(ctx, message)

assert.Equal(t, 1, fakeWatcherService.ReparseConfigCallCount())
assert.Equal(t, 1, fakeWatcherService.HandleNginxConfigContextUpdateCallCount())
assert.Empty(t, watcherPlugin.instancesWithConfigApplyInProgress)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion test/integration/grpc_management_plane_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ func TestGrpc_ConfigApply(t *testing.T) {

performConfigApply(t, nginxInstanceID)

time.Sleep(5 * time.Second)
responses = getManagementPlaneResponses(t, 2)
t.Logf("Config apply responses: %v", responses)

Expand Down