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

API V2 tags filter

pierre-yves_tai
Visitor

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

7 REPLIES 7

pahofmann
DynaMight Guru
DynaMight Guru

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 

Dynatrace Certified Master - Dynatrace Partner - 360Performance.net

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)? 

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")
Dynatrace Certified Master - Dynatrace Partner - 360Performance.net

It work fine, thank for your help. 👏

What if I wanted to pull all of the associated tags?  Not just specific one's.

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.

Dynatrace Certified Master - Dynatrace Partner - 360Performance.net

Thank you so much.

Featured Posts