Skip to content

Commit

Permalink
Cleanup the Prometheus config
Browse files Browse the repository at this point in the history
Signed-off-by: FlamingSaint <raghuramkannan400@gmail.com>
  • Loading branch information
FlamingSaint committed Jul 8, 2024
1 parent 31e8ef9 commit b36ef9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion plugin/metrics/prometheus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ func (f *Factory) Initialize(logger *zap.Logger) error {

// CreateMetricsReader implements storage.MetricsFactory.
func (f *Factory) CreateMetricsReader() (metricsstore.Reader, error) {
return prometheusstore.NewMetricsReader(f.options.Primary.Configuration, f.logger, f.tracer)
return prometheusstore.NewMetricsReader(f.options.Configuration, f.logger, f.tracer)
}
28 changes: 14 additions & 14 deletions plugin/metrics/prometheus/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func TestPrometheusFactory(t *testing.T) {
f := NewFactory()
require.NoError(t, f.Initialize(zap.NewNop()))
assert.NotNil(t, f.logger)
assert.Equal(t, "prometheus", f.options.Primary.namespace)
assert.Equal(t, "prometheus", f.options.namespace)

listener, err := net.Listen("tcp", "localhost:")
require.NoError(t, err)
assert.NotNil(t, listener)
defer listener.Close()

f.options.Primary.ServerURL = "http://" + listener.Addr().String()
f.options.ServerURL = "http://" + listener.Addr().String()
reader, err := f.CreateMetricsReader()

require.NoError(t, err)
Expand All @@ -50,11 +50,11 @@ func TestPrometheusFactory(t *testing.T) {

func TestWithDefaultConfiguration(t *testing.T) {
f := NewFactory()
assert.Equal(t, "http://localhost:9090", f.options.Primary.ServerURL)
assert.Equal(t, 30*time.Second, f.options.Primary.ConnectTimeout)
assert.Equal(t, "http://localhost:9090", f.options.ServerURL)
assert.Equal(t, 30*time.Second, f.options.ConnectTimeout)

assert.Empty(t, f.options.Primary.MetricNamespace)
assert.Equal(t, "ms", f.options.Primary.LatencyUnit)
assert.Empty(t, f.options.MetricNamespace)
assert.Equal(t, "ms", f.options.LatencyUnit)
}

func TestWithConfiguration(t *testing.T) {
Expand All @@ -69,10 +69,10 @@ func TestWithConfiguration(t *testing.T) {
})
require.NoError(t, err)
f.InitFromViper(v, zap.NewNop())
assert.Equal(t, "http://localhost:1234", f.options.Primary.ServerURL)
assert.Equal(t, 5*time.Second, f.options.Primary.ConnectTimeout)
assert.Equal(t, "test/test_file.txt", f.options.Primary.TokenFilePath)
assert.False(t, f.options.Primary.TokenOverrideFromContext)
assert.Equal(t, "http://localhost:1234", f.options.ServerURL)
assert.Equal(t, 5*time.Second, f.options.ConnectTimeout)
assert.Equal(t, "test/test_file.txt", f.options.TokenFilePath)
assert.False(t, f.options.TokenOverrideFromContext)
})
t.Run("with space in token file path", func(t *testing.T) {
f := NewFactory()
Expand All @@ -82,7 +82,7 @@ func TestWithConfiguration(t *testing.T) {
})
require.NoError(t, err)
f.InitFromViper(v, zap.NewNop())
assert.Equal(t, "test/ test file.txt", f.options.Primary.TokenFilePath)
assert.Equal(t, "test/ test file.txt", f.options.TokenFilePath)
})
t.Run("with custom configuration of prometheus.query", func(t *testing.T) {
f := NewFactory()
Expand All @@ -93,8 +93,8 @@ func TestWithConfiguration(t *testing.T) {
})
require.NoError(t, err)
f.InitFromViper(v, zap.NewNop())
assert.Equal(t, "mynamespace", f.options.Primary.MetricNamespace)
assert.Equal(t, "ms", f.options.Primary.LatencyUnit)
assert.Equal(t, "mynamespace", f.options.MetricNamespace)
assert.Equal(t, "ms", f.options.LatencyUnit)
})
t.Run("with invalid prometheus.query.duration-unit", func(t *testing.T) {
defer func() {
Expand All @@ -110,7 +110,7 @@ func TestWithConfiguration(t *testing.T) {
})
require.NoError(t, err)
f.InitFromViper(v, zap.NewNop())
require.Empty(t, f.options.Primary.LatencyUnit)
require.Empty(t, f.options.LatencyUnit)
})
}

Expand Down
22 changes: 8 additions & 14 deletions plugin/metrics/prometheus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ const (
defaultNormalizeDuration = false
)

type namespaceConfig struct {
config.Configuration `mapstructure:",squash"`
namespace string
}

// Options stores the configuration entries for this storage.
type Options struct {
Primary namespaceConfig `mapstructure:",squash"`
config.Configuration `mapstructure:",squash"`
namespace string
}

// NewOptions creates a new Options struct.
Expand All @@ -71,16 +67,14 @@ func NewOptions(primaryNamespace string) *Options {
}

return &Options{
Primary: namespaceConfig{
Configuration: defaultConfig,
namespace: primaryNamespace,
},
Configuration: defaultConfig,
namespace: primaryNamespace,
}
}

// AddFlags from this storage to the CLI.
func (opt *Options) AddFlags(flagSet *flag.FlagSet) {
nsConfig := &opt.Primary
nsConfig := opt
flagSet.String(nsConfig.namespace+suffixServerURL, defaultServerURL,
"The Prometheus server's URL, must include the protocol scheme e.g. http://localhost:9090")
flagSet.Duration(nsConfig.namespace+suffixConnectTimeout, defaultConnectTimeout,
Expand Down Expand Up @@ -113,7 +107,7 @@ func (opt *Options) AddFlags(flagSet *flag.FlagSet) {

// InitFromViper initializes the options struct with values from Viper.
func (opt *Options) InitFromViper(v *viper.Viper) error {
cfg := &opt.Primary
cfg := opt
cfg.ServerURL = stripWhiteSpace(v.GetString(cfg.namespace + suffixServerURL))
cfg.ConnectTimeout = v.GetDuration(cfg.namespace + suffixConnectTimeout)
cfg.TokenFilePath = v.GetString(cfg.namespace + suffixTokenFilePath)
Expand All @@ -137,9 +131,9 @@ func (opt *Options) InitFromViper(v *viper.Viper) error {
return nil
}

func (config *namespaceConfig) getTLSFlagsConfig() tlscfg.ClientFlagsConfig {
func (opt *Options) getTLSFlagsConfig() tlscfg.ClientFlagsConfig {
return tlscfg.ClientFlagsConfig{
Prefix: config.namespace,
Prefix: opt.namespace,
}
}

Expand Down

0 comments on commit b36ef9c

Please sign in to comment.