28 Jan 2022 05:53 PM - last edited on 27 Mar 2023 10:37 AM by MaciejNeumann
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 ] } ] } ] }
Solved! Go to Solution.
28 Jan 2022 07:28 PM
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:
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
]
}
]
}
]
}
28 Jan 2022 07:59 PM
Thank you. That's a fantastic solution. Much appreciated.
Dynatrace Developers: Any thoughts of having a "Last" resolution or Transformation to make this easier?
28 Jan 2022 08:19 PM - edited 28 Jan 2022 08:24 PM
@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.