| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package x |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"crypto/tls" |
| 10 |
"crypto/x509" |
| 11 |
"errors" |
| 12 |
"net" |
| 13 |
"net/http" |
| 14 |
"net/http/httptest" |
| 15 |
"os" |
| 16 |
"path/filepath" |
| 17 |
"reflect" |
| 18 |
"runtime" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
"github.com/stretchr/testify/require" |
| 24 |
"go.opentelemetry.io/otel/attribute" |
| 25 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" |
| 26 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp" |
| 27 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog" |
| 28 |
"go.opentelemetry.io/otel/log" |
| 29 |
"go.opentelemetry.io/otel/log/noop" |
| 30 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 31 |
sdklogtest "go.opentelemetry.io/otel/sdk/log/logtest" |
| 32 |
"go.opentelemetry.io/otel/sdk/resource" |
| 33 |
collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1" |
| 34 |
"google.golang.org/grpc" |
| 35 |
"google.golang.org/grpc/credentials" |
| 36 |
) |
| 37 |
|
| 38 |
func TestLoggerProvider(t *testing.T) { |
| 39 |
tests := []struct { |
| 40 |
name string |
| 41 |
cfg configOptions |
| 42 |
wantProvider log.LoggerProvider |
| 43 |
wantErr error |
| 44 |
}{ |
| 45 |
{ |
| 46 |
name: "no-logger-provider-configured", |
| 47 |
wantProvider: noop.NewLoggerProvider(), |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "error-in-config", |
| 51 |
cfg: configOptions{ |
| 52 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 53 |
LoggerProvider: &LoggerProvider{ |
| 54 |
Processors: []LogRecordProcessor{ |
| 55 |
{ |
| 56 |
Simple: &SimpleLogRecordProcessor{}, |
| 57 |
Batch: &BatchLogRecordProcessor{}, |
| 58 |
}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
wantProvider: noop.NewLoggerProvider(), |
| 64 |
wantErr: newErrInvalid("must not specify multiple log processor type"), |
| 65 |
}, |
| 66 |
} |
| 67 |
for _, tt := range tests { |
| 68 |
t.Run(tt.name, func(t *testing.T) { |
| 69 |
mp, shutdown, err := loggerProvider(tt.cfg, resource.Default()) |
| 70 |
require.Equal(t, tt.wantProvider, mp) |
| 71 |
assert.ErrorIs(t, err, tt.wantErr) |
| 72 |
require.NoError(t, shutdown(t.Context())) |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestLogProcessor(t *testing.T) { |
| 78 |
ctx := t.Context() |
| 79 |
|
| 80 |
otlpHTTPExporter, err := otlploghttp.New(ctx) |
| 81 |
require.NoError(t, err) |
| 82 |
|
| 83 |
otlpGRPCExporter, err := otlploggrpc.New(ctx) |
| 84 |
require.NoError(t, err) |
| 85 |
|
| 86 |
consoleExporter, err := stdoutlog.New( |
| 87 |
stdoutlog.WithPrettyPrint(), |
| 88 |
) |
| 89 |
require.NoError(t, err) |
| 90 |
|
| 91 |
testCases := []struct { |
| 92 |
name string |
| 93 |
processor LogRecordProcessor |
| 94 |
args any |
| 95 |
wantErrT error |
| 96 |
wantProcessor sdklog.Processor |
| 97 |
}{ |
| 98 |
{ |
| 99 |
name: "no processor", |
| 100 |
wantErrT: newErrInvalid("unsupported log processor type, must be one of simple or batch"), |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "multiple processor types", |
| 104 |
processor: LogRecordProcessor{ |
| 105 |
Batch: &BatchLogRecordProcessor{ |
| 106 |
Exporter: LogRecordExporter{}, |
| 107 |
}, |
| 108 |
Simple: &SimpleLogRecordProcessor{}, |
| 109 |
}, |
| 110 |
wantErrT: newErrInvalid("must not specify multiple log processor type"), |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "batch processor invalid batch size otlphttp exporter", |
| 114 |
|
| 115 |
processor: LogRecordProcessor{ |
| 116 |
Batch: &BatchLogRecordProcessor{ |
| 117 |
MaxExportBatchSize: ptr(0), |
| 118 |
Exporter: LogRecordExporter{ |
| 119 |
OTLPHttp: &OTLPHttpExporter{}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
wantErrT: newErrGreaterThanZero("max_export_batch_size"), |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "batch processor invalid export timeout otlphttp exporter", |
| 127 |
processor: LogRecordProcessor{ |
| 128 |
Batch: &BatchLogRecordProcessor{ |
| 129 |
ExportTimeout: ptr(-2), |
| 130 |
Exporter: LogRecordExporter{ |
| 131 |
OTLPHttp: &OTLPHttpExporter{}, |
| 132 |
}, |
| 133 |
}, |
| 134 |
}, |
| 135 |
wantErrT: newErrGreaterOrEqualZero("export_timeout"), |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "batch processor invalid queue size otlphttp exporter", |
| 139 |
|
| 140 |
processor: LogRecordProcessor{ |
| 141 |
Batch: &BatchLogRecordProcessor{ |
| 142 |
MaxQueueSize: ptr(-3), |
| 143 |
Exporter: LogRecordExporter{ |
| 144 |
OTLPHttp: &OTLPHttpExporter{}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
}, |
| 148 |
wantErrT: newErrGreaterThanZero("max_queue_size"), |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "batch processor invalid schedule delay console exporter", |
| 152 |
processor: LogRecordProcessor{ |
| 153 |
Batch: &BatchLogRecordProcessor{ |
| 154 |
ScheduleDelay: ptr(-4), |
| 155 |
Exporter: LogRecordExporter{ |
| 156 |
OTLPHttp: &OTLPHttpExporter{}, |
| 157 |
}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
wantErrT: newErrGreaterOrEqualZero("schedule_delay"), |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "batch processor invalid exporter", |
| 164 |
processor: LogRecordProcessor{ |
| 165 |
Batch: &BatchLogRecordProcessor{ |
| 166 |
Exporter: LogRecordExporter{}, |
| 167 |
}, |
| 168 |
}, |
| 169 |
wantErrT: newErrInvalid("no valid log exporter"), |
| 170 |
}, |
| 171 |
{ |
| 172 |
name: "batch/console", |
| 173 |
processor: LogRecordProcessor{ |
| 174 |
Batch: &BatchLogRecordProcessor{ |
| 175 |
MaxExportBatchSize: ptr(1), |
| 176 |
ExportTimeout: ptr(0), |
| 177 |
MaxQueueSize: ptr(1), |
| 178 |
ScheduleDelay: ptr(0), |
| 179 |
Exporter: LogRecordExporter{ |
| 180 |
Console: ConsoleExporter{}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
}, |
| 184 |
wantProcessor: sdklog.NewBatchProcessor(consoleExporter), |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "batch/otlp-grpc-exporter-no-endpoint", |
| 188 |
processor: LogRecordProcessor{ |
| 189 |
Batch: &BatchLogRecordProcessor{ |
| 190 |
MaxExportBatchSize: ptr(1), |
| 191 |
ExportTimeout: ptr(0), |
| 192 |
MaxQueueSize: ptr(1), |
| 193 |
ScheduleDelay: ptr(0), |
| 194 |
Exporter: LogRecordExporter{ |
| 195 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 196 |
Compression: ptr("gzip"), |
| 197 |
Timeout: ptr(1000), |
| 198 |
Headers: []NameStringValuePair{ |
| 199 |
{Name: "test", Value: ptr("test1")}, |
| 200 |
}, |
| 201 |
}, |
| 202 |
}, |
| 203 |
}, |
| 204 |
}, |
| 205 |
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter), |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "batch/otlp-grpc-exporter", |
| 209 |
processor: LogRecordProcessor{ |
| 210 |
Batch: &BatchLogRecordProcessor{ |
| 211 |
MaxExportBatchSize: ptr(1), |
| 212 |
ExportTimeout: ptr(0), |
| 213 |
MaxQueueSize: ptr(1), |
| 214 |
ScheduleDelay: ptr(0), |
| 215 |
Exporter: LogRecordExporter{ |
| 216 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 217 |
Endpoint: ptr("http://localhost:4317"), |
| 218 |
Compression: ptr("gzip"), |
| 219 |
Timeout: ptr(1000), |
| 220 |
Headers: []NameStringValuePair{ |
| 221 |
{Name: "test", Value: ptr("test1")}, |
| 222 |
}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
}, |
| 227 |
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter), |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "batch/otlp-grpc-exporter-socket-endpoint", |
| 231 |
processor: LogRecordProcessor{ |
| 232 |
Batch: &BatchLogRecordProcessor{ |
| 233 |
MaxExportBatchSize: ptr(1), |
| 234 |
ExportTimeout: ptr(0), |
| 235 |
MaxQueueSize: ptr(1), |
| 236 |
ScheduleDelay: ptr(0), |
| 237 |
Exporter: LogRecordExporter{ |
| 238 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 239 |
Endpoint: ptr("unix:collector.sock"), |
| 240 |
Compression: ptr("gzip"), |
| 241 |
Timeout: ptr(1000), |
| 242 |
Headers: []NameStringValuePair{ |
| 243 |
{Name: "test", Value: ptr("test1")}, |
| 244 |
}, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter), |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "batch/otlp-grpc-good-ca-certificate", |
| 253 |
processor: LogRecordProcessor{ |
| 254 |
Batch: &BatchLogRecordProcessor{ |
| 255 |
Exporter: LogRecordExporter{ |
| 256 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 257 |
Endpoint: ptr("localhost:4317"), |
| 258 |
Compression: ptr("gzip"), |
| 259 |
Timeout: ptr(1000), |
| 260 |
Tls: &GrpcTls{ |
| 261 |
CaFile: ptr(filepath.Join("..", "testdata", "ca.crt")), |
| 262 |
}, |
| 263 |
}, |
| 264 |
}, |
| 265 |
}, |
| 266 |
}, |
| 267 |
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter), |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "batch/otlp-grpc-bad-ca-certificate", |
| 271 |
processor: LogRecordProcessor{ |
| 272 |
Batch: &BatchLogRecordProcessor{ |
| 273 |
Exporter: LogRecordExporter{ |
| 274 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 275 |
Endpoint: ptr("localhost:4317"), |
| 276 |
Compression: ptr("gzip"), |
| 277 |
Timeout: ptr(1000), |
| 278 |
Tls: &GrpcTls{ |
| 279 |
CaFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
}, |
| 284 |
}, |
| 285 |
wantErrT: newErrInvalid("tls configuration"), |
| 286 |
}, |
| 287 |
{ |
| 288 |
name: "batch/otlp-grpc-bad-headerslist", |
| 289 |
processor: LogRecordProcessor{ |
| 290 |
Batch: &BatchLogRecordProcessor{ |
| 291 |
Exporter: LogRecordExporter{ |
| 292 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 293 |
Endpoint: ptr("localhost:4317"), |
| 294 |
Compression: ptr("gzip"), |
| 295 |
Timeout: ptr(1000), |
| 296 |
HeadersList: ptr("==="), |
| 297 |
}, |
| 298 |
}, |
| 299 |
}, |
| 300 |
}, |
| 301 |
wantErrT: newErrInvalid("invalid headers_list"), |
| 302 |
}, |
| 303 |
{ |
| 304 |
name: "batch/otlp-grpc-bad-client-certificate", |
| 305 |
processor: LogRecordProcessor{ |
| 306 |
Batch: &BatchLogRecordProcessor{ |
| 307 |
Exporter: LogRecordExporter{ |
| 308 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 309 |
Endpoint: ptr("localhost:4317"), |
| 310 |
Compression: ptr("gzip"), |
| 311 |
Timeout: ptr(1000), |
| 312 |
Tls: &GrpcTls{ |
| 313 |
KeyFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 314 |
CertFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 315 |
}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
wantErrT: newErrInvalid("tls configuration"), |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "batch/otlp-grpc-exporter-no-scheme", |
| 324 |
processor: LogRecordProcessor{ |
| 325 |
Batch: &BatchLogRecordProcessor{ |
| 326 |
MaxExportBatchSize: ptr(1), |
| 327 |
ExportTimeout: ptr(0), |
| 328 |
MaxQueueSize: ptr(1), |
| 329 |
ScheduleDelay: ptr(0), |
| 330 |
Exporter: LogRecordExporter{ |
| 331 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 332 |
Endpoint: ptr("localhost:4317"), |
| 333 |
Compression: ptr("gzip"), |
| 334 |
Timeout: ptr(1000), |
| 335 |
Headers: []NameStringValuePair{ |
| 336 |
{Name: "test", Value: ptr("test1")}, |
| 337 |
}, |
| 338 |
}, |
| 339 |
}, |
| 340 |
}, |
| 341 |
}, |
| 342 |
wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter), |
| 343 |
}, |
| 344 |
{ |
| 345 |
name: "batch/otlp-grpc-invalid-endpoint", |
| 346 |
processor: LogRecordProcessor{ |
| 347 |
Batch: &BatchLogRecordProcessor{ |
| 348 |
MaxExportBatchSize: ptr(1), |
| 349 |
ExportTimeout: ptr(0), |
| 350 |
MaxQueueSize: ptr(1), |
| 351 |
ScheduleDelay: ptr(0), |
| 352 |
Exporter: LogRecordExporter{ |
| 353 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 354 |
Endpoint: ptr(" "), |
| 355 |
Compression: ptr("gzip"), |
| 356 |
Timeout: ptr(1000), |
| 357 |
Headers: []NameStringValuePair{ |
| 358 |
{Name: "test", Value: ptr("test1")}, |
| 359 |
}, |
| 360 |
}, |
| 361 |
}, |
| 362 |
}, |
| 363 |
}, |
| 364 |
wantErrT: newErrInvalid("endpoint parsing failed"), |
| 365 |
}, |
| 366 |
{ |
| 367 |
name: "batch/otlp-grpc-invalid-compression", |
| 368 |
processor: LogRecordProcessor{ |
| 369 |
Batch: &BatchLogRecordProcessor{ |
| 370 |
MaxExportBatchSize: ptr(1), |
| 371 |
ExportTimeout: ptr(0), |
| 372 |
MaxQueueSize: ptr(1), |
| 373 |
ScheduleDelay: ptr(0), |
| 374 |
Exporter: LogRecordExporter{ |
| 375 |
OTLPGrpc: &OTLPGrpcExporter{ |
| 376 |
Endpoint: ptr("localhost:4317"), |
| 377 |
Compression: ptr("invalid"), |
| 378 |
Timeout: ptr(1000), |
| 379 |
Headers: []NameStringValuePair{ |
| 380 |
{Name: "test", Value: ptr("test1")}, |
| 381 |
}, |
| 382 |
}, |
| 383 |
}, |
| 384 |
}, |
| 385 |
}, |
| 386 |
wantErrT: newErrInvalid("unsupported compression \"invalid\""), |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: "batch/otlp-http-exporter", |
| 390 |
processor: LogRecordProcessor{ |
| 391 |
Batch: &BatchLogRecordProcessor{ |
| 392 |
MaxExportBatchSize: ptr(1), |
| 393 |
ExportTimeout: ptr(0), |
| 394 |
MaxQueueSize: ptr(1), |
| 395 |
ScheduleDelay: ptr(0), |
| 396 |
Exporter: LogRecordExporter{ |
| 397 |
OTLPHttp: &OTLPHttpExporter{ |
| 398 |
Endpoint: ptr("http://localhost:4318"), |
| 399 |
Compression: ptr("gzip"), |
| 400 |
Timeout: ptr(1000), |
| 401 |
Headers: []NameStringValuePair{ |
| 402 |
{Name: "test", Value: ptr("test1")}, |
| 403 |
}, |
| 404 |
}, |
| 405 |
}, |
| 406 |
}, |
| 407 |
}, |
| 408 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 409 |
}, |
| 410 |
{ |
| 411 |
name: "batch/otlp-http-good-ca-certificate", |
| 412 |
processor: LogRecordProcessor{ |
| 413 |
Batch: &BatchLogRecordProcessor{ |
| 414 |
Exporter: LogRecordExporter{ |
| 415 |
OTLPHttp: &OTLPHttpExporter{ |
| 416 |
Endpoint: ptr("localhost:4317"), |
| 417 |
Compression: ptr("gzip"), |
| 418 |
Timeout: ptr(1000), |
| 419 |
Tls: &HttpTls{ |
| 420 |
CaFile: ptr(filepath.Join("..", "testdata", "ca.crt")), |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
}, |
| 426 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 427 |
}, |
| 428 |
{ |
| 429 |
name: "batch/otlp-http-bad-ca-certificate", |
| 430 |
processor: LogRecordProcessor{ |
| 431 |
Batch: &BatchLogRecordProcessor{ |
| 432 |
Exporter: LogRecordExporter{ |
| 433 |
OTLPHttp: &OTLPHttpExporter{ |
| 434 |
Endpoint: ptr("localhost:4317"), |
| 435 |
Compression: ptr("gzip"), |
| 436 |
Timeout: ptr(1000), |
| 437 |
Tls: &HttpTls{ |
| 438 |
CaFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
}, |
| 443 |
}, |
| 444 |
wantErrT: newErrInvalid("tls configuration"), |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "batch/otlp-http-bad-client-certificate", |
| 448 |
processor: LogRecordProcessor{ |
| 449 |
Batch: &BatchLogRecordProcessor{ |
| 450 |
Exporter: LogRecordExporter{ |
| 451 |
OTLPHttp: &OTLPHttpExporter{ |
| 452 |
Endpoint: ptr("localhost:4317"), |
| 453 |
Compression: ptr("gzip"), |
| 454 |
Timeout: ptr(1000), |
| 455 |
Tls: &HttpTls{ |
| 456 |
KeyFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 457 |
CertFile: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 458 |
}, |
| 459 |
}, |
| 460 |
}, |
| 461 |
}, |
| 462 |
}, |
| 463 |
wantErrT: newErrInvalid("tls configuration"), |
| 464 |
}, |
| 465 |
{ |
| 466 |
name: "batch/otlp-http-bad-headerslist", |
| 467 |
processor: LogRecordProcessor{ |
| 468 |
Batch: &BatchLogRecordProcessor{ |
| 469 |
Exporter: LogRecordExporter{ |
| 470 |
OTLPHttp: &OTLPHttpExporter{ |
| 471 |
Endpoint: ptr("localhost:4317"), |
| 472 |
Compression: ptr("gzip"), |
| 473 |
Timeout: ptr(1000), |
| 474 |
HeadersList: ptr("==="), |
| 475 |
}, |
| 476 |
}, |
| 477 |
}, |
| 478 |
}, |
| 479 |
wantErrT: newErrInvalid("invalid headers_list"), |
| 480 |
}, |
| 481 |
{ |
| 482 |
name: "batch/otlp-http-exporter-with-path", |
| 483 |
processor: LogRecordProcessor{ |
| 484 |
Batch: &BatchLogRecordProcessor{ |
| 485 |
MaxExportBatchSize: ptr(1), |
| 486 |
ExportTimeout: ptr(0), |
| 487 |
MaxQueueSize: ptr(1), |
| 488 |
ScheduleDelay: ptr(0), |
| 489 |
Exporter: LogRecordExporter{ |
| 490 |
OTLPHttp: &OTLPHttpExporter{ |
| 491 |
Endpoint: ptr("http://localhost:4318/path/123"), |
| 492 |
Compression: ptr("none"), |
| 493 |
Timeout: ptr(1000), |
| 494 |
Headers: []NameStringValuePair{ |
| 495 |
{Name: "test", Value: ptr("test1")}, |
| 496 |
}, |
| 497 |
}, |
| 498 |
}, |
| 499 |
}, |
| 500 |
}, |
| 501 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 502 |
}, |
| 503 |
{ |
| 504 |
name: "batch/otlp-http-exporter-no-endpoint", |
| 505 |
processor: LogRecordProcessor{ |
| 506 |
Batch: &BatchLogRecordProcessor{ |
| 507 |
MaxExportBatchSize: ptr(1), |
| 508 |
ExportTimeout: ptr(0), |
| 509 |
MaxQueueSize: ptr(1), |
| 510 |
ScheduleDelay: ptr(0), |
| 511 |
Exporter: LogRecordExporter{ |
| 512 |
OTLPHttp: &OTLPHttpExporter{ |
| 513 |
Compression: ptr("gzip"), |
| 514 |
Timeout: ptr(1000), |
| 515 |
Headers: []NameStringValuePair{ |
| 516 |
{Name: "test", Value: ptr("test1")}, |
| 517 |
}, |
| 518 |
}, |
| 519 |
}, |
| 520 |
}, |
| 521 |
}, |
| 522 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 523 |
}, |
| 524 |
{ |
| 525 |
name: "batch/otlp-http-exporter-no-scheme", |
| 526 |
processor: LogRecordProcessor{ |
| 527 |
Batch: &BatchLogRecordProcessor{ |
| 528 |
MaxExportBatchSize: ptr(1), |
| 529 |
ExportTimeout: ptr(0), |
| 530 |
MaxQueueSize: ptr(1), |
| 531 |
ScheduleDelay: ptr(0), |
| 532 |
Exporter: LogRecordExporter{ |
| 533 |
OTLPHttp: &OTLPHttpExporter{ |
| 534 |
Endpoint: ptr("localhost:4318"), |
| 535 |
Compression: ptr("gzip"), |
| 536 |
Timeout: ptr(1000), |
| 537 |
Headers: []NameStringValuePair{ |
| 538 |
{Name: "test", Value: ptr("test1")}, |
| 539 |
}, |
| 540 |
}, |
| 541 |
}, |
| 542 |
}, |
| 543 |
}, |
| 544 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 545 |
}, |
| 546 |
{ |
| 547 |
name: "batch/otlp-http-invalid-endpoint", |
| 548 |
processor: LogRecordProcessor{ |
| 549 |
Batch: &BatchLogRecordProcessor{ |
| 550 |
MaxExportBatchSize: ptr(1), |
| 551 |
ExportTimeout: ptr(0), |
| 552 |
MaxQueueSize: ptr(1), |
| 553 |
ScheduleDelay: ptr(0), |
| 554 |
Exporter: LogRecordExporter{ |
| 555 |
OTLPHttp: &OTLPHttpExporter{ |
| 556 |
Endpoint: ptr(" "), |
| 557 |
Compression: ptr("gzip"), |
| 558 |
Timeout: ptr(1000), |
| 559 |
Headers: []NameStringValuePair{ |
| 560 |
{Name: "test", Value: ptr("test1")}, |
| 561 |
}, |
| 562 |
}, |
| 563 |
}, |
| 564 |
}, |
| 565 |
}, |
| 566 |
wantErrT: newErrInvalid("endpoint parsing failed"), |
| 567 |
}, |
| 568 |
{ |
| 569 |
name: "batch/otlp-http-none-compression", |
| 570 |
processor: LogRecordProcessor{ |
| 571 |
Batch: &BatchLogRecordProcessor{ |
| 572 |
MaxExportBatchSize: ptr(1), |
| 573 |
ExportTimeout: ptr(0), |
| 574 |
MaxQueueSize: ptr(1), |
| 575 |
ScheduleDelay: ptr(0), |
| 576 |
Exporter: LogRecordExporter{ |
| 577 |
OTLPHttp: &OTLPHttpExporter{ |
| 578 |
Endpoint: ptr("localhost:4318"), |
| 579 |
Compression: ptr("none"), |
| 580 |
Timeout: ptr(1000), |
| 581 |
Headers: []NameStringValuePair{ |
| 582 |
{Name: "test", Value: ptr("test1")}, |
| 583 |
}, |
| 584 |
}, |
| 585 |
}, |
| 586 |
}, |
| 587 |
}, |
| 588 |
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter), |
| 589 |
}, |
| 590 |
{ |
| 591 |
name: "batch/otlp-http-invalid-compression", |
| 592 |
processor: LogRecordProcessor{ |
| 593 |
Batch: &BatchLogRecordProcessor{ |
| 594 |
MaxExportBatchSize: ptr(1), |
| 595 |
ExportTimeout: ptr(0), |
| 596 |
MaxQueueSize: ptr(1), |
| 597 |
ScheduleDelay: ptr(0), |
| 598 |
Exporter: LogRecordExporter{ |
| 599 |
OTLPHttp: &OTLPHttpExporter{ |
| 600 |
Endpoint: ptr("localhost:4318"), |
| 601 |
Compression: ptr("invalid"), |
| 602 |
Timeout: ptr(1000), |
| 603 |
Headers: []NameStringValuePair{ |
| 604 |
{Name: "test", Value: ptr("test1")}, |
| 605 |
}, |
| 606 |
}, |
| 607 |
}, |
| 608 |
}, |
| 609 |
}, |
| 610 |
wantErrT: newErrInvalid("unsupported compression \"invalid\""), |
| 611 |
}, |
| 612 |
{ |
| 613 |
name: "simple/no-exporter", |
| 614 |
processor: LogRecordProcessor{ |
| 615 |
Simple: &SimpleLogRecordProcessor{ |
| 616 |
Exporter: LogRecordExporter{}, |
| 617 |
}, |
| 618 |
}, |
| 619 |
wantErrT: newErrInvalid("no valid log exporter"), |
| 620 |
}, |
| 621 |
{ |
| 622 |
name: "simple/console", |
| 623 |
processor: LogRecordProcessor{ |
| 624 |
Simple: &SimpleLogRecordProcessor{ |
| 625 |
Exporter: LogRecordExporter{ |
| 626 |
Console: ConsoleExporter{}, |
| 627 |
}, |
| 628 |
}, |
| 629 |
}, |
| 630 |
wantProcessor: sdklog.NewSimpleProcessor(consoleExporter), |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "simple/otlp_file", |
| 634 |
processor: LogRecordProcessor{ |
| 635 |
Simple: &SimpleLogRecordProcessor{ |
| 636 |
Exporter: LogRecordExporter{ |
| 637 |
OTLPFileDevelopment: &ExperimentalOTLPFileExporter{}, |
| 638 |
}, |
| 639 |
}, |
| 640 |
}, |
| 641 |
wantErrT: newErrInvalid("otlp_file/development"), |
| 642 |
}, |
| 643 |
{ |
| 644 |
name: "simple/multiple", |
| 645 |
processor: LogRecordProcessor{ |
| 646 |
Simple: &SimpleLogRecordProcessor{ |
| 647 |
Exporter: LogRecordExporter{ |
| 648 |
Console: ConsoleExporter{}, |
| 649 |
OTLPGrpc: &OTLPGrpcExporter{}, |
| 650 |
}, |
| 651 |
}, |
| 652 |
}, |
| 653 |
wantErrT: newErrInvalid("must not specify multiple exporters"), |
| 654 |
}, |
| 655 |
{ |
| 656 |
name: "simple/otlp-exporter", |
| 657 |
processor: LogRecordProcessor{ |
| 658 |
Simple: &SimpleLogRecordProcessor{ |
| 659 |
Exporter: LogRecordExporter{ |
| 660 |
OTLPHttp: &OTLPHttpExporter{ |
| 661 |
Endpoint: ptr("localhost:4318"), |
| 662 |
Compression: ptr("gzip"), |
| 663 |
Timeout: ptr(1000), |
| 664 |
Headers: []NameStringValuePair{ |
| 665 |
{Name: "test", Value: ptr("test1")}, |
| 666 |
}, |
| 667 |
}, |
| 668 |
}, |
| 669 |
}, |
| 670 |
}, |
| 671 |
wantProcessor: sdklog.NewSimpleProcessor(otlpHTTPExporter), |
| 672 |
}, |
| 673 |
} |
| 674 |
for _, tt := range testCases { |
| 675 |
t.Run(tt.name, func(t *testing.T) { |
| 676 |
got, err := logProcessor(t.Context(), tt.processor) |
| 677 |
require.ErrorIs(t, err, tt.wantErrT) |
| 678 |
if tt.wantProcessor == nil { |
| 679 |
require.Nil(t, got) |
| 680 |
} else { |
| 681 |
require.Equal(t, reflect.TypeOf(tt.wantProcessor), reflect.TypeOf(got)) |
| 682 |
wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantProcessor)).FieldByName("exporter").Elem().Type() |
| 683 |
gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("exporter").Elem().Type() |
| 684 |
require.Equal(t, wantExporterType.String(), gotExporterType.String()) |
| 685 |
} |
| 686 |
}) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
func TestLoggerProviderOptions(t *testing.T) { |
| 691 |
var calls int |
| 692 |
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 693 |
calls++ |
| 694 |
})) |
| 695 |
defer srv.Close() |
| 696 |
|
| 697 |
cfg := OpenTelemetryConfiguration{ |
| 698 |
LoggerProvider: &LoggerProvider{ |
| 699 |
Processors: []LogRecordProcessor{{ |
| 700 |
Simple: &SimpleLogRecordProcessor{ |
| 701 |
Exporter: LogRecordExporter{ |
| 702 |
OTLPHttp: &OTLPHttpExporter{ |
| 703 |
Endpoint: ptr(srv.URL), |
| 704 |
}, |
| 705 |
}, |
| 706 |
}, |
| 707 |
}}, |
| 708 |
}, |
| 709 |
} |
| 710 |
|
| 711 |
var buf bytes.Buffer |
| 712 |
stdoutlogExporter, err := stdoutlog.New(stdoutlog.WithWriter(&buf)) |
| 713 |
require.NoError(t, err) |
| 714 |
|
| 715 |
res := resource.NewSchemaless(attribute.String("foo", "bar")) |
| 716 |
sdk, err := NewSDK( |
| 717 |
WithOpenTelemetryConfiguration(cfg), |
| 718 |
WithLoggerProviderOptions(sdklog.WithProcessor(sdklog.NewSimpleProcessor(stdoutlogExporter))), |
| 719 |
WithLoggerProviderOptions(sdklog.WithResource(res)), |
| 720 |
) |
| 721 |
require.NoError(t, err) |
| 722 |
defer func() { |
| 723 |
assert.NoError(t, sdk.Shutdown(t.Context())) |
| 724 |
}() |
| 725 |
|
| 726 |
// The exporter, which we passed in as an extra option to NewSDK, |
| 727 |
// should be wired up to the provider in addition to the |
| 728 |
// configuration-based OTLP exporter. |
| 729 |
logger := sdk.LoggerProvider().Logger("test") |
| 730 |
logger.Emit(t.Context(), log.Record{}) |
| 731 |
assert.NotZero(t, buf) |
| 732 |
assert.Equal(t, 1, calls) |
| 733 |
// Options provided by WithMeterProviderOptions may be overridden |
| 734 |
// by configuration, e.g. the resource is always defined via |
| 735 |
// configuration. |
| 736 |
assert.NotContains(t, buf.String(), "foo") |
| 737 |
} |
| 738 |
|
| 739 |
func Test_otlpGRPCLogExporter(t *testing.T) { |
| 740 |
if runtime.GOOS == "windows" { |
| 741 |
// TODO (#7446): Fix the flakiness on Windows. |
| 742 |
t.Skip("Test is flaky on Windows.") |
| 743 |
} |
| 744 |
type args struct { |
| 745 |
ctx context.Context |
| 746 |
otlpConfig *OTLPGrpcExporter |
| 747 |
} |
| 748 |
tests := []struct { |
| 749 |
name string |
| 750 |
args args |
| 751 |
grpcServerOpts func() ([]grpc.ServerOption, error) |
| 752 |
}{ |
| 753 |
{ |
| 754 |
name: "no TLS config", |
| 755 |
args: args{ |
| 756 |
ctx: t.Context(), |
| 757 |
otlpConfig: &OTLPGrpcExporter{ |
| 758 |
Compression: ptr("gzip"), |
| 759 |
Timeout: ptr(5000), |
| 760 |
Tls: &GrpcTls{ |
| 761 |
Insecure: ptr(true), |
| 762 |
}, |
| 763 |
Headers: []NameStringValuePair{ |
| 764 |
{Name: "test", Value: ptr("test1")}, |
| 765 |
}, |
| 766 |
}, |
| 767 |
}, |
| 768 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 769 |
return []grpc.ServerOption{}, nil |
| 770 |
}, |
| 771 |
}, |
| 772 |
{ |
| 773 |
name: "with TLS config", |
| 774 |
args: args{ |
| 775 |
ctx: t.Context(), |
| 776 |
otlpConfig: &OTLPGrpcExporter{ |
| 777 |
Compression: ptr("gzip"), |
| 778 |
Timeout: ptr(5000), |
| 779 |
Tls: &GrpcTls{ |
| 780 |
CaFile: ptr("../testdata/server-certs/server.crt"), |
| 781 |
}, |
| 782 |
Headers: []NameStringValuePair{ |
| 783 |
{Name: "test", Value: ptr("test1")}, |
| 784 |
}, |
| 785 |
}, |
| 786 |
}, |
| 787 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 788 |
opts := []grpc.ServerOption{} |
| 789 |
tlsCreds, err := credentials.NewServerTLSFromFile("../testdata/server-certs/server.crt", "../testdata/server-certs/server.key") |
| 790 |
if err != nil { |
| 791 |
return nil, err |
| 792 |
} |
| 793 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 794 |
return opts, nil |
| 795 |
}, |
| 796 |
}, |
| 797 |
{ |
| 798 |
name: "with TLS config and client key", |
| 799 |
args: args{ |
| 800 |
ctx: t.Context(), |
| 801 |
otlpConfig: &OTLPGrpcExporter{ |
| 802 |
Compression: ptr("gzip"), |
| 803 |
Timeout: ptr(5000), |
| 804 |
Tls: &GrpcTls{ |
| 805 |
CaFile: ptr("../testdata/server-certs/server.crt"), |
| 806 |
KeyFile: ptr("../testdata/client-certs/client.key"), |
| 807 |
CertFile: ptr("../testdata/client-certs/client.crt"), |
| 808 |
}, |
| 809 |
Headers: []NameStringValuePair{ |
| 810 |
{Name: "test", Value: ptr("test1")}, |
| 811 |
}, |
| 812 |
}, |
| 813 |
}, |
| 814 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 815 |
opts := []grpc.ServerOption{} |
| 816 |
cert, err := tls.LoadX509KeyPair("../testdata/server-certs/server.crt", "../testdata/server-certs/server.key") |
| 817 |
if err != nil { |
| 818 |
return nil, err |
| 819 |
} |
| 820 |
caCert, err := os.ReadFile("../testdata/ca.crt") |
| 821 |
if err != nil { |
| 822 |
return nil, err |
| 823 |
} |
| 824 |
caCertPool := x509.NewCertPool() |
| 825 |
caCertPool.AppendCertsFromPEM(caCert) |
| 826 |
tlsCreds := credentials.NewTLS(&tls.Config{ |
| 827 |
Certificates: []tls.Certificate{cert}, |
| 828 |
ClientCAs: caCertPool, |
| 829 |
ClientAuth: tls.RequireAndVerifyClientCert, |
| 830 |
}) |
| 831 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 832 |
return opts, nil |
| 833 |
}, |
| 834 |
}, |
| 835 |
} |
| 836 |
for _, tt := range tests { |
| 837 |
t.Run(tt.name, func(t *testing.T) { |
| 838 |
n, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp4", "localhost:0") |
| 839 |
require.NoError(t, err) |
| 840 |
|
| 841 |
// We need to manually construct the endpoint using the port on which the server is listening. |
| 842 |
// |
| 843 |
// n.Addr() always returns 127.0.0.1 instead of localhost. |
| 844 |
// But our certificate is created with CN as 'localhost', not '127.0.0.1'. |
| 845 |
// So we have to manually form the endpoint as "localhost:<port>". |
| 846 |
_, port, err := net.SplitHostPort(n.Addr().String()) |
| 847 |
require.NoError(t, err) |
| 848 |
tt.args.otlpConfig.Endpoint = ptr("localhost:" + port) |
| 849 |
|
| 850 |
serverOpts, err := tt.grpcServerOpts() |
| 851 |
require.NoError(t, err) |
| 852 |
|
| 853 |
startGRPCLogsCollector(t, n, serverOpts) |
| 854 |
|
| 855 |
exporter, err := otlpGRPCLogExporter(tt.args.ctx, tt.args.otlpConfig) |
| 856 |
require.NoError(t, err) |
| 857 |
|
| 858 |
logFactory := sdklogtest.RecordFactory{ |
| 859 |
Body: log.StringValue("test"), |
| 860 |
} |
| 861 |
|
| 862 |
assert.EventuallyWithT(t, func(collect *assert.CollectT) { |
| 863 |
assert.NoError(collect, exporter.Export(context.Background(), []sdklog.Record{ //nolint:usetesting // required to avoid getting a canceled context. |
| 864 |
logFactory.NewRecord(), |
| 865 |
})) |
| 866 |
}, 10*time.Second, 1*time.Second) |
| 867 |
}) |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
// grpcLogsCollector is an OTLP gRPC server that collects all requests it receives. |
| 872 |
type grpcLogsCollector struct { |
| 873 |
collogpb.UnimplementedLogsServiceServer |
| 874 |
} |
| 875 |
|
| 876 |
var _ collogpb.LogsServiceServer = (*grpcLogsCollector)(nil) |
| 877 |
|
| 878 |
// startGRPCLogsCollector returns a *grpcLogsCollector that is listening at the provided |
| 879 |
// endpoint. |
| 880 |
// |
| 881 |
// If endpoint is an empty string, the returned collector will be listening on |
| 882 |
// the localhost interface at an OS chosen port. |
| 883 |
func startGRPCLogsCollector(t *testing.T, listener net.Listener, serverOptions []grpc.ServerOption) { |
| 884 |
srv := grpc.NewServer(serverOptions...) |
| 885 |
c := &grpcLogsCollector{} |
| 886 |
|
| 887 |
collogpb.RegisterLogsServiceServer(srv, c) |
| 888 |
|
| 889 |
errCh := make(chan error, 1) |
| 890 |
go func() { errCh <- srv.Serve(listener) }() |
| 891 |
|
| 892 |
t.Cleanup(func() { |
| 893 |
srv.GracefulStop() |
| 894 |
if err := <-errCh; err != nil && !errors.Is(err, grpc.ErrServerStopped) { |
| 895 |
assert.NoError(t, err) |
| 896 |
} |
| 897 |
}) |
| 898 |
} |
| 899 |
|
| 900 |
// Export handles the export req. |
| 901 |
func (*grpcLogsCollector) Export( |
| 902 |
_ context.Context, |
| 903 |
_ *collogpb.ExportLogsServiceRequest, |
| 904 |
) (*collogpb.ExportLogsServiceResponse, error) { |
| 905 |
return &collogpb.ExportLogsServiceResponse{}, nil |
| 906 |
} |