Skip to content

Commit 8229d0f

Browse files
authored
Add Google Cloud Reporter (census-instrumentation#15)
1 parent 3ead8a9 commit 8229d0f

7 files changed

Lines changed: 153 additions & 8 deletions

File tree

README.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ trace backend later.
108108
reporter = file_reporter.FileReporter(file_name='traces')
109109
tracer = context_tracer.ContextTracer(reporter=reporter)
110110
111+
Report to Stackdriver Trace:
112+
113+
.. code:: python
114+
115+
from opencensus.trace.reporters import google_cloud_reporter
116+
from opencensus.trace.tracer import context_tracer
117+
118+
reporter = google_cloud_reporter.GoogleCloudReporter(
119+
project_id='your_cloud_project')
120+
tracer = context_tracer.ContextTracer(reporter=reporter)
121+
111122
Propagators
112123
~~~~~~~~~~~
113124

@@ -249,4 +260,4 @@ Apache 2.0 - See `LICENSE <LICENSE>`__ for more information.
249260
Disclaimer
250261
----------
251262

252-
This is not an official Google product.
263+
This is not an official Google product.

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
"""A setup module for Open Source Census Instrumentation Library"""
1616

17-
import io
1817
from setuptools import setup, find_packages
1918

2019
install_requires = [
2120
'google-gax>=0.15.7, <0.16dev',
2221
'googleapis-common-protos[grpc]>=1.5.2, <2.0dev',
23-
'google-cloud-core >= 0.24.0, < 0.25dev',
2422
]
2523

2624
setup(

trace/nox.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ def unit_tests(session, python_version):
2626
session.interpreter = 'python{}'.format(python_version)
2727

2828
# Install all test dependencies, then install this package in-place.
29-
session.install('mock', 'pytest', 'pytest-cov', 'google-cloud-core',
30-
'django', 'flask', 'webapp2', 'webob')
29+
session.install('mock', 'pytest', 'pytest-cov',
30+
'django', 'flask', 'webapp2', 'webob',
31+
'google-cloud-trace')
3132

3233
session.install('-e', '.')
3334

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud.trace.client import Client
16+
17+
18+
class GoogleCloudReporter(object):
19+
"""A reporter that send traces and trace spans to Google Cloud Stackdriver
20+
Trace.
21+
"""
22+
def __init__(self, client=None, project_id=None):
23+
# The client will handler the case when project_id is None
24+
if client is None:
25+
client = Client(project=project_id)
26+
27+
self.client = client
28+
self.project_id = client.project
29+
30+
def report(self, trace):
31+
"""
32+
:type trace: dict
33+
:param trace: Trace collected.
34+
"""
35+
stackdriver_traces = self.translate_to_stackdriver(trace)
36+
self.client.patch_traces(stackdriver_traces)
37+
38+
def translate_to_stackdriver(self, trace):
39+
"""
40+
:type trace: dict
41+
:param trace: Trace collected.
42+
43+
:rtype: dict
44+
:returns: Traces in Google Cloud StackDriver Trace format.
45+
"""
46+
trace['projectId'] = self.project_id
47+
traces = {'traces': [trace]}
48+
return traces

trace/setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
install_requires = [
2121
'google-gax>=0.15.7, <0.16dev',
2222
'googleapis-common-protos[grpc]>=1.5.2, <2.0dev',
23-
'google-cloud-core >= 0.24.0, < 0.25dev',
2423
]
2524

2625
setup(
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import mock
18+
19+
from opencensus.trace.reporters import google_cloud_reporter
20+
21+
22+
class _Client(object):
23+
def __init__(self, project=None):
24+
if project is None:
25+
project = 'PROJECT'
26+
27+
self.project = project
28+
29+
30+
class TestGoogleCloudReporter(unittest.TestCase):
31+
32+
def test_constructor_default(self):
33+
patch = mock.patch(
34+
'opencensus.trace.reporters.google_cloud_reporter.Client',
35+
new=_Client)
36+
37+
with patch:
38+
reporter = google_cloud_reporter.GoogleCloudReporter()
39+
40+
project_id = 'PROJECT'
41+
self.assertEqual(reporter.project_id, project_id)
42+
43+
def test_constructor_explicit(self):
44+
client = mock.Mock()
45+
project_id = 'PROJECT'
46+
client.project = project_id
47+
48+
reporter = google_cloud_reporter.GoogleCloudReporter(
49+
client=client,
50+
project_id=project_id)
51+
52+
self.assertIs(reporter.client, client)
53+
self.assertEqual(reporter.project_id, project_id)
54+
55+
def test_report(self):
56+
trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'}
57+
58+
client = mock.Mock()
59+
project_id = 'PROJECT'
60+
client.project = project_id
61+
62+
reporter = google_cloud_reporter.GoogleCloudReporter(
63+
client=client,
64+
project_id=project_id)
65+
66+
reporter.report(trace)
67+
68+
trace['projectId'] = project_id
69+
traces = {'traces': [trace]}
70+
71+
client.patch_traces.assert_called_with(traces)
72+
self.assertTrue(client.patch_traces.called)
73+
74+
def test_translate_to_stackdriver(self):
75+
project_id = 'PROJECT'
76+
trace = {'spans': [], 'traceId': '6e0c63257de34c92bf9efcd03927272e'}
77+
78+
client = mock.Mock()
79+
client.project = project_id
80+
reporter = google_cloud_reporter.GoogleCloudReporter(
81+
client=client,
82+
project_id=project_id)
83+
84+
85+
traces = reporter.translate_to_stackdriver(trace)
86+
87+
trace['projectId'] = project_id
88+
expected_traces = {'traces': [trace]}
89+
90+
self.assertEqual(traces, expected_traces)

trace/tests/unit/test_trace.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
class TestTrace(unittest.TestCase):
2121

22-
project = 'PROJECT'
23-
2422
@staticmethod
2523
def _get_target_class():
2624
from opencensus.trace.trace import Trace

0 commit comments

Comments
 (0)