---
title: memcache_trace (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/memcachetrace-python-agent-api
---

## Syntax

```py
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](https://docs.newrelic.com/docs/accounts-partnerships/education/getting-started-new-relic/glossary#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 [#alternate]

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 [#path-based]

`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](https://docs.newrelic.com/docs/python-agent-api-different-call-forms).

## Parameters

### Parameters for decorator [#main-parameters]

```py
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

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

Parameters for the wrapper include all [parameters](#main-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 [#path-wrap-parameters]

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

Parameters include all [parameters](#main-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 [#trace_example]

An example of using the `memcache_trace` decorator:

```py
import newrelic.agent

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

### Wrapper example [#trace-wrap-example]

An example of using the `MemcacheTraceWrapper`:

```py
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.

```py
import newrelic.agent

def get(id):
    ...

get = newrelic.agent.MemcacheTraceWrapper(get, command=lambda id: f"get:{id}")
```
