Skip to content

Commit

Permalink
Merge pull request #1194 from newrelic/capture-host-port-info-pymemcache
Browse files Browse the repository at this point in the history
Capture memcache host & port
  • Loading branch information
hmstepanek authored Sep 17, 2024
2 parents 2d6525e + c201bf1 commit 8094552
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
20 changes: 19 additions & 1 deletion newrelic/hooks/datastore_aiomcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@
)


def capture_host(self, *args, **kwargs):
if hasattr(self, "_pool") and hasattr(self._pool, "_host"):
return self._pool._host


def capture_port(self, *args, **kwargs):
if hasattr(self, "_pool") and hasattr(self._pool, "_port"):
return self._pool._port


def instrument_aiomcache_client(module):
for name in _memcache_client_methods:
if hasattr(module.Client, name):
wrap_datastore_trace(module, "Client.%s" % name, product="Memcached", target=None, operation=name)
wrap_datastore_trace(
module,
"Client.%s" % name,
product="Memcached",
target=None,
operation=name,
host=capture_host,
port_path_or_id=capture_port,
)
24 changes: 23 additions & 1 deletion newrelic/hooks/datastore_bmemcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,29 @@
)


def capture_host(self, *args, **kwargs):
if hasattr(self, "servers"):
for s in self.servers:
if hasattr(s, "host"):
return s.host


def capture_port(self, *args, **kwargs):
if hasattr(self, "servers"):
for s in self.servers:
if hasattr(s, "port"):
return s.port


def instrument_bmemcached_client(module):
for name in _memcache_client_methods:
if hasattr(module.Client, name):
wrap_datastore_trace(module, "Client.%s" % name, product="Memcached", target=None, operation=name)
wrap_datastore_trace(
module,
"Client.%s" % name,
product="Memcached",
target=None,
operation=name,
host=capture_host,
port_path_or_id=capture_port,
)
20 changes: 19 additions & 1 deletion newrelic/hooks/datastore_pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@
)


def capture_host(self, *args, **kwargs):
if hasattr(self, "server") and self.server and len(self.server) >= 2:
return self.server[0]


def capture_port(self, *args, **kwargs):
if hasattr(self, "server") and self.server and len(self.server) >= 2:
return self.server[1]


def instrument_pymemcache_client(module):
for name in _memcache_client_methods:
if hasattr(module.Client, name):
wrap_datastore_trace(module, "Client.%s" % name, product="Memcached", target=None, operation=name)
wrap_datastore_trace(
module,
"Client.%s" % name,
product="Memcached",
target=None,
operation=name,
host=capture_host,
port_path_or_id=capture_port,
)
7 changes: 4 additions & 3 deletions tests/datastore_aiomcache/test_aiomcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

import aiomcache
from testing_support.db_settings import memcached_settings
from testing_support.fixture.event_loop import event_loop as loop
from testing_support.validators.validate_transaction_metrics import (
validate_transaction_metrics,
)

from newrelic.api.background_task import background_task
from newrelic.api.transaction import set_background_task

from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from testing_support.fixture.event_loop import event_loop as loop

DB_SETTINGS = memcached_settings()[0]

MEMCACHED_HOST = DB_SETTINGS["host"]
MEMCACHED_PORT = DB_SETTINGS["port"]
MEMCACHED_NAMESPACE = str(os.getpid())
INSTANCE_METRIC_NAME = "Datastore/instance/Memcached/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT)

_test_bt_set_get_delete_scoped_metrics = [
("Datastore/operation/Memcached/set", 1),
Expand All @@ -43,6 +42,7 @@
("Datastore/allOther", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allOther", 3),
(INSTANCE_METRIC_NAME, 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
Expand Down Expand Up @@ -81,6 +81,7 @@ def test_bt_set_get_delete(loop):
("Datastore/allWeb", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allWeb", 3),
(INSTANCE_METRIC_NAME, 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
Expand Down
5 changes: 3 additions & 2 deletions tests/datastore_bmemcached/test_memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
from newrelic.api.background_task import background_task
from newrelic.api.transaction import set_background_task

from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics

DB_SETTINGS = memcached_settings()[0]

MEMCACHED_HOST = DB_SETTINGS["host"]
MEMCACHED_PORT = DB_SETTINGS["port"]
MEMCACHED_NAMESPACE = str(os.getpid())
MEMCACHED_ADDR = "%s:%s" % (MEMCACHED_HOST, MEMCACHED_PORT)
INSTANCE_METRIC_NAME = "Datastore/instance/Memcached/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT)

_test_bt_set_get_delete_scoped_metrics = [
("Datastore/operation/Memcached/set", 1),
Expand All @@ -43,6 +42,7 @@
("Datastore/allOther", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allOther", 3),
(INSTANCE_METRIC_NAME, 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
Expand Down Expand Up @@ -80,6 +80,7 @@ def test_bt_set_get_delete():
("Datastore/allWeb", 3),
("Datastore/Memcached/all", 3),
("Datastore/Memcached/allWeb", 3),
(INSTANCE_METRIC_NAME, 3),
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
Expand Down
4 changes: 2 additions & 2 deletions tests/datastore_pymemcache/test_memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import pymemcache.client
from testing_support.db_settings import memcached_settings
from testing_support.validators.validate_transaction_metrics import (
Expand Down Expand Up @@ -45,6 +43,7 @@
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
("Datastore/instance/Memcached/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT), 3),
]


Expand Down Expand Up @@ -82,6 +81,7 @@ def test_bt_set_get_delete():
("Datastore/operation/Memcached/set", 1),
("Datastore/operation/Memcached/get", 1),
("Datastore/operation/Memcached/delete", 1),
("Datastore/instance/Memcached/%s/%s" % (MEMCACHED_HOST, MEMCACHED_PORT), 3),
]


Expand Down

0 comments on commit 8094552

Please sign in to comment.