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

Commit 3b42664

Browse files
Liudmila Molkovasongy23
authored andcommitted
Set SpanKind in extensions (#271)
1 parent 7bb6e3f commit 3b42664

18 files changed

Lines changed: 42 additions & 4 deletions

File tree

opencensus/trace/ext/dbapi/trace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616

1717
from opencensus.trace import execution_context
18+
from opencensus.trace import span as span_module
1819

1920
CURSOR_WRAP_METHOD = 'cursor'
2021
QUERY_WRAP_METHODS = ['execute', 'executemany']
@@ -55,6 +56,7 @@ def call(query, *args, **kwargs):
5556
_tracer = execution_context.get_opencensus_tracer()
5657
_span = _tracer.start_span()
5758
_span.name = 'mysql.query'
59+
_span.span_kind = span_module.SpanKind.CLIENT
5860
_tracer.add_attribute_to_current_span('mysql/query', query)
5961
_tracer.add_attribute_to_current_span(
6062
'mysql/cursor/method/name',

opencensus/trace/ext/django/middleware.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from opencensus.trace.ext import utils
1919
from opencensus.trace.ext.django.config import (settings, convert_to_import)
2020
from opencensus.trace import attributes_helper
21-
from opencensus.trace import tracer as tracer_module
2221
from opencensus.trace import execution_context
22+
from opencensus.trace import span as span_module
23+
from opencensus.trace import tracer as tracer_module
2324
from opencensus.trace.samplers import probability
2425

2526
try:
@@ -164,7 +165,8 @@ def process_request(self, request):
164165
propagator=self.propagator)
165166

166167
# Span name is being set at process_view
167-
tracer.start_span()
168+
span = tracer.start_span()
169+
span.span_kind = span_module.SpanKind.SERVER
168170
tracer.add_attribute_to_current_span(
169171
attribute_key=HTTP_METHOD,
170172
attribute_value=request.method)

opencensus/trace/ext/flask/flask_middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from opencensus.trace import attributes_helper
2323
from opencensus.trace import execution_context
24+
from opencensus.trace import span as span_module
2425
from opencensus.trace import stack_trace
2526
from opencensus.trace import status
2627
from opencensus.trace import tracer as tracer_module
@@ -175,7 +176,7 @@ def _before_request(self):
175176
propagator=self.propagator)
176177

177178
span = tracer.start_span()
178-
179+
span.span_kind = span_module.SpanKind.SERVER
179180
# Set the span name as the name of the current module name
180181
span.name = '[{}]{}'.format(
181182
flask.request.method,

opencensus/trace/ext/grpc/client_interceptor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from opencensus.trace import attributes_helper
2222
from opencensus.trace import execution_context
23+
from opencensus.trace import span as span_module
2324
from opencensus.trace import time_event
2425
from opencensus.trace.ext import grpc as oc_grpc
2526
from opencensus.trace.ext.grpc import utils as grpc_utils
@@ -67,6 +68,7 @@ def _start_client_span(self, client_call_details):
6768
name=_get_span_name(client_call_details)
6869
)
6970

