How to query host CPU metric using NRQL?

API Question Template

I can query the CPU metric of a specific host using REST API
https://api.newrelic.com/v2/applications/{{application_id}}/hosts/{{host_id}}/metrics/data.json?names[]=CPU/User+Time&period=60

image

how I can do that using NRQL?
I tried the following

{
  actor {
    account(id: {{account_id}}) {
      nrql(query: """
        SELECT * FROM Metric SINCE 30 MINUTES AGO WHERE metricName = 'CPU/User Time' LIMIT 1000

      """) {
        results
      }
    }
  }
}

It returns nothing

Hi @jerry.wang

While using NRQL to query METRIC, you will need to filter data by using WHERE clause and explicitly specifying the entity’s guid or name, in order to fetch the results (unlike events).

Sample:

FROM METRIC SELECT * WHERE (entityGuid = '*********' or entity.guid = '**********') AND metricName = '#########'

Give it a try share your findings with us.

1 Like

Thanks, but it does not work from my test.

I get the entity Guid here. is that correct? then I fill this guid into query, it returns nothing.

Is there someway I can query entity guid in GraphQL?

A couple of clarifications:

  1. the metricName would be newrelic.goldenmetrics.infra.host.cpuUsage
  2. We can use either entityGuid or entityName for scoping the query to a specific entity

Then, the NRQL query would look something like this:

FROM Metric SELECT * WHERE (entity.guid = '*******************'  or entityName = 'XX-XX-XX-XX') and metricName = 'newrelic.goldenmetrics.infra.host.cpuUsage'

And, to answer your question regarding identifying/querying GUID in GraphQL, Entity Search would do the trick. Here is a sample query to fetch the GUID for a specific INFRA - HOST:

{
  actor {
    entitySearch(queryBuilder: {type: HOST, domain: INFRA, name: "XX-XX-XX-XX"}) {
      results {
        entities {
          guid
          name
          entityType
        }
      }
    }
  }
}

I hope this helps!

1 Like

Hi @jerry.wang

Thanks for reaching out, I hope you are well.

I note that @anil.sonaji has reached out with some workarounds and additional guidance ( many thanks ). Please do let us know if this proves successful, or should you require further support.

Thanks @dcody @anil.sonaji

It does not work

Then I tried to get the entities using following query, and cannot find my pod

{
  actor {
    entitySearch(queryBuilder: {type: HOST, domain: INFRA}) {
      results {
        entities {
          guid
          name
          entityType
        }
      }
    }
  }
}

My application is running as Pod in Kubernetes.
The REST API works well for me, but I have to call REST API for each of my Pod to retrieve their metrics.
That’s why I want to use GraphQL, I suppose I can aggregate and group the data in a single request.

Is GraphQL API a replacement for the RESTAPI? I doube it now.

Hi @jerry.wang - Have you seen the document below? It might provide some additional help.

Nerdgraph will eventually replace the REST API. The NRQL query within Nerdgraph works the same as the REST API. I noticed you are querying golden metrics which are queries slightly different