|
5 | 5 | from unittest import TestCase |
6 | 6 | from unittest.mock import MagicMock, patch |
7 | 7 |
|
| 8 | +import pytest |
| 9 | + |
8 | 10 | from amazon.opentelemetry.distro.always_record_sampler import AlwaysRecordSampler |
9 | 11 | from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor |
10 | 12 | from amazon.opentelemetry.distro.aws_batch_unsampled_span_processor import BatchUnsampledSpanProcessor |
11 | 13 | from amazon.opentelemetry.distro.aws_lambda_span_processor import AwsLambdaSpanProcessor |
12 | 14 | from amazon.opentelemetry.distro.aws_metric_attributes_span_exporter import AwsMetricAttributesSpanExporter |
13 | 15 | from amazon.opentelemetry.distro.aws_opentelemetry_configurator import ( |
14 | 16 | LAMBDA_SPAN_EXPORT_BATCH_SIZE, |
| 17 | + OTEL_EXPORTER_OTLP_LOGS_ENDPOINT, |
| 18 | + OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, |
15 | 19 | ApplicationSignalsExporterProvider, |
16 | 20 | AwsOpenTelemetryConfigurator, |
17 | 21 | _custom_import_sampler, |
| 22 | + _customize_logs_exporter, |
18 | 23 | _customize_metric_exporters, |
19 | 24 | _customize_sampler, |
20 | 25 | _customize_span_exporter, |
21 | 26 | _customize_span_processors, |
22 | 27 | _export_unsampled_span_for_lambda, |
| 28 | + _init_logging, |
23 | 29 | _is_application_signals_enabled, |
24 | 30 | _is_application_signals_runtime_enabled, |
25 | 31 | _is_defer_to_workers_enabled, |
26 | 32 | _is_wsgi_master_process, |
27 | 33 | ) |
28 | 34 | from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro |
29 | 35 | from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor |
| 36 | +from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter |
| 37 | +from amazon.opentelemetry.distro.exporter.otlp.aws.traces.otlp_aws_span_exporter import OTLPAwsSpanExporter |
30 | 38 | from amazon.opentelemetry.distro.otlp_udp_exporter import OTLPUdpSpanExporter |
31 | 39 | from amazon.opentelemetry.distro.sampler._aws_xray_sampling_client import _AwsXRaySamplingClient |
32 | 40 | from amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler import _AwsXRayRemoteSampler |
33 | 41 | from amazon.opentelemetry.distro.scope_based_exporter import ScopeBasedPeriodicExportingMetricReader |
34 | 42 | from opentelemetry.environment_variables import OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_TRACES_EXPORTER |
35 | 43 | from opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder import OTLPMetricExporterMixin |
| 44 | +from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter as OTLPGrpcLogExporter |
36 | 45 | from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPGrpcOTLPMetricExporter |
| 46 | +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPGrpcSpanExporter |
| 47 | +from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter |
37 | 48 | from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as OTLPHttpOTLPMetricExporter |
38 | 49 | from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
39 | 50 | from opentelemetry.sdk.environment_variables import OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG |
@@ -297,6 +308,177 @@ def test_customize_span_exporter(self): |
297 | 308 | self.assertIsInstance(customized_exporter._delegate, OTLPUdpSpanExporter) |
298 | 309 | os.environ.pop("AWS_LAMBDA_FUNCTION_NAME", None) |
299 | 310 |
|
| 311 | + def test_customize_span_exporter_sigv4(self): |
| 312 | + |
| 313 | + traces_good_endpoints = [ |
| 314 | + "https://xray.us-east-1.amazonaws.com/v1/traces", |
| 315 | + "https://XRAY.US-EAST-1.AMAZONAWS.COM/V1/TRACES", |
| 316 | + "https://xray.us-east-1.amazonaws.com/v1/traces", |
| 317 | + "https://XRAY.US-EAST-1.amazonaws.com/v1/traces", |
| 318 | + "https://xray.US-EAST-1.AMAZONAWS.com/v1/traces", |
| 319 | + "https://Xray.Us-East-1.amazonaws.com/v1/traces", |
| 320 | + "https://xRAY.us-EAST-1.amazonaws.com/v1/traces", |
| 321 | + "https://XRAY.us-EAST-1.AMAZONAWS.com/v1/TRACES", |
| 322 | + "https://xray.US-EAST-1.amazonaws.com/V1/Traces", |
| 323 | + "https://xray.us-east-1.AMAZONAWS.COM/v1/traces", |
| 324 | + "https://XrAy.Us-EaSt-1.AmAzOnAwS.cOm/V1/TrAcEs", |
| 325 | + "https://xray.US-EAST-1.amazonaws.com/v1/traces", |
| 326 | + "https://xray.us-east-1.amazonaws.com/V1/TRACES", |
| 327 | + "https://XRAY.US-EAST-1.AMAZONAWS.COM/v1/traces", |
| 328 | + "https://xray.us-east-1.AMAZONAWS.COM/V1/traces", |
| 329 | + ] |
| 330 | + |
| 331 | + traces_bad_endpoints = [ |
| 332 | + "http://localhost:4318/v1/traces", |
| 333 | + "http://xray.us-east-1.amazonaws.com/v1/traces", |
| 334 | + "ftp://xray.us-east-1.amazonaws.com/v1/traces", |
| 335 | + "https://ray.us-east-1.amazonaws.com/v1/traces", |
| 336 | + "https://xra.us-east-1.amazonaws.com/v1/traces", |
| 337 | + "https://x-ray.us-east-1.amazonaws.com/v1/traces", |
| 338 | + "https://xray.amazonaws.com/v1/traces", |
| 339 | + "https://xray.us-east-1.amazon.com/v1/traces", |
| 340 | + "https://xray.us-east-1.aws.com/v1/traces", |
| 341 | + "https://xray.us_east_1.amazonaws.com/v1/traces", |
| 342 | + "https://xray.us.east.1.amazonaws.com/v1/traces", |
| 343 | + "https://xray..amazonaws.com/v1/traces", |
| 344 | + "https://xray.us-east-1.amazonaws.com/traces", |
| 345 | + "https://xray.us-east-1.amazonaws.com/v2/traces", |
| 346 | + "https://xray.us-east-1.amazonaws.com/v1/trace", |
| 347 | + "https://xray.us-east-1.amazonaws.com/v1/traces/", |
| 348 | + "https://xray.us-east-1.amazonaws.com//v1/traces", |
| 349 | + "https://xray.us-east-1.amazonaws.com/v1//traces", |
| 350 | + "https://xray.us-east-1.amazonaws.com/v1/traces?param=value", |
| 351 | + "https://xray.us-east-1.amazonaws.com/v1/traces#fragment", |
| 352 | + "https://xray.us-east-1.amazonaws.com:443/v1/traces", |
| 353 | + "https:/xray.us-east-1.amazonaws.com/v1/traces", |
| 354 | + "https:://xray.us-east-1.amazonaws.com/v1/traces", |
| 355 | + ] |
| 356 | + |
| 357 | + good_configs = [] |
| 358 | + bad_configs = [] |
| 359 | + |
| 360 | + for endpoint in traces_good_endpoints: |
| 361 | + config = { |
| 362 | + OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: endpoint, |
| 363 | + } |
| 364 | + |
| 365 | + good_configs.append(config) |
| 366 | + |
| 367 | + for endpoint in traces_bad_endpoints: |
| 368 | + config = { |
| 369 | + OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: endpoint, |
| 370 | + } |
| 371 | + |
| 372 | + bad_configs.append(config) |
| 373 | + |
| 374 | + for config in good_configs: |
| 375 | + self.customize_exporter_test(config, OTLPSpanExporter(), _customize_span_exporter, OTLPAwsSpanExporter) |
| 376 | + for config in bad_configs: |
| 377 | + self.customize_exporter_test(config, OTLPSpanExporter(), _customize_span_exporter, OTLPSpanExporter) |
| 378 | + |
| 379 | + self.customize_exporter_test(config, OTLPGrpcSpanExporter(), _customize_span_exporter, OTLPGrpcSpanExporter) |
| 380 | + |
| 381 | + def test_customize_logs_exporter_sigv4(self): |
| 382 | + logs_good_endpoints = [ |
| 383 | + "https://logs.us-east-1.amazonaws.com/v1/logs", |
| 384 | + "https://LOGS.US-EAST-1.AMAZONAWS.COM/V1/LOGS", |
| 385 | + "https://logs.us-east-1.amazonaws.com/v1/logs", |
| 386 | + "https://LOGS.US-EAST-1.amazonaws.com/v1/logs", |
| 387 | + "https://logs.US-EAST-1.AMAZONAWS.com/v1/logs", |
| 388 | + "https://Logs.Us-East-1.amazonaws.com/v1/logs", |
| 389 | + "https://lOGS.us-EAST-1.amazonaws.com/v1/logs", |
| 390 | + "https://LOGS.us-EAST-1.AMAZONAWS.com/v1/LOGS", |
| 391 | + "https://logs.US-EAST-1.amazonaws.com/V1/Logs", |
| 392 | + "https://logs.us-east-1.AMAZONAWS.COM/v1/logs", |
| 393 | + "https://LoGs.Us-EaSt-1.AmAzOnAwS.cOm/V1/LoGs", |
| 394 | + "https://logs.US-EAST-1.amazonaws.com/v1/logs", |
| 395 | + "https://logs.us-east-1.amazonaws.com/V1/LOGS", |
| 396 | + "https://LOGS.US-EAST-1.AMAZONAWS.COM/v1/logs", |
| 397 | + "https://logs.us-east-1.AMAZONAWS.COM/V1/logs", |
| 398 | + ] |
| 399 | + |
| 400 | + logs_bad_endpoints = [ |
| 401 | + "http://localhost:4318/v1/logs", |
| 402 | + "http://logs.us-east-1.amazonaws.com/v1/logs", |
| 403 | + "ftp://logs.us-east-1.amazonaws.com/v1/logs", |
| 404 | + "https://log.us-east-1.amazonaws.com/v1/logs", |
| 405 | + "https://logging.us-east-1.amazonaws.com/v1/logs", |
| 406 | + "https://cloud-logs.us-east-1.amazonaws.com/v1/logs", |
| 407 | + "https://logs.amazonaws.com/v1/logs", |
| 408 | + "https://logs.us-east-1.amazon.com/v1/logs", |
| 409 | + "https://logs.us-east-1.aws.com/v1/logs", |
| 410 | + "https://logs.US-EAST-1.amazonaws.com/v1/logs", |
| 411 | + "https://logs.us_east_1.amazonaws.com/v1/logs", |
| 412 | + "https://logs.us.east.1.amazonaws.com/v1/logs", |
| 413 | + "https://logs..amazonaws.com/v1/logs", |
| 414 | + "https://logs.us-east-1.amazonaws.com/logs", |
| 415 | + "https://logs.us-east-1.amazonaws.com/v2/logs", |
| 416 | + "https://logs.us-east-1.amazonaws.com/v1/log", |
| 417 | + "https://logs.us-east-1.amazonaws.com/v1/logs/", |
| 418 | + "https://logs.us-east-1.amazonaws.com//v1/logs", |
| 419 | + "https://logs.us-east-1.amazonaws.com/v1//logs", |
| 420 | + "https://logs.us-east-1.amazonaws.com/v1/logs?param=value", |
| 421 | + "https://logs.us-east-1.amazonaws.com/v1/logs#fragment", |
| 422 | + "https://logs.us-east-1.amazonaws.com:443/v1/logs", |
| 423 | + "https:/logs.us-east-1.amazonaws.com/v1/logs", |
| 424 | + "https:://logs.us-east-1.amazonaws.com/v1/logs", |
| 425 | + "https://LOGS.us-east-1.amazonaws.com/v1/logs", |
| 426 | + "https://logs.us-east-1.amazonaws.com/V1/LOGS", |
| 427 | + "https://logs.us-east-1.amazonaws.com/v1/logging", |
| 428 | + "https://logs.us-east-1.amazonaws.com/v1/cloudwatchlogs", |
| 429 | + "https://logs.us-east-1.amazonaws.com/v1/cwlogs", |
| 430 | + ] |
| 431 | + |
| 432 | + good_configs = [] |
| 433 | + bad_configs = [] |
| 434 | + |
| 435 | + for endpoint in logs_good_endpoints: |
| 436 | + config = { |
| 437 | + OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: endpoint, |
| 438 | + } |
| 439 | + |
| 440 | + good_configs.append(config) |
| 441 | + |
| 442 | + for endpoint in logs_bad_endpoints: |
| 443 | + config = { |
| 444 | + OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: endpoint, |
| 445 | + } |
| 446 | + |
| 447 | + bad_configs.append(config) |
| 448 | + |
| 449 | + for config in good_configs: |
| 450 | + self.customize_exporter_test(config, OTLPLogExporter(), _customize_logs_exporter, OTLPAwsLogExporter) |
| 451 | + for config in bad_configs: |
| 452 | + self.customize_exporter_test(config, OTLPLogExporter(), _customize_logs_exporter, OTLPLogExporter) |
| 453 | + |
| 454 | + self.customize_exporter_test(config, OTLPGrpcLogExporter(), _customize_logs_exporter, OTLPGrpcLogExporter) |
| 455 | + |
| 456 | + def test_init_logging(self): |
| 457 | + captured_exporter = None |
| 458 | + |
| 459 | + def capture_exporter(*args, **kwargs): |
| 460 | + nonlocal captured_exporter |
| 461 | + result = original_func(*args, **kwargs) |
| 462 | + captured_exporter = result |
| 463 | + return result |
| 464 | + |
| 465 | + with patch( |
| 466 | + "amazon.opentelemetry.distro.aws_opentelemetry_configurator._customize_logs_exporter" |
| 467 | + ) as mock_customize_logs_exporter: |
| 468 | + original_func = _customize_logs_exporter |
| 469 | + mock_customize_logs_exporter.side_effect = capture_exporter |
| 470 | + |
| 471 | + os.environ[OTEL_EXPORTER_OTLP_LOGS_ENDPOINT] = "https://logs.us-east-1.amazonaws.com/v1/logs" |
| 472 | + |
| 473 | + _init_logging({"logs_exporter": OTLPLogExporter}, Resource.get_empty()) |
| 474 | + |
| 475 | + self.assertIsNotNone(captured_exporter) |
| 476 | + self.assertIsInstance(captured_exporter, OTLPAwsLogExporter) |
| 477 | + |
| 478 | + _init_logging({}, Resource.get_empty()) |
| 479 | + |
| 480 | + mock_customize_logs_exporter.assert_called_once() |
| 481 | + |
300 | 482 | def test_customize_span_processors(self): |
301 | 483 | mock_tracer_provider: TracerProvider = MagicMock() |
302 | 484 | _customize_span_processors(mock_tracer_provider, Resource.get_empty()) |
@@ -436,6 +618,17 @@ def test_customize_metric_exporter(self): |
436 | 618 |
|
437 | 619 | os.environ.pop("OTEL_METRIC_EXPORT_INTERVAL", None) |
438 | 620 |
|
| 621 | + def customize_exporter_test(self, config, default_exporter, executor, expected_exporter_type): |
| 622 | + for key, value in config.items(): |
| 623 | + os.environ[key] = value |
| 624 | + |
| 625 | + try: |
| 626 | + result = executor(default_exporter, Resource.get_empty()) |
| 627 | + self.assertIsInstance(result, expected_exporter_type) |
| 628 | + finally: |
| 629 | + for key in config.keys(): |
| 630 | + os.environ.pop(key, None) |
| 631 | + |
439 | 632 |
|
440 | 633 | def validate_distro_environ(): |
441 | 634 | tc: TestCase = TestCase() |
|
0 commit comments