• /
  • EnglishEspañolFrançais日本語한국어Português
  • Log inStart now

memcache_trace (Python agent API)

|View as Markdown

Syntax

newrelic.agent.memcache_trace(command, async_wrapper=None)

Used to instrument memcache command functions for functions that aren't instrumented by default.

Description

memcache_trace is a decorator for adding to memcache command functions. Adding this decorator lets you collect additional transaction information (including transaction trace information).

The memcache_trace decorator can be used on functions with agent version 2.102.0.85 or higher. Timing of these objects begins when consumption starts, and ends when the object is exhausted or goes out of scope.

You can use the decorator in conjunction with existing decorators, including those for static and class methods. When nesting multiple decorators, have the memcache_trace decorator as the outermost decorator.

Alternate call forms

For setups where you cannot use the decorator, these alternate call forms are available:

The wrapper

If you know in advance where the specific functions you want to trace are, you could use the memcache_trace decorator. However, if you don't know all the functions that need to be traced (for example, if they're being looked up dynamically as part of a routing system), then you must use the MemcacheTraceWrapper to wrap the function at the time of registration or at the time of calling.

Path-based wrapping

wrap_memcache_trace is used for wrapping functions outside of the code they're declared in. For example: you might use this to instrument library code that you don't want to modify.

For more about the differences between these call formats, see Different call formats.

Parameters

Parameters for decorator

newrelic.agent.memcache_trace(command, async_wrapper=None)

This call includes these parameters:

Parameter

Description

command

string or function

Required. The memcache command. Could be a function that accepts the same parameters as the function being wrapped.

Wrapper parameters

newrelic.agent.MemcacheTraceWrapper(wrapped, command, async_wrapper=None)

Parameters for the wrapper include all parameters for memcache_trace and a wrapped parameter:

Parameter

Description

wrapped

function

command

string or function

Required. The memcache command. Could be a function that accepts the same parameters as the function being wrapped.

Path-based wrapping parameters

newrelic.agent.wrap_memcache_trace(module, object_path, command, async_wrapper=None)

Parameters include all parameters for memcache_trace and these parameters:

Parameter

Description

module

object

Required. The module containing the function to be instrumented.

object_path

string

The path to the location of the function.

Examples

memcache_trace example

An example of using the memcache_trace decorator:

import newrelic.agent
@newrelic.agent.memcache_trace("get")
def get(id):
...

Wrapper example

An example of using the MemcacheTraceWrapper:

import newrelic.agent
def get(id):
...
get = newrelic.agent.MemcacheTraceWrapper(get, command="get")

If you want to dynamically return the value of the command, you can supply the command as a function when the MemcacheTraceWrapper object is created.

import newrelic.agent
def get(id):
...
get = newrelic.agent.MemcacheTraceWrapper(get, command=lambda id: f"get:{id}")
Copyright © 2026 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.