# Increase setCustomContext context character limit

**URL:** <https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818>\
**Category:** APM\
**Created:** [March 25, 2019, 7:00pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818 "2019-03-25T19:00:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![isurfbecause](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@isurfbecause](https://discuss.elastic.co/u/isurfbecause)\
**Post date:** [March 25, 2019, 7:00pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/1 "2019-03-25T19:00:04Z")

</div>

**Kibana version** :  
6.5.4

**Elasticsearch version** :  
6.5.4

**APM Server version** :  
6.5.4

**APM Agent language and version** :  
elastic-apm-node 2.5.1

**Browser version** :  
Google Chrome  
Version 73.0.3683.86 (Official Build) (64-bit)

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:  
Docker community images

**Fresh install or upgraded from other version?**  
Fresh install

**Is there anything special in your setup?**  
For testing purposes I am running my Node.js app locally that is sending metrics to the apm server behind an AWS ALB.

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:  
I am using express ^3.21.2 and trying to to send back the response body via the `setCustomContext` method. However the data seems to be truncated around character 578. I assume there is a max number of characters that can be sent. Is there a way to override the default limit. In the screenshot below take a look in `responseData` the characters are cut off at the key `"fil` (key should be `filterField`)

![2019-03-25%2011-46-09](https://us1.discourse-cdn.com/elastic/original/3X/f/3/f3139fd2f429e33587369f06034537533c3521c1.png)

Here is the code I am using to send back the response.

```auto
app.use (function (req, res, next) {
  const oldWrite = res.write;
  const oldEnd = res.end;

  const chunks = [];

  res.write = (...restArgs) => {
    chunks.push(Buffer.from(restArgs[0]));
    oldWrite.apply(res, restArgs);
  };

  res.end = (...restArgs) => {
    if (restArgs[0]) {
      chunks.push(Buffer.from(restArgs[0]));
    }
    const body = Buffer.concat(chunks).toString('utf8');

    let customData = {
      time: new Date().toUTCString(),
      fromIP: req.headers['x-forwarded-for'] || 
      req.connection.remoteAddress,
      method: req.method,
      originalUri: req.originalUrl,
      uri: req.url,
      requestData: req.body,
      responseData: body,
      referer: req.headers.referer || '',
      ua: req.headers['user-agent']
    };

    apm.setCustomContext({
      responseBody: customData
    });
    
    console.eyes(customData);

    // console.log(body);
    oldEnd.apply(res, restArgs);
  };

  next();
});

```

**Steps to reproduce** :  
Use the JS code above in your express app and send back large response (over 578 characters) to the `setCustomContext` method.

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [March 25, 2019, 7:52pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/2 "2019-03-25T19:52:24Z")

</div>

All strings under `context.custom` are truncated at 1024 bytes before being sent to the APM Server. (the only reason I can think of why it would truncate at a lower byte-size is if the string contains unicode characters).

For just capturing the HTTP body, you should instead use the [`captureBody`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#capture-body) config option. I think this is limited to a slightly higher 2048 bytes max length + you don't need to manually add the body as custom data.

There's currently no way to override the max limit of 1024 bytes for `context.custom` strings. To do that you'd have to add the following line to the [sanitize config](https://github.com/elastic/apm-agent-nodejs/blob/7aeb74ae1743b7e7cb86d750f3a0263986898d63/lib/config.js#L194-L196):

```javascript
        truncateStringsAt: <new-limit>,

```

This however would influence many of the other strings that are sent to the APM Server and not only under `custom.context`.

The reason why we limit these is that it would otherwise fill up the index in Elasticsearch and would require more bandwidth between the agent and APM Server. Is this is a one-off need, or is this a use-case that you need normally?

---

<div class="post-metadata">

**Author:** ![isurfbecause](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@isurfbecause](https://discuss.elastic.co/u/isurfbecause)\
**Post date:** [March 26, 2019, 11:39pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/3 "2019-03-26T23:39:05Z")

</div>

Thank you for the reply.

Yes, it is strange to truncate on the key name `filter` that has no unicode characters.

I was looking at the docs for `captureBody` but that targets the request body instead of the response body. We are interested in the response body but I am sure as a last option we could attach the response body to the request body.

This is not a one-off need, we think the response body would be great for debugging. For example, a specific user was receiving an error. We searched the transactions by user's email (awesome!) but we needed a bit more context in the response such as the response body. If there is a different approach on gaining debugging incites please do share. Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [March 27, 2019, 3:39pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/4 "2019-03-27T15:39:26Z")

</div>

I'll discuss this with the rest of the APM team to see if this is something we can add support for. I'll keep you posted 🙂

---

<div class="post-metadata">

**Author:** ![isurfbecause](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@isurfbecause](https://discuss.elastic.co/u/isurfbecause)\
**Post date:** [March 27, 2019, 6:20pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/5 "2019-03-27T18:20:04Z")

</div>

Thanks Thomas.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 17, 2019, 2:20pm UTC](https://discuss.elastic.co/t/increase-setcustomcontext-context-character-limit/173818/6 "2019-04-17T14:20:11Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
