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