Skip to content

Commit a7d8236

Browse files
committed
Replaced Tracer.use_span() with opentelemetry.trace.use_span()
1 parent aa31e73 commit a7d8236

File tree

32 files changed

+55
-90
lines changed

32 files changed

+55
-90
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- 'release/*'
77
pull_request:
88
env:
9-
CORE_REPO_SHA: 834462941942229e308117220e1fa1fad7cb06e3
9+
CORE_REPO_SHA: ddff32ac77e2e22b3193b71f1e71a590a99d1eda
1010

1111
jobs:
1212
build:

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python-contrib/compare/v0.18b0...HEAD)
8+
- Updated instrumentations to use `opentelemetry.trace.use_span` instead of `Tracer.use_span()`
9+
([#364](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/364))
810

911
### Changed
1012
- Rename `IdsGenerator` to `IdGenerator`

instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
DatabaseApiIntegration,
99
)
1010
from opentelemetry.trace import SpanKind
11-
from opentelemetry.trace.status import Status, StatusCode
1211

1312

1413
# pylint: disable=abstract-method
@@ -117,13 +116,7 @@ async def traced_execution(
117116
name, kind=SpanKind.CLIENT
118117
) as span:
119118
self._populate_span(span, cursor, *args)
120-
try:
121-
result = await query_method(*args, **kwargs)
122-
return result
123-
except Exception as ex: # pylint: disable=broad-except
124-
if span.is_recording():
125-
span.set_status(Status(StatusCode.ERROR, str(ex)))
126-
raise ex
119+
return await query_method(*args, **kwargs)
127120

128121

129122
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):

instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ def test_span_not_recording(self):
289289
mock_span = mock.Mock()
290290
mock_span.is_recording.return_value = False
291291
mock_tracer.start_span.return_value = mock_span
292-
mock_tracer.use_span.return_value.__enter__ = mock_span
293-
mock_tracer.use_span.return_value.__exit__ = True
294292
db_integration = AiopgIntegration(
295293
mock_tracer, "testcomponent", connection_attributes
296294
)
@@ -322,7 +320,7 @@ def test_span_failed(self):
322320
self.assertIs(
323321
span.status.status_code, trace_api.status.StatusCode.ERROR
324322
)
325-
self.assertEqual(span.status.description, "Test Exception")
323+
self.assertEqual(span.status.description, "Exception: Test Exception")
326324

327325
def test_executemany(self):
328326
db_integration = AiopgIntegration(self.tracer, "testcomponent")

instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ def test_not_recording(self):
8282
mock_span = Mock()
8383
mock_span.is_recording.return_value = False
8484
mock_tracer.start_span.return_value = mock_span
85-
mock_tracer.use_span.return_value.__enter__ = mock_span
86-
mock_tracer.use_span.return_value.__exit__ = True
8785
with patch("opentelemetry.trace.get_tracer") as tracer:
8886
tracer.return_value = mock_tracer
8987
ec2 = boto.ec2.connect_to_region("us-west-2")

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def test_not_recording(self):
8181
mock_span = Mock()
8282
mock_span.is_recording.return_value = False
8383
mock_tracer.start_span.return_value = mock_span
84-
mock_tracer.use_span.return_value.__enter__ = mock_span
85-
mock_tracer.use_span.return_value.__exit__ = True
8684
with patch("opentelemetry.trace.get_tracer") as tracer:
8785
tracer.return_value = mock_tracer
8886
ec2 = self.session.create_client("ec2", region_name="us-west-2")

instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _trace_prerun(self, *args, **kwargs):
137137
operation_name, context=tracectx, kind=trace.SpanKind.CONSUMER
138138
)
139139

140-
activation = self._tracer.use_span(span, end_on_exit=True)
140+
activation = trace.use_span(span, end_on_exit=True)
141141
activation.__enter__()
142142
utils.attach_span(task, task_id, (span, activation))
143143

@@ -186,8 +186,9 @@ def _trace_before_publish(self, *args, **kwargs):
186186
span.set_attribute(_TASK_NAME_KEY, task.name)
187187
utils.set_attributes_from_context(span, kwargs)
188188

189-
activation = self._tracer.use_span(span, end_on_exit=True)
190-
activation.__enter__()
189+
activation = trace.use_span(span, end_on_exit=True)
190+
activation.__enter__() # pylint: disable=E1101
191+
191192
utils.attach_span(task, task_id, (span, activation), is_publish=True)
192193

