From 7ddd2428e51e3b541b0a18d66f7acfb13bc21af2 Mon Sep 17 00:00:00 2001 From: Aman Brar Date: Thu, 29 Oct 2020 14:03:12 -0400 Subject: [PATCH] Add user agent header to outgoing prometheus remote write exporter http 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" --- exporter/prometheusremotewriteexporter/exporter.go | 2 ++ exporter/prometheusremotewriteexporter/exporter_test.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/exporter/prometheusremotewriteexporter/exporter.go b/exporter/prometheusremotewriteexporter/exporter.go index eaa59793da9..ee44eb2f3b9 100644 --- a/exporter/prometheusremotewriteexporter/exporter.go +++ b/exporter/prometheusremotewriteexporter/exporter.go @@ -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 @@ -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 { diff --git a/exporter/prometheusremotewriteexporter/exporter_test.go b/exporter/prometheusremotewriteexporter/exporter_test.go index 11caee79ea7..b23168db68a 100644 --- a/exporter/prometheusremotewriteexporter/exporter_test.go +++ b/exporter/prometheusremotewriteexporter/exporter_test.go @@ -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 @@ -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{} @@ -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{}