From 2a25eb3829ea8674a99f1785b6a267621b58bd35 Mon Sep 17 00:00:00 2001 From: Joel Takvorian Date: Fri, 16 Feb 2024 17:55:40 +0100 Subject: [PATCH] Fix yaml unmarshaling of inlined config --- pkg/config/config.go | 10 +++++----- pkg/config/config_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 3d37c3f72..ea10e2045 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -55,11 +55,11 @@ type Profile struct { // Also, currently FLP doesn't support defining more than one PromEncode stage. If this feature is added later, these global settings // will help configuring common setting for all PromEncode stages - PromEncode settings would then act as overrides. type MetricsSettings struct { - api.PromConnectionInfo - DisableGlobalServer bool `yaml:"disableGlobalServer,omitempty" json:"disableGlobalServer,omitempty" doc:"disabling the global metrics server makes operational metrics unavailable. If prometheus-encoding stages are defined, they need to contain their own metrics server parameters."` - Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" doc:"prefix for names of the operational metrics"` - NoPanic bool `yaml:"noPanic,omitempty" json:"noPanic,omitempty"` - SuppressGoMetrics bool `yaml:"suppressGoMetrics,omitempty" json:"suppressGoMetrics,omitempty" doc:"filter out Go and process metrics"` + api.PromConnectionInfo `yaml:",inline"` + DisableGlobalServer bool `yaml:"disableGlobalServer,omitempty" json:"disableGlobalServer,omitempty" doc:"disabling the global metrics server makes operational metrics unavailable. If prometheus-encoding stages are defined, they need to contain their own metrics server parameters."` + Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" doc:"prefix for names of the operational metrics"` + NoPanic bool `yaml:"noPanic,omitempty" json:"noPanic,omitempty"` + SuppressGoMetrics bool `yaml:"suppressGoMetrics,omitempty" json:"suppressGoMetrics,omitempty" doc:"filter out Go and process metrics"` } // PerfSettings allows setting some internal configuration parameters diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index ce9b7ac3a..605553a1a 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -18,9 +18,11 @@ package config import ( + "encoding/json" "testing" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" ) func TestJsonUnmarshalStrict(t *testing.T) { @@ -39,3 +41,17 @@ func TestJsonUnmarshalStrict(t *testing.T) { err = JsonUnmarshalStrict([]byte(msg), &actualMsg) require.Error(t, err) } + +func TestUnmarshalInline(t *testing.T) { + cfg := `{"metricsSettings":{"port":9102,"prefix":"netobserv_"}}` + var cfs ConfigFileStruct + err := yaml.Unmarshal([]byte(cfg), &cfs) + require.NoError(t, err) + require.Equal(t, "netobserv_", cfs.MetricsSettings.Prefix) + require.Equal(t, 9102, cfs.MetricsSettings.PromConnectionInfo.Port) + + err = json.Unmarshal([]byte(cfg), &cfs) + require.NoError(t, err) + require.Equal(t, "netobserv_", cfs.MetricsSettings.Prefix) + require.Equal(t, 9102, cfs.MetricsSettings.PromConnectionInfo.Port) +}