21 Apr 2021 04:21 PM - last edited on 08 Jan 2024 10:23 AM by MaciejNeumann
Hello,
I want to extract metrics with a python script and Dynatrace API V2.
import requests
import json
from pathlib import Path
#fichier de parametrage
file=Path("logs/dump.json")
API_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# données authentification
headers = {'content-type': 'application/json',
'Authorization' : 'Api-Token ' + API_TOKEN}
#param
payload = {
"metricSelector": "builtin:service.response.time:names",
"entitySelector": "type(SERVICE)",
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environnement",
"value": "Production",
"stringRepresentation": "Environnement:Production"
},
{
"context": "CONTEXTLESS",
"key": "Projet OpenShift",
"value": "ged"
},
{
"context": "CONTEXTLESS",
"key": "Service Name",
"value": "svc-document"
}
]
}
r = requests.get('https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/api/v2/metrics/query', verify=False, headers=headers, params=payload)
print("CODE RETOUR: ", r.status_code)
with open(file,'w', encoding="utf8") as outputjson_file:
outputjson_file.write(json.dumps(r.json(), indent=4))
outputjson_file.close()
I've got a HTTP 200 code for result, but i take back all the metrics.
It seem that the tags filter doesn't work.
How i sould use tags for only get the metric for service name svc-document
Solved! Go to Solution.
21 Apr 2021 04:56 PM
The tag filter is part of the entity selector, you should include it there, e.g.:
type(SERVICE),tag("[context]key1:value-1","key2:value-2","key3")
So in your case:
type(SERVICE),tag("Environnement:Production","Projet OpenShift:ged","Service Name:svc-document")
You can find it in the docs here: api-entities-v2-selector
22 Apr 2021 10:40 AM
Hi @pahofmann ,
Thanks for your reply, it's work find 👍
In the api-entities-v2-selector i saw that "An entity with any of the specified tags is included to the response.". In my case, the first tag Environnement:Production return all entity of our environment. But i want to filter also with this other tag Projet Openshift:ged.
Is it possible to add conditions to the tags (ex : AND)?
22 Apr 2021 11:04 AM
Yes that's possible, should be added in the docs.
All entity selector parts are AND connected and you can use the same one multiple times.
So if it should have all the tags use:
type(SERVICE),tag("Environnement:Production"),tag("Projet OpenShift:ged"),tag("Service Name:svc-document")
22 Apr 2021 12:17 PM
It work fine, thank for your help. 👏
05 Jan 2024 11:49 PM
What if I wanted to pull all of the associated tags? Not just specific one's.
15 Jan 2024 10:34 AM
If you want all of them, no matter what tag, then there is no need to use the tag filter. Just leave the tag filter out.
24 Jan 2024 12:08 AM
Thank you so much.