diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index aa61592a056..f2f1c2cacd8 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -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 { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index bf05adcf1b1..8280999937e 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -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)} +} diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go index d322a0a3e2e..e6519c859e6 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go @@ -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 @@ -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 }) } @@ -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 + }) +}