193194
headers = kwargs.get("headers")
@@ -208,7 +209,7 @@ def _trace_after_publish(*args, **kwargs):
208209
logger.warning("no existing span found for task_id=%s", task_id)
209210
return
210211

211-
activation.__exit__(None, None, None)
212+
activation.__exit__(None, None, None) # pylint: disable=E1101
212213
utils.detach_span(task, task_id, is_publish=True)
213214

214215
@staticmethod

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,7 @@ def traced_execution(
371371
name, kind=SpanKind.CLIENT
372372
) as span:
373373
self._populate_span(span, cursor, *args)
374-
try:
375-
result = query_method(*args, **kwargs)
376-
return result
377-
except Exception as ex: # pylint: disable=broad-except
378-
if span.is_recording():
379-
span.set_status(Status(StatusCode.ERROR, str(ex)))
380-
raise ex
374+
return query_method(*args, **kwargs)
381375

382376

383377
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ def test_span_not_recording(self):
146146
mock_span = mock.Mock()
147147
mock_span.is_recording.return_value = False
148148
mock_tracer.start_span.return_value = mock_span
149-
mock_tracer.use_span.return_value.__enter__ = mock_span
150-
mock_tracer.use_span.return_value.__exit__ = True
151149
db_integration = dbapi.DatabaseApiIntegration(
152150
mock_tracer, "testcomponent", connection_attributes
153151
)
@@ -179,7 +177,7 @@ def test_span_failed(self):
179177
self.assertIs(
180178
span.status.status_code, trace_api.status.StatusCode.ERROR
181179
)
182-
self.assertEqual(span.status.description, "Test Exception")
180+
self.assertEqual(span.status.description, "Exception: Test Exception")
183181

184182
def test_executemany(self):
185183
db_integration = dbapi.DatabaseApiIntegration(

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
collect_request_attributes,
2525
)
2626
from opentelemetry.propagate import extract
27-
from opentelemetry.trace import SpanKind, get_tracer
27+
from opentelemetry.trace import SpanKind, get_tracer, use_span
2828
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
2929

3030
try:
@@ -118,8 +118,8 @@ def process_request(self, request):
118118
for key, value in attributes.items():
119119
span.set_attribute(key, value)
120120

121-
activation = tracer.use_span(span, end_on_exit=True)
122-
activation.__enter__()
121+
activation = use_span(span, end_on_exit=True)
122+
activation.__enter__() # pylint: disable=E1101
123123

124124
request.META[self._environ_activation_key] = activation
125125
request.META[self._environ_span_key] = span

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ def test_not_recording(self):
146146
mock_span = Mock()
147147
mock_span.is_recording.return_value = False
148148
mock_tracer.start_span.return_value = mock_span
149-
mock_tracer.use_span.return_value.__enter__ = mock_span
150-
mock_tracer.use_span.return_value.__exit__ = True
151149
with patch("opentelemetry.trace.get_tracer") as tracer:
152150
tracer.return_value = mock_tracer
153151
Client().get("/traced/")

instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,15 @@ def wrapper(wrapped, _, args, kwargs):
136136
attributes["elasticsearch.params"] = str(params)
137137
for key, value in attributes.items():
138138
span.set_attribute(key, value)
139-
try:
140-
rv = wrapped(*args, **kwargs)
141-
if isinstance(rv, dict) and span.is_recording():
142-
for member in _ATTRIBUTES_FROM_RESULT:
143-
if member in rv:
144-
span.set_attribute(
145-
"elasticsearch.{0}".format(member),
146-
str(rv[member]),
147-
)
148-
return rv
149-
except Exception as ex: # pylint: disable=broad-except
150-
if span.is_recording():
151-
span.set_status(Status(StatusCode.ERROR, str(ex)))
152-
raise ex
139+
140+
rv = wrapped(*args, **kwargs)
141+
if isinstance(rv, dict) and span.is_recording():
142+
for member in _ATTRIBUTES_FROM_RESULT:
143+
if member in rv:
144+
span.set_attribute(
145+
"elasticsearch.{0}".format(member),
146+
str(rv[member]),
147+
)
148+
return rv
153149

154150
return wrapper

instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from elasticsearch_dsl import Search
2424

