Skip to content

Commit

Permalink
[exporter/elasticsearch] set the default User-Agent header in the out…
Browse files Browse the repository at this point in the history
…going HTTP requests (open-telemetry#29911)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

**Link to tracking Issue:** close
open-telemetry#29898

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

---------

Signed-off-by: Jared Tan <jian.tan@daocloud.io>
  • Loading branch information
JaredTan95 authored Dec 21, 2023
1 parent 94c4da0 commit e8ab66d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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: []
18 changes: 18 additions & 0 deletions exporter/elasticsearchexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry
import (
"context"
"fmt"
"runtime"
"time"

"go.opentelemetry.io/collector/component"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
23 changes: 23 additions & 0 deletions exporter/elasticsearchexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
})
}

0 comments on commit e8ab66d

Please sign in to comment.