Skip to content

Support query params #6726

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

Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ func newClient(cfg oconf.Config) (*client, error) {
if cfg.Metrics.Insecure {
u.Scheme = "http"
}

// Add query parameters to the URL
if len(cfg.Metrics.QueryParams) > 0 {
query := u.Query()
for key, value := range cfg.Metrics.QueryParams {
query.Set(key, value)
}
u.RawQuery = query.Encode()
}

// Body is set when this is cloned during upload.
req, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,12 @@ func WithAggregationSelector(selector metric.AggregationSelector) Option {
func WithProxy(pf HTTPTransportProxyFunc) Option {
return wrappedOption{oconf.WithProxy(oconf.HTTPTransportProxyFunc(pf))}
}

// WithQueryParams sets the query parameters to be included in the endpoint URL.
//
// If query parameters are provided in the endpoint URL, they will be merged
// with the parameters set using this function. Parameters set using this
// function will take precedence in case of conflicts.
func WithQueryParams(params map[string]string) Option {
return wrappedOption{oconf.WithQueryParams(params)}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type (
Compression Compression
Timeout time.Duration
URLPath string
QueryParams map[string]string // New variable to store query params

// gRPC configurations
GRPCCredentials credentials.TransportCredentials
Expand Down Expand Up @@ -290,6 +291,17 @@ func WithEndpointURL(v string) GenericOption {
cfg.Metrics.URLPath = u.Path
cfg.Metrics.Insecure = u.Scheme != "https"

// Extract and store query parameters
if u.RawQuery != "" {
queryParams := make(map[string]string)
for key, values := range u.Query() {
if len(values) > 0 {
queryParams[key] = values[0]
}
}
cfg.Metrics.QueryParams = queryParams // New variable to store query params
}

return cfg
})
}
Expand Down Expand Up @@ -373,3 +385,15 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
return cfg
})
}

func WithQueryParams(params map[string]string) HTTPOption {
return NewHTTPOption(func(cfg Config) Config {
if cfg.Metrics.QueryParams == nil {
cfg.Metrics.QueryParams = make(map[string]string)
}
for key, value := range params {
cfg.Metrics.QueryParams[key] = value
}
return cfg
})
}