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

Commit dc1ac01

Browse files
Liudmila Molkovaliyanhui1228
authored andcommitted
Rename attributes to the default naming schema and use standard http attributes (#272)
1 parent 3b42664 commit dc1ac01

21 files changed

Lines changed: 486 additions & 119 deletions

File tree

opencensus/trace/attributes_helper.py

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

1515
COMMON_ATTRIBUTES = {
1616
'AGENT': 'g.co/agent',
17-
'COMPONENT': '/component',
18-
'ERROR_MESSAGE': '/error/message',
19-
'ERROR_NAME': '/error/name',
20-
'HTTP_CLIENT_CITY': '/http/client_city',
21-
'HTTP_CLIENT_COUNTRY': '/http/client_country',
22-
'HTTP_CLIENT_PROTOCOL': '/http/client_protocol',
23-
'HTTP_CLIENT_REGION': '/http/client_region',
24-
'HTTP_HOST': '/http/host',
25-
'HTTP_METHOD': '/http/method',
26-
'HTTP_REDIRECTED_URL': '/http/redirected_url',
27-
'HTTP_REQUEST_SIZE': '/http/request/size',
28-
'HTTP_RESPONSE_SIZE': '/http/response/size',
29-
'HTTP_STATUS_CODE': '/http/status_code',
30-
'HTTP_URL': '/http/url',
31-
'HTTP_USER_AGENT': '/http/user_agent',
32-
'PID': '/pid',
33-
'STACKTRACE': '/stacktrace',
34-
'TID': '/tid',
17+
'COMPONENT': 'component',
18+
'ERROR_MESSAGE': 'error.message',
19+
'ERROR_NAME': 'error.name',
20+
'HTTP_CLIENT_CITY': 'http.client_city',
21+
'HTTP_CLIENT_COUNTRY': 'http.client_country',
22+
'HTTP_CLIENT_PROTOCOL': 'http.client_protocol',
23+
'HTTP_CLIENT_REGION': 'http.client_region',
24+
'HTTP_HOST': 'http.host',
25+
'HTTP_METHOD': 'http.method',
26+
'HTTP_REDIRECTED_URL': 'http.redirected_url',
27+
'HTTP_REQUEST_SIZE': 'http.request_size',
28+
'HTTP_RESPONSE_SIZE': 'http.response_size',
29+
'HTTP_STATUS_CODE': 'http.status_code',
30+
'HTTP_URL': 'http.url',
31+
'HTTP_USER_AGENT': 'http.user_agent',
32+
'PID': 'pid',
33+
'STACKTRACE': 'stacktrace',
34+
'TID': 'tid',
3535
}
3636

3737

3838
GRPC_ATTRIBUTES = {
39-
'GRPC_HOST_PORT': '/grpc/host_port',
40-
'GRPC_METHOD': '/grpc/method',
39+
'GRPC_HOST_PORT': 'grpc.host_port',
40+
'GRPC_METHOD': 'grpc.method',
4141
}

opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class StackdriverExporter(base.Exporter):
125125
:class:`.SyncTransport`. The other option is
126126
:class:`.BackgroundThreadTransport`.
127127
"""
128+
128129
def __init__(self, client=None, project_id=None,
129130
transport=sync.SyncTransport):
130131
# The client will handle the case when project_id is None
@@ -187,7 +188,7 @@ def translate_to_stackdriver(self, trace):
187188
'startTime': span.get('startTime'),
188189
'endTime': span.get('endTime'),
189190
'spanId': str(span.get('spanId')),
190-
'attributes': span.get('attributes'),
191+
'attributes': self.map_attributes(span.get('attributes')),
191192
'links': span.get('links'),
192193
'status': span.get('status'),
193194
'stackTrace': span.get('stackTrace'),
@@ -204,3 +205,46 @@ def translate_to_stackdriver(self, trace):
204205

205206
spans = {'spans': spans_list}
206207
return spans
208+
209+
def map_attributes(self, attribute_map):
210+
if attribute_map is None:
211+
return attribute_map
212+
213+
for (key, value) in attribute_map.items():
214+
if (key != 'attributeMap'):
215+
continue
216+
for attribute_key in list(value.keys()):
217+
if (attribute_key in ATTRIBUTE_MAPPING):
218+
new_key = ATTRIBUTE_MAPPING.get(attribute_key)
219+
value[new_key] = value.pop(attribute_key)
220+
221+
return attribute_map
222+
223+
224+
ATTRIBUTE_MAPPING = {
225+
'component': '/component',
226+
'error.message': '/error/message',
227+
'error.name': '/error/name',
228+
'http.client_city': '/http/client_city',
229+
'http.client_country': '/http/client_country',
230+
'http.client_protocol': '/http/client_protocol',
231+
'http.client_region': '/http/client_region',
232+
233+
'http.host': '/http/host',
234+
'http.method': '/http/method',
235+
236+
'http.redirected_url': '/http/redirected_url',
237+
'http.request_size': '/http/request/size',
238+
'http.response_size': '/http/response/size',
239+
240+
'http.status_code': '/http/status_code',
241+
'http.url': '/http/url',
242+
'http.user_agent': '/http/user_agent',
243+
244+
'pid': '/pid',
245+
'stacktrace': '/stacktrace',
246+
'tid': '/tid',
247+
248+
'grpc.host_port': '/grpc/host_port',
249+
'grpc.method': '/grpc/method',
250+
}

opencensus/trace/ext/dbapi/trace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def call(query, *args, **kwargs):
5757
_span = _tracer.start_span()
5858
_span.name = 'mysql.query'
5959
_span.span_kind = span_module.SpanKind.CLIENT
60-
_tracer.add_attribute_to_current_span('mysql/query', query)
60+
_tracer.add_attribute_to_current_span('mysql.query', query)
6161
_tracer.add_attribute_to_current_span(
62-
'mysql/cursor/method/name',
62+
'mysql.cursor.method.name',
6363
query_func.__name__)
6464

6565
result = query_func(query, *args, **kwargs)

opencensus/trace/ext/django/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def _set_django_attributes(tracer, request):
8585

8686
# User id is the django autofield for User model as the primary key
8787
if user_id is not None:
88-
tracer.add_attribute_to_current_span('/django/user/id', str(user_id))
88+
tracer.add_attribute_to_current_span('django.user.id', str(user_id))
8989

9090
if user_name is not None:
91-
tracer.add_attribute_to_current_span('/django/user/name', user_name)
91+
tracer.add_attribute_to_current_span('django.user.name', user_name)
9292

9393

9494
class OpencensusMiddleware(MiddlewareMixin):

opencensus/trace/ext/httplib/trace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def wrap_httplib_request(request_func):
5858
"""Wrap the httplib request function to trace. Create a new span and update
5959
and close the span in the response later.
6060
"""
61+
6162
def call(self, method, url, body, headers, *args, **kwargs):
6263
_tracer = execution_context.get_opencensus_tracer()
6364
_span = _tracer.start_span()
@@ -90,6 +91,7 @@ def wrap_httplib_response(response_func):
9091
If there is a corresponding httplib request span, update and close it.
9192
If not, return the response.
9293
"""
94+
9395
def call(self, *args, **kwargs):
9496
_tracer = execution_context.get_opencensus_tracer()
9597
current_span_id = execution_context.get_opencensus_attr(

opencensus/trace/ext/postgresql/trace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def call(query, *args, **kwargs):
5858
_span.name = '{}.query'.format(MODULE_NAME)
5959
_span.span_kind = span_module.SpanKind.CLIENT
6060
_tracer.add_attribute_to_current_span(
61-
'{}/query'.format(MODULE_NAME), query)
61+
'{}.query'.format(MODULE_NAME), query)
6262
_tracer.add_attribute_to_current_span(
63-
'{}/cursor/method/name'.format(MODULE_NAME),
63+
'{}.cursor.method.name'.format(MODULE_NAME),
6464
query_func.__name__)
6565

6666
result = query_func(query, *args, **kwargs)

opencensus/trace/ext/pyramid/pyramid_middleware.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class OpenCensusTweenFactory(object):
4343
For details on pyramid tweens, see
4444
https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#creating-a-tween
4545
"""
46+
4647
def __init__(self, handler, registry):
4748
"""Constructor for the pyramid tween
4849

opencensus/trace/ext/requests/trace.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import requests
1717
import wrapt
1818

19+
from opencensus.trace import attributes_helper
1920
from opencensus.trace import execution_context
2021
from opencensus.trace import span as span_module
2122

@@ -27,6 +28,9 @@
2728
SESSION_WRAP_METHODS = 'request'
2829
SESSION_CLASS_NAME = 'Session'
2930

31+
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
32+
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
33+
3034

3135
def trace_integration(tracer=None):
3236
"""Wrap the requests library to trace it."""
@@ -54,13 +58,13 @@ def call(url, *args, **kwargs):
5458
_span.span_kind = span_module.SpanKind.CLIENT
5559

5660
# Add the requests url to attributes
57-
_tracer.add_attribute_to_current_span('requests/url', url)
61+
_tracer.add_attribute_to_current_span(HTTP_URL, url)
5862

5963
result = requests_func(url, *args, **kwargs)
6064

6165
# Add the status code to attributes
6266
_tracer.add_attribute_to_current_span(
63-
'requests/status_code', str(result.status_code))
67+
HTTP_STATUS_CODE, str(result.status_code))
6468

