Skip to content

Commit

Permalink
[mezmoexporter] Add user-agent string (#10470)
Browse files Browse the repository at this point in the history
This commit adds a `user-agent` string to the outgoing HTTP
requests from the Mezmo exporter.
  • Loading branch information
jsumners authored May 31, 2022
1 parent 6e1e1ae commit bf505ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
### 💡 Enhancements 💡

- `tailsamplingprocessor`: Add support for string invert matching to `and` policy (#9553)
- `mezemoexporter`: Add user agent string to outgoing HTTP requests (#10470)

### 🧰 Bug fixes 🧰

Expand Down
17 changes: 10 additions & 7 deletions exporter/mezmoexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import (
)

type mezmoExporter struct {
config *Config
settings component.TelemetrySettings
client *http.Client
wg sync.WaitGroup
config *Config
settings component.TelemetrySettings
client *http.Client
userAgentString string
wg sync.WaitGroup
}

type MezmoLogLine struct {
Expand All @@ -47,10 +48,11 @@ type MezmoLogBody struct {
Lines []MezmoLogLine `json:"lines"`
}

func newLogsExporter(config *Config, settings component.TelemetrySettings) *mezmoExporter {
func newLogsExporter(config *Config, settings component.TelemetrySettings, buildInfo component.BuildInfo) *mezmoExporter {
var e = &mezmoExporter{
config: config,
settings: settings,
config: config,
settings: settings,
userAgentString: fmt.Sprintf("mezmo-otel-exporter/%s", buildInfo.Version),
}
return e
}
Expand Down Expand Up @@ -156,6 +158,7 @@ func (m *mezmoExporter) sendLinesToMezmo(post string) (errs error) {
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(post)))
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", m.userAgentString)
req.Header.Add("apikey", m.config.IngestKey)

var res *http.Response
Expand Down
8 changes: 7 additions & 1 deletion exporter/mezmoexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ import (

"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/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)

var buildInfo = component.BuildInfo{
Version: "1.0",
}

func createSimpleLogData(numberOfLogs int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
Expand Down Expand Up @@ -95,6 +100,7 @@ func TestLogsExporter(t *testing.T) {
// Spin up a HTTP server to receive the test exporters...
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "mezmo-otel-exporter/"+buildInfo.Version, r.Header.Get("User-Agent"))

body, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -117,7 +123,7 @@ func TestLogsExporter(t *testing.T) {
IngestURL: serverURL.String(),
}

exp := newLogsExporter(config, componenttest.NewNopTelemetrySettings())
exp := newLogsExporter(config, componenttest.NewNopTelemetrySettings(), buildInfo)
require.NotNil(t, exp)

err = exp.start(context.Background(), componenttest.NewNopHost())
Expand Down
2 changes: 1 addition & 1 deletion exporter/mezmoexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func createLogsExporter(ctx context.Context, settings component.ExporterCreateSe
return nil, err
}

exp := newLogsExporter(expCfg, settings.TelemetrySettings)
exp := newLogsExporter(expCfg, settings.TelemetrySettings, settings.BuildInfo)

return exporterhelper.NewLogsExporter(
expCfg,
Expand Down

0 comments on commit bf505ce

Please sign in to comment.