Getting APM metrics using the/an API

Hi!
I'm have the elastic APM set up on some machines. It's working quite well and all the stats are showing perfectly fine, but I don't want to use just the GUI. I'd like to get metrics (CPU Usage, Disk Usage, etc) for each machine using the/an API. Is there any way to do this? I've looked into the docs and the only similar thing that I can find are node/cluster metrics.

Thanks

1 Like

Welcome to our community! :smiley:

Are you talking about Elasticsearch host stats, or something else?

Thanks!

Something like that, for example:
I have the APM installed on 4 machines and they're all configured to have SERVICE_NAME as let's say: "SERV_1". I can see the 4 machines under the instance section on SERV_1, the APM shows their CPU Usage, Memory Usage, etc on the GUI. Is there an API to get the CPU Usage of these machines as shown on the APM?

Sorry if I don't explain myself correctly, english is my second language :sweat_smile:

Different APM agents collect different metrics. Which language are you using??

Also the APM agent is not going to collect as detailed infrastructure metrics as metricbeat or the elastic agent which collect Up to literally hundreds of infrastructure metrics.

So the question is what are you looking for to do?.

Also, all this information is simply stored in elastic indexes so you will be able to get to that data through the rest API. But let's figure out what you want to do first

I'm looking to get infrastructure data, such as CPU Usage, System Memory Usage and Disk Space. I want to create a script that gets this data given a time period and stores it in some file(Like a CSV). I'm using Python for some machines, Javascript for some other machines and Ruby as well. The language for the script is going to be python.

You're better off using Metricbeat, it can handle this all by default and give you dashboards in Kibana alongside your APM data.

1 Like

Does the Elasticsearch API support this? Do you have an example of it? Thanks!

I would take these steps. And no, I don't have a simple example for you.

Install metricbeat and collect system metrics. Send them to Elasticsearch It's very easy to do this as system metrics are the default collection meaning CPU, RAM disk, etc .

Then you can use the Python client to extract the data the way you like through the elastic API using the query DSL.

Ok, so here's my question: Which endpoint do I have to send a request to in order to get the metrics? I've been trying GET /metricbeat-/ with many options and GET /apm/ and nothing gives me what I need. I'm testing all this stuff with the dev tools console.

GET /metricbeat-*/_search

That is a generic search

Then if you want documents say just from 1 host it would be like

GET /metricbeat-*/_search
{
  "query": {
    "term": {
      "host.name": {
        "value": "my-hostname"
      }
    }
  }
}

You are going to need to read a bit about the query DSL

Most the time for search on data like metrics you will use a query + a time range filter

Get the metrics for host my-hostname for the last 15m

GET /metricbeat-*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "host.name": {
              "value": "my-hostname"
            }
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-15m/m"
            }
          }
        }
      ]
    }
  }
}

This worked! Thanks!

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