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

Commit 3438dcc

Browse files
authored
Remove vendor specific labels from core library (#62)
1 parent a155f47 commit 3438dcc

7 files changed

Lines changed: 93 additions & 146 deletions

File tree

trace/opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,63 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
1517
from opencensus.trace.exporters import base
1618

1719
from google.cloud.trace.client import Client
1820

21+
# Environment variable set in App Engine when vm:true is set.
22+
_APPENGINE_FLEXIBLE_ENV_VM = 'GAE_APPENGINE_HOSTNAME'
23+
24+
# Environment variable set in App Engine when env:flex is set.
25+
_APPENGINE_FLEXIBLE_ENV_FLEX = 'GAE_INSTANCE'
26+
27+
# GAE common labels
28+
# See: https://cloud.google.com/appengine/docs/flexible/python/runtime#
29+
# environment_variables
30+
GAE_LABELS = {
31+
'GAE_FLEX_VERSION': 'g.co/gae/app/version',
32+
'GAE_FLEX_SERVICE': 'g.co/gae/app/service',
33+
'GAE_FLEX_PROJECT': 'g.co/gae/app/project',
34+
'GAE_FLEX_INSTANCE': 'g.co/gae/app/instance',
35+
'GAE_FLEX_MEMORY_MB': 'g.co/gae/app/memory_mb',
36+
'GAE_FLEX_PORT': 'g.co/gae/app/port',
37+
}
38+
39+
# GCE common labels
40+
GCE_LABELS = {
41+
'GCE_INSTANCE_ID': 'g.co/gce/instanceid',
42+
'GCE_HOSTNAME': 'g.co/gce/hostname',
43+
}
44+
45+
46+
def set_labels(trace):
47+
"""Automatically set labels for Google Cloud environment."""
48+
if is_gae_environment():
49+
set_gae_labels(trace)
50+
51+
52+
def set_gae_labels(trace):
53+
"""Set the GAE environment common labels."""
54+
spans = trace.get('spans')
55+
56+
for env_var, label_key in GAE_LABELS.items():
57+
label_value = os.environ.get(env_var)
58+
59+
if label_value is not None:
60+
for span in spans:
61+
labels = span.get('labels')
62+
labels[label_key] = label_value
63+
span['labels'] = labels
64+
65+
66+
def is_gae_environment():
67+
"""Return True if the GAE related env vars is detected."""
68+
if (_APPENGINE_FLEXIBLE_ENV_VM in os.environ or
69+
_APPENGINE_FLEXIBLE_ENV_FLEX in os.environ):
70+
return True
71+
1972

2073
class StackdriverExporter(base.Exporter):
2174
"""A exporter that send traces and trace spans to Google Cloud Stackdriver
@@ -45,6 +98,7 @@ def translate_to_stackdriver(self, trace):
4598
:rtype: dict
4699
:returns: Traces in Google Cloud StackDriver Trace format.
47100
"""
101+
set_labels(trace)
48102
trace['projectId'] = self.project_id
49103
traces = {'traces': [trace]}
50104
return traces

trace/opencensus/trace/ext/django/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from opencensus.trace import execution_context
2323
from opencensus.trace.samplers import probability
2424

25-
HTTP_METHOD = labels_helper.STACKDRIVER_LABELS['HTTP_METHOD']
26-
HTTP_URL = labels_helper.STACKDRIVER_LABELS['HTTP_URL']
27-
HTTP_STATUS_CODE = labels_helper.STACKDRIVER_LABELS['HTTP_STATUS_CODE']
25+
HTTP_METHOD = labels_helper.COMMON_LABELS['HTTP_METHOD']
26+
HTTP_URL = labels_helper.COMMON_LABELS['HTTP_URL']
27+
HTTP_STATUS_CODE = labels_helper.COMMON_LABELS['HTTP_STATUS_CODE']
2828

2929
REQUEST_THREAD_LOCAL_KEY = 'django_request'
3030

trace/opencensus/trace/ext/flask/flask_middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
_FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT'
2626

27-
HTTP_METHOD = labels_helper.STACKDRIVER_LABELS['HTTP_METHOD']
28-
HTTP_URL = labels_helper.STACKDRIVER_LABELS['HTTP_URL']
29-
HTTP_STATUS_CODE = labels_helper.STACKDRIVER_LABELS['HTTP_STATUS_CODE']
27+
HTTP_METHOD = labels_helper.COMMON_LABELS['HTTP_METHOD']
28+
HTTP_URL = labels_helper.COMMON_LABELS['HTTP_URL']
29+
HTTP_STATUS_CODE = labels_helper.COMMON_LABELS['HTTP_STATUS_CODE']
3030

3131
log = logging.getLogger(__name__)
3232

trace/opencensus/trace/labels_helper.py

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
17-
# Environment variable set in App Engine when vm:true is set.
18-
_APPENGINE_FLEXIBLE_ENV_VM = 'GAE_APPENGINE_HOSTNAME'
19-
20-
# Environment variable set in App Engine when env:flex is set.
21-
_APPENGINE_FLEXIBLE_ENV_FLEX = 'GAE_INSTANCE'
22-
23-
# Stackdriver Trace common labels.
24-
# See: https://cloud.google.com/trace/docs/reference/v1/rpc/
25-
# google.devtools.cloudtrace.v1#google.devtools.cloudtrace.v1.TraceSpan
26-
STACKDRIVER_LABELS = {
15+
COMMON_LABELS = {
2716
'AGENT': '/agent',
2817
'COMPONENT': '/component',
2918
'ERROR_MESSAGE': '/error/message',
@@ -44,47 +33,3 @@
4433
'STACKTRACE': '/stacktrace',
4534
'TID': '/tid',
4635
}
47-
48-
# GAE common labels
49-
# See: https://cloud.google.com/appengine/docs/flexible/python/runtime#
50-
# environment_variables
51-
GAE_LABELS = {
52-
'GAE_FLEX_VERSION': 'g.co/gae/app/version',
53-
'GAE_FLEX_SERVICE': 'g.co/gae/app/service',
54-
'GAE_FLEX_PROJECT': 'g.co/gae/app/project',
55-
'GAE_FLEX_INSTANCE': 'g.co/gae/app/instance',
56-
'GAE_FLEX_MEMORY_MB': 'g.co/gae/app/memory_mb',
57-
'GAE_FLEX_PORT': 'g.co/gae/app/port',
58-
}
59-
60-
# GCE common labels
61-
GCE_LABELS = {
62-
'GCE_INSTANCE_ID': 'g.co/gce/instanceid',
63-
'GCE_HOSTNAME': 'g.co/gce/hostname',
64-
}
65-
66-
67-
class LabelsHelper(object):
68-
"""Helper class to automatically set labels."""
69-
70-
def __init__(self, tracer):
71-
self.tracer = tracer
72-
73-
def set_labels(self):
74-
"""Automatically set labels for each environment."""
75-
if self.is_gae_environment():
76-
self.set_gae_labels()
77-
78-
def set_gae_labels(self):
79-
"""Set the GAE environment common labels."""
80-
for env_var, label_key in GAE_LABELS.items():
81-
label_value = os.environ.get(env_var)
82-
83-
if label_value is not None:
84-
self.tracer.add_label_to_spans(label_key, label_value)
85-
86-
def is_gae_environment(self):
87-
"""Return True if the GAE related env vars is detected."""
88-
if (_APPENGINE_FLEXIBLE_ENV_VM in os.environ or
89-
_APPENGINE_FLEXIBLE_ENV_FLEX in os.environ):
90-
return True

trace/opencensus/trace/tracer/context_tracer.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from opencensus.trace import execution_context
1818
from opencensus.trace.span_context import SpanContext
19-
from opencensus.trace import labels_helper
2019
from opencensus.trace import span as trace_span
2120
from opencensus.trace.tracer import base
2221

@@ -45,10 +44,6 @@ def finish(self):
4544
:rtype: dict
4645
:returns: JSON format trace.
4746
"""
48-
# Insert the common labels to spans
49-
helper = labels_helper.LabelsHelper(self)
50-
helper.set_labels()
51-
5247
trace = self._get_trace_json()
5348

5449
return trace

trace/tests/unit/exporters/test_stackdriver_exporter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,35 @@ def test_translate_to_stackdriver(self):
8888
expected_traces = {'traces': [trace]}
8989

9090
self.assertEqual(traces, expected_traces)
91+
92+
93+
class Test_set_labels_gae(unittest.TestCase):
94+
95+
def test_set_labels_gae(self):
96+
import os
97+
98+
trace = {
99+
'spans': [
100+
{
101+
'labels':{},
102+
'span_id': 123,
103+
},
104+
],
105+
}
106+
107+
expected_labels = {
108+
stackdriver_exporter.GAE_LABELS['GAE_FLEX_PROJECT']: 'project',
109+
stackdriver_exporter.GAE_LABELS['GAE_FLEX_SERVICE']: 'service',
110+
stackdriver_exporter.GAE_LABELS['GAE_FLEX_VERSION']: 'version',
111+
}
112+
113+
with mock.patch.dict(
114+
os.environ,
115+
{stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_VM: 'vm',
116+
stackdriver_exporter._APPENGINE_FLEXIBLE_ENV_FLEX: 'flex',
117+
'GAE_FLEX_PROJECT': 'project',
118+
'GAE_FLEX_SERVICE': 'service',
119+
'GAE_FLEX_VERSION': 'version'}):
120+
stackdriver_exporter.set_labels(trace)
121+
122+
self.assertEqual(trace['spans'][0]['labels'], expected_labels)

trace/tests/unit/test_labels_helper.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)