Skip to content

Commit

Permalink
[exporter/honeycombmarker] Set user-agent header (open-telemetry#29912)
Browse files Browse the repository at this point in the history
**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#29894

**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 18, 2023
1 parent e70f1e2 commit 80c69e6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 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/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: []
2 changes: 1 addition & 1 deletion exporter/honeycombmarkerexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 20 additions & 11 deletions exporter/honeycombmarkerexporter/logs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,32 +24,38 @@ 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)
}

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
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion exporter/honeycombmarkerexporter/logs_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 80c69e6

Please sign in to comment.