# How to get custom metrics in APM?

**URL:** <https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [December 13, 2022, 11:26am UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116 "2022-12-13T11:26:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![stretch0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stretch0/32/114624_2.png) [@stretch0](https://discuss.elastic.co/u/stretch0)\
**Post date:** [December 13, 2022, 11:26am UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/1 "2022-12-13T11:26:31Z")

</div>

I have setup application performance monitoring in my Nodejs application. I can see my metrics coming through for CPU / memory etc but can't seem to get things to work for custom metrics.

I am following [the docs](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-register-custom-metrics) on adding a metric for tracking the clients connected to our web socket server but can't see the metrics in the Elastic dashboard anywhere:

I have the following code:

```auto
apm.registerMetric('ws.connections', () => socketClients.length);

```

Under APM -\> Services -\> Metrics I am searching for `ws.connections` but no results are showing up.

Do I need to add anything else to get the custom metrics to pull through?

Am I correct in thinking I should see these under APM -\> Services -\> Metrics

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 13, 2022, 4:40pm UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/2 "2022-12-13T16:40:14Z")

</div>

Hi @stretch0. Thanks for the question.

> Am I correct in thinking I should see these under APM -\> Services -\> Metrics

No. The `APM > Services > $serviceName > Metrics` page only shows visualizations for a strict set of well-known metrics -- specifically some of the `system.*` metrics described at [Metrics | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/metrics.html) that are always collected by the Node.js APM agent. Eventually we hope to add visualizations for some of the `nodejs.*` runtime metrics there.

There has been some discussion in the past for allowing user-defined metrics visualizations on this page, but I am not aware of current plans for that.

> Do I need to add anything else to get the custom metrics to pull through?

