Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit ae72795

Browse files
authored
Add agent label to stackdriver exporter (#111)
1 parent 54d11c9 commit ae72795

File tree

3 files changed

+115
-31
lines changed

3 files changed

+115
-31
lines changed

opencensus/trace/attributes_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
COMMON_ATTRIBUTES = {
16-
'AGENT': '/agent',
16+
'AGENT': 'g.co/agent',
1717
'COMPONENT': '/component',
1818
'ERROR_MESSAGE': '/error/message',
1919
'ERROR_NAME': '/error/name',

opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414

1515
import os
1616

17+
from opencensus.trace.attributes import Attributes
18+
from opencensus.trace import attributes_helper
1719
from opencensus.trace.exporters import base
1820
from opencensus.trace.exporters.transports import sync
1921

2022
from google.cloud.trace.client import Client
2123

2224

25+
# OpenCensus Version
26+
VERSION = '0.1.1'
27+
28+
# Agent
29+
AGENT = 'opencensus-python [{}]'.format(VERSION)
30+
2331
# Environment variable set in App Engine when vm:true is set.
2432
_APPENGINE_FLEXIBLE_ENV_VM = 'GAE_APPENGINE_HOSTNAME'
2533

@@ -45,24 +53,49 @@
4553
}
4654

4755

56+
def _update_attr_map(span, attrs):
57+
attr_map = span.get('attributes', {}).get('attributeMap', {})
58+
attr_map.update(attrs)
59+
span['attributes']['attributeMap'] = attr_map
60+
61+
4862
def set_attributes(trace):
4963
"""Automatically set attributes for Google Cloud environment."""
50-
if is_gae_environment():
51-
set_gae_attributes(trace)
64+
spans = trace.get('spans')
65+
for span in spans:
66+
if span.get('attributes') is None:
67+
span['attributes'] = {}
5268

69+
if is_gae_environment():
70+
set_gae_attributes(span)
5371

54-
def set_gae_attributes(trace):
55-
"""Set the GAE environment common attributes."""
56-
spans = trace.get('spans')
72+
set_common_attributes(span)
5773

74+
75+
def set_common_attributes(span):
76+
"""Set the common attributes."""
77+
common = {
78+
attributes_helper.COMMON_ATTRIBUTES.get('AGENT'): AGENT,
79+
}
80+
common_attrs = Attributes(common)\
81+
.format_attributes_json()\
82+
.get('attributeMap')
83+
84+
_update_attr_map(span, common_attrs)
85+
86+
87+
def set_gae_attributes(span):
88+
"""Set the GAE environment common attributes."""
5889
for env_var, attribute_key in GAE_ATTRIBUTES.items():
5990
attribute_value = os.environ.get(env_var)
6091

6192
if attribute_value is not None:
62-
for span in spans:
63-
attributes = span.get('attributes')
64-
attributes[attribute_key] = attribute_value
65-
span['attributes'] = attributes
93+
pair = {attribute_key: attribute_value}
94+
pair_attrs = Attributes(pair)\
95+
.format_attributes_json()\
96+
.get('attributeMap')
97+
98+
_update_attr_map(span, pair_attrs)
6699

67100

68101
def is_gae_environment():

tests/unit/trace/exporters/test_stackdriver_exporter.py

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ def test_emit(self):
8787
'links': None,
8888
'startTime': None,
8989
'spanId': '1111',
90-
'attributes': None,
90+
'attributes': {
91+
'attributeMap': {
92+
'g.co/agent': {
93+
'string_value': {
94+
'truncated_byte_count': 0,
95+
'value': 'opencensus-python [0.1.1]'
96+
}
97+
}
98+
}
99+
},
91100
'stackTrace': None,
92101
'displayName':
93102
{
@@ -122,8 +131,15 @@ def test_translate_to_stackdriver(self):
122131
trace_id = '6e0c63257de34c92bf9efcd03927272e'
123132
span_name = 'test span'
124133
span_id = 1234
125-
attributes = {'attributeMap': {
126-
'key': 'value'}
134+
attributes = {
135+
'attributeMap': {
136+
'key': {
137+
'string_value': {
138+
'truncated_byte_count': 0,
139+
'value': 'value'
140+
}
141+
}
142+
}
127143
}
128144
parent_span_id = 1111
129145
start_time = 'test start time'
@@ -164,7 +180,21 @@ def test_translate_to_stackdriver(self):
164180
'value': span_name,
165181
'truncated_byte_count': 0
166182
},
167-
'attributes': {'attributeMap': {'key': 'value'}},
183+
'attributes': {
184+
'attributeMap': {
185+
'g.co/agent': {
186+
'string_value': {
187+
'truncated_byte_count': 0,
188+
'value': 'opencensus-python [0.1.1]'}
189+
},
190+
'key': {
191+
'string_value': {
192+
'truncated_byte_count': 0,
193+
'value': 'value'
194+
}
195+
}
196+
}
197+
},
168198
'spanId': str(span_id),
169199
'startTime': start_time,
170200
'endTime': end_time,
@@ -179,9 +209,6 @@ def test_translate_to_stackdriver(self):
179209
]
180210
}
181211

182-
print(spans)
183-
print(expected_traces)
184-
185212
self.assertEqual(spans, expected_traces)
186213

187214

@@ -190,19 +217,41 @@ class Test_set_attributes_gae(unittest.TestCase):
190217
def test_set_attributes_gae(self):
191218
import os
192219

193-
trace = {
194-
'spans': [
195-
{
196-
'attributes':{},
197-
'span_id': 123,
198-
},
199-
],
200-
}
201-
202-
expected_attributes = {
203-
stackdriver_exporter.GAE_ATTRIBUTES['GAE_FLEX_PROJECT']: 'project',
204-
stackdriver_exporter.GAE_ATTRIBUTES['GAE_FLEX_SERVICE']: 'service',
205-
stackdriver_exporter.GAE_ATTRIBUTES['GAE_FLEX_VERSION']: 'version',
220+
trace = {'spans': [
221+
{
222+
'attributes': {}
223+
}
224+
]}
225+
226+
expected = {
227+
'attributes': {
228+
'attributeMap': {
229+
'g.co/gae/app/service': {
230+
'string_value': {
231+
'truncated_byte_count': 0,
232+
'value': 'service'
233+
}
234+
},
235+
'g.co/gae/app/version': {
236+
'string_value': {
237+
'truncated_byte_count': 0,
238+
'value': 'version'
239+
}
240+
},
241+
'g.co/gae/app/project': {
242+
'string_value': {
243+
'truncated_byte_count': 0,
244+
'value': 'project'
245+
}
246+
},
247+
'g.co/agent': {
248+
'string_value': {
249+
'truncated_byte_count': 0,
250+
'value': 'opencensus-python [0.1.1]'
251+
}
252+
},
253+
}
254+
}
206255
}
207256

208257
with mock.patch.dict(
@@ -212,9 +261,11 @@ def test_set_attributes_gae(self):
212261
'GAE_FLEX_PROJECT': 'project',
213262
'GAE_FLEX_SERVICE': 'service',
214263
'GAE_FLEX_VERSION': 'version'}):
264+
self.assertTrue(stackdriver_exporter.is_gae_environment())
215265
stackdriver_exporter.set_attributes(trace)
216266

217-
self.assertEqual(trace['spans'][0]['attributes'], expected_attributes)
267+
span = trace.get('spans')[0]
268+
self.assertEqual(span, expected)
218269

219270

220271
class MockTransport(object):

0 commit comments

Comments
 (0)