diff --git a/.chloggen/support-query-params.yaml b/.chloggen/support-query-params.yaml new file mode 100644 index 0000000000000..ded85678d2b3d --- /dev/null +++ b/.chloggen/support-query-params.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog) +component: exporter/elasticsearch + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add support for extra query parameters to the outgoing bulk request + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [44480] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/exporter/elasticsearchexporter/bulkindexer.go b/exporter/elasticsearchexporter/bulkindexer.go index c3fbc6d881189..d48d327b8cf9c 100644 --- a/exporter/elasticsearchexporter/bulkindexer.go +++ b/exporter/elasticsearchexporter/bulkindexer.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "net/http" + "net/url" "regexp" "runtime" "strings" @@ -80,7 +81,7 @@ func newBulkIndexer( return newSyncBulkIndexer(client, config, requireDataStream, tb, logger) } -func bulkIndexerConfig(client esapi.Transport, config *Config, requireDataStream bool) docappender.BulkIndexerConfig { +func bulkIndexerConfig(client esapi.Transport, config *Config, requireDataStream bool, logger *zap.Logger) docappender.BulkIndexerConfig { var maxDocRetries int if config.Retry.Enabled { maxDocRetries = defaultMaxRetries @@ -101,9 +102,31 @@ func bulkIndexerConfig(client esapi.Transport, config *Config, requireDataStream CompressionLevel: compressionLevel, PopulateFailedDocsInput: config.LogFailedDocsInput, IncludeSourceOnError: bulkIndexerIncludeSourceOnError(config.IncludeSourceOnError), + QueryParams: getQueryParamsFromEndpoint(config, logger), } } +func getQueryParamsFromEndpoint(config *Config, logger *zap.Logger) (queryParams map[string][]string) { + endpoints, _ := config.endpoints() + + if len(endpoints) != 0 { + // we check the query params set on the first endpoint only + // this is enough to replicate to all requests + parsedURL, err := url.Parse(endpoints[0]) + if err != nil { + logger.Warn("Failed to parse URL from endpoint", zap.Error(err)) + } + + rawQuery := parsedURL.RawQuery + queryParams, err = url.ParseQuery(rawQuery) + if err != nil { + logger.Warn("Failed to parse query parameters from endpoint", zap.Error(err)) + } + return queryParams + } + return nil +} + func bulkIndexerIncludeSourceOnError(includeSourceOnError *bool) docappender.Value { if includeSourceOnError == nil { return docappender.Unset @@ -129,7 +152,7 @@ func newSyncBulkIndexer( } } return &syncBulkIndexer{ - config: bulkIndexerConfig(client, config, requireDataStream), + config: bulkIndexerConfig(client, config, requireDataStream, logger), maxFlushBytes: maxFlushBytes, flushTimeout: config.Timeout, retryConfig: config.Retry, diff --git a/exporter/elasticsearchexporter/bulkindexer_test.go b/exporter/elasticsearchexporter/bulkindexer_test.go index 2be2645de6465..b3281b657050e 100644 --- a/exporter/elasticsearchexporter/bulkindexer_test.go +++ b/exporter/elasticsearchexporter/bulkindexer_test.go @@ -23,6 +23,7 @@ import ( "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest/observer" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/metadata" @@ -186,6 +187,18 @@ func TestSyncBulkIndexer(t *testing.T) { } } +func TestQueryParamsParsedFromEndpoints(t *testing.T) { + client, err := elasticsearch.NewDefaultClient() + require.NoError(t, err) + cfg := createDefaultConfig().(*Config) + cfg.Endpoints = []string{"http://localhost:9200?pipeline=test-pipeline"} + + bi := bulkIndexerConfig(client, cfg, true, zaptest.NewLogger(t)) + require.Equal(t, map[string][]string{ + "pipeline": {"test-pipeline"}, + }, bi.QueryParams) +} + func TestNewBulkIndexer(t *testing.T) { client, err := elasticsearch.NewDefaultClient() require.NoError(t, err)