Skip to content

Commit

Permalink
Cleanup the Prometheus config (#5720)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Part of #5632

## Description of the changes
- Cleaned up the Prometheus config
- Removed namespaceConfig as its not needed for a metrics storage

## How was this change tested?
- `make test`

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
<s> - [ ] I have added unit tests for the new functionality </s>
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: FlamingSaint <[email protected]>
  • Loading branch information
FlamingSaint committed Jul 8, 2024
1 parent ea2ec18 commit 295293c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
2 changes: 1 addition & 1 deletion pkg/version/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ func TestString(t *testing.T) {
}
expectedOutput := "git-commit=foobar, git-version=v1.2.3, build-date=2024-01-04"
assert.Equal(t, expectedOutput, test.String())
}
}
4 changes: 2 additions & 2 deletions plugin/metrics/prometheus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Factory struct {
func NewFactory() *Factory {
return &Factory{
tracer: otel.GetTracerProvider(),
options: NewOptions("prometheus"),
options: NewOptions(),
}
}

Expand All @@ -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)
}
27 changes: 13 additions & 14 deletions plugin/metrics/prometheus/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ 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)

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 +49,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 +68,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 +81,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 +92,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 +109,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
62 changes: 27 additions & 35 deletions plugin/metrics/prometheus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
)

const (
prefix = "prometheus"

suffixServerURL = ".server-url"
suffixConnectTimeout = ".connect-timeout"
suffixTokenFilePath = ".token-file"
Expand All @@ -48,18 +50,13 @@ 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"`
}

// NewOptions creates a new Options struct.
func NewOptions(primaryNamespace string) *Options {
func NewOptions() *Options {
defaultConfig := config.Configuration{
ServerURL: defaultServerURL,
ConnectTimeout: defaultConnectTimeout,
Expand All @@ -71,75 +68,70 @@ func NewOptions(primaryNamespace string) *Options {
}

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

// AddFlags from this storage to the CLI.
func (opt *Options) AddFlags(flagSet *flag.FlagSet) {
nsConfig := &opt.Primary
flagSet.String(nsConfig.namespace+suffixServerURL, defaultServerURL,
flagSet.String(prefix+suffixServerURL, defaultServerURL,
"The Prometheus server's URL, must include the protocol scheme e.g. http://localhost:9090")
flagSet.Duration(nsConfig.namespace+suffixConnectTimeout, defaultConnectTimeout,
flagSet.Duration(prefix+suffixConnectTimeout, defaultConnectTimeout,
"The period to wait for a connection to Prometheus when executing queries.")
flagSet.String(nsConfig.namespace+suffixTokenFilePath, defaultTokenFilePath,
flagSet.String(prefix+suffixTokenFilePath, defaultTokenFilePath,
"The path to a file containing the bearer token which will be included when executing queries against the Prometheus API.")
flagSet.Bool(nsConfig.namespace+suffixOverrideFromContext, true,
flagSet.Bool(prefix+suffixOverrideFromContext, true,
"Whether the bearer token should be overridden from context (incoming request)")
flagSet.String(nsConfig.namespace+suffixMetricNamespace, defaultMetricNamespace,
flagSet.String(prefix+suffixMetricNamespace, defaultMetricNamespace,
`The metric namespace that is prefixed to the metric name. A '.' separator will be added between `+
`the namespace and the metric name.`)
flagSet.String(nsConfig.namespace+suffixLatencyUnit, defaultLatencyUnit,
flagSet.String(prefix+suffixLatencyUnit, defaultLatencyUnit,
`The units used for the "latency" histogram. It can be either "ms" or "s" and should be consistent with the `+
`histogram unit value set in the spanmetrics connector (see: `+
`https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/connector/spanmetricsconnector#configurations). `+
`This also helps jaeger-query determine the metric name when querying for "latency" metrics.`)
flagSet.Bool(nsConfig.namespace+suffixNormalizeCalls, defaultNormalizeCalls,
flagSet.Bool(prefix+suffixNormalizeCalls, defaultNormalizeCalls,
`Whether to normalize the "calls" metric name according to `+
`https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/translator/prometheus/README.md. `+
`For example: `+
`"calls" (not normalized) -> "calls_total" (normalized), `)
flagSet.Bool(nsConfig.namespace+suffixNormalizeDuration, defaultNormalizeDuration,
flagSet.Bool(prefix+suffixNormalizeDuration, defaultNormalizeDuration,
`Whether to normalize the "duration" metric name according to `+
`https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/translator/prometheus/README.md. `+
`For example: `+
`"duration_bucket" (not normalized) -> "duration_milliseconds_bucket (normalized)"`)

nsConfig.getTLSFlagsConfig().AddFlags(flagSet)
opt.getTLSFlagsConfig().AddFlags(flagSet)
}

// InitFromViper initializes the options struct with values from Viper.
func (opt *Options) InitFromViper(v *viper.Viper) error {
cfg := &opt.Primary
cfg.ServerURL = stripWhiteSpace(v.GetString(cfg.namespace + suffixServerURL))
cfg.ConnectTimeout = v.GetDuration(cfg.namespace + suffixConnectTimeout)
cfg.TokenFilePath = v.GetString(cfg.namespace + suffixTokenFilePath)
opt.ServerURL = stripWhiteSpace(v.GetString(prefix + suffixServerURL))
opt.ConnectTimeout = v.GetDuration(prefix + suffixConnectTimeout)
opt.TokenFilePath = v.GetString(prefix + suffixTokenFilePath)

cfg.MetricNamespace = v.GetString(cfg.namespace + suffixMetricNamespace)
cfg.LatencyUnit = v.GetString(cfg.namespace + suffixLatencyUnit)
cfg.NormalizeCalls = v.GetBool(cfg.namespace + suffixNormalizeCalls)
cfg.NormalizeDuration = v.GetBool(cfg.namespace + suffixNormalizeDuration)
cfg.TokenOverrideFromContext = v.GetBool(cfg.namespace + suffixOverrideFromContext)
opt.MetricNamespace = v.GetString(prefix + suffixMetricNamespace)
opt.LatencyUnit = v.GetString(prefix + suffixLatencyUnit)
opt.NormalizeCalls = v.GetBool(prefix + suffixNormalizeCalls)
opt.NormalizeDuration = v.GetBool(prefix + suffixNormalizeDuration)
opt.TokenOverrideFromContext = v.GetBool(prefix + suffixOverrideFromContext)

isValidUnit := map[string]bool{"ms": true, "s": true}
if _, ok := isValidUnit[cfg.LatencyUnit]; !ok {
return fmt.Errorf(`duration-unit must be one of "ms" or "s", not %q`, cfg.LatencyUnit)
if _, ok := isValidUnit[opt.LatencyUnit]; !ok {
return fmt.Errorf(`duration-unit must be one of "ms" or "s", not %q`, opt.LatencyUnit)
}

var err error
cfg.TLS, err = cfg.getTLSFlagsConfig().InitFromViper(v)
opt.TLS, err = opt.getTLSFlagsConfig().InitFromViper(v)
if err != nil {
return fmt.Errorf("failed to process Prometheus TLS options: %w", err)
}
return nil
}

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

Expand Down

0 comments on commit 295293c

Please sign in to comment.