diff --git a/.chloggen/elasticsearchexporter-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.yaml b/.chloggen/elasticsearchexporter-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.yaml new file mode 100644 index 000000000000..88ef31c7a42c --- /dev/null +++ b/.chloggen/elasticsearchexporter-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.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. filelogreceiver) +component: exporter/elasticsearch + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: set the User-Agent header in the outgoing HTTP requests. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [29898] + +# (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: [] diff --git a/exporter/elasticsearchexporter/factory.go b/exporter/elasticsearchexporter/factory.go index 8c49dc87ed46..855913fe2dd5 100644 --- a/exporter/elasticsearchexporter/factory.go +++ b/exporter/elasticsearchexporter/factory.go @@ -8,6 +8,7 @@ package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry import ( "context" "fmt" + "runtime" "time" "go.opentelemetry.io/collector/component" @@ -21,6 +22,7 @@ const ( // The value of "type" key in configuration. defaultLogsIndex = "logs-generic-default" defaultTracesIndex = "traces-generic-default" + userAgentHeaderKey = "User-Agent" ) // NewFactory creates a factory for Elastic exporter. @@ -76,6 +78,8 @@ func createLogsExporter( set.Logger.Warn("index option are deprecated and replaced with logs_index and traces_index.") } + setDefaultUserAgentHeader(cf, set.BuildInfo) + logsExporter, err := newLogsExporter(set.Logger, cf) if err != nil { return nil, fmt.Errorf("cannot configure Elasticsearch logsExporter: %w", err) @@ -96,6 +100,9 @@ func createTracesExporter(ctx context.Context, cfg component.Config) (exporter.Traces, error) { cf := cfg.(*Config) + + setDefaultUserAgentHeader(cf, set.BuildInfo) + tracesExporter, err := newTracesExporter(set.Logger, cf) if err != nil { return nil, fmt.Errorf("cannot configure Elasticsearch tracesExporter: %w", err) @@ -108,3 +115,14 @@ func createTracesExporter(ctx context.Context, exporterhelper.WithShutdown(tracesExporter.Shutdown), exporterhelper.WithQueue(cf.QueueSettings)) } + +// set default User-Agent header with BuildInfo if User-Agent is empty +func setDefaultUserAgentHeader(cf *Config, info component.BuildInfo) { + if _, found := cf.Headers[userAgentHeaderKey]; found { + return + } + if cf.Headers == nil { + cf.Headers = make(map[string]string) + } + cf.Headers[userAgentHeaderKey] = fmt.Sprintf("%s/%s (%s/%s)", info.Description, info.Version, runtime.GOOS, runtime.GOARCH) +} diff --git a/exporter/elasticsearchexporter/factory_test.go b/exporter/elasticsearchexporter/factory_test.go index 477aeabd8b3c..86557365b09b 100644 --- a/exporter/elasticsearchexporter/factory_test.go +++ b/exporter/elasticsearchexporter/factory_test.go @@ -5,10 +5,12 @@ package elasticsearchexporter import ( "context" + "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exportertest" ) @@ -66,3 +68,24 @@ func TestFactory_CreateLogsAndTracesExporterWithDeprecatedIndexOption(t *testing require.NotNil(t, tracesExporter) require.NoError(t, tracesExporter.Shutdown(context.TODO())) } + +func TestSetDefaultUserAgentHeader(t *testing.T) { + t.Run("insert default user agent header into empty", func(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + setDefaultUserAgentHeader(cfg, component.BuildInfo{Description: "mock OpenTelemetry Collector", Version: "latest"}) + assert.Equal(t, len(cfg.Headers), 1) + assert.Equal(t, strings.Contains(cfg.Headers[userAgentHeaderKey], "OpenTelemetry Collector"), true) + }) + + t.Run("ignore user agent header if configured", func(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + cfg.Headers = map[string]string{ + userAgentHeaderKey: "mock user agent header", + } + setDefaultUserAgentHeader(cfg, component.BuildInfo{Description: "mock OpenTelemetry Collector", Version: "latest"}) + assert.Equal(t, len(cfg.Headers), 1) + assert.Equal(t, cfg.Headers[userAgentHeaderKey], "mock user agent header") + }) +}