From b512b6c4504711337f229dae507a7a9ab8c01e9b Mon Sep 17 00:00:00 2001 From: Chris Kleinknecht Date: Thu, 21 Feb 2019 20:14:00 -0800 Subject: [PATCH] Try using *args in merge --- .../monitored_resource/monitored_resource.py | 2 +- opencensus/common/resource.py | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/opencensus/common/monitored_resource/monitored_resource.py b/opencensus/common/monitored_resource/monitored_resource.py index 1bd2d2bc8..bc08e0870 100644 --- a/opencensus/common/monitored_resource/monitored_resource.py +++ b/opencensus/common/monitored_resource/monitored_resource.py @@ -86,4 +86,4 @@ def get_instance(): if not resources: return None - return resource.merge_resources(resources) + return resource.merge_resources(resources[0], *resources[1:]) diff --git a/opencensus/common/resource.py b/opencensus/common/resource.py index 44f70ea2c..786592594 100644 --- a/opencensus/common/resource.py +++ b/opencensus/common/resource.py @@ -49,7 +49,7 @@ _UNQUOTE_RE = re.compile(r'^([\'"]?)([^\1]*)(\1)$') -def merge_resources(resource_list): +def merge_resources(r1, *rest): """Merge multiple resources to get a new resource. Resources earlier in the list take precedence: if multiple resources share @@ -57,21 +57,26 @@ def merge_resources(resource_list): key. The combined resource's type will be the first non-null type in the list. - :type resource_list: list(:class:`Resource`) - :param resource_list: The list of resources to combine. + :type r1: class:`Resource` + :param r1: The first of one or more resources to merge. + + :type *rest: list(:class:`Resource`) + :param *rest: The rest of the resources to merge. :rtype: :class:`Resource` :return: The new combined resource. """ - if not resource_list: + if not r1: raise ValueError - rtype = None - for rr in resource_list: + if not rest: + return r1 + rtype = r1.type + for rr in rest: if rr.type: rtype = rr.type break - labels = {} - for rr in reversed(resource_list): + labels = copy(r1.labels) + for rr in reversed(rest): labels.update(rr.labels) return Resource(rtype, labels) @@ -159,7 +164,7 @@ def merge(self, other): :rtype: :class:`Resource` :return: The new combined resource. """ - return merge_resources([self, other]) + return merge_resources(self, other) def unquote(string):