Skip to content

Commit da16fb1

Browse files
authored
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212)
1 parent 3a78606 commit da16fb1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@
9090

9191

9292
def _set_connection_attributes(span, instance):
93+
if not span.is_recording():
94+
return
9395
for key, value in _get_address_attributes(instance).items():
9496
span.set_attribute(key, value)
9597

9698

9799
def _with_tracer_wrapper(func):
98-
"""Helper for providing tracer for wrapper functions.
99-
"""
100+
"""Helper for providing tracer for wrapper functions."""
100101

101102
def _with_tracer(tracer, cmd):
102103
def wrapper(wrapped, instance, args, kwargs):

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from unittest import mock
1415

1516
import pymemcache
1617
from pymemcache.exceptions import (
@@ -84,6 +85,23 @@ def test_set_success(self):
8485

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

88+
def test_set_not_recording(self):
89+
mock_tracer = mock.Mock()
90+
mock_span = mock.Mock()
91+
mock_span.is_recording.return_value = False
92+
mock_tracer.start_span.return_value = mock_span
93+
mock_tracer.use_span.return_value.__enter__ = mock_span
94+
mock_tracer.use_span.return_value.__exit__ = True
95+
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
96+
tracer.return_value = mock_tracer
97+
client = self.make_client([b"STORED\r\n"])
98+
result = client.set(b"key", b"value", noreply=False)
99+
self.assertTrue(result)
100+
self.assertFalse(mock_span.is_recording())
101+
self.assertTrue(mock_span.is_recording.called)
102+
self.assertFalse(mock_span.set_attribute.called)
103+
self.assertFalse(mock_span.set_status.called)
104+
87105
def test_get_many_none_found(self):
88106
client = self.make_client([b"END\r\n"])
89107
result = client.get_many([b"key1", b"key2"])

0 commit comments

Comments
 (0)