Skip to content

Commit

Permalink
Replaced Tracer.use_span() with opentelemetry.trace.use_span()
Browse files Browse the repository at this point in the history
  • Loading branch information
owais committed Mar 4, 2021
1 parent aa31e73 commit 77228e6
Show file tree
Hide file tree
Showing 27 changed files with 21 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

### Changed
- Rename `IdsGenerator` to `IdGenerator`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ def test_span_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
db_integration = AiopgIntegration(
mock_tracer, "testcomponent", connection_attributes
)
Expand Down Expand Up @@ -322,7 +320,7 @@ def test_span_failed(self):
self.assertIs(
span.status.status_code, trace_api.status.StatusCode.ERROR
)
self.assertEqual(span.status.description, "Test Exception")
self.assertEqual(span.status.description, "Exception: Test Exception")

def test_executemany(self):
db_integration = AiopgIntegration(self.tracer, "testcomponent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
ec2 = boto.ec2.connect_to_region("us-west-2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
ec2 = self.session.create_client("ec2", region_name="us-west-2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _trace_prerun(self, *args, **kwargs):
operation_name, context=tracectx, kind=trace.SpanKind.CONSUMER
)

activation = self._tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
utils.attach_span(task, task_id, (span, activation))

Expand Down Expand Up @@ -186,7 +186,7 @@ def _trace_before_publish(self, *args, **kwargs):
span.set_attribute(_TASK_NAME_KEY, task.name)
utils.set_attributes_from_context(span, kwargs)

activation = self._tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
utils.attach_span(task, task_id, (span, activation), is_publish=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ def test_span_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
db_integration = dbapi.DatabaseApiIntegration(
mock_tracer, "testcomponent", connection_attributes
)
Expand Down Expand Up @@ -179,7 +177,7 @@ def test_span_failed(self):
self.assertIs(
span.status.status_code, trace_api.status.StatusCode.ERROR
)
self.assertEqual(span.status.description, "Test Exception")
self.assertEqual(span.status.description, "Exception: Test Exception")

def test_executemany(self):
db_integration = dbapi.DatabaseApiIntegration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
collect_request_attributes,
)
from opentelemetry.propagate import extract
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.trace import SpanKind, get_tracer, use_span
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs

try:
Expand Down Expand Up @@ -118,7 +118,7 @@ def process_request(self, request):
for key, value in attributes.items():
span.set_attribute(key, value)

activation = tracer.use_span(span, end_on_exit=True)
activation = use_span(span, end_on_exit=True)
activation.__enter__()

request.META[self._environ_activation_key] = activation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
Client().get("/traced/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from elasticsearch_dsl import Search

import opentelemetry.instrumentation.elasticsearch
from opentelemetry import trace
from opentelemetry.instrumentation.elasticsearch import (
ElasticsearchInstrumentor,
)
Expand Down Expand Up @@ -94,8 +95,6 @@ def test_span_not_recording(self, request_mock):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = mock_span
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
Elasticsearch()
Expand Down Expand Up @@ -174,7 +173,9 @@ def _test_trace_error(self, code, exc):
span = spans[0]
self.assertFalse(span.status.is_ok)
self.assertEqual(span.status.status_code, code)
self.assertEqual(span.status.description, str(exc))
self.assertEqual(
span.status.description, "{}: {}".format(type(exc).__name__, exc)
)

def test_parent(self, request_mock):
request_mock.return_value = (1, {}, {})
Expand All @@ -201,7 +202,7 @@ def test_multithread(self, request_mock):
# 2. Trace something from thread-2, make thread-1 join before finishing.
# 3. Check the spans got different parents, and are in the expected order.
def target1(parent_span):
with self.tracer.use_span(parent_span):
with trace.use_span(parent_span):
es.get(index="test-index", doc_type="tweet", id=1)
ev.set()
ev.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __call__(self, env, start_response):
for key, value in attributes.items():
span.set_attribute(key, value)

activation = self._tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
env[_ENVIRON_SPAN_KEY] = span
env[_ENVIRON_ACTIVATION_KEY] = activation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ def test_traced_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = mock_span
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
self.client().simulate_get(path="/hello?q=abc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _before_request():
for key, value in attributes.items():
span.set_attribute(key, value)

activation = tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
flask_request_environ[_ENVIRON_SPAN_KEY] = span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = mock_span
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
self.client.get("/hello/123")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def test_render_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = mock_span
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
jinja2.environment.Template("Hello {{name}}!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ def test_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
Psycopg2Instrumentor().instrument()
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def test_set_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
client = self.make_client([b"STORED\r\n"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def test_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
mock_event = MockEvent({})
command_tracer = CommandTracer(mock_tracer)
command_tracer.started(event=mock_event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _before_traversal(event):
for key, value in attributes.items():
span.set_attribute(key, value)

activation = tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
request_environ[_ENVIRON_ACTIVATION_KEY] = activation
request_environ[_ENVIRON_SPAN_KEY] = span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with patch("opentelemetry.trace.get_tracer"):
self.client.get("/hello/123")
span_list = self.memory_exporter.get_finished_spans()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def test_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
with mock.patch.object(redis_client, "connection"):
tracer.return_value = mock_tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _before_cur_exec(self, conn, cursor, statement, *args):
self._operation_name(db_name, statement),
kind=trace.SpanKind.CLIENT,
)
with self.tracer.use_span(self.current_span, end_on_exit=False):
with trace.use_span(self.current_span, end_on_exit=False):
if self.current_span.is_recording():
self.current_span.set_attribute(_STMT, statement)
self.current_span.set_attribute("db.system", self.vendor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def test_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
engine = create_engine("sqlite:///:memory:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _start_span(tracer, handler, start_time) -> _TraceContext:
for key, value in attributes.items():
span.set_attribute(key, value)

activation = tracer.use_span(span, end_on_exit=True)
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__()
ctx = _TraceContext(activation, span, token)
setattr(handler, _HANDLER_CONTEXT_KEY, ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def fetch_async(tracer, func, _, args, kwargs):
for key, value in attributes.items():
span.set_attribute(key, value)

with tracer.use_span(span):
with trace.use_span(span):
inject(type(request.headers).__setitem__, request.headers)
future = func(*args, **kwargs)
future.add_done_callback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ def test_not_recording(self):
mock_span = Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
self.fetch("/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __call__(self, environ, start_response):
)

try:
with self.tracer.use_span(span):
with trace.use_span(span):
start_response = self._create_start_response(
span, start_response
)
Expand All @@ -239,7 +239,7 @@ def __call__(self, environ, start_response):
# behavior as little as possible).
def _end_span_after_iterating(iterable, span, tracer, token):
try:
with tracer.use_span(span):
with trace.use_span(span):
for yielded in iterable:
yield yielded
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ def test_wsgi_not_recording(self):
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = mock_span
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
app = otel_wsgi.OpenTelemetryMiddleware(simple_wsgi)
Expand Down

0 comments on commit 77228e6

Please sign in to comment.