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

feat: add instrumentation to list methods #239

Merged
merged 54 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
b13ad47
testing first trace export
aravinsiva Aug 1, 2020
cacea7c
instrumention client.py
aravinsiva Aug 3, 2020
63e9581
instrumenting job.py and adding documentation
aravinsiva Aug 4, 2020
719ed83
reconfiguring imports
aravinsiva Aug 4, 2020
72f22b6
quick cleanup of unused variable
aravinsiva Aug 5, 2020
ff8171f
adding more attributes in module and limiting complexity of instrumen…
aravinsiva Aug 7, 2020
456b445
adding tests, nox and correct attribute additions in client & job
aravinsiva Aug 9, 2020
f4aba6d
adding tests, nox and correct attribute additions in client & job (le…
aravinsiva Aug 9, 2020
e223657
linting
aravinsiva Aug 9, 2020
78b8298
reformatting noxfile.[y
aravinsiva Aug 9, 2020
88b1a57
Merge branch 'master' into opentelemetry-tracing
aravinsiva Aug 9, 2020
e0895c9
Merge branch 'master' into opentelemetry-tracing
aravinsiva Aug 10, 2020
95a35ad
addressing suggested changes
aravinsiva Aug 11, 2020
3bf4574
Merge branch 'opentelemetry-tracing' of https://github.com/aravinsiva…
aravinsiva Aug 11, 2020
4dbb692
adding suggested changes
aravinsiva Aug 12, 2020
406f61a
removing print statements
aravinsiva Aug 12, 2020
c2b9add
setting same version across all OT [ackages and other reccommended ch…
aravinsiva Aug 12, 2020
d425909
suggested changes
aravinsiva Aug 12, 2020
64ec5fd
fixing packages issue in nox and updating documentation
aravinsiva Aug 13, 2020
73230dc
fixing module install issue
aravinsiva Aug 13, 2020
ae04fc6
restructuring design for testing adding first layer of tests (some st…
aravinsiva Aug 14, 2020
ac56488
adding reamining client tests and all job tests
aravinsiva Aug 15, 2020
d4de926
fixing linting issues
aravinsiva Aug 15, 2020
2d8aeb3
Merge branch 'master' into opentelemetry-tracing
tswast Aug 17, 2020
cd8e303
fixing trace not defined issue
aravinsiva Aug 17, 2020
d8c601d
fixing lint issues
aravinsiva Aug 17, 2020
2bcd106
fixing documentation issues and python2 testing issue
aravinsiva Aug 17, 2020
f1de589
linting and fixing coverage issues
aravinsiva Aug 18, 2020
72a77d1
Merge branch 'master' into opentelemetry-tracing
tswast Aug 18, 2020
0c7b354
adding suggested changes
aravinsiva Aug 18, 2020
51045f1
Merge branch 'opentelemetry-tracing' of https://github.com/aravinsiva…
aravinsiva Aug 18, 2020
ec96f5c
linting
aravinsiva Aug 18, 2020
35afadf
Merge branch 'master' into opentelemetry-tracing
aravinsiva Aug 18, 2020
5a32ad4
adding Shawn's suggested changes
aravinsiva Aug 19, 2020
1d7155c
Merge branch 'opentelemetry-tracing' of https://github.com/aravinsiva…
aravinsiva Aug 19, 2020
54c133a
fixing _default_span_attribute_bug
aravinsiva Aug 19, 2020
3aff14d
reverting uneccesxsary changes
aravinsiva Aug 19, 2020
59477ff
adding more tests for all job_ref parameters
aravinsiva Aug 19, 2020
0d15998
removing dependecny, ordering imports and other changes
aravinsiva Aug 20, 2020
d997175
addressing Shawn concerns
aravinsiva Aug 20, 2020
61b5b85
adding test and suggested changes
aravinsiva Aug 20, 2020
4d1dec1
adding opentelemetry to setup.py and other suggested changes
aravinsiva Aug 20, 2020
7704975
adding reasoning for not adding to [all]
aravinsiva Aug 20, 2020
839cd92
linting
aravinsiva Aug 20, 2020
55c322e
adding nested functions
aravinsiva Aug 21, 2020
4f66ffb
adding test
aravinsiva Aug 21, 2020
dd1e246
adding Tim suggested changes
aravinsiva Aug 21, 2020
5b3a202
adding full tests
aravinsiva Aug 22, 2020
9757812
Merge branch 'opentelemetry-tracing' of https://github.com/aravinsiva…
aravinsiva Aug 22, 2020
b0ac6eb
Merge pull request #1 from aravinsiva/adding_telemetry_to_non_api_calls
aravinsiva Aug 22, 2020
c75f827
Merge branch 'opentelemetry-tracing' of https://github.com/aravinsiva…
aravinsiva Aug 22, 2020
745775e
Merge branch 'master' of https://github.com/aravinsiva/python-bigquer…
aravinsiva Aug 24, 2020
9e087ff
linting and fixing missing test
aravinsiva Aug 24, 2020
d858ef5
Merge branch 'master' into adding_telemetry_to_non_api_calls
aravinsiva Aug 24, 2020
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
63 changes: 57 additions & 6 deletions google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,17 @@ def list_projects(
Iterator of :class:`~google.cloud.bigquery.client.Project`
accessible to the current client.
"""
span_attributes = {"path": "/projects"}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listProjects", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

return page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path="/projects",
item_to_value=_item_to_project,
items_key="projects",
Expand Down Expand Up @@ -353,9 +361,18 @@ def list_datasets(
# and converting it into a string here.
extra_params["filter"] = filter
path = "/projects/%s/datasets" % (project,)

span_attributes = {"path": path}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listDatasets", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

return page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path=path,
item_to_value=_item_to_dataset,
items_key="datasets",
Expand Down Expand Up @@ -1067,9 +1084,17 @@ def list_models(
raise TypeError("dataset must be a Dataset, DatasetReference, or string")

path = "%s/models" % dataset.path
span_attributes = {"path": path}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listModels", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

result = page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path=path,
item_to_value=_item_to_model,
items_key="models",
Expand Down Expand Up @@ -1132,9 +1157,18 @@ def list_routines(
raise TypeError("dataset must be a Dataset, DatasetReference, or string")

path = "{}/routines".format(dataset.path)

span_attributes = {"path": path}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listRoutines", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

result = page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path=path,
item_to_value=_item_to_routine,
items_key="routines",
Expand Down Expand Up @@ -1197,9 +1231,17 @@ def list_tables(
raise TypeError("dataset must be a Dataset, DatasetReference, or string")

path = "%s/tables" % dataset.path
span_attributes = {"path": path}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listTables", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

result = page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path=path,
item_to_value=_item_to_table,
items_key="tables",
Expand Down Expand Up @@ -1765,9 +1807,18 @@ def list_jobs(
project = self.project

path = "/projects/%s/jobs" % (project,)

span_attributes = {"path": path}

def api_request(*args, **kwargs):
with create_span(
name="BigQuery.listJobs", attributes=span_attributes, client=self
):
return self._call_api(retry, *args, timeout=timeout, **kwargs)

return page_iterator.HTTPIterator(
client=self,
api_request=functools.partial(self._call_api, retry, timeout=timeout),
api_request=api_request,
path=path,
item_to_value=_item_to_job,
items_key="jobs",
Expand Down
122 changes: 105 additions & 17 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,14 @@ def test_list_projects_defaults(self):
creds = _make_credentials()
client = self._make_one(PROJECT_1, creds)
conn = client._connection = make_connection(DATA)

iterator = client.list_projects()
page = six.next(iterator.pages)

with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"

Choose a reason for hiding this comment

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

Optional comment: this is a really long string that is repeated many times in this file. It's probably a good idea to put it in a const string in your test client.

Since that would affect a bunch of other tests in this file, it would be best to do in a separate PR.

) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/projects"}, client, None)
projects = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -455,7 +460,13 @@ def test_list_projects_w_timeout(self):
conn = client._connection = make_connection(DATA)

iterator = client.list_projects(timeout=7.5)
six.next(iterator.pages)

with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/projects"}, client, None)

conn.api_request.assert_called_once_with(
method="GET", path="/projects", query_params={}, timeout=7.5
Expand All @@ -469,7 +480,13 @@ def test_list_projects_explicit_response_missing_projects_key(self):
conn = client._connection = make_connection(DATA)

iterator = client.list_projects(max_results=3, page_token=TOKEN)
page = six.next(iterator.pages)

with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/projects"}, client, None)
projects = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -518,7 +535,12 @@ def test_list_datasets_defaults(self):
conn = client._connection = make_connection(DATA)

iterator = client.list_datasets()
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
datasets = list(page)
token = iterator.next_page_token

Expand All @@ -538,7 +560,14 @@ def test_list_datasets_w_project_and_timeout(self):
client = self._make_one(self.PROJECT, creds)
conn = client._connection = make_connection({})

list(client.list_datasets(project="other-project", timeout=7.5))
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
list(client.list_datasets(project="other-project", timeout=7.5))

final_attributes.assert_called_once_with(
{"path": "/projects/other-project/datasets"}, client, None
)

conn.api_request.assert_called_once_with(
method="GET",
Expand All @@ -559,7 +588,12 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
iterator = client.list_datasets(
include_all=True, filter=FILTER, max_results=3, page_token=TOKEN
)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
datasets = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -2838,7 +2872,12 @@ def test_list_tables_empty_w_timeout(self):
dataset = DatasetReference(self.PROJECT, self.DS_ID)
iterator = client.list_tables(dataset, timeout=7.5)
self.assertIs(iterator.dataset, dataset)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": path}, client, None)
tables = list(page)
token = iterator.next_page_token

Expand All @@ -2856,7 +2895,12 @@ def test_list_models_empty_w_timeout(self):

dataset_id = "{}.{}".format(self.PROJECT, self.DS_ID)
iterator = client.list_models(dataset_id, timeout=7.5)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": path}, client, None)
models = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -2900,7 +2944,12 @@ def test_list_models_defaults(self):

iterator = client.list_models(dataset)
self.assertIs(iterator.dataset, dataset)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
models = list(page)
token = iterator.next_page_token

Expand All @@ -2926,7 +2975,16 @@ def test_list_routines_empty_w_timeout(self):
conn = client._connection = make_connection({})

iterator = client.list_routines("test-routines.test_routines", timeout=7.5)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with(
{"path": "/projects/test-routines/datasets/test_routines/routines"},
client,
None,
)
routines = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -2975,7 +3033,12 @@ def test_list_routines_defaults(self):

iterator = client.list_routines(dataset)
self.assertIs(iterator.dataset, dataset)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": path}, client, None)
routines = list(page)
actual_token = iterator.next_page_token

Expand Down Expand Up @@ -3039,7 +3102,12 @@ def test_list_tables_defaults(self):

iterator = client.list_tables(dataset)
self.assertIs(iterator.dataset, dataset)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
tables = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -3098,7 +3166,12 @@ def test_list_tables_explicit(self):
page_token=TOKEN,
)
self.assertEqual(iterator.dataset, dataset)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
tables = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -3921,7 +3994,12 @@ def test_list_jobs_defaults(self):
conn = client._connection = make_connection(DATA)

iterator = client.list_jobs()
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
jobs = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -3966,7 +4044,12 @@ def test_list_jobs_load_job_wo_sourceUris(self):
conn = client._connection = make_connection(DATA)

iterator = client.list_jobs()
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
jobs = list(page)
token = iterator.next_page_token

Expand Down Expand Up @@ -3995,7 +4078,12 @@ def test_list_jobs_explicit_missing(self):
iterator = client.list_jobs(
max_results=1000, page_token=TOKEN, all_users=True, state_filter="done"
)
page = six.next(iterator.pages)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = six.next(iterator.pages)

final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
jobs = list(page)
token = iterator.next_page_token

Expand Down