Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extra custom query parameters in requests to Prometheus backend #6360

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Addressed comments
Signed-off-by: Alok Kumar Singh <dev.alok.singh123@gmail.com>
  • Loading branch information
akstron committed Dec 15, 2024
commit 5023abd60c7df644e7089225290021d8bdcab581
12 changes: 7 additions & 5 deletions pkg/prometheus/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ type Configuration struct {
TokenFilePath string `mapstructure:"token_file_path"`
TokenOverrideFromContext bool `mapstructure:"token_override_from_context"`

MetricNamespace string `mapstructure:"metric_namespace"`
LatencyUnit string `mapstructure:"latency_unit"`
NormalizeCalls bool `mapstructure:"normalize_calls"`
NormalizeDuration bool `mapstructure:"normalize_duration"`
AdditionalParameters map[string]string `mapstructure:"additional_parameters"`
MetricNamespace string `mapstructure:"metric_namespace"`
LatencyUnit string `mapstructure:"latency_unit"`
NormalizeCalls bool `mapstructure:"normalize_calls"`
NormalizeDuration bool `mapstructure:"normalize_duration"`
// ExtraQueryParams is used to provide extra query parameters to the backend which
akstron marked this conversation as resolved.
Show resolved Hide resolved
// is being called for metric read/write
akstron marked this conversation as resolved.
Show resolved Hide resolved
ExtraQueryParams map[string]string `mapstructure:"extra_query_parameters"`
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *Configuration) Validate() error {
Expand Down
10 changes: 4 additions & 6 deletions plugin/metricstore/prometheus/metricstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type (

promClient struct {
api.Client
params map[string]string
extraParams map[string]string
}
)

Expand All @@ -73,7 +73,7 @@ func (p promClient) URL(ep string, args map[string]string) *url.URL {
u := p.Client.URL(ep, args)

yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
query := u.Query()
for k, v := range p.params {
for k, v := range p.extraParams {
query.Set(k, v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Set and not Add? We are not asked to replace params, only append them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Changed it to Add and updated test accordingly.

}
u.RawQuery = query.Encode()
Expand All @@ -96,17 +96,15 @@ func createPromClient(logger *zap.Logger, cfg config.Configuration) (api.Client,
}

customClient := promClient{
Client: client,
params: cfg.AdditionalParameters,
Client: client,
extraParams: cfg.ExtraQueryParams,
}

return customClient, nil
}

// NewMetricsReader returns a new MetricsReader.
func NewMetricsReader(cfg config.Configuration, logger *zap.Logger, tracer trace.TracerProvider) (*MetricsReader, error) {
logger.Info("Creating metrics reader", zap.Any("configuration", cfg))

const operationLabel = "span_name"

promClient, err := createPromClient(logger, cfg)
Expand Down
6 changes: 3 additions & 3 deletions plugin/metricstore/prometheus/metricstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,16 +863,16 @@ func TestInvalidCertFile(t *testing.T) {
assert.Nil(t, reader)
}

func TestCreatePromClientWithAdditionalParameters(t *testing.T) {
func TestCreatePromClientWithExtraQueryParameters(t *testing.T) {
expParams := map[string]string{
"param1": "value1",
"param2": "value2",
}

logger := zap.NewNop()
cfg := config.Configuration{
ServerURL: "http://localhost:1234",
AdditionalParameters: expParams,
ServerURL: "http://localhost:1234",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ServerURL: "http://localhost:1234",
ServerURL: "http://localhost:1234?param1=value0",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ExtraQueryParams: expParams,
}

customClient, err := createPromClient(logger, cfg)
Expand Down
9 changes: 4 additions & 5 deletions plugin/metricstore/prometheus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ func DefaultConfig() config.Configuration {
ServerURL: defaultServerURL,
ConnectTimeout: defaultConnectTimeout,

MetricNamespace: defaultMetricNamespace,
LatencyUnit: defaultLatencyUnit,
NormalizeCalls: defaultNormalizeCalls,
NormalizeDuration: defaultNormalizeCalls,
AdditionalParameters: nil,
MetricNamespace: defaultMetricNamespace,
LatencyUnit: defaultLatencyUnit,
NormalizeCalls: defaultNormalizeCalls,
NormalizeDuration: defaultNormalizeCalls,
}
}

Expand Down
Loading