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

Commit 0373087

Browse files
dhendryc24t
authored andcommitted
Fix: Tracing broken in GCE (app engine flexible) - Trivial (#365)
* Update Readme to use OPENCENSUS_TRACE.EXPORTER It looks like OPENCENSUS_TRACE.REPORTER is an old thing. * Fix byte string concat problem * Fix other case where byte to string conversion was necessary * Testing * Response to comments * Fix super silly mistake in test assertion. Been a long week
1 parent ab95507 commit 0373087

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

opencensus/common/monitored_resource_util/gcp_metadata_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,10 @@ def _get_attribute(attribute_key):
149149
_GKE_ATTRIBUTES[attribute_key],
150150
_GCP_METADATA_URI_HEADER)
151151

152+
if attribute_value is not None and isinstance(attribute_value, bytes):
153+
# At least in python3, bytes are are returned from
154+
# urllib (although the response is text), convert
155+
# to a normal string:
156+
attribute_value = attribute_value.decode('utf-8')
157+
152158
return attribute_value

opencensus/trace/ext/requests/trace.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def trace_integration(tracer=None):
3636
"""Wrap the requests library to trace it."""
3737
log.info('Integrated module: {}'.format(MODULE_NAME))
3838

39-
execution_context.set_opencensus_tracer(tracer)
39+
if tracer is not None:
40+
# The execution_context tracer should never be None - if it has not
41+
# been set it returns a no-op tracer. Most code in this library does
42+
# not handle None being used in the execution context.
43+
execution_context.set_opencensus_tracer(tracer)
4044

4145
# Wrap the requests functions
4246
for func in REQUESTS_WRAP_METHODS:

tests/unit/common/monitored_resource_util/test_gcp_metadata_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ def assign_attribute_value(*args, **kwargs):
5454

5555
self.assertEquals(labels_list, expected_labels)
5656

57+
@mock.patch('opencensus.common.monitored_resource_util.'
58+
'gcp_metadata_config.get_request')
59+
def test_get_gce_metadata_binary_strings(self, http_request_mock):
60+
"""
61+
At least in python 3 binary strings are returned from urllib
62+
"""
63+
def assign_attribute_value(*args, **kwargs):
64+
attribute_uri = args[0].split('/')[-1]
65+
if attribute_uri == 'id':
66+
return b'my-instance'
67+
elif attribute_uri == 'project-id':
68+
return b'my-project'
69+
elif attribute_uri == 'zone':
70+
return b'us-east1'
71+
72+
http_request_mock.side_effect = assign_attribute_value
73+
GcpMetadataConfig.inited = False
74+
GcpMetadataConfig.is_running = False
75+
gcp_metadata_config.gcp_metadata_map = {}
76+
77+
self.assertTrue(GcpMetadataConfig.is_running_on_gcp())
78+
79+
labels_list = GcpMetadataConfig().get_gce_metadata()
80+
81+
self.assertEquals(len(labels_list), 3)
82+
83+
expected_labels = {
84+
'instance_id': 'my-instance',
85+
'project_id': 'my-project',
86+
'zone': 'us-east1'
87+
}
88+
89+
self.assertEquals(labels_list, expected_labels)
90+
5791
@mock.patch.dict(os.environ,
5892
{'KUBERNETES_SERVICE_HOST': '127.0.0.1',
5993
'CONTAINER_NAME': 'container',

tests/unit/trace/ext/requests/test_requests_trace.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import unittest
1616

1717
import mock
18+
from opencensus.trace.tracers import noop_tracer
1819

19-
from opencensus.trace import span as span_module
20+
from opencensus.trace import span as span_module, execution_context
2021
from opencensus.trace.ext.requests import trace
2122

2223

@@ -42,9 +43,37 @@ def test_trace_integration(self):
4243
with patch_wrap, patch_requests:
4344
trace.trace_integration()
4445

46+
self.assertIsInstance(execution_context.get_opencensus_tracer(),
47+
noop_tracer.NoopTracer)
48+
4549
for func in trace.REQUESTS_WRAP_METHODS:
4650
self.assertEqual(getattr(mock_requests, func), wrap_result)
4751

52+
def test_trace_integration_set_tracer(self):
53+
mock_wrap = mock.Mock()
54+
mock_requests = mock.Mock()
55+
56+
wrap_result = 'wrap result'
57+
mock_wrap.return_value = wrap_result
58+
59+
for func in trace.REQUESTS_WRAP_METHODS:
60+
mock_func = mock.Mock()
61+
mock_func.__name__ = func
62+
setattr(mock_requests, func, mock_func)
63+
64+
patch_wrap = mock.patch(
65+
'opencensus.trace.ext.requests.trace.wrap_requests', mock_wrap)
66+
patch_requests = mock.patch(
67+
'opencensus.trace.ext.requests.trace.requests', mock_requests)
68+
69+
class TmpTracer(noop_tracer.NoopTracer):
70+
pass
71+
72+
with patch_wrap, patch_requests:
73+
trace.trace_integration(tracer=TmpTracer())
74+
75+
self.assertIsInstance(execution_context.get_opencensus_tracer(), TmpTracer)
76+
4877
def test_wrap_requests(self):
4978
mock_return = mock.Mock()
5079
mock_return.status_code = 200

0 commit comments

Comments
 (0)