| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package oconf |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/envconfig" |
| 19 |
"go.opentelemetry.io/otel/sdk/metric" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
WeakCertificate = ` |
| 25 |
-----BEGIN CERTIFICATE----- |
| 26 |
MIIBhzCCASygAwIBAgIRANHpHgAWeTnLZpTSxCKs0ggwCgYIKoZIzj0EAwIwEjEQ |
| 27 |
MA4GA1UEChMHb3RlbC1nbzAeFw0yMTA0MDExMzU5MDNaFw0yMTA0MDExNDU5MDNa |
| 28 |
MBIxEDAOBgNVBAoTB290ZWwtZ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAS9 |
| 29 |
nWSkmPCxShxnp43F+PrOtbGV7sNfkbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0Z |
| 30 |
sJCLHGogQsYnWJBXUZOVo2MwYTAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYI |
| 31 |
KwYBBQUHAwEwDAYDVR0TAQH/BAIwADAsBgNVHREEJTAjgglsb2NhbGhvc3SHEAAA |
| 32 |
AAAAAAAAAAAAAAAAAAGHBH8AAAEwCgYIKoZIzj0EAwIDSQAwRgIhANwZVVKvfvQ/ |
| 33 |
1HXsTvgH+xTQswOwSSKYJ1cVHQhqK7ZbAiEAus8NxpTRnp5DiTMuyVmhVNPB+bVH |
| 34 |
Lhnm4N/QDk5rek0= |
| 35 |
-----END CERTIFICATE----- |
| 36 |
` |
| 37 |
WeakPrivateKey = ` |
| 38 |
-----BEGIN PRIVATE KEY----- |
| 39 |
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgN8HEXiXhvByrJ1zK |
| 40 |
SFT6Y2l2KqDWwWzKf+t4CyWrNKehRANCAAS9nWSkmPCxShxnp43F+PrOtbGV7sNf |
| 41 |
kbQ/kxzi9Ego0ZJdiXxkmv/C05QFddCW7Y0ZsJCLHGogQsYnWJBXUZOV |
| 42 |
-----END PRIVATE KEY----- |
| 43 |
` |
| 44 |
) |
| 45 |
|
| 46 |
type env map[string]string |
| 47 |
|
| 48 |
func (e *env) getEnv(env string) string { |
| 49 |
return (*e)[env] |
| 50 |
} |
| 51 |
|
| 52 |
type fileReader map[string][]byte |
| 53 |
|
| 54 |
func (f *fileReader) readFile(filename string) ([]byte, error) { |
| 55 |
if b, ok := (*f)[filename]; ok { |
| 56 |
return b, nil |
| 57 |
} |
| 58 |
return nil, errors.New("file not found") |
| 59 |
} |
| 60 |
|
| 61 |
func TestConfigs(t *testing.T) { |
| 62 |
tlsCert, err := CreateTLSConfig([]byte(WeakCertificate)) |
| 63 |
assert.NoError(t, err) |
| 64 |
|
| 65 |
tests := []struct { |
| 66 |
name string |
| 67 |
opts []GenericOption |
| 68 |
env env |
| 69 |
fileReader fileReader |
| 70 |
asserts func(t *testing.T, c *Config, grpcOption bool) |
| 71 |
}{ |
| 72 |
{ |
| 73 |
name: "Test default configs", |
| 74 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 75 |
if grpcOption { |
| 76 |
assert.Equal(t, "localhost:4317", c.Metrics.Endpoint) |
| 77 |
} else { |
| 78 |
assert.Equal(t, "localhost:4318", c.Metrics.Endpoint) |
| 79 |
} |
| 80 |
assert.Equal(t, NoCompression, c.Metrics.Compression) |
| 81 |
assert.Equal(t, map[string]string(nil), c.Metrics.Headers) |
| 82 |
assert.Equal(t, 10*time.Second, c.Metrics.Timeout) |
| 83 |
}, |
| 84 |
}, |
| 85 |
|
| 86 |
// Endpoint Tests |
| 87 |
{ |
| 88 |
name: "Test With Endpoint", |
| 89 |
opts: []GenericOption{ |
| 90 |
WithEndpoint("someendpoint"), |
| 91 |
}, |
| 92 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 93 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "Test With Endpoint URL", |
| 98 |
opts: []GenericOption{ |
| 99 |
WithEndpointURL("http://someendpoint/somepath"), |
| 100 |
}, |
| 101 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 102 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 103 |
assert.Equal(t, "/somepath", c.Metrics.URLPath) |
| 104 |
assert.True(t, c.Metrics.Insecure) |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "Test With Secure Endpoint URL", |
| 109 |
opts: []GenericOption{ |
| 110 |
WithEndpointURL("https://someendpoint/somepath"), |
| 111 |
}, |
| 112 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 113 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 114 |
assert.Equal(t, "/somepath", c.Metrics.URLPath) |
| 115 |
assert.False(t, c.Metrics.Insecure) |
| 116 |
}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "Test With Invalid Endpoint URL", |
| 120 |
opts: []GenericOption{ |
| 121 |
WithEndpointURL("%invalid"), |
| 122 |
}, |
| 123 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 124 |
if grpcOption { |
| 125 |
assert.Equal(t, "localhost:4317", c.Metrics.Endpoint) |
| 126 |
} else { |
| 127 |
assert.Equal(t, "localhost:4318", c.Metrics.Endpoint) |
| 128 |
} |
| 129 |
assert.Equal(t, "/v1/metrics", c.Metrics.URLPath) |
| 130 |
}, |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "Test With Endpoint last used", |
| 134 |
opts: []GenericOption{ |
| 135 |
WithEndpointURL("https://someendpoint/somepath"), |
| 136 |
WithEndpoint("someendpoint2"), |
| 137 |
}, |
| 138 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 139 |
assert.Equal(t, "someendpoint2", c.Metrics.Endpoint) |
| 140 |
}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "Test With WithEndpointURL last used", |
| 144 |
opts: []GenericOption{ |
| 145 |
WithEndpoint("someendpoint2"), |
| 146 |
WithEndpointURL("https://someendpoint/somepath"), |
| 147 |
}, |
| 148 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 149 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 150 |
}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure", |
| 154 |
env: map[string]string{ |
| 155 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix", |
| 156 |
}, |
| 157 |
opts: []GenericOption{ |
| 158 |
WithEndpointURL("https://someendpoint/somepath"), |
| 159 |
}, |
| 160 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 161 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 162 |
assert.Equal(t, "/somepath", c.Metrics.URLPath) |
| 163 |
assert.False(t, c.Metrics.Insecure) |
| 164 |
}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: "Test With WithEndpointURL secure when Environment insecure is set true", |
| 168 |
env: map[string]string{ |
| 169 |
"OTEL_EXPORTER_OTLP_INSECURE": "true", |
| 170 |
}, |
| 171 |
opts: []GenericOption{ |
| 172 |
WithEndpointURL("https://someendpoint/somepath"), |
| 173 |
}, |
| 174 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 175 |
assert.Equal(t, "someendpoint", c.Metrics.Endpoint) |
| 176 |
assert.Equal(t, "/somepath", c.Metrics.URLPath) |
| 177 |
assert.False(t, c.Metrics.Insecure) |
| 178 |
}, |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "Test Environment Endpoint", |
| 182 |
env: map[string]string{ |
| 183 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "https://env.endpoint/prefix", |
| 184 |
}, |
| 185 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 186 |
assert.False(t, c.Metrics.Insecure) |
| 187 |
if grpcOption { |
| 188 |
assert.Equal(t, "env.endpoint/prefix", c.Metrics.Endpoint) |
| 189 |
} else { |
| 190 |
assert.Equal(t, "env.endpoint", c.Metrics.Endpoint) |
| 191 |
assert.Equal(t, "/prefix/v1/metrics", c.Metrics.URLPath) |
| 192 |
} |
| 193 |
}, |
| 194 |
}, |
| 195 |
{ |
| 196 |
name: "Test Environment Signal Specific Endpoint", |
| 197 |
env: map[string]string{ |
| 198 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "https://overrode.by.signal.specific/env/var", |
| 199 |
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "http://env.metrics.endpoint", |
| 200 |
}, |
| 201 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 202 |
assert.True(t, c.Metrics.Insecure) |
| 203 |
assert.Equal(t, "env.metrics.endpoint", c.Metrics.Endpoint) |
| 204 |
if !grpcOption { |
| 205 |
assert.Equal(t, "/", c.Metrics.URLPath) |
| 206 |
} |
| 207 |
}, |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "Test Mixed Environment and With Endpoint", |
| 211 |
opts: []GenericOption{ |
| 212 |
WithEndpointURL("https://metrics_endpoint2/somepath"), |
| 213 |
WithEndpoint("metrics_endpoint"), |
| 214 |
}, |
| 215 |
env: map[string]string{ |
| 216 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "env_endpoint", |
| 217 |
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "env_endpoint2", |
| 218 |
}, |
| 219 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 220 |
assert.Equal(t, "metrics_endpoint", c.Metrics.Endpoint) |
| 221 |
}, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "Test Mixed Environment and With Endpoint", |
| 225 |
opts: []GenericOption{ |
| 226 |
WithEndpoint("metrics_endpoint"), |
| 227 |
WithEndpointURL("https://metrics_endpoint2/somepath"), |
| 228 |
}, |
| 229 |
env: map[string]string{ |
| 230 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "env_endpoint", |
| 231 |
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "env_endpoint2", |
| 232 |
}, |
| 233 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 234 |
assert.Equal(t, "metrics_endpoint2", c.Metrics.Endpoint) |
| 235 |
}, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "Test Environment Endpoint with HTTP scheme", |
| 239 |
env: map[string]string{ |
| 240 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env_endpoint", |
| 241 |
}, |
| 242 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 243 |
assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) |
| 244 |
assert.True(t, c.Metrics.Insecure) |
| 245 |
}, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "Test Environment Endpoint with HTTP scheme and leading & trailingspaces", |
| 249 |
env: map[string]string{ |
| 250 |
"OTEL_EXPORTER_OTLP_ENDPOINT": " http://env_endpoint ", |
| 251 |
}, |
| 252 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 253 |
assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) |
| 254 |
assert.True(t, c.Metrics.Insecure) |
| 255 |
}, |
| 256 |
}, |
| 257 |
{ |
| 258 |
name: "Test Environment Endpoint with HTTPS scheme", |
| 259 |
env: map[string]string{ |
| 260 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "https://env_endpoint", |
| 261 |
}, |
| 262 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 263 |
assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) |
| 264 |
assert.False(t, c.Metrics.Insecure) |
| 265 |
}, |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "Test Environment Signal Specific Endpoint with uppercase scheme", |
| 269 |
env: map[string]string{ |
| 270 |
"OTEL_EXPORTER_OTLP_ENDPOINT": "HTTPS://overrode_by_signal_specific", |
| 271 |
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "HtTp://env_metrics_endpoint", |
| 272 |
}, |
| 273 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 274 |
assert.Equal(t, "env_metrics_endpoint", c.Metrics.Endpoint) |
| 275 |
assert.True(t, c.Metrics.Insecure) |
| 276 |
}, |
| 277 |
}, |
| 278 |
|
| 279 |
// Certificate tests |
| 280 |
{ |
| 281 |
name: "Test Default Certificate", |
| 282 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 283 |
if grpcOption { |
| 284 |
assert.NotNil(t, c.Metrics.GRPCCredentials) |
| 285 |
} else { |
| 286 |
assert.Nil(t, c.Metrics.TLSCfg) |
| 287 |
} |
| 288 |
}, |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "Test With Certificate", |
| 292 |
opts: []GenericOption{ |
| 293 |
WithTLSClientConfig(tlsCert), |
| 294 |
}, |
| 295 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 296 |
if grpcOption { |
| 297 |
// TODO: make sure gRPC's credentials actually works |
| 298 |
assert.NotNil(t, c.Metrics.GRPCCredentials) |
| 299 |
} else { |
| 300 |
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool. |
| 301 |
assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Metrics.TLSCfg.RootCAs.Subjects()) |
| 302 |
} |
| 303 |
}, |
| 304 |
}, |
| 305 |
{ |
| 306 |
name: "Test Environment Certificate", |
| 307 |
env: map[string]string{ |
| 308 |
"OTEL_EXPORTER_OTLP_CERTIFICATE": "cert_path", |
| 309 |
}, |
| 310 |
fileReader: fileReader{ |
| 311 |
"cert_path": []byte(WeakCertificate), |
| 312 |
}, |
| 313 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 314 |
if grpcOption { |
| 315 |
assert.NotNil(t, c.Metrics.GRPCCredentials) |
| 316 |
} else { |
| 317 |
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool. |
| 318 |
assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Metrics.TLSCfg.RootCAs.Subjects()) |
| 319 |
} |
| 320 |
}, |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "Test Environment Signal Specific Certificate", |
| 324 |
env: map[string]string{ |
| 325 |
"OTEL_EXPORTER_OTLP_CERTIFICATE": "overrode_by_signal_specific", |
| 326 |
"OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE": "cert_path", |
| 327 |
}, |
| 328 |
fileReader: fileReader{ |
| 329 |
"cert_path": []byte(WeakCertificate), |
| 330 |
"invalid_cert": []byte("invalid certificate file."), |
| 331 |
}, |
| 332 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 333 |
if grpcOption { |
| 334 |
assert.NotNil(t, c.Metrics.GRPCCredentials) |
| 335 |
} else { |
| 336 |
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool. |
| 337 |
assert.Equal(t, tlsCert.RootCAs.Subjects(), c.Metrics.TLSCfg.RootCAs.Subjects()) |
| 338 |
} |
| 339 |
}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
name: "Test Mixed Environment and With Certificate", |
| 343 |
opts: []GenericOption{}, |
| 344 |
env: map[string]string{ |
| 345 |
"OTEL_EXPORTER_OTLP_CERTIFICATE": "cert_path", |
| 346 |
}, |
| 347 |
fileReader: fileReader{ |
| 348 |
"cert_path": []byte(WeakCertificate), |
| 349 |
}, |
| 350 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 351 |
if grpcOption { |
| 352 |
assert.NotNil(t, c.Metrics.GRPCCredentials) |
| 353 |
} else { |
| 354 |
// nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool. |
| 355 |
assert.Len(t, c.Metrics.TLSCfg.RootCAs.Subjects(), 1) |
| 356 |
} |
| 357 |
}, |
| 358 |
}, |
| 359 |
|
| 360 |
// Headers tests |
| 361 |
{ |
| 362 |
name: "Test With Headers", |
| 363 |
opts: []GenericOption{ |
| 364 |
WithHeaders(map[string]string{"h1": "v1"}), |
| 365 |
}, |
| 366 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 367 |
assert.Equal(t, map[string]string{"h1": "v1"}, c.Metrics.Headers) |
| 368 |
}, |
| 369 |
}, |
| 370 |
{ |
| 371 |
name: "Test Environment Headers", |
| 372 |
env: map[string]string{"OTEL_EXPORTER_OTLP_HEADERS": "h1=v1,h2=v2"}, |
| 373 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 374 |
assert.Equal(t, map[string]string{"h1": "v1", "h2": "v2"}, c.Metrics.Headers) |
| 375 |
}, |
| 376 |
}, |
| 377 |
{ |
| 378 |
name: "Test Environment Signal Specific Headers", |
| 379 |
env: map[string]string{ |
| 380 |
"OTEL_EXPORTER_OTLP_HEADERS": "overrode_by_signal_specific", |
| 381 |
"OTEL_EXPORTER_OTLP_METRICS_HEADERS": "h1=v1,h2=v2", |
| 382 |
}, |
| 383 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 384 |
assert.Equal(t, map[string]string{"h1": "v1", "h2": "v2"}, c.Metrics.Headers) |
| 385 |
}, |
| 386 |
}, |
| 387 |
{ |
| 388 |
name: "Test Mixed Environment and With Headers", |
| 389 |
env: map[string]string{"OTEL_EXPORTER_OTLP_HEADERS": "h1=v1,h2=v2"}, |
| 390 |
opts: []GenericOption{ |
| 391 |
WithHeaders(map[string]string{"m1": "mv1"}), |
| 392 |
}, |
| 393 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 394 |
assert.Equal(t, map[string]string{"m1": "mv1"}, c.Metrics.Headers) |
| 395 |
}, |
| 396 |
}, |
| 397 |
|
| 398 |
// Compression Tests |
| 399 |
{ |
| 400 |
name: "Test With Compression", |
| 401 |
opts: []GenericOption{ |
| 402 |
WithCompression(GzipCompression), |
| 403 |
}, |
| 404 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 405 |
assert.Equal(t, GzipCompression, c.Metrics.Compression) |
| 406 |
}, |
| 407 |
}, |
| 408 |
{ |
| 409 |
name: "Test Environment Compression", |
| 410 |
env: map[string]string{ |
| 411 |
"OTEL_EXPORTER_OTLP_COMPRESSION": "gzip", |
| 412 |
}, |
| 413 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 414 |
assert.Equal(t, GzipCompression, c.Metrics.Compression) |
| 415 |
}, |
| 416 |
}, |
| 417 |
{ |
| 418 |
name: "Test Environment Signal Specific Compression", |
| 419 |
env: map[string]string{ |
| 420 |
"OTEL_EXPORTER_OTLP_METRICS_COMPRESSION": "gzip", |
| 421 |
}, |
| 422 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 423 |
assert.Equal(t, GzipCompression, c.Metrics.Compression) |
| 424 |
}, |
| 425 |
}, |
| 426 |
{ |
| 427 |
name: "Test Mixed Environment and With Compression", |
| 428 |
opts: []GenericOption{ |
| 429 |
WithCompression(NoCompression), |
| 430 |
}, |
| 431 |
env: map[string]string{ |
| 432 |
"OTEL_EXPORTER_OTLP_METRICS_COMPRESSION": "gzip", |
| 433 |
}, |
| 434 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 435 |
assert.Equal(t, NoCompression, c.Metrics.Compression) |
| 436 |
}, |
| 437 |
}, |
| 438 |
|
| 439 |
// Timeout Tests |
| 440 |
{ |
| 441 |
name: "Test With Timeout", |
| 442 |
opts: []GenericOption{ |
| 443 |
WithTimeout(5 * time.Second), |
| 444 |
}, |
| 445 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 446 |
assert.Equal(t, 5*time.Second, c.Metrics.Timeout) |
| 447 |
}, |
| 448 |
}, |
| 449 |
{ |
| 450 |
name: "Test Environment Timeout", |
| 451 |
env: map[string]string{ |
| 452 |
"OTEL_EXPORTER_OTLP_TIMEOUT": "15000", |
| 453 |
}, |
| 454 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 455 |
assert.Equal(t, 15*time.Second, c.Metrics.Timeout) |
| 456 |
}, |
| 457 |
}, |
| 458 |
{ |
| 459 |
name: "Test Environment Signal Specific Timeout", |
| 460 |
env: map[string]string{ |
| 461 |
"OTEL_EXPORTER_OTLP_TIMEOUT": "15000", |
| 462 |
"OTEL_EXPORTER_OTLP_METRICS_TIMEOUT": "28000", |
| 463 |
}, |
| 464 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 465 |
assert.Equal(t, 28*time.Second, c.Metrics.Timeout) |
| 466 |
}, |
| 467 |
}, |
| 468 |
{ |
| 469 |
name: "Test Mixed Environment and With Timeout", |
| 470 |
env: map[string]string{ |
| 471 |
"OTEL_EXPORTER_OTLP_TIMEOUT": "15000", |
| 472 |
"OTEL_EXPORTER_OTLP_METRICS_TIMEOUT": "28000", |
| 473 |
}, |
| 474 |
opts: []GenericOption{ |
| 475 |
WithTimeout(5 * time.Second), |
| 476 |
}, |
| 477 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 478 |
assert.Equal(t, 5*time.Second, c.Metrics.Timeout) |
| 479 |
}, |
| 480 |
}, |
| 481 |
|
| 482 |
// Temporality Selector Tests |
| 483 |
{ |
| 484 |
name: "WithTemporalitySelector", |
| 485 |
opts: []GenericOption{ |
| 486 |
WithTemporalitySelector(deltaSelector), |
| 487 |
}, |
| 488 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 489 |
// Function value comparisons are disallowed, test non-default |
| 490 |
// behavior of a TemporalitySelector here to ensure our "catch |
| 491 |
// all" was set. |
| 492 |
var undefinedKind metric.InstrumentKind |
| 493 |
got := c.Metrics.TemporalitySelector |
| 494 |
assert.Equal(t, metricdata.DeltaTemporality, got(undefinedKind)) |
| 495 |
}, |
| 496 |
}, |
| 497 |
|
| 498 |
// Aggregation Selector Tests |
| 499 |
{ |
| 500 |
name: "WithAggregationSelector", |
| 501 |
opts: []GenericOption{ |
| 502 |
WithAggregationSelector(dropSelector), |
| 503 |
}, |
| 504 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 505 |
// Function value comparisons are disallowed, test non-default |
| 506 |
// behavior of a AggregationSelector here to ensure our "catch |
| 507 |
// all" was set. |
| 508 |
var undefinedKind metric.InstrumentKind |
| 509 |
got := c.Metrics.AggregationSelector |
| 510 |
assert.Equal(t, metric.AggregationDrop{}, got(undefinedKind)) |
| 511 |
}, |
| 512 |
}, |
| 513 |
|
| 514 |
// Proxy Tests |
| 515 |
{ |
| 516 |
name: "Test With Proxy", |
| 517 |
opts: []GenericOption{ |
| 518 |
WithProxy(func(r *http.Request) (*url.URL, error) { |
| 519 |
return url.Parse("http://proxy.com") |
| 520 |
}), |
| 521 |
}, |
| 522 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 523 |
assert.NotNil(t, c.Metrics.Proxy) |
| 524 |
proxyURL, err := c.Metrics.Proxy(&http.Request{}) |
| 525 |
assert.NoError(t, err) |
| 526 |
assert.Equal(t, "http://proxy.com", proxyURL.String()) |
| 527 |
}, |
| 528 |
}, |
| 529 |
{ |
| 530 |
name: "Test Without Proxy", |
| 531 |
opts: []GenericOption{}, |
| 532 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 533 |
assert.Nil(t, c.Metrics.Proxy) |
| 534 |
}, |
| 535 |
}, |
| 536 |
|
| 537 |
// HTTP Client Tests |
| 538 |
{ |
| 539 |
name: "Test With HTTP Client", |
| 540 |
opts: []GenericOption{ |
| 541 |
WithHTTPClient(http.DefaultClient), |
| 542 |
}, |
| 543 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 544 |
assert.Equal(t, http.DefaultClient, c.Metrics.HTTPClient) |
| 545 |
}, |
| 546 |
}, |
| 547 |
{ |
| 548 |
name: "Test Without HTTP Client", |
| 549 |
opts: []GenericOption{}, |
| 550 |
asserts: func(t *testing.T, c *Config, grpcOption bool) { |
| 551 |
assert.Nil(t, c.Metrics.HTTPClient) |
| 552 |
}, |
| 553 |
}, |
| 554 |
} |
| 555 |
|
| 556 |
for _, tt := range tests { |
| 557 |
t.Run(tt.name, func(t *testing.T) { |
| 558 |
origEOR := DefaultEnvOptionsReader |
| 559 |
DefaultEnvOptionsReader = envconfig.EnvOptionsReader{ |
| 560 |
GetEnv: tt.env.getEnv, |
| 561 |
ReadFile: tt.fileReader.readFile, |
| 562 |
Namespace: "OTEL_EXPORTER_OTLP", |
| 563 |
} |
| 564 |
t.Cleanup(func() { DefaultEnvOptionsReader = origEOR }) |
| 565 |
|
| 566 |
// Tests Generic options as HTTP Options |
| 567 |
cfg := NewHTTPConfig(asHTTPOptions(tt.opts)...) |
| 568 |
tt.asserts(t, &cfg, false) |
| 569 |
|
| 570 |
// Tests Generic options as gRPC Options |
| 571 |
cfg = NewGRPCConfig(asGRPCOptions(tt.opts)...) |
| 572 |
tt.asserts(t, &cfg, true) |
| 573 |
}) |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
func dropSelector(metric.InstrumentKind) metric.Aggregation { |
| 578 |
return metric.AggregationDrop{} |
| 579 |
} |
| 580 |
|
| 581 |
func deltaSelector(metric.InstrumentKind) metricdata.Temporality { |
| 582 |
return metricdata.DeltaTemporality |
| 583 |
} |
| 584 |
|
| 585 |
func asHTTPOptions(opts []GenericOption) []HTTPOption { |
| 586 |
converted := make([]HTTPOption, len(opts)) |
| 587 |
for i, o := range opts { |
| 588 |
converted[i] = NewHTTPOption(o.ApplyHTTPOption) |
| 589 |
} |
| 590 |
return converted |
| 591 |
} |
| 592 |
|
| 593 |
func asGRPCOptions(opts []GenericOption) []GRPCOption { |
| 594 |
converted := make([]GRPCOption, len(opts)) |
| 595 |
for i, o := range opts { |
| 596 |
converted[i] = NewGRPCOption(o.ApplyGRPCOption) |
| 597 |
} |
| 598 |
return converted |
| 599 |
} |
| 600 |
|
| 601 |
func TestCleanPath(t *testing.T) { |
| 602 |
type args struct { |
| 603 |
urlPath string |
| 604 |
defaultPath string |
| 605 |
} |
| 606 |
tests := []struct { |
| 607 |
name string |
| 608 |
args args |
| 609 |
want string |
| 610 |
}{ |
| 611 |
{ |
| 612 |
name: "clean empty path", |
| 613 |
args: args{ |
| 614 |
urlPath: "", |
| 615 |
defaultPath: "DefaultPath", |
| 616 |
}, |
| 617 |
want: "DefaultPath", |
| 618 |
}, |
| 619 |
{ |
| 620 |
name: "clean metrics path", |
| 621 |
args: args{ |
| 622 |
urlPath: "/prefix/v1/metrics", |
| 623 |
defaultPath: "DefaultMetricsPath", |
| 624 |
}, |
| 625 |
want: "/prefix/v1/metrics", |
| 626 |
}, |
| 627 |
{ |
| 628 |
name: "clean traces path", |
| 629 |
args: args{ |
| 630 |
urlPath: "https://env_endpoint/ ", |
| 631 |
defaultPath: "DefaultTracesPath", |
| 632 |
}, |
| 633 |
want: "/https://env_endpoint/", |
| 634 |
}, |
| 635 |
{ |
| 636 |
name: "spaces trimmed", |
| 637 |
args: args{ |
| 638 |
urlPath: " /dir", |
| 639 |
}, |
| 640 |
want: "/dir", |
| 641 |
}, |
| 642 |
{ |
| 643 |
name: "make absolute", |
| 644 |
args: args{ |
| 645 |
urlPath: "dir/a", |
| 646 |
}, |
| 647 |
want: "/dir/a", |
| 648 |
}, |
| 649 |
} |
| 650 |
for _, tt := range tests { |
| 651 |
t.Run(tt.name, func(t *testing.T) { |
| 652 |
if got := cleanPath(tt.args.urlPath, tt.args.defaultPath); got != tt.want { |
| 653 |
t.Errorf("CleanPath() = %v, want %v", got, tt.want) |
| 654 |
} |
| 655 |
}) |
| 656 |
} |
| 657 |
} |