From 71183434a3b61b9bdb17cdc82fa798ee50413ea8 Mon Sep 17 00:00:00 2001 From: "o.omahony" Date: Mon, 10 Oct 2022 14:55:06 +0100 Subject: [PATCH] client config version via configuration file --- src/core/config/config.go | 4 ++++ src/core/config/defaults.go | 2 ++ src/core/config/types.go | 1 + src/core/metrics/collectors/nginx.go | 2 +- src/core/metrics/metrics_util.go | 1 + src/core/metrics/sources/nginx_plus.go | 13 +++++++------ src/core/metrics/sources/nginx_plus_test.go | 4 ++-- src/plugins/metrics.go | 1 + test/component/advanced_metrics_component_test.go | 2 +- .../nginx/agent/v2/src/core/config/config.go | 4 ++++ .../nginx/agent/v2/src/core/config/defaults.go | 2 ++ .../nginx/agent/v2/src/core/config/types.go | 1 + .../agent/v2/src/core/metrics/collectors/nginx.go | 2 +- .../nginx/agent/v2/src/core/metrics/metrics_util.go | 1 + .../agent/v2/src/core/metrics/sources/nginx_plus.go | 13 +++++++------ .../nginx/agent/v2/src/plugins/metrics.go | 1 + 16 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/core/config/config.go b/src/core/config/config.go index cdc988afc..9e7d36007 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -67,6 +67,9 @@ func SetDefaults() { Viper.SetDefault(MetricsBulkSize, Defaults.AgentMetrics.BulkSize) Viper.SetDefault(MetricsReportInterval, Defaults.AgentMetrics.ReportInterval) Viper.SetDefault(MetricsCollectionInterval, Defaults.AgentMetrics.CollectionInterval) + + // NGINX DEFAULTS + Viper.SetDefault(NginxClientVersion, Defaults.Nginx.NginxClientVersion) } func SetAdvancedMetricsDefaults() { @@ -304,6 +307,7 @@ func getNginx() Nginx { ExcludeLogs: Viper.GetString(NginxExcludeLogs), Debug: Viper.GetBool(NginxDebug), NginxCountingSocket: Viper.GetString(NginxCountingSocket), + NginxClientVersion: Viper.GetInt(NginxClientVersion), } } diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 2584c8b0d..11ec40568 100644 --- a/src/core/config/defaults.go +++ b/src/core/config/defaults.go @@ -47,6 +47,7 @@ var ( Nginx: Nginx{ Debug: false, NginxCountingSocket: "unix:/var/run/nginx-agent/nginx.sock", + NginxClientVersion: 6, }, ConfigDirs: "/etc/nginx:/usr/local/etc/nginx:/usr/share/nginx/modules:/etc/nms", AllowedDirectoriesMap: map[string]struct{}{}, @@ -140,6 +141,7 @@ const ( NginxExcludeLogs = NginxKey + KeyDelimiter + "exclude_logs" NginxDebug = NginxKey + KeyDelimiter + "debug" NginxCountingSocket = NginxKey + KeyDelimiter + "socket" + NginxClientVersion = NginxKey + KeyDelimiter + "client_version" // viper keys used in config DataplaneKey = "dataplane" diff --git a/src/core/config/types.go b/src/core/config/types.go index 1ceddff13..66ab795b6 100644 --- a/src/core/config/types.go +++ b/src/core/config/types.go @@ -58,6 +58,7 @@ type Nginx struct { ExcludeLogs string `mapstructure:"exclude_logs" yaml:"-"` Debug bool `mapstructure:"debug" yaml:"-"` NginxCountingSocket string `mapstructure:"socket" yaml:"-"` + NginxClientVersion int `mapstructure:"client_version" yaml:"-"` } type Dataplane struct { diff --git a/src/core/metrics/collectors/nginx.go b/src/core/metrics/collectors/nginx.go index 6efcd378f..e8bdb03ed 100644 --- a/src/core/metrics/collectors/nginx.go +++ b/src/core/metrics/collectors/nginx.go @@ -52,7 +52,7 @@ func buildSources(dimensions *metrics.CommonDim, binary core.NginxBinary, collec nginxSources = append(nginxSources, sources.NewNginxAccessLog(dimensions, sources.OSSNamespace, binary, sources.OSSNginxType, collectorConf.CollectionInterval)) nginxSources = append(nginxSources, sources.NewNginxErrorLog(dimensions, sources.OSSNamespace, binary, sources.OSSNginxType, collectorConf.CollectionInterval)) } else if collectorConf.PlusAPI != "" { - nginxSources = append(nginxSources, sources.NewNginxPlus(dimensions, sources.OSSNamespace, sources.PlusNamespace, collectorConf.PlusAPI)) + nginxSources = append(nginxSources, sources.NewNginxPlus(dimensions, sources.OSSNamespace, sources.PlusNamespace, collectorConf.PlusAPI, collectorConf.ClientVersion)) nginxSources = append(nginxSources, sources.NewNginxAccessLog(dimensions, sources.OSSNamespace, binary, sources.PlusNginxType, collectorConf.CollectionInterval)) nginxSources = append(nginxSources, sources.NewNginxErrorLog(dimensions, sources.OSSNamespace, binary, sources.PlusNginxType, collectorConf.CollectionInterval)) } else { diff --git a/src/core/metrics/metrics_util.go b/src/core/metrics/metrics_util.go index 0a96c87df..283e96286 100644 --- a/src/core/metrics/metrics_util.go +++ b/src/core/metrics/metrics_util.go @@ -35,6 +35,7 @@ type NginxCollectorConfig struct { CollectionInterval time.Duration AccessLogs []string ErrorLogs []string + ClientVersion int } func NewStatsEntity(dims []*proto.Dimension, samples []*proto.SimpleMetric) *proto.StatsEntity { diff --git a/src/core/metrics/sources/nginx_plus.go b/src/core/metrics/sources/nginx_plus.go index 058a2b0b5..2f68108f6 100644 --- a/src/core/metrics/sources/nginx_plus.go +++ b/src/core/metrics/sources/nginx_plus.go @@ -33,18 +33,19 @@ type NginxPlus struct { plusNamespace, plusAPI string // This is for keeping the previous stats. Need to report the delta. - prevStats *plusclient.Stats - init sync.Once + prevStats *plusclient.Stats + init sync.Once + clientVersion int } -func NewNginxPlus(baseDimensions *metrics.CommonDim, nginxNamespace, plusNamespace, plusAPI string) *NginxPlus { - return &NginxPlus{baseDimensions: baseDimensions, nginxNamespace: nginxNamespace, plusNamespace: plusNamespace, plusAPI: plusAPI} +func NewNginxPlus(baseDimensions *metrics.CommonDim, nginxNamespace, plusNamespace, plusAPI string, clientVersion int) *NginxPlus { + return &NginxPlus{baseDimensions: baseDimensions, nginxNamespace: nginxNamespace, plusNamespace: plusNamespace, plusAPI: plusAPI, clientVersion: clientVersion} } func (c *NginxPlus) Collect(ctx context.Context, wg *sync.WaitGroup, m chan<- *proto.StatsEntity) { defer wg.Done() c.init.Do(func() { - cl, err := plusclient.NewNginxClient(&http.Client{}, c.plusAPI) + cl, err := plusclient.NewNginxClientWithVersion(&http.Client{}, c.plusAPI, c.clientVersion) if err != nil { log.Errorf("Failed to create plus metrics client: %v", err) SendNginxDownStatus(ctx, c.baseDimensions.ToDimensions(), m) @@ -59,7 +60,7 @@ func (c *NginxPlus) Collect(ctx context.Context, wg *sync.WaitGroup, m chan<- *p } }) - cl, err := plusclient.NewNginxClient(&http.Client{}, c.plusAPI) + cl, err := plusclient.NewNginxClientWithVersion(&http.Client{}, c.plusAPI, c.clientVersion) if err != nil { log.Errorf("Failed to create plus metrics client: %v", err) SendNginxDownStatus(ctx, c.baseDimensions.ToDimensions(), m) diff --git a/src/core/metrics/sources/nginx_plus_test.go b/src/core/metrics/sources/nginx_plus_test.go index f9179cd70..49631fe3e 100644 --- a/src/core/metrics/sources/nginx_plus_test.go +++ b/src/core/metrics/sources/nginx_plus_test.go @@ -404,7 +404,7 @@ func (f *FakeNginxPlus) Collect(ctx context.Context, wg *sync.WaitGroup, m chan< } func TestNginxPlusUpdate(t *testing.T) { - nginxPlus := NewNginxPlus(&metrics.CommonDim{}, "test", PlusNamespace, "http://localhost:8080/api") + nginxPlus := NewNginxPlus(&metrics.CommonDim{}, "test", PlusNamespace, "http://localhost:8080/api", 6) assert.Equal(t, "", nginxPlus.baseDimensions.InstanceTags) assert.Equal(t, "http://localhost:8080/api", nginxPlus.plusAPI) @@ -643,7 +643,7 @@ func TestNginxPlus_Collect(t *testing.T) { for _, test := range tests { ctx := context.TODO() - f := &FakeNginxPlus{NewNginxPlus(test.baseDimensions, "nginx", "plus", "")} + f := &FakeNginxPlus{NewNginxPlus(test.baseDimensions, "nginx", "plus", "", 6)} wg := &sync.WaitGroup{} wg.Add(1) go f.Collect(ctx, wg, test.m) diff --git a/src/plugins/metrics.go b/src/plugins/metrics.go index d5f5db688..0a9fa0a14 100644 --- a/src/plugins/metrics.go +++ b/src/plugins/metrics.go @@ -317,6 +317,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina AccessLogs: sdk.GetAccessLogs(accessLogs), ErrorLogs: sdk.GetErrorLogs(errorLogs), NginxId: detail.NginxId, + ClientVersion: config.Nginx.NginxClientVersion, } } diff --git a/test/component/advanced_metrics_component_test.go b/test/component/advanced_metrics_component_test.go index fa3f1ca14..ffd02bb74 100644 --- a/test/component/advanced_metrics_component_test.go +++ b/test/component/advanced_metrics_component_test.go @@ -775,7 +775,7 @@ func toMessage(data ...interface{}) []byte { func assertMessageSent(t *testing.T, addr string, dataToSend [][]byte) { numberOfRetries := 3 conn, err := net.Dial("unix", addr) - + // if net.Dial fails connection will be retried 3 times for i := 0; i <= numberOfRetries && err != nil; i++ { time.Sleep(5 * time.Millisecond) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go index cdc988afc..9e7d36007 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go @@ -67,6 +67,9 @@ func SetDefaults() { Viper.SetDefault(MetricsBulkSize, Defaults.AgentMetrics.BulkSize) Viper.SetDefault(MetricsReportInterval, Defaults.AgentMetrics.ReportInterval) Viper.SetDefault(MetricsCollectionInterval, Defaults.AgentMetrics.CollectionInterval) + + // NGINX DEFAULTS + Viper.SetDefault(NginxClientVersion, Defaults.Nginx.NginxClientVersion) } func SetAdvancedMetricsDefaults() { @@ -304,6 +307,7 @@ func getNginx() Nginx { ExcludeLogs: Viper.GetString(NginxExcludeLogs), Debug: Viper.GetBool(NginxDebug), NginxCountingSocket: Viper.GetString(NginxCountingSocket), + NginxClientVersion: Viper.GetInt(NginxClientVersion), } } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go index 2584c8b0d..11ec40568 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go @@ -47,6 +47,7 @@ var ( Nginx: Nginx{ Debug: false, NginxCountingSocket: "unix:/var/run/nginx-agent/nginx.sock", + NginxClientVersion: 6, }, ConfigDirs: "/etc/nginx:/usr/local/etc/nginx:/usr/share/nginx/modules:/etc/nms", AllowedDirectoriesMap: map[string]struct{}{}, @@ -140,6 +141,7 @@ const ( NginxExcludeLogs = NginxKey + KeyDelimiter + "exclude_logs" NginxDebug = NginxKey + KeyDelimiter + "debug" NginxCountingSocket = NginxKey + KeyDelimiter + "socket" + NginxClientVersion = NginxKey + KeyDelimiter + "client_version" // viper keys used in config DataplaneKey = "dataplane" diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go index 1ceddff13..66ab795b6 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go @@ -58,6 +58,7 @@ type Nginx struct { ExcludeLogs string `mapstructure:"exclude_logs" yaml:"-"` Debug bool `mapstructure:"debug" yaml:"-"` NginxCountingSocket string `mapstructure:"socket" yaml:"-"` + NginxClientVersion int `mapstructure:"client_version" yaml:"-"` } type Dataplane struct { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/collectors/nginx.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/collectors/nginx.go index 6efcd378f..e8bdb03ed 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/collectors/nginx.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/collectors/nginx.go @@ -52,7 +52,7 @@ func buildSources(dimensions *metrics.CommonDim, binary core.NginxBinary, collec nginxSources = append(nginxSources, sources.NewNginxAccessLog(dimensions, sources.OSSNamespace, binary, sources.OSSNginxType, collectorConf.CollectionInterval)) nginxSources = append(nginxSources, sources.NewNginxErrorLog(dimensions, sources.OSSNamespace, binary, sources.OSSNginxType, collectorConf.CollectionInterval)) } else if collectorConf.PlusAPI != "" { - nginxSources = append(nginxSources, sources.NewNginxPlus(dimensions, sources.OSSNamespace, sources.PlusNamespace, collectorConf.PlusAPI)) + nginxSources = append(nginxSources, sources.NewNginxPlus(dimensions, sources.OSSNamespace, sources.PlusNamespace, collectorConf.PlusAPI, collectorConf.ClientVersion)) nginxSources = append(nginxSources, sources.NewNginxAccessLog(dimensions, sources.OSSNamespace, binary, sources.PlusNginxType, collectorConf.CollectionInterval)) nginxSources = append(nginxSources, sources.NewNginxErrorLog(dimensions, sources.OSSNamespace, binary, sources.PlusNginxType, collectorConf.CollectionInterval)) } else { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/metrics_util.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/metrics_util.go index 0a96c87df..283e96286 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/metrics_util.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/metrics_util.go @@ -35,6 +35,7 @@ type NginxCollectorConfig struct { CollectionInterval time.Duration AccessLogs []string ErrorLogs []string + ClientVersion int } func NewStatsEntity(dims []*proto.Dimension, samples []*proto.SimpleMetric) *proto.StatsEntity { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/nginx_plus.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/nginx_plus.go index 058a2b0b5..2f68108f6 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/nginx_plus.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/nginx_plus.go @@ -33,18 +33,19 @@ type NginxPlus struct { plusNamespace, plusAPI string // This is for keeping the previous stats. Need to report the delta. - prevStats *plusclient.Stats - init sync.Once + prevStats *plusclient.Stats + init sync.Once + clientVersion int } -func NewNginxPlus(baseDimensions *metrics.CommonDim, nginxNamespace, plusNamespace, plusAPI string) *NginxPlus { - return &NginxPlus{baseDimensions: baseDimensions, nginxNamespace: nginxNamespace, plusNamespace: plusNamespace, plusAPI: plusAPI} +func NewNginxPlus(baseDimensions *metrics.CommonDim, nginxNamespace, plusNamespace, plusAPI string, clientVersion int) *NginxPlus { + return &NginxPlus{baseDimensions: baseDimensions, nginxNamespace: nginxNamespace, plusNamespace: plusNamespace, plusAPI: plusAPI, clientVersion: clientVersion} } func (c *NginxPlus) Collect(ctx context.Context, wg *sync.WaitGroup, m chan<- *proto.StatsEntity) { defer wg.Done() c.init.Do(func() { - cl, err := plusclient.NewNginxClient(&http.Client{}, c.plusAPI) + cl, err := plusclient.NewNginxClientWithVersion(&http.Client{}, c.plusAPI, c.clientVersion) if err != nil { log.Errorf("Failed to create plus metrics client: %v", err) SendNginxDownStatus(ctx, c.baseDimensions.ToDimensions(), m) @@ -59,7 +60,7 @@ func (c *NginxPlus) Collect(ctx context.Context, wg *sync.WaitGroup, m chan<- *p } }) - cl, err := plusclient.NewNginxClient(&http.Client{}, c.plusAPI) + cl, err := plusclient.NewNginxClientWithVersion(&http.Client{}, c.plusAPI, c.clientVersion) if err != nil { log.Errorf("Failed to create plus metrics client: %v", err) SendNginxDownStatus(ctx, c.baseDimensions.ToDimensions(), m) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go index d5f5db688..0a9fa0a14 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go @@ -317,6 +317,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina AccessLogs: sdk.GetAccessLogs(accessLogs), ErrorLogs: sdk.GetErrorLogs(errorLogs), NginxId: detail.NginxId, + ClientVersion: config.Nginx.NginxClientVersion, } }