Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using *args in merge #1

Open
wants to merge 1 commit into
base: better-resource-detection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really a beautiful way to do this here since any of the resources could be null.

23 changes: 14 additions & 9 deletions opencensus/common/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,34 @@
_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
a label key, use the value from the first resource in the list with that
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)

Expand Down Expand Up @@ -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):
Expand Down