25 Apr 2025 01:15 PM
Within my environment, I have hosts with various tags against them. I'm looking for a tag which contains:
DEPARTMENT:
and this tag can be in index 5 or 4, it is random.
Here is my code so far:
timeseries { percent = avg(dt.host.disk.used.percent), used = avg(dt.host.disk.used), avail = avg(dt.host.disk.avail) }, by: { dt.entity.host, dt.entity.disk }
| fieldsAdd Utilisation = arrayAvg(percent), entityName(dt.entity.disk)
| fieldsAdd entityName(dt.entity.host)
| lookup [ fetch dt.entity.host
| fields id, tags[] ], sourceField:dt.entity.host, lookupField:id
| fieldsAdd tags = `lookup.tags[]`
| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")
| fieldsAdd test = arrayIndexOf(buIndex, TRUE)
| fieldsAdd BU = tags[test]
| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")
The code above checks each array element and return true if it matches the above.
The next piece of code then the element where 'true' was found including its index.
Where I have the field BU, is to access the index stored in test to give me the DEPARTMENT: text. However, I can't seem to figure it out. Any ideas?
Solved! Go to Solution.
25 Apr 2025 01:40 PM
Do you know expand can be used to separate the tags? https://docs.dynatrace.com/docs/shortlink/structuring-commands#expand
25 Apr 2025 02:55 PM
How would I utilise it to grab a specific tag if its located in different array elements?
28 Apr 2025 07:25 AM
Expand tags and filter tags containing Department ?
27 Apr 2025 08:41 PM
You can use iterative expressions to interact with arrays (fields tags is array of strings):
First I parse tags into array of records containing context, tag and its value:
timeseries { percent = avg(dt.host.disk.used.percent), used = avg(dt.host.disk.used), avail = avg(dt.host.disk.avail) }, by: { dt.entity.host, dt.entity.disk }
| fieldsAdd Utilisation = arrayAvg(percent), entityName(dt.entity.disk)
| fieldsAdd entityName(dt.entity.host)
| fieldsAdd entityAttr(dt.entity.host, "tags")
| fieldsAdd ptags = parse(dt.entity.host.tags[], """('[' LD:context ']')?((LD:tag (!<<'\\' ':') LD:value)|LD:tag)""")
| fieldsAdd ptags = record(context=ptags[][context], tag=replaceString(ptags[][tag],"\\:",":"), value=replaceString(ptags[][value],"\\:",":"))
| fieldsAdd department= arrayFirst(iCollectArray((if(ptags[][tag]=="DEPARTMENT", ptags[][value]))))
and later I can pick any element of this array using any condition based on record (I filter by tag name only in this example.