Skip to content

Commit

Permalink
Merge branch 'main' into perf-hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Sep 21, 2023
2 parents d88b7d8 + 97eda9a commit 482674c
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 61 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {

binary := core.NewNginxBinary(env, loadedConfig)

corePlugins, extensionPlugins := plugins.LoadPlugins(commander, binary, env, reporter, loadedConfig)
corePlugins, extensionPlugins := plugins.LoadPlugins(commander, binary, env, reporter, loadedConfig, eventMeta)

pipe := core.InitializePipe(ctx, corePlugins, extensionPlugins, agent_config.DefaultPluginSize)

Expand Down
14 changes: 9 additions & 5 deletions src/core/metrics/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ func SaveCollections(metricsCollections Collections, reports ...*proto.MetricsRe
}
}

for _, simpleMetric := range stats.Simplemetrics {
if metrics, ok := metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name]; ok {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = metrics + simpleMetric.GetValue()
} else {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = simpleMetric.GetValue()
simpleMetrics := stats.GetSimplemetrics()

if simpleMetrics != nil {
for _, simpleMetric := range simpleMetrics {
if metrics, ok := metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name]; ok {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = metrics + simpleMetric.GetValue()
} else {
metricsCollections.Data[dimensionsChecksum].RunningSumMap[simpleMetric.Name] = simpleMetric.GetValue()
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
log "github.com/sirupsen/logrus"

agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/agent/events"

sdkGRPC "github.com/nginx/agent/sdk/v2/grpc"

"github.com/google/uuid"
)

func LoadPlugins(commander client.Commander, binary core.NginxBinary, env core.Environment, reporter client.MetricReporter, loadedConfig *config.Config) ([]core.Plugin, []core.ExtensionPlugin) {
func LoadPlugins(commander client.Commander, binary core.NginxBinary, env core.Environment, reporter client.MetricReporter, loadedConfig *config.Config, agentEventsMeta *events.AgentEventMeta) ([]core.Plugin, []core.ExtensionPlugin) {
var corePlugins []core.Plugin
var extensionPlugins []core.ExtensionPlugin

Expand All @@ -41,7 +42,7 @@ func LoadPlugins(commander client.Commander, binary core.NginxBinary, env core.E
NewConfigReader(loadedConfig),
NewNginx(commander, binary, env, loadedConfig),
NewExtensions(loadedConfig, env),
NewFeatures(commander, loadedConfig, env, binary, loadedConfig.Version),
NewFeatures(commander, loadedConfig, env, binary, loadedConfig.Version, agentEventsMeta),
)

if loadedConfig.IsFeatureEnabled(agent_config.FeatureRegistration) {
Expand All @@ -66,7 +67,7 @@ func LoadPlugins(commander client.Commander, binary core.NginxBinary, env core.E
}

if loadedConfig.IsFeatureEnabled(agent_config.FeatureActivityEvents) {
corePlugins = append(corePlugins, NewEvents(loadedConfig, env, sdkGRPC.NewMessageMeta(uuid.NewString()), binary))
corePlugins = append(corePlugins, NewEvents(loadedConfig, env, sdkGRPC.NewMessageMeta(uuid.NewString()), binary, agentEventsMeta))
}

if loadedConfig.AgentAPI.Port != 0 && loadedConfig.IsFeatureEnabled(agent_config.FeatureAgentAPI) {
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

sdk "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/agent/events"
"github.com/nginx/agent/v2/src/core/config"
tutils "github.com/nginx/agent/v2/test/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -57,7 +58,17 @@ func TestLoadPlugins(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
corePlugins, extensionPlugins := LoadPlugins(cmdr, binary, env, reporter, tt.loadedConfig)
corePlugins, extensionPlugins := LoadPlugins(cmdr, binary, env,
reporter,
tt.loadedConfig,
events.NewAgentEventMeta(
"NGINX-AGENT",
"v0.0.1",
"75231",
"test-host",
"12345678",
"group-a",
[]string{"tag-a", "tag-b"}))

assert.NotNil(t, corePlugins)
assert.Len(t, corePlugins, tt.expectedPluginSize)
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ type Events struct {
agentEventsMeta *events.AgentEventMeta
}

func NewEvents(conf *config.Config, env core.Environment, meta *proto.Metadata, nginxBinary core.NginxBinary) *Events {
func NewEvents(conf *config.Config, env core.Environment, meta *proto.Metadata, nginxBinary core.NginxBinary, agentEventsMeta *events.AgentEventMeta) *Events {
return &Events{
conf: conf,
env: env,
meta: meta,
nginxBinary: nginxBinary,
conf: conf,
env: env,
meta: meta,
nginxBinary: nginxBinary,
agentEventsMeta: agentEventsMeta,
}
}

Expand Down Expand Up @@ -119,7 +120,6 @@ func (a *Events) sendAgentStartedEvent(msg *core.Message) {

log.Debugf("Created event: %v", event)
a.pipeline.Process(core.NewMessage(core.Events, event))
a.agentEventsMeta = agentEventMeta
}

func (a *Events) sendNingxFoundEvent(msg *core.Message) {
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,20 @@ func TestActivityEvents_Process(t *testing.T) {
},
}

pluginUnderTest := NewEvents(config, env, grpc.NewMessageMeta(uuid.New().String()), core.NewNginxBinary(env, config))
pluginUnderTest := NewEvents(
config,
env,
grpc.NewMessageMeta(uuid.New().String()),
core.NewNginxBinary(env, config),
events.NewAgentEventMeta(
"test-agent",
"v0.0.1",
"75231",
"test-host",
"12345678",
"group-a",
[]string{"tag-a", "tag-b"},
))
messagePipe := core.SetupMockMessagePipe(t, ctx, []core.Plugin{pluginUnderTest}, []core.ExtensionPlugin{})

if test.name != "test AgentStart message" {
Expand Down
31 changes: 17 additions & 14 deletions src/plugins/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package plugins

import (
agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/agent/events"
"github.com/nginx/agent/sdk/v2/client"
sdkGRPC "github.com/nginx/agent/sdk/v2/grpc"
"github.com/nginx/agent/v2/src/core"
Expand All @@ -19,22 +20,24 @@ import (
)

type Features struct {
commander client.Commander
pipeline core.MessagePipeInterface
conf *config.Config
env core.Environment
binary core.NginxBinary
version string
featureMap map[string]func(data string) []core.Plugin
commander client.Commander
pipeline core.MessagePipeInterface
conf *config.Config
env core.Environment
binary core.NginxBinary
version string
featureMap map[string]func(data string) []core.Plugin
agentEventsMeta *events.AgentEventMeta
}

func NewFeatures(commander client.Commander, conf *config.Config, env core.Environment, binary core.NginxBinary, version string) *Features {
func NewFeatures(commander client.Commander, conf *config.Config, env core.Environment, binary core.NginxBinary, version string, agentEventsMeta *events.AgentEventMeta) *Features {
return &Features{
commander: commander,
conf: conf,
env: env,
binary: binary,
version: version,
commander: commander,
conf: conf,
env: env,
binary: binary,
version: version,
agentEventsMeta: agentEventsMeta,
}
}

Expand Down Expand Up @@ -255,7 +258,7 @@ func (f *Features) enableActivityEventsFeature(data string) []core.Plugin {
}
f.conf = conf

events := NewEvents(f.conf, f.env, sdkGRPC.NewMessageMeta(uuid.NewString()), f.binary)
events := NewEvents(f.conf, f.env, sdkGRPC.NewMessageMeta(uuid.NewString()), f.binary, f.agentEventsMeta)

return []core.Plugin{events}
}
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/agent/events"
"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
Expand Down Expand Up @@ -96,7 +97,15 @@ func TestFeatures_Process(t *testing.T) {

configuration, _ := config.GetConfig("1234")

pluginUnderTest := NewFeatures(cmdr, configuration, env, binary, "agentVersion")
pluginUnderTest := NewFeatures(cmdr, configuration, env, binary, "agentVersion", events.NewAgentEventMeta(
config.MODULE,
"v0.0.1",
"75231",
"test-host",
"12345678",
"group-a",
[]string{"tag-a", "tag-b"},
))

for _, tc := range testCases {
messagePipe := core.SetupMockMessagePipe(t, ctx, []core.Plugin{pluginUnderTest}, []core.ExtensionPlugin{})
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (fw *FileWatcher) addWatcher(info os.FileInfo, path string) (err error) {
if info == nil {
info, err = fw.env.FileStat(path)
if err != nil {
if os.IsNotExist(err) {
log.Debugf("Unable to add file watcher for %v as file doesn't exist: %v", path, err)
return nil
}
log.Warnf("Error unable to add file watcher for %v : %v", path, err)
return err
}
Expand Down Expand Up @@ -189,6 +193,7 @@ func (fw *FileWatcher) watchLoop() {
if event == emptyEvent ||
event.Name == "" ||
strings.HasSuffix(event.Name, ".swp") ||
strings.HasSuffix(event.Name, ".swx") ||
strings.HasSuffix(event.Name, "~") {
log.Tracef("Skipping FSNotify EVENT! %v\n", event)
continue
Expand Down
13 changes: 12 additions & 1 deletion test/performance/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sdk "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/agent/events"
"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
Expand Down Expand Up @@ -170,7 +171,17 @@ func BenchmarkFeaturesExtensionsAndPlugins(b *testing.B) {
for i := 0; i < b.N; i++ {
b.ResetTimer()
controller, cmdr, reporter := core.CreateGrpcClients(ctx, tt.loadedConfig)
corePlugins, extensionPlugins = plugins.LoadPlugins(cmdr, binary, env, reporter, tt.loadedConfig)
corePlugins, extensionPlugins = plugins.LoadPlugins(cmdr, binary, env, reporter, tt.loadedConfig,
events.NewAgentEventMeta(
"NGINX-AGENT",
"v0.0.1",
"75231",
"test-host",
"12345678",
"group-a",
[]string{"tag-a", "tag-b"},
),
)
pipe = core.InitializePipe(ctx, corePlugins, extensionPlugins, 20)
core.HandleSignals(ctx, cmdr, tt.loadedConfig, env, pipe, cancel, controller)
}
Expand Down

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

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

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

Loading

0 comments on commit 482674c

Please sign in to comment.