6569
_tracer.end_span()
6670
return result
@@ -79,13 +83,13 @@ def wrap_session_request(wrapped, instance, args, kwargs):
7983
_span.span_kind = span_module.SpanKind.CLIENT
8084

8185
# Add the requests url to attributes
82-
_tracer.add_attribute_to_current_span('requests/url', url)
86+
_tracer.add_attribute_to_current_span(HTTP_URL, url)
8387

8488
result = wrapped(*args, **kwargs)
8589

8690
# Add the status code to attributes
8791
_tracer.add_attribute_to_current_span(
88-
'requests/status_code', str(result.status_code))
92+
HTTP_STATUS_CODE, str(result.status_code))
8993

9094
_tracer.end_span()
9195
return result

opencensus/trace/ext/sqlalchemy/trace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ def _before_cursor_execute(conn, cursor, statement, parameters,
6868

6969
# Set query statement attribute
7070
_tracer.add_attribute_to_current_span(
71-
'{}/query'.format(MODULE_NAME), statement)
71+
'{}.query'.format(MODULE_NAME), statement)
7272

7373
# Set query parameters attribute
7474
_tracer.add_attribute_to_current_span(
75-
'{}/query/parameters'.format(MODULE_NAME), str(parameters))
75+
'{}.query.parameters'.format(MODULE_NAME), str(parameters))
7676