Ultimately, the best way to visualize custom metrics is by building your own [dashboard](https://www.elastic.co/kibana/kibana-dashboard).

First, however, let's take an aside to look at the raw "metricset" documents that the Node.js APM agent should be sending to verify that your custom metric is getting through. In my example I used the following Node.js script. I used `foo.connections` to have a different metric name:

```auto
// example-registerMetric.js
const apm = require('elastic-apm-node').start({
  serviceName: 'example-registerMetric',
  serverUrl: 'XXX',
  secretToken: 'XXX',
  metricsInterval: '5s',
  apiRequestTime: '5s',
})

const socketClients = []

apm.registerMetric('foo.connections', () => socketClients.length);

// Randomly change the length of socketClients every 1s.
setInterval(function () {
  if (Math.random() < 0.75) {
    socketClients.push({type: 'socket', startTime: Date.now()})
  } else {
    socketClients.pop()
  }
}, 1000)

```

I ran that for a while. Then I went to "Discover" in Kibana and searched for documents with that metric field using `foo.connections: *`. That verifies that documents with my custom metric are getting into Elasticsearch.

 ![Screen Shot 2022-12-13 at 8.35.46 AM](https://us1.discourse-cdn.com/elastic/original/3X/3/9/39dee4b6070e49a13ef04141aaa1ba1f2849d736.png)

Then I went to "Dashboard" in Kibana and created a dashboard with a visualization of "foo.connections". For example:

 ![Screen Shot 2022-12-13 at 8.39.31 AM](https://us1.discourse-cdn.com/elastic/original/3X/5/f/5fec54b6d31fdb76275aac277385945fff6b785e.png)

Please let me know if that doesn't answer your question.

---

<div class="post-metadata">

**Author:** ![stretch0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stretch0/32/114624_2.png) [@stretch0](https://discuss.elastic.co/u/stretch0)\
**Post date:** [December 14, 2022, 11:22am UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/3 "2022-12-14T11:22:12Z")

</div>

Hi @trentm

Ok that makes more sense as to why I couldn't see the metrics automatically pulling through.

I have tried following your example of searching for `ws.connections: *` in Kibana but seems like the metric isn't even pulling through to there

 ![Screenshot 2022-12-14 at 11.19.39](https://us1.discourse-cdn.com/elastic/original/3X/8/3/83b9a392830354c97e2e085985b5de173252b069.png)

I noticed you've included the following attributes when starting APM node

```auto
metricsInterval: '5s',
apiRequestTime: '5s',

```

I have updated mine to include these now too which didn't seem to make any difference but I can't find any docs on these and wasn't sure if they are required?

My start config is as follows:

```auto
apm.start({
  serverUrl: env.ELASTIC_SERVER_URL,
  secretToken: env.ELASTIC_SECRET_TOKEN,
  environment: env.NODE_ENV,
  active: true,
  metricsInterval: '5s',
  apiRequestTime: '5s',
});

```

Is there anything else I am missing that would cause the metrics not to show up for me?

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 14, 2022, 6:35pm UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/4 "2022-12-14T18:35:01Z")

</div>

> I have tried following your example of searching for `ws.connections: *` in Kibana but seems like the metric isn't even pulling through to there

It looks like you are searching in the "functionbeat-7.11.1\*" index, which will only hold data from the separate [functionbeat](https://www.elastic.co/beats/functionbeat) tool. You will need to change to the appropriate apm index. I don't know off hand the exact name of the index for version 7.11 of the stack. In version 7.15 it is this index pattern: `traces-apm*,logs-apm*,metrics-apm*`.

> [@stretch0](#):
>
> I noticed you've included the following attributes when starting APM node
> 
> ```auto
> metricsInterval: '5s',
> apiRequestTime: '5s',
> 
> ```

Those aren't _required_, but they tell the APM agent to (a) gather and send metrics every 5s instead of the usual 30s and (b) forward data on to the APM server every 5s instead of the usual ~10s. I added these to my demo to get a faster turn around while playing. Generally you would _not_ want to use these settings for production. They are both documented on this page: [Configuration options | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#metrics-interval)

---

<div class="post-metadata">

**Author:** ![stretch0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stretch0/32/114624_2.png) [@stretch0](https://discuss.elastic.co/u/stretch0)\
**Post date:** [December 15, 2022, 2:36pm UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/5 "2022-12-15T14:36:51Z")

</div>

Ok that makes sense. I can now see the metrics pulling through into `functionbeat` so I will go ahead and setup a dashboard for that data being pulled in. Thanks for your help there.

One more question, is there an advantage of doing it this way with registering metrics vs if I was to just add an info level log and create a dashboard based on filtering of that log?

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [December 15, 2022, 7:51pm UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/6 "2022-12-15T19:51:31Z")

</div>

> [@stretch0](#):
>
> is there an advantage of doing it this way with registering metrics vs if I was to just add an info level log and create a dashboard based on filtering of that log?

Currently with [`apm.registerMetric()`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-register-custom-metrics) there probably wouldn't be much difference. The `registerMetric()` API _does_ support adding a set of labels which could be used for segmentation (e.g. collecting latency for requests that are 500 errors vs. successful responses). However, you could manually accomplish the same with structured log records.

The APM agent's handling will ensure a regular interval of collection of the metrics, which can be important for sanely aggregating metrics over time periods in Elasticsearch later. Doing this manually with a setInterval is straightforward as well, though.

The APM system will setup the Elasticsearch index templates so that the _types_ for your metric fields are appropriate. Doing this via logs might require manually setting up index templates appropriately.

The current `apm.registerMetric()` is limited to a simple Gauge metric type. We eventually will support counters and histograms -- likely based on the OpenTelemetry Metrics SDK. The latter (histograms) would be much more complex to manually handle with log records.

---

<div class="post-metadata">

**Author:** ![stretch0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stretch0/32/114624_2.png) [@stretch0](https://discuss.elastic.co/u/stretch0)\
**Post date:** [December 16, 2022, 1:30pm UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/7 "2022-12-16T13:30:26Z")

</div>

Ok thanks for the detailed response. That makes sense.

Seems like I've got things working with your help now so really appreciate that.

Sounds like `registerMetric` still has some more development to go so I look forward to seeing more features and functionality being built around it.

---

<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:** [January 6, 2023, 9:30am UTC](https://discuss.elastic.co/t/how-to-get-custom-metrics-in-apm/321116/8 "2023-01-06T09:30:33Z")

</div>

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