2525
import opentelemetry.instrumentation.elasticsearch
26+
from opentelemetry import trace
2627
from opentelemetry.instrumentation.elasticsearch import (
2728
ElasticsearchInstrumentor,
2829
)
@@ -94,8 +95,6 @@ def test_span_not_recording(self, request_mock):
9495
mock_span = mock.Mock()
9596
mock_span.is_recording.return_value = False
9697
mock_tracer.start_span.return_value = mock_span
97-
mock_tracer.use_span.return_value.__enter__ = mock_span
98-
mock_tracer.use_span.return_value.__exit__ = mock_span
9998
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
10099
tracer.return_value = mock_tracer
101100
Elasticsearch()
@@ -174,7 +173,9 @@ def _test_trace_error(self, code, exc):
174173
span = spans[0]
175174
self.assertFalse(span.status.is_ok)
176175
self.assertEqual(span.status.status_code, code)
177-
self.assertEqual(span.status.description, str(exc))
176+
self.assertEqual(
177+
span.status.description, "{}: {}".format(type(exc).__name__, exc)
178+
)
178179

179180
def test_parent(self, request_mock):
180181
request_mock.return_value = (1, {}, {})
@@ -201,7 +202,7 @@ def test_multithread(self, request_mock):
201202
# 2. Trace something from thread-2, make thread-1 join before finishing.
202203
# 3. Check the spans got different parents, and are in the expected order.
203204
def target1(parent_span):
204-
with self.tracer.use_span(parent_span):
205+
with trace.use_span(parent_span):
205206
es.get(index="test-index", doc_type="tweet", id=1)
206207
ev.set()
207208
ev.wait()

instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def __init__(self, *args, **kwargs):
104104
super().__init__(*args, **kwargs)
105105

106106
def __call__(self, env, start_response):
107+
# pylint: disable=E1101
107108
if _excluded_urls.url_disabled(env.get("PATH_INFO", "/")):
108109
return super().__call__(env, start_response)
109110

@@ -120,7 +121,7 @@ def __call__(self, env, start_response):
120121
for key, value in attributes.items():
121122
span.set_attribute(key, value)
122123

123-
activation = self._tracer.use_span(span, end_on_exit=True)
124+
activation = trace.use_span(span, end_on_exit=True)
124125
activation.__enter__()
125126
env[_ENVIRON_SPAN_KEY] = span
126127
env[_ENVIRON_ACTIVATION_KEY] = activation

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ def test_traced_not_recording(self):
199199
mock_span = Mock()
200200
mock_span.is_recording.return_value = False
201201
mock_tracer.start_span.return_value = mock_span
202-
mock_tracer.use_span.return_value.__enter__ = mock_span
203-
mock_tracer.use_span.return_value.__exit__ = mock_span
204202
with patch("opentelemetry.trace.get_tracer") as tracer:
205203
tracer.return_value = mock_tracer
206204
self.client().simulate_get(path="/hello?q=abc")

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def _before_request():
138138
for key, value in attributes.items():
139139
span.set_attribute(key, value)
140140

141-
activation = tracer.use_span(span, end_on_exit=True)
142-
activation.__enter__()
141+
activation = trace.use_span(span, end_on_exit=True)
142+
activation.__enter__() # pylint: disable=E1101
143143
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
144144
flask_request_environ[_ENVIRON_SPAN_KEY] = span
145145
flask_request_environ[_ENVIRON_TOKEN] = token
@@ -148,6 +148,7 @@ def _before_request():
148148

149149

150150
def _teardown_request(exc):
151+
# pylint: disable=E1101
151152
if _excluded_urls.url_disabled(flask.request.url):
152153
return
153154

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ def test_not_recording(self):
125125
mock_span = Mock()
126126
mock_span.is_recording.return_value = False
127127
mock_tracer.start_span.return_value = mock_span
128-
mock_tracer.use_span.return_value.__enter__ = mock_span
129-
mock_tracer.use_span.return_value.__exit__ = mock_span
130128
with patch("opentelemetry.trace.get_tracer") as tracer:
131129
tracer.return_value = mock_tracer
132130
self.client.get("/hello/123")

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_server.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ def _set_remote_context(self, servicer_context):
191191
else:
192192
yield
193193

194-
def _start_span(self, handler_call_details, context):
194+
def _start_span(
195+
self, handler_call_details, context, set_status_on_exception=False
196+
):
195197

