|
8 | 8 | "net" |
9 | 9 | "path/filepath" |
10 | 10 | "runtime" |
| 11 | + "strings" |
11 | 12 | "sync" |
12 | 13 | "sync/atomic" |
13 | 14 | "testing" |
@@ -1042,3 +1043,48 @@ func TestSendProfilesWhenEndpointHasHttpScheme(t *testing.T) { |
1042 | 1043 | }) |
1043 | 1044 | } |
1044 | 1045 | } |
| 1046 | + |
| 1047 | +func TestUserAgentHeader_OverriddenByConfig(t *testing.T) { |
| 1048 | + // Start an OTLP-compatible receiver. |
| 1049 | + ln, err := net.Listen("tcp", "localhost:") |
| 1050 | + require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err) |
| 1051 | + rcv := otlpMetricsReceiverOnGRPCServer(ln) |
| 1052 | + defer rcv.srv.GracefulStop() |
| 1053 | + |
| 1054 | + // Start an OTLP exporter with a custom User-Agent header. |
| 1055 | + factory := NewFactory() |
| 1056 | + cfg := factory.CreateDefaultConfig().(*Config) |
| 1057 | + cfg.QueueConfig = configoptional.None[exporterhelper.QueueBatchConfig]() |
| 1058 | + cfg.ClientConfig = configgrpc.ClientConfig{ |
| 1059 | + Endpoint: ln.Addr().String(), |
| 1060 | + TLS: configtls.ClientConfig{ |
| 1061 | + Insecure: true, |
| 1062 | + }, |
| 1063 | + UserAgent: "My Distribution For The Collector", |
| 1064 | + } |
| 1065 | + set := exportertest.NewNopSettings(factory.Type()) |
| 1066 | + set.BuildInfo.Description = "Collector" |
| 1067 | + set.BuildInfo.Version = "1.2.3test" |
| 1068 | + |
| 1069 | + exp, err := factory.CreateMetrics(context.Background(), set, cfg) |
| 1070 | + require.NoError(t, err) |
| 1071 | + require.NotNil(t, exp) |
| 1072 | + defer func() { |
| 1073 | + assert.NoError(t, exp.Shutdown(context.Background())) |
| 1074 | + }() |
| 1075 | + |
| 1076 | + require.NoError(t, exp.Start(context.Background(), componenttest.NewNopHost())) |
| 1077 | + |
| 1078 | + // Send a metric to trigger an RPC. |
| 1079 | + require.NoError(t, exp.ConsumeMetrics(context.Background(), pmetric.NewMetrics())) |
| 1080 | + |
| 1081 | + assert.Eventually(t, func() bool { |
| 1082 | + return rcv.requestCount.Load() > 0 |
| 1083 | + }, 10*time.Second, 5*time.Millisecond) |
| 1084 | + |
| 1085 | + md := rcv.getMetadata() |
| 1086 | + require.Len(t, md.Get("User-Agent"), 1) |
| 1087 | + // User-configured value must win over the builtin BuildInfo agent. |
| 1088 | + require.True(t, strings.HasPrefix(md.Get("User-Agent")[0], "My Distribution For The Collector")) |
| 1089 | + require.NotContains(t, md.Get("User-Agent")[0], "Collector/1.2.3test") |
| 1090 | +} |
0 commit comments