Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212)

This commit is contained in:
Leighton Chen 2020-10-08 15:25:20 -04:00 committed by GitHub
parent 3a78606956
commit da16fb1fc3
2 changed files with 21 additions and 2 deletions

View File

@ -90,13 +90,14 @@ COMMANDS = [
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):

View File

@ -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 (
@ -84,6 +85,23 @@ class PymemcacheClientTestCase(
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"])