diff --git a/CHANGELOG.md b/CHANGELOG.md index f359c6b633f..b46a17246ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Improve Resource Detector timeout messaging + ([#3645](https://github.com/open-telemetry/opentelemetry-python/pull/3645)) + ## Version 1.22.0/0.43b0 (2023-12-15) - Prometheus exporter sanitize info metric ([#3572](https://github.com/open-telemetry/opentelemetry-python/pull/3572)) diff --git a/docs/examples/opencensus-exporter-tracer/collector.py b/docs/examples/opencensus-exporter-tracer/collector.py index 5c98cc4ce9f..cd33c89617b 100644 --- a/docs/examples/opencensus-exporter-tracer/collector.py +++ b/docs/examples/opencensus-exporter-tracer/collector.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright The OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index f92fdb964e0..852b23f5002 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -390,6 +390,14 @@ def get_aggregated_resources( detected_resource: Resource = _EMPTY_RESOURCE try: detected_resource = future.result(timeout=timeout) + except concurrent.futures.TimeoutError as ex: + if detector.raise_on_error: + raise ex + logger.warning( + "Detector %s took longer than %s seconds, skipping", + detector, + timeout, + ) # pylint: disable=broad-except except Exception as ex: if detector.raise_on_error: diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 53ecf30cab9..da3f9469617 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -15,6 +15,7 @@ import sys import unittest import uuid +from concurrent.futures import TimeoutError from logging import ERROR, WARNING from os import environ from unittest.mock import Mock, patch @@ -420,6 +421,23 @@ def test_resource_detector_raise_error(self): Exception, get_aggregated_resources, [resource_detector] ) + @patch("opentelemetry.sdk.resources.logger") + def test_resource_detector_timeout(self, mock_logger): + resource_detector = Mock(spec=ResourceDetector) + resource_detector.detect.side_effect = TimeoutError() + resource_detector.raise_on_error = False + self.assertEqual( + get_aggregated_resources([resource_detector]), + _DEFAULT_RESOURCE.merge( + Resource({SERVICE_NAME: "unknown_service"}, "") + ), + ) + mock_logger.warning.assert_called_with( + "Detector %s took longer than %s seconds, skipping", + resource_detector, + 5, + ) + @patch.dict( environ, {"OTEL_RESOURCE_ATTRIBUTES": "key1=env_value1,key2=env_value2"}, diff --git a/tests/w3c_tracecontext_validation_server.py b/tests/w3c_tracecontext_validation_server.py index d6c468025e4..5c47708ee1c 100644 --- a/tests/w3c_tracecontext_validation_server.py +++ b/tests/w3c_tracecontext_validation_server.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright The OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License");