cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

API metric/query - how to return just one result (data point)

ct_27
DynaMight Pro
DynaMight Pro

I'm trying to use the API to pull back the latest data point value for a given metric.  I've tried playing with transforms and resolutions. I can get it down to 2 metrics. But any suggestions on how to just get the latest datapoint for a given metric on a given host?

 

We're trying to integrate two monitoring systems by sending metrics out of DT and into the other monitoring tool using its APIs.

 

https://xxxxxx.live.dynatrace.com/api/v2/metrics/query?metricSelector=builtin%3Ahost.cpu.system&resolution=2&entitySelector=type%28%22HOST%22%29%2CentityName%28%22xxxxxxxxx%22%29 

{
  "totalCount": 1,
  "nextPageKey": null,
  "resolution": "1h",
  "result": [
    {
      "metricId": "builtin:host.cpu.system",
      "data": [
        {
          "dimensions": [
            "HOST-xxxxx"
          ],
          "dimensionMap": {
            "dt.entity.host": "HOST-xxxxx"
          },
          "timestamps": [
            1643385600000,
            1643389200000,
            1643392800000
          ],
          "values": [
            1.8281565772162542,
            1.7218984444936116,
            1.766819120298886
          ]
        }
      ]
    }
  ]
}
HigherEd
3 REPLIES 3

edward-buckler
Dynatrace Enthusiast
Dynatrace Enthusiast

Hello,

 

OK assuming you can't use whatever is running the API to grab the first element you can do something like this: 

 

 

curl -X GET "https://XXXXXXXX.live.dynatrace.com/api/v2/metrics/query?metricSelector=builtin%3Ahost.cpu.system&from=now-2m%2Fm&to=now-1m%2Fm&entitySelector=type%28HOST%29%2CentityId%28HOST-XXXXXXXXXXXXXXXX%29" -H "accept: application/json; charset=utf-8" -H "Authorization: Api-Token [TOKEN]"

 

Effectively I am just using the timeframe selector to get the last datapoint: 

edwardbuckler_1-1643398056346.png

 

 

The response would look like this: 

 

{
  "totalCount": 1,
  "nextPageKey": null,
  "resolution": "1m",
  "result": [
    {
      "metricId": "builtin:host.cpu.system",
      "data": [
        {
          "dimensions": [
            "HOST-XXXXXXXXXXXXXXXX"
          ],
          "dimensionMap": {
            "dt.entity.host": "HOST-XXXXXXXXXXXXXXXX"
          },
          "timestamps": [
            1643397300000
          ],
          "values": [
            0.32281724611918133
          ]
        }
      ]
    }
  ]
}

 

ct_27
DynaMight Pro
DynaMight Pro

Thank you.  That's a fantastic solution.  Much appreciated.

 

Dynatrace Developers:  Any thoughts of having a "Last" resolution or Transformation to make this easier?

HigherEd

dannemca
DynaMight Guru
DynaMight Guru

@ct_27 Try to add the ":lastReal" at the end of your metricselector query 🙂.

Even if you remove the resolution, it will always get the last datapoint.

Site Reliability Engineer @ Kyndryl

Featured Posts