| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelconf |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"net/url" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" |
| 15 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 16 |
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" |
| 17 |
"go.opentelemetry.io/otel/sdk/resource" |
| 18 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 19 |
"go.opentelemetry.io/otel/trace" |
| 20 |
"go.opentelemetry.io/otel/trace/noop" |
| 21 |
) |
| 22 |
|
| 23 |
func TestTracerPovider(t *testing.T) { |
| 24 |
tests := []struct { |
| 25 |
name string |
| 26 |
cfg configOptions |
| 27 |
wantProvider trace.TracerProvider |
| 28 |
wantErr error |
| 29 |
}{ |
| 30 |
{ |
| 31 |
name: "no-tracer-provider-configured", |
| 32 |
wantProvider: noop.NewTracerProvider(), |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "error-in-config", |
| 36 |
cfg: configOptions{ |
| 37 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 38 |
TracerProvider: &TracerProvider{ |
| 39 |
Processors: []SpanProcessor{ |
| 40 |
{ |
| 41 |
Batch: &BatchSpanProcessor{}, |
| 42 |
Simple: &SimpleSpanProcessor{}, |
| 43 |
}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
}, |
| 47 |
}, |
| 48 |
wantProvider: noop.NewTracerProvider(), |
| 49 |
wantErr: errors.Join(errors.New("must not specify multiple span processor type")), |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "multiple-errors-in-config", |
| 53 |
cfg: configOptions{ |
| 54 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 55 |
TracerProvider: &TracerProvider{ |
| 56 |
Processors: []SpanProcessor{ |
| 57 |
{ |
| 58 |
Batch: &BatchSpanProcessor{}, |
| 59 |
Simple: &SimpleSpanProcessor{}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
Simple: &SimpleSpanProcessor{ |
| 63 |
Exporter: SpanExporter{ |
| 64 |
Console: Console{}, |
| 65 |
OTLP: &OTLP{}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
wantProvider: noop.NewTracerProvider(), |
| 74 |
wantErr: errors.Join(errors.New("must not specify multiple span processor type"), errors.New("must not specify multiple exporters")), |
| 75 |
}, |
| 76 |
} |
| 77 |
for _, tt := range tests { |
| 78 |
tp, shutdown, err := tracerProvider(tt.cfg, resource.Default()) |
| 79 |
require.Equal(t, tt.wantProvider, tp) |
| 80 |
assert.Equal(t, tt.wantErr, err) |
| 81 |
require.NoError(t, shutdown(t.Context())) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestSpanProcessor(t *testing.T) { |
| 86 |
consoleExporter, err := stdouttrace.New( |
| 87 |
stdouttrace.WithPrettyPrint(), |
| 88 |
) |
| 89 |
require.NoError(t, err) |
| 90 |
ctx := t.Context() |
| 91 |
otlpGRPCExporter, err := otlptracegrpc.New(ctx) |
| 92 |
require.NoError(t, err) |
| 93 |
otlpHTTPExporter, err := otlptracehttp.New(ctx) |
| 94 |
require.NoError(t, err) |
| 95 |
testCases := []struct { |
| 96 |
name string |
| 97 |
processor SpanProcessor |
| 98 |
args any |
| 99 |
wantErr error |
| 100 |
wantProcessor sdktrace.SpanProcessor |
| 101 |
}{ |
| 102 |
{ |
| 103 |
name: "no processor", |
| 104 |
wantErr: errors.New("unsupported span processor type, must be one of simple or batch"), |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "multiple processor types", |
| 108 |
processor: SpanProcessor{ |
| 109 |
Batch: &BatchSpanProcessor{ |
| 110 |
Exporter: SpanExporter{}, |
| 111 |
}, |
| 112 |
Simple: &SimpleSpanProcessor{}, |
| 113 |
}, |
| 114 |
wantErr: errors.New("must not specify multiple span processor type"), |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "batch processor invalid exporter", |
| 118 |
processor: SpanProcessor{ |
| 119 |
Batch: &BatchSpanProcessor{ |
| 120 |
Exporter: SpanExporter{}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
wantErr: errors.New("no valid span exporter"), |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "batch processor invalid batch size console exporter", |
| 127 |
processor: SpanProcessor{ |
| 128 |
Batch: &BatchSpanProcessor{ |
| 129 |
MaxExportBatchSize: ptr(-1), |
| 130 |
Exporter: SpanExporter{ |
| 131 |
Console: Console{}, |
| 132 |
}, |
| 133 |
}, |
| 134 |
}, |
| 135 |
wantErr: errors.New("invalid batch size -1"), |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "batch processor invalid export timeout console exporter", |
| 139 |
processor: SpanProcessor{ |
| 140 |
Batch: &BatchSpanProcessor{ |
| 141 |
ExportTimeout: ptr(-2), |
| 142 |
Exporter: SpanExporter{ |
| 143 |
Console: Console{}, |
| 144 |
}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
wantErr: errors.New("invalid export timeout -2"), |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "batch processor invalid queue size console exporter", |
| 151 |
processor: SpanProcessor{ |
| 152 |
Batch: &BatchSpanProcessor{ |
| 153 |
MaxQueueSize: ptr(-3), |
| 154 |
Exporter: SpanExporter{ |
| 155 |
Console: Console{}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
}, |
| 159 |
wantErr: errors.New("invalid queue size -3"), |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "batch processor invalid schedule delay console exporter", |
| 163 |
processor: SpanProcessor{ |
| 164 |
Batch: &BatchSpanProcessor{ |
| 165 |
ScheduleDelay: ptr(-4), |
| 166 |
Exporter: SpanExporter{ |
| 167 |
Console: Console{}, |
| 168 |
}, |
| 169 |
}, |
| 170 |
}, |
| 171 |
wantErr: errors.New("invalid schedule delay -4"), |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "batch processor with multiple exporters", |
| 175 |
processor: SpanProcessor{ |
| 176 |
Batch: &BatchSpanProcessor{ |
| 177 |
Exporter: SpanExporter{ |
| 178 |
Console: Console{}, |
| 179 |
OTLP: &OTLP{}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
wantErr: errors.New("must not specify multiple exporters"), |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "batch processor console exporter", |
| 187 |
processor: SpanProcessor{ |
| 188 |
Batch: &BatchSpanProcessor{ |
| 189 |
MaxExportBatchSize: ptr(0), |
| 190 |
ExportTimeout: ptr(0), |
| 191 |
MaxQueueSize: ptr(0), |
| 192 |
ScheduleDelay: ptr(0), |
| 193 |
Exporter: SpanExporter{ |
| 194 |
Console: Console{}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
}, |
| 198 |
wantProcessor: sdktrace.NewBatchSpanProcessor(consoleExporter), |
| 199 |
}, |
| 200 |
{ |
| 201 |
name: "batch/otlp-exporter-invalid-protocol", |
| 202 |
processor: SpanProcessor{ |
| 203 |
Batch: &BatchSpanProcessor{ |
| 204 |
MaxExportBatchSize: ptr(0), |
| 205 |
ExportTimeout: ptr(0), |
| 206 |
MaxQueueSize: ptr(0), |
| 207 |
ScheduleDelay: ptr(0), |
| 208 |
Exporter: SpanExporter{ |
| 209 |
OTLP: &OTLP{ |
| 210 |
Protocol: "http/invalid", |
| 211 |
}, |
| 212 |
}, |
| 213 |
}, |
| 214 |
}, |
| 215 |
wantErr: errors.New("unsupported protocol \"http/invalid\""), |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "batch/otlp-grpc-exporter-no-endpoint", |
| 219 |
processor: SpanProcessor{ |
| 220 |
Batch: &BatchSpanProcessor{ |
| 221 |
MaxExportBatchSize: ptr(0), |
| 222 |
ExportTimeout: ptr(0), |
| 223 |
MaxQueueSize: ptr(0), |
| 224 |
ScheduleDelay: ptr(0), |
| 225 |
Exporter: SpanExporter{ |
| 226 |
OTLP: &OTLP{ |
| 227 |
Protocol: "grpc/protobuf", |
| 228 |
Compression: ptr("gzip"), |
| 229 |
Timeout: ptr(1000), |
| 230 |
Headers: map[string]string{ |
| 231 |
"test": "test1", |
| 232 |
}, |
| 233 |
}, |
| 234 |
}, |
| 235 |
}, |
| 236 |
}, |
| 237 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpGRPCExporter), |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "batch/otlp-grpc-exporter", |
| 241 |
processor: SpanProcessor{ |
| 242 |
Batch: &BatchSpanProcessor{ |
| 243 |
MaxExportBatchSize: ptr(0), |
| 244 |
ExportTimeout: ptr(0), |
| 245 |
MaxQueueSize: ptr(0), |
| 246 |
ScheduleDelay: ptr(0), |
| 247 |
Exporter: SpanExporter{ |
| 248 |
OTLP: &OTLP{ |
| 249 |
Protocol: "grpc/protobuf", |
| 250 |
Endpoint: "http://localhost:4317", |
| 251 |
Compression: ptr("gzip"), |
| 252 |
Timeout: ptr(1000), |
| 253 |
Headers: map[string]string{ |
| 254 |
"test": "test1", |
| 255 |
}, |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpGRPCExporter), |
| 261 |
}, |
| 262 |
{ |
| 263 |
name: "batch/otlp-grpc-exporter-no-scheme", |
| 264 |
processor: SpanProcessor{ |
| 265 |
Batch: &BatchSpanProcessor{ |
| 266 |
MaxExportBatchSize: ptr(0), |
| 267 |
ExportTimeout: ptr(0), |
| 268 |
MaxQueueSize: ptr(0), |
| 269 |
ScheduleDelay: ptr(0), |
| 270 |
Exporter: SpanExporter{ |
| 271 |
OTLP: &OTLP{ |
| 272 |
Protocol: "grpc/protobuf", |
| 273 |
Endpoint: "localhost:4317", |
| 274 |
Compression: ptr("gzip"), |
| 275 |
Timeout: ptr(1000), |
| 276 |
Headers: map[string]string{ |
| 277 |
"test": "test1", |
| 278 |
}, |
| 279 |
}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpGRPCExporter), |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "batch/otlp-grpc-invalid-endpoint", |
| 287 |
processor: SpanProcessor{ |
| 288 |
Batch: &BatchSpanProcessor{ |
| 289 |
MaxExportBatchSize: ptr(0), |
| 290 |
ExportTimeout: ptr(0), |
| 291 |
MaxQueueSize: ptr(0), |
| 292 |
ScheduleDelay: ptr(0), |
| 293 |
Exporter: SpanExporter{ |
| 294 |
OTLP: &OTLP{ |
| 295 |
Protocol: "grpc/protobuf", |
| 296 |
Endpoint: " ", |
| 297 |
Compression: ptr("gzip"), |
| 298 |
Timeout: ptr(1000), |
| 299 |
Headers: map[string]string{ |
| 300 |
"test": "test1", |
| 301 |
}, |
| 302 |
}, |
| 303 |
}, |
| 304 |
}, |
| 305 |
}, |
| 306 |
wantErr: &url.Error{Op: "parse", URL: " ", Err: errors.New("invalid URI for request")}, |
| 307 |
}, |
| 308 |
{ |
| 309 |
name: "batch/otlp-grpc-invalid-compression", |
| 310 |
processor: SpanProcessor{ |
| 311 |
Batch: &BatchSpanProcessor{ |
| 312 |
MaxExportBatchSize: ptr(0), |
| 313 |
ExportTimeout: ptr(0), |
| 314 |
MaxQueueSize: ptr(0), |
| 315 |
ScheduleDelay: ptr(0), |
| 316 |
Exporter: SpanExporter{ |
| 317 |
OTLP: &OTLP{ |
| 318 |
Protocol: "grpc/protobuf", |
| 319 |
Endpoint: "localhost:4317", |
| 320 |
Compression: ptr("invalid"), |
| 321 |
Timeout: ptr(1000), |
| 322 |
Headers: map[string]string{ |
| 323 |
"test": "test1", |
| 324 |
}, |
| 325 |
}, |
| 326 |
}, |
| 327 |
}, |
| 328 |
}, |
| 329 |
wantErr: errors.New("unsupported compression \"invalid\""), |
| 330 |
}, |
| 331 |
{ |
| 332 |
name: "batch/otlp-http-exporter", |
| 333 |
processor: SpanProcessor{ |
| 334 |
Batch: &BatchSpanProcessor{ |
| 335 |
MaxExportBatchSize: ptr(0), |
| 336 |
ExportTimeout: ptr(0), |
| 337 |
MaxQueueSize: ptr(0), |
| 338 |
ScheduleDelay: ptr(0), |
| 339 |
Exporter: SpanExporter{ |
| 340 |
OTLP: &OTLP{ |
| 341 |
Protocol: "http/protobuf", |
| 342 |
Endpoint: "http://localhost:4318", |
| 343 |
Compression: ptr("gzip"), |
| 344 |
Timeout: ptr(1000), |
| 345 |
Headers: map[string]string{ |
| 346 |
"test": "test1", |
| 347 |
}, |
| 348 |
}, |
| 349 |
}, |
| 350 |
}, |
| 351 |
}, |
| 352 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter), |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "batch/otlp-http-exporter-with-path", |
| 356 |
processor: SpanProcessor{ |
| 357 |
Batch: &BatchSpanProcessor{ |
| 358 |
MaxExportBatchSize: ptr(0), |
| 359 |
ExportTimeout: ptr(0), |
| 360 |
MaxQueueSize: ptr(0), |
| 361 |
ScheduleDelay: ptr(0), |
| 362 |
Exporter: SpanExporter{ |
| 363 |
OTLP: &OTLP{ |
| 364 |
Protocol: "http/protobuf", |
| 365 |
Endpoint: "http://localhost:4318/path/123", |
| 366 |
Compression: ptr("none"), |
| 367 |
Timeout: ptr(1000), |
| 368 |
Headers: map[string]string{ |
| 369 |
"test": "test1", |
| 370 |
}, |
| 371 |
}, |
| 372 |
}, |
| 373 |
}, |
| 374 |
}, |
| 375 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter), |
| 376 |
}, |
| 377 |
{ |
| 378 |
name: "batch/otlp-http-exporter-no-endpoint", |
| 379 |
processor: SpanProcessor{ |
| 380 |
Batch: &BatchSpanProcessor{ |
| 381 |
MaxExportBatchSize: ptr(0), |
| 382 |
ExportTimeout: ptr(0), |
| 383 |
MaxQueueSize: ptr(0), |
| 384 |
ScheduleDelay: ptr(0), |
| 385 |
Exporter: SpanExporter{ |
| 386 |
OTLP: &OTLP{ |
| 387 |
Protocol: "http/protobuf", |
| 388 |
Compression: ptr("gzip"), |
| 389 |
Timeout: ptr(1000), |
| 390 |
Headers: map[string]string{ |
| 391 |
"test": "test1", |
| 392 |
}, |
| 393 |
}, |
| 394 |
}, |
| 395 |
}, |
| 396 |
}, |
| 397 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter), |
| 398 |
}, |
| 399 |
{ |
| 400 |
name: "batch/otlp-http-exporter-no-scheme", |
| 401 |
processor: SpanProcessor{ |
| 402 |
Batch: &BatchSpanProcessor{ |
| 403 |
MaxExportBatchSize: ptr(0), |
| 404 |
ExportTimeout: ptr(0), |
| 405 |
MaxQueueSize: ptr(0), |
| 406 |
ScheduleDelay: ptr(0), |
| 407 |
Exporter: SpanExporter{ |
| 408 |
OTLP: &OTLP{ |
| 409 |
Protocol: "http/protobuf", |
| 410 |
Endpoint: "localhost:4318", |
| 411 |
Compression: ptr("gzip"), |
| 412 |
Timeout: ptr(1000), |
| 413 |
Headers: map[string]string{ |
| 414 |
"test": "test1", |
| 415 |
}, |
| 416 |
}, |
| 417 |
}, |
| 418 |
}, |
| 419 |
}, |
| 420 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter), |
| 421 |
}, |
| 422 |
{ |
| 423 |
name: "batch/otlp-http-invalid-endpoint", |
| 424 |
processor: SpanProcessor{ |
| 425 |
Batch: &BatchSpanProcessor{ |
| 426 |
MaxExportBatchSize: ptr(0), |
| 427 |
ExportTimeout: ptr(0), |
| 428 |
MaxQueueSize: ptr(0), |
| 429 |
ScheduleDelay: ptr(0), |
| 430 |
Exporter: SpanExporter{ |
| 431 |
OTLP: &OTLP{ |
| 432 |
Protocol: "http/protobuf", |
| 433 |
Endpoint: " ", |
| 434 |
Compression: ptr("gzip"), |
| 435 |
Timeout: ptr(1000), |
| 436 |
Headers: map[string]string{ |
| 437 |
"test": "test1", |
| 438 |
}, |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
}, |
| 443 |
wantErr: &url.Error{Op: "parse", URL: " ", Err: errors.New("invalid URI for request")}, |
| 444 |
}, |
| 445 |
{ |
| 446 |
name: "batch/otlp-http-none-compression", |
| 447 |
processor: SpanProcessor{ |
| 448 |
Batch: &BatchSpanProcessor{ |
| 449 |
MaxExportBatchSize: ptr(0), |
| 450 |
ExportTimeout: ptr(0), |
| 451 |
MaxQueueSize: ptr(0), |
| 452 |
ScheduleDelay: ptr(0), |
| 453 |
Exporter: SpanExporter{ |
| 454 |
OTLP: &OTLP{ |
| 455 |
Protocol: "http/protobuf", |
| 456 |
Endpoint: "localhost:4318", |
| 457 |
Compression: ptr("none"), |
| 458 |
Timeout: ptr(1000), |
| 459 |
Headers: map[string]string{ |
| 460 |
"test": "test1", |
| 461 |
}, |
| 462 |
}, |
| 463 |
}, |
| 464 |
}, |
| 465 |
}, |
| 466 |
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter), |
| 467 |
}, |
| 468 |
{ |
| 469 |
name: "batch/otlp-http-invalid-compression", |
| 470 |
processor: SpanProcessor{ |
| 471 |
Batch: &BatchSpanProcessor{ |
| 472 |
MaxExportBatchSize: ptr(0), |
| 473 |
ExportTimeout: ptr(0), |
| 474 |
MaxQueueSize: ptr(0), |
| 475 |
ScheduleDelay: ptr(0), |
| 476 |
Exporter: SpanExporter{ |
| 477 |
OTLP: &OTLP{ |
| 478 |
Protocol: "http/protobuf", |
| 479 |
Endpoint: "localhost:4318", |
| 480 |
Compression: ptr("invalid"), |
| 481 |
Timeout: ptr(1000), |
| 482 |
Headers: map[string]string{ |
| 483 |
"test": "test1", |
| 484 |
}, |
| 485 |
}, |
| 486 |
}, |
| 487 |
}, |
| 488 |
}, |
| 489 |
wantErr: errors.New("unsupported compression \"invalid\""), |
| 490 |
}, |
| 491 |
{ |
| 492 |
name: "simple/no-exporter", |
| 493 |
processor: SpanProcessor{ |
| 494 |
Simple: &SimpleSpanProcessor{ |
| 495 |
Exporter: SpanExporter{}, |
| 496 |
}, |
| 497 |
}, |
| 498 |
wantErr: errors.New("no valid span exporter"), |
| 499 |
}, |
| 500 |
{ |
| 501 |
name: "simple/console-exporter", |
| 502 |
processor: SpanProcessor{ |
| 503 |
Simple: &SimpleSpanProcessor{ |
| 504 |
Exporter: SpanExporter{ |
| 505 |
Console: Console{}, |
| 506 |
}, |
| 507 |
}, |
| 508 |
}, |
| 509 |
wantProcessor: sdktrace.NewSimpleSpanProcessor(consoleExporter), |
| 510 |
}, |
| 511 |
} |
| 512 |
for _, tt := range testCases { |
| 513 |
t.Run(tt.name, func(t *testing.T) { |
| 514 |
got, err := spanProcessor(t.Context(), tt.processor) |
| 515 |
require.Equal(t, tt.wantErr, err) |
| 516 |
if tt.wantProcessor == nil { |
| 517 |
require.Nil(t, got) |
| 518 |
} else { |
| 519 |
require.Equal(t, reflect.TypeOf(tt.wantProcessor), reflect.TypeOf(got)) |
| 520 |
var fieldName string |
| 521 |
switch reflect.TypeOf(tt.wantProcessor).String() { |
| 522 |
case "*trace.simpleSpanProcessor": |
| 523 |
fieldName = "exporter" |
| 524 |
default: |
| 525 |
fieldName = "e" |
| 526 |
} |
| 527 |
wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantProcessor)).FieldByName(fieldName).Elem().Type() |
| 528 |
gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName(fieldName).Elem().Type() |
| 529 |
require.Equal(t, wantExporterType.String(), gotExporterType.String()) |
| 530 |
} |
| 531 |
}) |
| 532 |
} |
| 533 |
} |