Skip to content

Commit

Permalink
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache,…
Browse files Browse the repository at this point in the history
… pymongo, redis, sqlalchemy instrumentations (#1212)
  • Loading branch information
lzchen committed Oct 8, 2020
1 parent 3a78606 commit da16fb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@


def _set_connection_attributes(span, instance):
if not span.is_recording():
return
for key, value in _get_address_attributes(instance).items():
span.set_attribute(key, value)


def _with_tracer_wrapper(func):
"""Helper for providing tracer for wrapper functions.
"""
"""Helper for providing tracer for wrapper functions."""

def _with_tracer(tracer, cmd):
def wrapper(wrapped, instance, args, kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock

import pymemcache
from pymemcache.exceptions import (
Expand Down Expand Up @@ -84,6 +85,23 @@ def test_set_success(self):

self.check_spans(spans, 1, ["set key"])

def test_set_not_recording(self):
mock_tracer = mock.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__ = True
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
client = self.make_client([b"STORED\r\n"])
result = client.set(b"key", b"value", noreply=False)
self.assertTrue(result)
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

def test_get_many_none_found(self):
client = self.make_client([b"END\r\n"])
result = client.get_many([b"key1", b"key2"])
Expand Down

0 comments on commit da16fb1

Please sign in to comment.