7777
# Set query function attribute
7878
_tracer.add_attribute_to_current_span(
79-
'{}/cursor/method/name'.format(MODULE_NAME),
79+
'{}.cursor.method.name'.format(MODULE_NAME),
8080
query_func)
8181

8282

tests/system/trace/django/django_system_test.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
HOST_PORT = 'localhost:8000'
3131
BASE_URL = 'http://localhost:8000/'
3232

33-
RETRY_WAIT_PERIOD = 8000 # Wait 8 seconds between each retry
34-
RETRY_MAX_ATTEMPT = 10 # Retry 10 times
33+
RETRY_WAIT_PERIOD = 8000 # Wait 8 seconds between each retry
34+
RETRY_MAX_ATTEMPT = 10 # Retry 10 times
3535

3636

3737
def wait_app_to_start():
@@ -53,7 +53,8 @@ def generate_header():
5353

5454
def run_application():
5555
"""Start running the django application."""
56-
cmd = 'python tests/system/trace/django/manage.py runserver {}'.format(HOST_PORT)
56+
cmd = 'python tests/system/trace/django/manage.py runserver {}'.format(
57+
HOST_PORT)
5758
process = subprocess.Popen(
5859
cmd,
5960
stdout=subprocess.PIPE,
@@ -138,8 +139,9 @@ def test_with_retry(self):
138139
request_succeeded = True
139140

140141
if span.get('name') == '[mysql.query]SELECT 2*3':
141-
self.assertEqual(labels.get('mysql/cursor/method/name'), 'execute')
142-
self.assertEqual(labels.get('mysql/query'), 'SELECT 2*3')
142+
self.assertEqual(labels.get(
143+
'mysql.cursor.method.name'), 'execute')
144+
self.assertEqual(labels.get('mysql.query'), 'SELECT 2*3')
143145

144146
self.assertTrue(request_succeeded)
145147

@@ -170,8 +172,10 @@ def test_with_retry(self):
170172
request_succeeded = True
171173

172174
if span.get('name') == '[postgresql.query]SELECT 2*3':
173-
self.assertEqual(labels.get('postgresql/cursor/method/name'), 'execute')
174-
self.assertEqual(labels.get('postgresql/query'), 'SELECT 2*3')
175+
self.assertEqual(labels.get(
176+
'postgresql.cursor.method.name'), 'execute')
177+
self.assertEqual(labels.get(
178+
'postgresql.query'), 'SELECT 2*3')
175179

176180
self.assertTrue(request_succeeded)
177181

0 commit comments

Comments
 (0)