71+
span.span_kind = span_module.SpanKind.CLIENT
7072
# Add the component grpc to span attribute
7173
self.tracer.add_attribute_to_current_span(
7274
attribute_key=attributes_helper.COMMON_ATTRIBUTES.get(

opencensus/trace/ext/grpc/server_interceptor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
from opencensus.trace import attributes_helper
2121
from opencensus.trace import execution_context
22+
from opencensus.trace import span as span_module
2223
from opencensus.trace import stack_trace as stack_trace
2324
from opencensus.trace import status
25+
2426
from opencensus.trace import time_event
2527
from opencensus.trace import tracer as tracer_module
2628
from opencensus.trace.ext import grpc as oc_grpc
@@ -108,6 +110,8 @@ def _start_server_span(self, servicer_context):
108110
span = tracer.start_span(
109111
name=_get_span_name(servicer_context)
110112
)
113+
114+
span.span_kind = span_module.SpanKind.SERVER
111115
tracer.add_attribute_to_current_span(
112116
attribute_key=attributes_helper.COMMON_ATTRIBUTES.get(
113117
ATTRIBUTE_COMPONENT),

opencensus/trace/ext/httplib/trace.py

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

1818
from opencensus.trace import attributes_helper
1919
from opencensus.trace import execution_context
20+
from opencensus.trace import span as span_module
2021

2122
PYTHON2 = sys.version_info.major == 2
2223

@@ -60,6 +61,7 @@ def wrap_httplib_request(request_func):
6061
def call(self, method, url, body, headers, *args, **kwargs):
6162
_tracer = execution_context.get_opencensus_tracer()
6263
_span = _tracer.start_span()
64+
_span.span_kind = span_module.SpanKind.CLIENT
6365
_span.name = '[httplib]{}'.format(request_func.__name__)
6466

6567
# Add the request url to attributes

opencensus/trace/ext/postgresql/trace.py

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

1818
from opencensus.trace import execution_context
19+
from opencensus.trace import span as span_module
1920

2021
import psycopg2
2122
from psycopg2 import connect as pg_connect
@@ -55,6 +56,7 @@ def call(query, *args, **kwargs):
5556
# here
5657
_span = _tracer.start_span()
5758
_span.name = '{}.query'.format(MODULE_NAME)
59+
_span.span_kind = span_module.SpanKind.CLIENT
5860
_tracer.add_attribute_to_current_span(
5961
'{}/query'.format(MODULE_NAME), query)
6062
_tracer.add_attribute_to_current_span(

opencensus/trace/ext/pyramid/pyramid_middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
from opencensus.trace import attributes_helper
2121
from opencensus.trace import execution_context
22+
from opencensus.trace import span as span_module
2223
from opencensus.trace import tracer as tracer_module
2324

24-
2525
HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES['HTTP_METHOD']
2626
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
2727
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
@@ -91,6 +91,7 @@ def _before_request(self, request):
9191
request.method,
9292
request.path)
9393

94+
span.span_kind = span_module.SpanKind.SERVER
9495
tracer.add_attribute_to_current_span(
9596
attribute_key=HTTP_METHOD,
9697
attribute_value=request.method)

opencensus/trace/ext/requests/trace.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import wrapt
1818

1919
from opencensus.trace import execution_context
20+
from opencensus.trace import span as span_module
2021

2122
log = logging.getLogger(__name__)
2223

@@ -50,6 +51,7 @@ def call(url, *args, **kwargs):
5051
_tracer = execution_context.get_opencensus_tracer()
5152
_span = _tracer.start_span()
5253
_span.name = '[requests]{}'.format(requests_func.__name__)
54+
_span.span_kind = span_module.SpanKind.CLIENT
5355

5456
# Add the requests url to attributes
5557
_tracer.add_attribute_to_current_span('requests/url', url)
@@ -72,7 +74,9 @@ def wrap_session_request(wrapped, instance, args, kwargs):
7274
url = kwargs.get('url') or args[1]
7375
_tracer = execution_context.get_opencensus_tracer()
7476
_span = _tracer.start_span()
77+
7578
_span.name = '[requests]{}'.format(method)
79+
_span.span_kind = span_module.SpanKind.CLIENT
7680

7781
# Add the requests url to attributes
7882
_tracer.add_attribute_to_current_span('requests/url', url)

opencensus/trace/ext/sqlalchemy/trace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
from opencensus.trace import execution_context
22+
from opencensus.trace import span as span_module
2223

2324
log = logging.getLogger(__name__)
2425

@@ -63,6 +64,7 @@ def _before_cursor_execute(conn, cursor, statement, parameters,
6364
_tracer = execution_context.get_opencensus_tracer()
6465
_span = _tracer.start_span()
6566
_span.name = '{}.query'.format(MODULE_NAME)
67+
_span.span_kind = span_module.SpanKind.CLIENT
6668

6769
# Set query statement attribute
6870
_tracer.add_attribute_to_current_span(

0 commit comments

Comments
 (0)