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

Commit 68af772

Browse files
authored
Rename label(s) to attribute(s) (#89)
1 parent 717fa49 commit 68af772

27 files changed

Lines changed: 246 additions & 189 deletions

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Alternatively, you can explicitly start and end a span:
6969
7070
# Initialize a tracer, by default using the `PrintExporter`
7171
tracer = request_tracer.RequestTracer()
72-
tracer.start_trace()
7372
7473
tracer.start_span(name='span1')
7574
do_something_to_trace()

trace/opencensus/trace/labels_helper.py renamed to trace/opencensus/trace/attributes_helper.py

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

15-
COMMON_LABELS = {
15+
COMMON_ATTRIBUTES = {
1616
'AGENT': '/agent',
1717
'COMPONENT': '/component',
1818
'ERROR_MESSAGE': '/error/message',

trace/opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
# Environment variable set in App Engine when env:flex is set.
2626
_APPENGINE_FLEXIBLE_ENV_FLEX = 'GAE_INSTANCE'
2727

28-
# GAE common labels
28+
# GAE common attributes
2929
# See: https://cloud.google.com/appengine/docs/flexible/python/runtime#
3030
# environment_variables
31-
GAE_LABELS = {
31+
GAE_ATTRIBUTES = {
3232
'GAE_FLEX_VERSION': 'g.co/gae/app/version',
3333
'GAE_FLEX_SERVICE': 'g.co/gae/app/service',
3434
'GAE_FLEX_PROJECT': 'g.co/gae/app/project',
@@ -37,31 +37,31 @@
3737
'GAE_FLEX_PORT': 'g.co/gae/app/port',
3838
}
3939

40-
# GCE common labels
41-
GCE_LABELS = {
40+
# GCE common attributes
41+
GCE_ATTRIBUTES = {
4242
'GCE_INSTANCE_ID': 'g.co/gce/instanceid',
4343
'GCE_HOSTNAME': 'g.co/gce/hostname',
4444
}
4545

4646

47-
def set_labels(trace):
48-
"""Automatically set labels for Google Cloud environment."""
47+
def set_attributes(trace):
48+
"""Automatically set attributes for Google Cloud environment."""
4949
if is_gae_environment():
50-
set_gae_labels(trace)
50+
set_gae_attributes(trace)
5151

5252

53-
def set_gae_labels(trace):
54-
"""Set the GAE environment common labels."""
53+
def set_gae_attributes(trace):
54+
"""Set the GAE environment common attributes."""
5555
spans = trace.get('spans')
5656

57-
for env_var, label_key in GAE_LABELS.items():
58-
label_value = os.environ.get(env_var)
57+
for env_var, attribute_key in GAE_ATTRIBUTES.items():
58+
attribute_value = os.environ.get(env_var)
5959

60-
if label_value is not None:
60+
if attribute_value is not None:
6161
for span in spans:
62-
labels = span.get('labels')
63-
labels[label_key] = label_value
64-
span['labels'] = labels
62+
attributes = span.get('attributes')
63+
attributes[attribute_key] = attribute_value
64+
span['attributes'] = attributes
6565

6666

6767
def is_gae_environment():
@@ -117,7 +117,27 @@ def translate_to_stackdriver(self, trace):
117117
:rtype: dict
118118
:returns: Traces in Google Cloud StackDriver Trace format.
119119
"""
120-
set_labels(trace)
121-
trace['projectId'] = self.project_id
122-
traces = {'traces': [trace]}
120+
set_attributes(trace)
121+
spans = trace.get('spans')
122+
trace_id = trace.get('traceId')
123+
spans_json = []
124+
125+
for span in spans:
126+
span_json = {
127+
'name': span.get('name'),
128+
'startTime': span.get('startTime'),
129+
'endTime': span.get('endTime'),
130+
'spanId': span.get('spanId'),
131+
'parentSpanId': span.get('parentSpanId'),
132+
'labels': span.get('attributes')
133+
}
134+
spans_json.append(span_json)
135+
136+
trace_json = {
137+
'projectId': self.project_id,
138+
'traceId': trace_id,
139+
'spans': spans_json
140+
}
141+
142+
traces = {'traces': [trace_json]}
123143
return traces

trace/opencensus/trace/exporters/zipkin_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def translate_to_zipkin(self, trace_id, spans):
155155
'timestamp': int(round(start_timestamp_ms)),
156156
'duration': int(round(duration_ms)),
157157
'localEndpoint': local_endpoint,
158-
'tags': span.get('labels'),
158+
'tags': span.get('attributes'),
159159
}
160160

161161
span_kind = span.get('kind')

trace/opencensus/trace/ext/dbapi/trace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def call(query, *args, **kwargs):
5555
_tracer = execution_context.get_opencensus_tracer()
5656
_span = _tracer.start_span()
5757
_span.name = 'mysql.query'
58-
_tracer.add_label_to_current_span('mysql/query', query)
59-
_tracer.add_label_to_current_span(
58+
_tracer.add_attribute_to_current_span('mysql/query', query)
59+
_tracer.add_attribute_to_current_span(
6060
'mysql/cursor/method/name',
6161
query_func.__name__)
6262

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from opencensus.trace.ext import utils
1919
from opencensus.trace.ext.django.config import (settings, convert_to_import)
20-
from opencensus.trace import labels_helper
20+
from opencensus.trace import attributes_helper
2121
from opencensus.trace import request_tracer
2222
from opencensus.trace import execution_context
2323
from opencensus.trace.samplers import probability
@@ -27,9 +27,9 @@
2727
except ImportError: # pragma: NO COVER
2828
MiddlewareMixin = object
2929

30-
HTTP_METHOD = labels_helper.COMMON_LABELS['HTTP_METHOD']
31-
HTTP_URL = labels_helper.COMMON_LABELS['HTTP_URL']
32-
HTTP_STATUS_CODE = labels_helper.COMMON_LABELS['HTTP_STATUS_CODE']
30+
HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES['HTTP_METHOD']
31+
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
32+
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
3333

3434
REQUEST_THREAD_LOCAL_KEY = 'django_request'
3535

@@ -61,8 +61,8 @@ def _get_current_request_tracer():
6161
return execution_context.get_opencensus_tracer()
6262

6363

64-
def _set_django_labels(tracer, request):
65-
"""Set the django related labels."""
64+
def _set_django_attributes(tracer, request):
65+
"""Set the django related attributes."""
6666
django_user = getattr(request, 'user', None)
6767

6868
if django_user is None:
@@ -73,10 +73,10 @@ def _set_django_labels(tracer, request):
7373

7474
# User id is the django autofield for User model as the primary key
7575
if user_id is not None:
76-
tracer.add_label_to_current_span('/django/user/id', str(user_id))
76+
tracer.add_attribute_to_current_span('/django/user/id', str(user_id))
7777

7878
if user_name is not None:
79-
tracer.add_label_to_current_span('/django/user/name', user_name)
79+
tracer.add_attribute_to_current_span('/django/user/name', user_name)
8080

8181

8282
def get_django_header():
@@ -167,12 +167,12 @@ def process_request(self, request):
167167

168168
# Span name is being set at process_view
169169
tracer.start_span()
170-
tracer.add_label_to_current_span(
171-
label_key=HTTP_METHOD,
172-
label_value=request.method)
173-
tracer.add_label_to_current_span(
174-
label_key=HTTP_URL,
175-
label_value=request.path)
170+
tracer.add_attribute_to_current_span(
171+
attribute_key=HTTP_METHOD,
172+
attribute_value=request.method)
173+
tracer.add_attribute_to_current_span(
174+
attribute_key=HTTP_URL,
175+
attribute_value=request.path)
176176
except Exception: # pragma: NO COVER
177177
log.error('Failed to trace request', exc_info=True)
178178

@@ -200,11 +200,11 @@ def process_response(self, request, response):
200200

201201
try:
202202
tracer = _get_current_request_tracer()
203-
tracer.add_label_to_current_span(
204-
label_key=HTTP_STATUS_CODE,
205-
label_value=str(response.status_code))
203+
tracer.add_attribute_to_current_span(
204+
attribute_key=HTTP_STATUS_CODE,
205+
attribute_value=str(response.status_code))
206206

207-
_set_django_labels(tracer, request)
207+
_set_django_attributes(tracer, request)
208208

209209
tracer.end_span()
210210
tracer.finish()

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import flask
1616
import logging
1717

18-
from opencensus.trace import labels_helper
18+
from opencensus.trace import attributes_helper
1919
from opencensus.trace import execution_context
2020
from opencensus.trace.propagation import google_cloud_format
2121
from opencensus.trace.exporters import print_exporter
@@ -25,9 +25,9 @@
2525

2626
_FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT'
2727

28-
HTTP_METHOD = labels_helper.COMMON_LABELS['HTTP_METHOD']
29-
HTTP_URL = labels_helper.COMMON_LABELS['HTTP_URL']
30-
HTTP_STATUS_CODE = labels_helper.COMMON_LABELS['HTTP_STATUS_CODE']
28+
HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES['HTTP_METHOD']
29+
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
30+
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
3131

3232
log = logging.getLogger(__name__)
3333

@@ -107,8 +107,9 @@ def _before_request(self):
107107
span.name = '[{}]{}'.format(
108108
flask.request.method,
109109
flask.request.url)
110-
tracer.add_label_to_current_span(HTTP_METHOD, flask.request.method)
111-
tracer.add_label_to_current_span(HTTP_URL, flask.request.url)
110+
tracer.add_attribute_to_current_span(
111+
HTTP_METHOD, flask.request.method)
112+
tracer.add_attribute_to_current_span(HTTP_URL, flask.request.url)
112113
except Exception: # pragma: NO COVER
113114
log.error('Failed to trace request', exc_info=True)
114115

@@ -123,7 +124,7 @@ def _after_request(self, response):
123124

124125
try:
125126
tracer = execution_context.get_opencensus_tracer()
126-
tracer.add_label_to_current_span(
127+
tracer.add_attribute_to_current_span(
127128
HTTP_STATUS_CODE,
128129
str(response.status_code))
129130

trace/opencensus/trace/ext/postgresql/trace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def call(query, *args, **kwargs):
5050
_tracer = execution_context.get_opencensus_tracer()
5151
_span = _tracer.start_span()
5252
_span.name = '{}.query'.format(MODULE_NAME)
53-
_tracer.add_label_to_current_span(
53+
_tracer.add_attribute_to_current_span(
5454
'{}/query'.format(MODULE_NAME), query)
55-
_tracer.add_label_to_current_span(
55+
_tracer.add_attribute_to_current_span(
5656
'{}/cursor/method/name'.format(MODULE_NAME),
5757
query_func.__name__)
5858

trace/opencensus/trace/ext/requests/trace.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def call(url, *args, **kwargs):
4747
_span = _tracer.start_span()
4848
_span.name = '[requests]{}'.format(requests_func.__name__)
4949

50-
# Add the requests url to labels
51-
_tracer.add_label_to_current_span('requests/url', url)
50+
# Add the requests url to attributes
51+
_tracer.add_attribute_to_current_span('requests/url', url)
5252

5353
result = requests_func(url, *args, **kwargs)
5454

55-
# Add the status code to labels
56-
_tracer.add_label_to_current_span(
55+
# Add the status code to attributes
56+
_tracer.add_attribute_to_current_span(
5757
'requests/status_code', str(result.status_code))
5858

5959
_tracer.end_span()
@@ -69,13 +69,13 @@ def call(method, url, *args, **kwargs):
6969
_span = _tracer.start_span()
7070
_span.name = '[requests]{}'.format(method)
7171

72-
# Add the requests url to labels
73-
_tracer.add_label_to_current_span('requests/url', url)
72+
# Add the requests url to attributes
73+
_tracer.add_attribute_to_current_span('requests/url', url)
7474

7575
result = request_func(method, url, *args, **kwargs)
7676

77-
# Add the status code to labels
78-
_tracer.add_label_to_current_span(
77+
# Add the status code to attributes
78+
_tracer.add_attribute_to_current_span(
7979
'requests/status_code', str(result.status_code))
8080

8181
_tracer.end_span()

trace/opencensus/trace/ext/sqlalchemy/trace.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ def _before_cursor_execute(conn, cursor, statement, parameters,
6464
_span = _tracer.start_span()
6565
_span.name = '[{}.query]{}'.format(MODULE_NAME, statement)
6666

67-
# Set query statement label
68-
_tracer.add_label_to_current_span(
67+
# Set query statement attribute
68+
_tracer.add_attribute_to_current_span(
6969
'{}/query'.format(MODULE_NAME), statement)
7070

71-
# Set query parameters label
72-
_tracer.add_label_to_current_span(
71+
# Set query parameters attribute
72+
_tracer.add_attribute_to_current_span(
7373
'{}/query/parameters'.format(MODULE_NAME), str(parameters))
7474

75-
# Set query function label
76-
_tracer.add_label_to_current_span(
75+
# Set query function attribute
76+
_tracer.add_attribute_to_current_span(
7777
'{}/cursor/method/name'.format(MODULE_NAME),
7878
query_func)
7979

0 commit comments

Comments
 (0)