Skip to content

Commit

Permalink
Add user agent header to outgoing prometheus remote write exporter ht…
Browse files Browse the repository at this point in the history
…tp request (#2000)

Added User-Agent header to Prometheus Remote Write Exporter's outgoing http request. This allows endpoints to identify whether an incoming metric has come from an OTel Collector instance.

**Testing:**
Unit tests added conforming with existing tests to check headers.

**Documentation:**
No documentation has been added as it is for internal usage rather than customer usage.

Note: I am assuming here that the `VERSION` variable in the makefile will be set when we reach GA, as currently it is not and the version in the version package is just the default value of "latest"
  • Loading branch information
amanbrar1999 authored Oct 29, 2020
1 parent d379a99 commit 7ddd242
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/pdata"
otlp "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1"
"go.opentelemetry.io/collector/internal/version"
)

// PrwExporter converts OTLP metrics to Prometheus remote write TimeSeries and sends them to a remote endpoint
Expand Down Expand Up @@ -233,6 +234,7 @@ func (prwe *PrwExporter) export(ctx context.Context, tsMap map[string]*prompb.Ti
httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
httpReq.Header.Set("User-Agent", "OpenTelemetry-Collector/"+version.Version)

httpResp, err := prwe.client.Do(httpReq)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"go.opentelemetry.io/collector/exporter/exporterhelper"
otlp "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1"
"go.opentelemetry.io/collector/internal/data/testdata"
"go.opentelemetry.io/collector/internal/version"
)

// Test_ NewPrwExporter checks that a new exporter instance with non-nil fields is initialized
Expand Down Expand Up @@ -143,6 +144,7 @@ func Test_export(t *testing.T) {
// Receives the http requests and unzip, unmarshals, and extracts TimeSeries
assert.Equal(t, "0.1.0", r.Header.Get("X-Prometheus-Remote-Write-Version"))
assert.Equal(t, "snappy", r.Header.Get("Content-Encoding"))
assert.Equal(t, "OpenTelemetry-Collector/"+version.Version, r.Header.Get("User-Agent"))
writeReq := &prompb.WriteRequest{}
unzipped := []byte{}

Expand Down Expand Up @@ -437,6 +439,7 @@ func Test_PushMetrics(t *testing.T) {
dest, err := snappy.Decode(buf, body)
assert.Equal(t, "0.1.0", r.Header.Get("x-prometheus-remote-write-version"))
assert.Equal(t, "snappy", r.Header.Get("content-encoding"))
assert.Equal(t, "OpenTelemetry-Collector/"+version.Version, r.Header.Get("user-agent"))
assert.NotNil(t, r.Header.Get("tenant-id"))
require.NoError(t, err)
wr := &prompb.WriteRequest{}
Expand Down

0 comments on commit 7ddd242

Please sign in to comment.