From 80c69e6326b416e9f8937ed20fb0ffe63b13d7ec Mon Sep 17 00:00:00 2001 From: Jared Tan Date: Mon, 18 Dec 2023 23:18:39 +0800 Subject: [PATCH] [exporter/honeycombmarker] Set user-agent header (#29912) **Description:** **Link to tracking Issue:** close https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/29894 **Testing:** **Documentation:** --------- Signed-off-by: Jared Tan --- ...-header-in-the-outgoing-HTTP-requests.yaml | 27 ++++++++++++++++ exporter/honeycombmarkerexporter/factory.go | 2 +- .../honeycombmarkerexporter/logs_exporter.go | 31 ++++++++++++------- .../logs_exporter_test.go | 7 ++++- 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100755 .chloggen/honeycombmarker-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.yaml diff --git a/.chloggen/honeycombmarker-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.yaml b/.chloggen/honeycombmarker-set-the-User-Agent-header-in-the-outgoing-HTTP-requests.yaml new file mode 100755 index 000000000000..2f5674328249 --- /dev/null +++ b/.chloggen/honeycombmarker-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/honeycombmarker + +# 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: [29894] + +# (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/honeycombmarkerexporter/factory.go b/exporter/honeycombmarkerexporter/factory.go index e40f3240e83e..950fbdf34f10 100644 --- a/exporter/honeycombmarkerexporter/factory.go +++ b/exporter/honeycombmarkerexporter/factory.go @@ -34,7 +34,7 @@ func createLogsExporter( ) (exporter.Logs, error) { cf := cfg.(*Config) - logsExp, err := newHoneycombLogsExporter(set.TelemetrySettings, cf) + logsExp, err := newHoneycombLogsExporter(set, cf) if err != nil { return nil, err } diff --git a/exporter/honeycombmarkerexporter/logs_exporter.go b/exporter/honeycombmarkerexporter/logs_exporter.go index 07c595acbd6e..aa65d005a5ec 100644 --- a/exporter/honeycombmarkerexporter/logs_exporter.go +++ b/exporter/honeycombmarkerexporter/logs_exporter.go @@ -10,9 +10,11 @@ import ( "fmt" "io" "net/http" + "runtime" "strings" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/exporter" "go.opentelemetry.io/collector/pdata/plog" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl" @@ -22,22 +24,27 @@ import ( const ( defaultDatasetSlug = "__all__" + userAgentHeaderKey = "User-Agent" + contentType = "Content-Type" + honeycombTeam = "X-Honeycomb-Team" ) type honeycombLogsExporter struct { - set component.TelemetrySettings - markers []Marker - client *http.Client - config *Config + set component.TelemetrySettings + markers []Marker + client *http.Client + config *Config + userAgentHeader string } -func newHoneycombLogsExporter(set component.TelemetrySettings, config *Config) (*honeycombLogsExporter, error) { +func newHoneycombLogsExporter(set exporter.CreateSettings, config *Config) (*honeycombLogsExporter, error) { if config == nil { return nil, fmt.Errorf("unable to create honeycombLogsExporter without config") } + telemetrySettings := set.TelemetrySettings for i, m := range config.Markers { - matchLogConditions, err := filterottl.NewBoolExprForLog(m.Rules.LogConditions, filterottl.StandardLogFuncs(), ottl.PropagateError, set) + matchLogConditions, err := filterottl.NewBoolExprForLog(m.Rules.LogConditions, filterottl.StandardLogFuncs(), ottl.PropagateError, telemetrySettings) if err != nil { return nil, fmt.Errorf("failed to parse log conditions: %w", err) } @@ -45,9 +52,10 @@ func newHoneycombLogsExporter(set component.TelemetrySettings, config *Config) ( config.Markers[i].Rules.logBoolExpr = matchLogConditions } logsExp := &honeycombLogsExporter{ - set: set, - markers: config.Markers, - config: config, + set: telemetrySettings, + markers: config.Markers, + config: config, + userAgentHeader: fmt.Sprintf("%s/%s (%s/%s)", set.BuildInfo.Description, set.BuildInfo.Version, runtime.GOOS, runtime.GOARCH), } return logsExp, nil } @@ -111,8 +119,9 @@ func (e *honeycombLogsExporter) sendMarker(ctx context.Context, marker Marker, l return err } - req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Honeycomb-Team", fmt.Sprint(e.config.APIKey)) + req.Header.Set(contentType, "application/json") + req.Header.Set(honeycombTeam, fmt.Sprint(e.config.APIKey)) + req.Header.Set(userAgentHeaderKey, e.userAgentHeader) resp, err := e.client.Do(req) if err != nil { diff --git a/exporter/honeycombmarkerexporter/logs_exporter_test.go b/exporter/honeycombmarkerexporter/logs_exporter_test.go index 4a69de22b57f..9dd716f482be 100644 --- a/exporter/honeycombmarkerexporter/logs_exporter_test.go +++ b/exporter/honeycombmarkerexporter/logs_exporter_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -132,9 +133,13 @@ func TestExportMarkers(t *testing.T) { } assert.Contains(t, req.URL.Path, tt.expectedURL) - apiKey := req.Header.Get("X-Honeycomb-Team") + apiKey := req.Header.Get(honeycombTeam) assert.Equal(t, apiKey, string(tt.config.APIKey)) + userAgent := req.Header.Get(userAgentHeaderKey) + assert.NotEmpty(t, userAgent) + assert.Equal(t, strings.Contains(userAgent, "OpenTelemetry Collector"), true) + rw.WriteHeader(http.StatusAccepted) })) defer markerServer.Close()