196198
# standard attributes
197199
attributes = {
@@ -234,6 +236,7 @@ def _start_span(self, handler_call_details, context):
234236
name=handler_call_details.method,
235237
kind=trace.SpanKind.SERVER,
236238
attributes=attributes,
239+
set_status_on_exception=set_status_on_exception,
237240
)
238241

239242
def intercept_service(self, continuation, handler_call_details):
@@ -251,7 +254,9 @@ def telemetry_interceptor(request_or_iterator, context):
251254

252255
with self._set_remote_context(context):
253256
with self._start_span(
254-
handler_call_details, context
257+
handler_call_details,
258+
context,
259+
set_status_on_exception=False,
255260
) as span:
256261
# wrap the context
257262
context = _OpenTelemetryServicerContext(context, span)
@@ -283,7 +288,9 @@ def _intercept_server_stream(
283288
):
284289

285290
with self._set_remote_context(context):
286-
with self._start_span(handler_call_details, context) as span:
291+
with self._start_span(
292+
handler_call_details, context, set_status_on_exception=False
293+
) as span:
287294
context = _OpenTelemetryServicerContext(context, span)
288295

289296
try:

instrumentation/opentelemetry-instrumentation-jinja2/tests/test_jinja2.py

-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ def test_render_not_recording(self):
5959
mock_span = mock.Mock()
6060
mock_span.is_recording.return_value = False
6161
mock_tracer.start_span.return_value = mock_span
62-
mock_tracer.use_span.return_value.__enter__ = mock_span
63-
mock_tracer.use_span.return_value.__exit__ = mock_span
6462
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
6563
tracer.return_value = mock_tracer
6664
jinja2.environment.Template("Hello {{name}}!")

instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py

-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ def test_not_recording(self):
121121
mock_span = mock.Mock()
122122
mock_span.is_recording.return_value = False
123123
mock_tracer.start_span.return_value = mock_span
124-
mock_tracer.use_span.return_value.__enter__ = mock_span
125-
mock_tracer.use_span.return_value.__exit__ = True
126124
Psycopg2Instrumentor().instrument()
127125
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
128126
tracer.return_value = mock_tracer

instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def test_set_not_recording(self):
8484
mock_span = mock.Mock()
8585
mock_span.is_recording.return_value = False
8686
mock_tracer.start_span.return_value = mock_span
87-
mock_tracer.use_span.return_value.__enter__ = mock_span
88-
mock_tracer.use_span.return_value.__exit__ = True
8987
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
9088
tracer.return_value = mock_tracer
9189
client = self.make_client([b"STORED\r\n"])

instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py

-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ def test_not_recording(self):
7676
mock_span = mock.Mock()
7777
mock_span.is_recording.return_value = False
7878
mock_tracer.start_span.return_value = mock_span
79-
mock_tracer.use_span.return_value.__enter__ = mock_span
80-
mock_tracer.use_span.return_value.__exit__ = True
8179
mock_event = MockEvent({})
8280
command_tracer = CommandTracer(mock_tracer)
8381
command_tracer.started(event=mock_event)

instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def _before_traversal(event):
8383
for key, value in attributes.items():
8484
span.set_attribute(key, value)
8585

86-
activation = tracer.use_span(span, end_on_exit=True)
87-
activation.__enter__()
86+
activation = trace.use_span(span, end_on_exit=True)
87+
activation.__enter__() # pylint: disable=E1101
8888
request_environ[_ENVIRON_ACTIVATION_KEY] = activation
8989
request_environ[_ENVIRON_SPAN_KEY] = span
9090
request_environ[_ENVIRON_TOKEN] = token
@@ -105,6 +105,7 @@ def disabled_tween(request):
105105

106106
# make a request tracing function
107107
def trace_tween(request):
108+
# pylint: disable=E1101
108109
if _excluded_urls.url_disabled(request.url):
109110
request.environ[_ENVIRON_ENABLED_KEY] = False
110111
# short-circuit when we don't want to trace anything

instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py

-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ def test_not_recording(self):
104104
mock_span = Mock()
105105
mock_span.is_recording.return_value = False
106106
mock_tracer.start_span.return_value = mock_span
107-
mock_tracer.use_span.return_value.__enter__ = mock_span
108-
mock_tracer.use_span.return_value.__exit__ = True
109107
with patch("opentelemetry.trace.get_tracer"):
110108
self.client.get("/hello/123")
111109
span_list = self.memory_exporter.get_finished_spans()

0 commit comments

Comments
 (0)