| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlpexporter |
| 5 |
|
| 6 |
import ( |
| 7 |
"path/filepath" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/config/configauth" |
| 16 |
"go.opentelemetry.io/collector/config/configgrpc" |
| 17 |
"go.opentelemetry.io/collector/config/configopaque" |
| 18 |
"go.opentelemetry.io/collector/config/configoptional" |
| 19 |
"go.opentelemetry.io/collector/config/configretry" |
| 20 |
"go.opentelemetry.io/collector/config/configtls" |
| 21 |
"go.opentelemetry.io/collector/confmap" |
| 22 |
"go.opentelemetry.io/collector/confmap/confmaptest" |
| 23 |
"go.opentelemetry.io/collector/confmap/xconfmap" |
| 24 |
"go.opentelemetry.io/collector/exporter/exporterhelper" |
| 25 |
) |
| 26 |
|
| 27 |
func TestUnmarshalDefaultConfig(t *testing.T) { |
| 28 |
factory := NewFactory() |
| 29 |
cfg := factory.CreateDefaultConfig() |
| 30 |
require.NoError(t, confmap.New().Unmarshal(&cfg)) |
| 31 |
assert.Equal(t, factory.CreateDefaultConfig(), cfg) |
| 32 |
} |
| 33 |
|
| 34 |
func TestUnmarshalConfig(t *testing.T) { |
| 35 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 36 |
require.NoError(t, err) |
| 37 |
factory := NewFactory() |
| 38 |
cfg := factory.CreateDefaultConfig() |
| 39 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 40 |
require.NoError(t, xconfmap.Validate(&cfg)) |
| 41 |
assert.Equal(t, |
| 42 |
&Config{ |
| 43 |
TimeoutConfig: exporterhelper.TimeoutConfig{ |
| 44 |
Timeout: 10 * time.Second, |
| 45 |
}, |
| 46 |
RetryConfig: configretry.BackOffConfig{ |
| 47 |
Enabled: true, |
| 48 |
InitialInterval: 10 * time.Second, |
| 49 |
RandomizationFactor: 0.7, |
| 50 |
Multiplier: 1.3, |
| 51 |
MaxInterval: 1 * time.Minute, |
| 52 |
MaxElapsedTime: 10 * time.Minute, |
| 53 |
}, |
| 54 |
QueueConfig: exporterhelper.QueueBatchConfig{ |
| 55 |
Enabled: true, |
| 56 |
Sizer: exporterhelper.RequestSizerTypeItems, |
| 57 |
NumConsumers: 2, |
| 58 |
QueueSize: 100000, |
| 59 |
Batch: configoptional.Some(exporterhelper.BatchConfig{ |
| 60 |
FlushTimeout: 200 * time.Millisecond, |
| 61 |
Sizer: exporterhelper.RequestSizerTypeItems, |
| 62 |
MinSize: 1000, |
| 63 |
MaxSize: 10000, |
| 64 |
}), |
| 65 |
}, |
| 66 |
ClientConfig: configgrpc.ClientConfig{ |
| 67 |
Headers: configopaque.MapList{ |
| 68 |
{Name: "another", Value: "somevalue"}, |
| 69 |
{Name: "can you have a . here?", Value: "F0000000-0000-0000-0000-000000000000"}, |
| 70 |
{Name: "header1", Value: "234"}, |
| 71 |
}, |
| 72 |
Endpoint: "1.2.3.4:1234", |
| 73 |
Compression: "gzip", |
| 74 |
TLS: configtls.ClientConfig{ |
| 75 |
Config: configtls.Config{ |
| 76 |
CAFile: "/var/lib/mycert.pem", |
| 77 |
}, |
| 78 |
Insecure: false, |
| 79 |
}, |
| 80 |
Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{ |
| 81 |
Time: 20 * time.Second, |
| 82 |
PermitWithoutStream: true, |
| 83 |
Timeout: 30 * time.Second, |
| 84 |
}), |
| 85 |
WriteBufferSize: 512 * 1024, |
| 86 |
BalancerName: "round_robin", |
| 87 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: component.MustNewID("nop")}), |
| 88 |
}, |
| 89 |
}, cfg) |
| 90 |
} |
| 91 |
|
| 92 |
func TestUnmarshalDefaultBatchConfig(t *testing.T) { |
| 93 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "default-batch.yaml")) |
| 94 |
require.NoError(t, err) |
| 95 |
factory := NewFactory() |
| 96 |
cfg := factory.CreateDefaultConfig() |
| 97 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 98 |
require.NoError(t, xconfmap.Validate(&cfg)) |
| 99 |
assert.Equal(t, |
| 100 |
&Config{ |
| 101 |
TimeoutConfig: exporterhelper.TimeoutConfig{ |
| 102 |
Timeout: 10 * time.Second, |
| 103 |
}, |
| 104 |
RetryConfig: configretry.NewDefaultBackOffConfig(), |
| 105 |
QueueConfig: exporterhelper.QueueBatchConfig{ |
| 106 |
Enabled: true, |
| 107 |
Sizer: exporterhelper.RequestSizerTypeRequests, |
| 108 |
QueueSize: 1000, |
| 109 |
NumConsumers: 10, |
| 110 |
Batch: configoptional.Some(exporterhelper.BatchConfig{ |
| 111 |
FlushTimeout: 200 * time.Millisecond, |
| 112 |
Sizer: exporterhelper.RequestSizerTypeItems, |
| 113 |
MinSize: 8192, |
| 114 |
}), |
| 115 |
}, |
| 116 |
ClientConfig: configgrpc.ClientConfig{ |
| 117 |
Endpoint: "1.2.3.4:1234", |
| 118 |
Compression: "gzip", |
| 119 |
WriteBufferSize: 512 * 1024, |
| 120 |
}, |
| 121 |
}, cfg) |
| 122 |
} |
| 123 |
|
| 124 |
func TestUnmarshalInvalidConfig(t *testing.T) { |
| 125 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "invalid_configs.yaml")) |
| 126 |
require.NoError(t, err) |
| 127 |
factory := NewFactory() |
| 128 |
for _, tt := range []struct { |
| 129 |
name string |
| 130 |
errorMsg string |
| 131 |
}{ |
| 132 |
{ |
| 133 |
name: "no_endpoint", |
| 134 |
errorMsg: `requires a non-empty "endpoint"`, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "https_endpoint", |
| 138 |
errorMsg: `requires a non-empty "endpoint"`, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "http_endpoint", |
| 142 |
errorMsg: `requires a non-empty "endpoint"`, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "invalid_timeout", |
| 146 |
errorMsg: `'timeout' must be non-negative`, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "invalid_retry", |
| 150 |
errorMsg: `'randomization_factor' must be within [0, 1]`, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "invalid_tls", |
| 154 |
errorMsg: `invalid TLS min_version: unsupported TLS version: "asd"`, |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "missing_port", |
| 158 |
errorMsg: `missing port in address`, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "invalid_port", |
| 162 |
errorMsg: `invalid port "port"`, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "invalid_unix_socket", |
| 166 |
errorMsg: "unix socket path cannot be empty", |
| 167 |
}, |
| 168 |
} { |
| 169 |
t.Run(tt.name, func(t *testing.T) { |
| 170 |
cfg := factory.CreateDefaultConfig() |
| 171 |
sub, err := cm.Sub(tt.name) |
| 172 |
require.NoError(t, err) |
| 173 |
assert.NoError(t, sub.Unmarshal(&cfg)) |
| 174 |
assert.ErrorContains(t, xconfmap.Validate(cfg), tt.errorMsg) |
| 175 |
}) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestValidDNSEndpoint(t *testing.T) { |
| 180 |
factory := NewFactory() |
| 181 |
cfg := factory.CreateDefaultConfig().(*Config) |
| 182 |
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317" |
| 183 |
assert.NoError(t, cfg.Validate()) |
| 184 |
} |
| 185 |
|
| 186 |
func TestValidUnixSocketEndpoint(t *testing.T) { |
| 187 |
factory := NewFactory() |
| 188 |
cfg := factory.CreateDefaultConfig().(*Config) |
| 189 |
cfg.ClientConfig.Endpoint = "unix:///my/unix/socket.sock" |
| 190 |
assert.NoError(t, cfg.Validate()) |
| 191 |
} |
| 192 |
|
| 193 |
func TestSanitizeEndpoint(t *testing.T) { |
| 194 |
factory := NewFactory() |
| 195 |
cfg := factory.CreateDefaultConfig().(*Config) |
| 196 |
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317" |
| 197 |
assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint()) |
| 198 |
cfg.ClientConfig.Endpoint = "dns:///backend.example.com:4317" |
| 199 |
assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint()) |
| 200 |
cfg.ClientConfig.Endpoint = "dns:////backend.example.com:4317" |
| 201 |
assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint()) |
| 202 |
} |