diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 3ad01b0aec2..90b14cfde14 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -163,22 +163,11 @@ def create( Returns: The newly-created Resource. """ + if not attributes: attributes = {} - resource = _DEFAULT_RESOURCE.merge( - OTELResourceDetector().detect() - ).merge(Resource(attributes, schema_url)) - if not resource.attributes.get(SERVICE_NAME, None): - default_service_name = "unknown_service" - process_executable_name = resource.attributes.get( - PROCESS_EXECUTABLE_NAME, None - ) - if process_executable_name: - default_service_name += ":" + process_executable_name - resource = resource.merge( - Resource({SERVICE_NAME: default_service_name}, schema_url) - ) - return resource + + return get_aggregated_resources([], Resource(attributes, schema_url)) @staticmethod def get_empty() -> "Resource": @@ -276,7 +265,8 @@ def detect(self) -> "Resource": service_name = os.environ.get(OTEL_SERVICE_NAME) if service_name: env_resource_map[SERVICE_NAME] = service_name - return Resource(env_resource_map) + + return _DEFAULT_RESOURCE.merge(Resource(env_resource_map)) def get_aggregated_resources( @@ -292,7 +282,7 @@ def get_aggregated_resources( :return: """ final_resource = initial_resource or _EMPTY_RESOURCE - detectors = [OTELResourceDetector()] + detectors + detectors = [OTELResourceDetector] + detectors with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: futures = [executor.submit(detector.detect) for detector in detectors] @@ -310,4 +300,21 @@ def get_aggregated_resources( detected_resources = _EMPTY_RESOURCE finally: final_resource = final_resource.merge(detected_resources) - return final_resource + + if final_resource.attributes.get(SERVICE_NAME): + return final_resource + + default_service_name = "unknown_service" + + process_executable_name = final_resource.attributes.get( + PROCESS_EXECUTABLE_NAME + ) + + if process_executable_name: + default_service_name = ( + "%s:%s", + default_service_name, + process_executable_name, + ) + + return final_resource.merge(Resource({SERVICE_NAME: default_service_name}))