Skip to content

Commit ed0a8fb

Browse files
committed
Support query params
1 parent 6849a0a commit ed0a8fb

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

exporters/otlp/otlpmetric/otlpmetrichttp/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ func newClient(cfg oconf.Config) (*client, error) {
8080
if cfg.Metrics.Insecure {
8181
u.Scheme = "http"
8282
}
83+
84+
// Add query parameters to the URL
85+
if len(cfg.Metrics.QueryParams) > 0 {
86+
query := u.Query()
87+
for key, value := range cfg.Metrics.QueryParams {
88+
query.Set(key, value)
89+
}
90+
u.RawQuery = query.Encode()
91+
}
92+
8393
// Body is set when this is cloned during upload.
8494
req, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
8595
if err != nil {

exporters/otlp/otlpmetric/otlpmetrichttp/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,12 @@ func WithAggregationSelector(selector metric.AggregationSelector) Option {
222222
func WithProxy(pf HTTPTransportProxyFunc) Option {
223223
return wrappedOption{oconf.WithProxy(oconf.HTTPTransportProxyFunc(pf))}
224224
}
225+
226+
// WithQueryParams sets the query parameters to be included in the endpoint URL.
227+
//
228+
// If query parameters are provided in the endpoint URL, they will be merged
229+
// with the parameters set using this function. Parameters set using this
230+
// function will take precedence in case of conflicts.
231+
func WithQueryParams(params map[string]string) Option {
232+
return wrappedOption{oconf.WithQueryParams(params)}
233+
}

exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type (
5656
Compression Compression
5757
Timeout time.Duration
5858
URLPath string
59+
QueryParams map[string]string // New variable to store query params
5960

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

294+
// Extract and store query parameters
295+
if u.RawQuery != "" {
296+
queryParams := make(map[string]string)
297+
for key, values := range u.Query() {
298+
if len(values) > 0 {
299+
queryParams[key] = values[0]
300+
}
301+
}
302+
cfg.Metrics.QueryParams = queryParams // New variable to store query params
303+
}
304+
293305
return cfg
294306
})
295307
}
@@ -373,3 +385,15 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
373385
return cfg
374386
})
375387
}
388+
389+
func WithQueryParams(params map[string]string) HTTPOption {
390+
return NewHTTPOption(func(cfg Config) Config {
391+
if cfg.Metrics.QueryParams == nil {
392+
cfg.Metrics.QueryParams = make(map[string]string)
393+
}
394+
for key, value := range params {
395+
cfg.Metrics.QueryParams[key] = value
396+
}
397+
return cfg
398+
})
399+
}

0 commit comments

Comments
 (0)