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 3 commits
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
3 changes: 3 additions & 0 deletions pkg/prometheus/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Configuration struct {
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
// is being called for metric read/write
ExtraQueryParams map[string]string `mapstructure:"extra_query_parameters"`
}

func (c *Configuration) Validate() error {
Expand Down
40 changes: 36 additions & 4 deletions plugin/metricstore/prometheus/metricstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -60,16 +61,32 @@ type (
metricDesc string
buildPromQuery func(p promQueryParams) string
}

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

// 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))
// URL decorator enables adding additional query parameters to the request sent to prometheus backend
func (p promClient) URL(ep string, args map[string]string) *url.URL {
u := p.Client.URL(ep, args)

query := u.Query()
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()

return u
}

func createPromClient(logger *zap.Logger, cfg config.Configuration) (api.Client, error) {
Copy link
Member

Choose a reason for hiding this comment

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

you don't need logger here, and neither does getHTTPRoundTripper

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored to remove logger from both. Thanks

roundTripper, err := getHTTPRoundTripper(&cfg, logger)
if err != nil {
return nil, err
}

client, err := api.NewClient(api.Config{
Address: cfg.ServerURL,
RoundTripper: roundTripper,
Expand All @@ -78,10 +95,25 @@ func NewMetricsReader(cfg config.Configuration, logger *zap.Logger, tracer trace
return nil, fmt.Errorf("failed to initialize prometheus client: %w", err)
}

customClient := promClient{
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) {
const operationLabel = "span_name"

promClient, err := createPromClient(logger, cfg)
if err != nil {
return nil, err
}

mr := &MetricsReader{
client: promapi.NewAPI(client),
client: promapi.NewAPI(promClient),
logger: logger,
tracer: tracer.Tracer("prom-metrics-reader"),

Expand Down
25 changes: 25 additions & 0 deletions plugin/metricstore/prometheus/metricstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,31 @@ func TestInvalidCertFile(t *testing.T) {
assert.Nil(t, reader)
}

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

logger := zap.NewNop()
cfg := config.Configuration{
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)
require.NoError(t, err)

u := customClient.URL("", nil)

q := u.Query()

for k, v := range expParams {
recV := q.Get(k)
require.Equal(t, v, recV)
}
}

func startMockPrometheusServer(t *testing.T, wantPromQlQuery string, wantWarnings []string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(wantWarnings) > 0 {
Expand Down
Loading