Skip to content

Commit

Permalink
add test for with custom user agent of exporter/otlp
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <rogers@rogerogers.com>
  • Loading branch information
rogerogers committed Oct 7, 2022
1 parent f13752f commit 7a437d8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
Expand Down Expand Up @@ -189,4 +190,18 @@ func TestConfig(t *testing.T) {
err := exp.Export(ctx, metricdata.ResourceMetrics{})
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
})

t.Run("WithCustomUserAgent", func(t *testing.T) {
key := "user-agent"
customerUserAgent := "custom-user-agent"
exp, coll := factoryFunc(nil, WithDialOption(grpc.WithUserAgent(customerUserAgent)))
t.Cleanup(coll.Shutdown)
ctx := context.Background()
require.NoError(t, exp.Export(ctx, metricdata.ResourceMetrics{}))
// Ensure everything is flushed.
require.NoError(t, exp.Shutdown(ctx))

got := coll.Headers()
assert.Contains(t, got[key][0], customerUserAgent)
})
}
15 changes: 15 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,19 @@ func TestConfig(t *testing.T) {
assert.NoError(t, exp.Export(ctx, metricdata.ResourceMetrics{}))
assert.Len(t, coll.Collect().Dump(), 1)
})

t.Run("WithCustomUserAgent", func(t *testing.T) {
key := http.CanonicalHeaderKey("user-agent")
headers := map[string]string{key: "custom-user-agent"}
exp, coll := factoryFunc("", nil, WithHeaders(headers))
ctx := context.Background()
t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
require.NoError(t, exp.Export(ctx, metricdata.ResourceMetrics{}))
// Ensure everything is flushed.
require.NoError(t, exp.Shutdown(ctx))

got := coll.Headers()
require.Contains(t, got, key)
assert.Equal(t, got[key], []string{headers[key]})
})
}
15 changes: 15 additions & 0 deletions exporters/otlp/otlptrace/otlptracegrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,18 @@ func TestPartialSuccess(t *testing.T) {
require.Contains(t, errors[0].Error(), "partially successful")
require.Contains(t, errors[0].Error(), "2 spans rejected")
}

func TestCustomUserAgent(t *testing.T) {
customUserAgent := "custom-user-agent"
mc := runMockCollector(t)
t.Cleanup(func() { require.NoError(t, mc.stop()) })

ctx := context.Background()
exp := newGRPCExporter(t, ctx, mc.endpoint,
otlptracegrpc.WithDialOption(grpc.WithUserAgent(customUserAgent)))
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
require.NoError(t, exp.ExportSpans(ctx, roSpans))

headers := mc.getHeaders()
require.Contains(t, headers.Get("user-agent")[0], customUserAgent)
}
13 changes: 13 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (
"Otel-Go-Key-1": "somevalue",
"Otel-Go-Key-2": "someothervalue",
}

customUserAgentHeader = map[string]string{
"user-agent": "custome-user-agent",
}
)

func TestEndToEnd(t *testing.T) {
Expand Down Expand Up @@ -142,6 +146,15 @@ func TestEndToEnd(t *testing.T) {
ExpectedHeaders: testHeaders,
},
},
{
name: "with custom user agent",
opts: []otlptracehttp.Option{
otlptracehttp.WithHeaders(customUserAgentHeader),
},
mcCfg: mockCollectorConfig{
ExpectedHeaders: customUserAgentHeader,
},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 7a437d8

Please sign in to comment.