# Expand Tags values

**URL:** <https://community.dynatrace.com/t/expand-tags-values/42716>\
**Category:** DQL\
**Tags:** dql\
**Created:** [November 20, 2023, 1:16pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716 "2023-11-20T13:16:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RPbiaggio](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@RPbiaggio](https://community.dynatrace.com/u/RPbiaggio)\
**Post date:** [November 20, 2023, 1:16pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/1 "2023-11-20T13:16:29Z")

</div>

Guys, I need some help. I have my process group entities with some tags and I need to put two fields in one, one is the value of the tag “[Kubernetes]componentid:” and the other, the value of the tag, “env”. I can’t seem to put the two together. I’ve tried a few things, but it didn’t work. Can anyone help?

```markup
fetch dt.entity.process_group, from:now() - 1h
|fields entity.name, id, primeiraVisualizacao = lifetime[start], getDate = now(), tags
|expand tags
|fieldsAdd idComponent = replaceString(tags,"[Kubernetes]componentid:", "")
|filter (matchesPhrase(tags,"[Kubernetes]componentid"))
//|filter matchesPhrase(tags,"env:")
| sort primeiraVisualizacao desc
| limit 6000

```

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [November 20, 2023, 2:56pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/2 "2023-11-20T14:56:50Z")

</div>

Hi&nbsp;[@RPbiaggio](https://community.dynatrace.com/user/67564)

Are you looking for something like this

```markup
fetch dt.entity.process_group, from:now() - 1h
|fields entity.name, id, primeiraVisualizacao = lifetime[start], getDate = now(), tags
|expand tags, alias:ts
| parse toString(ts), """(('['LD:tag_context ']' LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key| (LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key)"""
| fieldsAdd componentId = if (tag_key == "componentid" and tag_context == "Kubernetes", tag_value)
| fieldsAdd environment = if (tag_key == "env", tag_value)
| summarize { 
  componentId = takeAny(componentId), 
  environment = takeAny(environment), 
  primeiraVisualizacao = takeAny(primeiraVisualizacao)}, by:{id}
| filter isNotNull(componentId) and isNotNull(environment)
| fieldsAdd myNewField = concat(componentId, ":", environment)
| sort primeiraVisualizacao desc
| limit 6000

```

So basically I am first adding 2 new fields (environmentid and environment) after expanding a parsing the tags array. Then I need to summarize (group) the records by process group id.

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![RPbiaggio](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@RPbiaggio](https://community.dynatrace.com/u/RPbiaggio)\
**Post date:** [November 20, 2023, 3:31pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/3 "2023-11-20T15:31:16Z")

</div>

Hi, [@sinisa\_zubic](https://community.dynatrace.com/user/38283)!!  
This is exactly what I need, but for example, my “env” tag still doesn’t appear. See this example where I added the tag manually. It does not return information in the DQL result.

 ![RPbiaggio_2-1700494224402.png](https://global.discourse-cdn.com/dynatrace/original/3X/0/4/04b76f1c42e838371d5be53448fea3d53d703550.png)

 ![RPbiaggio_1-1700494198457.png](https://global.discourse-cdn.com/dynatrace/original/3X/8/c/8c1df3a54da922741c0c3af89a8747f6559e92b4.png)

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [November 20, 2023, 3:50pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/4 "2023-11-20T15:50:55Z")

</div>

not sure why you have in line number 11 an “OR” instead of “AND”. I have added that one because I am assuming that process groups with tag “componentid” must have tag “env”.

but anyway, there might be some wired characters part of the tag and therefore the query is not working.  
can you maybe execute following query and let me know the result? basically it should only show the 2 relevant tags for the process group you have highlighted. And please share the result in “Raw” view in notebooks.

```markup
fetch dt.entity.process_group
| filter id == "PROCESS_GROUP-2AD8E1DA4761BD8E"
| expand tags
| filter contains(tags,"environmentid") or contains(tags,"env")

```

---

<div class="post-metadata">

**Author:** ![RPbiaggio](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@RPbiaggio](https://community.dynatrace.com/u/RPbiaggio)\
**Post date:** [November 20, 2023, 3:57pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/5 "2023-11-20T15:57:14Z")

</div>

I put the “OR” just to give me a result, it was blank with both conditions. Both tags must actually exist.

Below is the result, as requested.

fetch dt.entity.process\_group | filter id == “PROCESS\_GROUP-2AD8E1DA4761BD8E” | expand tags | filter contains(tags,“[Kubernetes]componentid”) or contains(tags,“env”)

```markup
{
  "records": [
    {
      "entity.name": "",
      "id": "PROCESS_GROUP-2AD8E1DA4761BD8E",
      "tags": "[Kubernetes]componentid:97ffed68873361906431ca240cbb3524"
    },
    {
      "entity.name": "",
      "id": "PROCESS_GROUP-2AD8E1DA4761BD8E",
      "tags": "env:DSV"
    },
    {
      "entity.name": "",
      "id": "PROCESS_GROUP-2AD8E1DA4761BD8E",
      "tags": "[Kubernetes]tags.datadoghq.com/env:dsv"
    }
  ],
  "metadata": {
    "grail": {
      "canonicalQuery": "fetch dt.entity.process_group\n| filter id == \"PROCESS_GROUP-2AD8E1DA4761BD8E\"\n| expand tags\n| filter contains(tags, \"[Kubernetes]componentid\") OR contains(tags, \"env\")",
      "timezone": "America/Sao_Paulo",
      "query": "fetch dt.entity.process_group | filter id == \"PROCESS_GROUP-2AD8E1DA4761BD8E\" | expand tags | filter contains(tags,\"[Kubernetes]componentid\") or contains(tags,\"env\")",
      "scannedRecords": 1,
      "dqlVersion": "V1_0",
      "scannedBytes": 4397,
      "analysisTimeframe": {
        "start": "2023-11-20T13:56:26.696Z",
        "end": "2023-11-20T15:56:26.696Z"
      },
      "locale": "",
      "executionTimeMilliseconds": 38,
      "notifications": [],
      "queryId": "0b7f4384-814b-4ec2-b9e9-29878c83f650",
      "sampled": false
    }
  },
  "types": [
    {
      "mappings": {
        "entity.name": {
          "type": "string"
        },
        "id": {
          "type": "string"
        },
        "tags": {
          "type": "string"
        }
      },
      "indexRange": [
        0,
        2
      ]
    }
  ]
}

```

Thanks.

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [November 20, 2023, 4:05pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/6 "2023-11-20T16:05:36Z")

</div>

I think I found the culprit, the DPL pattern was not correct. I have copied it [from help](https://docs.dynatrace.com/docs/shortlink/grail-querying-monitored-entities#entity-tags), but for the one special case it doesn’t work. I will trigger a documentation update.

Can you try this query now

```markup
fetch dt.entity.process_group, from:now() - 1h
|fields entity.name, id, primeiraVisualizacao = lifetime[start], getDate = now(), tags
|expand tags, alias:ts
| parse toString(ts), """(('['LD:tag_context ']' LD:tag_key (!<<'\\' ':') LD:tag_value)| (LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key)"""
| fieldsAdd componentId = if (tag_key == "componentid" and tag_context == "Kubernetes", tag_value)
| fieldsAdd environment = if (tag_key == "env", tag_value)
| summarize { 
  tags = takeAny(tags),
  componentId = takeAny(componentId), 
  environment = takeAny(environment), 
  primeiraVisualizacao = takeAny(primeiraVisualizacao)}, by:{id}
| filter isNotNull(componentId) and isNotNull(environment)
| fieldsAdd myNewField = concat(componentId, ":", environment)
| sort primeiraVisualizacao desc
| limit 6000

```

---

<div class="post-metadata">

**Author:** ![RPbiaggio](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@RPbiaggio](https://community.dynatrace.com/u/RPbiaggio)\
**Post date:** [November 20, 2023, 4:05pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/7 "2023-11-20T16:05:58Z")

</div>

When it includes another tag ([tags.datadoghq.com/env](http://tags.datadoghq.com/env)) that will be discontinued, which is from Datadog, it returned the value. This “env” tag will not have a context like the other, that is, it will not have “Kubernetes”.

 ![RPbiaggio_0-1700496252104.png](https://global.discourse-cdn.com/dynatrace/original/3X/e/b/ebb83177b47e28ef4648a97939fcafc26556554a.png)

---

<div class="post-metadata">

**Author:** ![RPbiaggio](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@RPbiaggio](https://community.dynatrace.com/u/RPbiaggio)\
**Post date:** [November 20, 2023, 4:08pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/8 "2023-11-20T16:08:39Z")

</div>

Now it works perfectly. Thanks for your help. Could you tell me what change you made? I appreciate the help.

 ![RPbiaggio_0-1700496474827.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/1/61b5c5aa8a3415b173eb7b864895981482f14d1f.png)

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [November 20, 2023, 4:15pm UTC](https://community.dynatrace.com/t/expand-tags-values/42716/9 "2023-11-20T16:15:47Z")

</div>

the change I did was in line 4

changed this

```markup
| parse toString(ts), """(('['LD:tag_context ']' LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key| (LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key)"""

```

to this

```markup
| parse toString(ts), """(('['LD:tag_context ']' LD:tag_key (!<<'\\' ':') LD:tag_value)| (LD:tag_key (!<<'\\' ':') LD:tag_value)|LD:tag_key)"""

```

What that line does is it parses the string of tags into context, key and value field. So how the pattern works basically it tries to pars the tag context, tag key, and tag value from the string.

```markup
['LD:tag_context ']' LD:tag_key (!<<'\\' ':') LD:tag_value)

```

In case it doesn’t match (there is no context) it should parse the key-value of the tag. and here was the issue. the initial pattern wanted to match & parse the full key, in case there is no context. but actually it should try to match & parse the tag key and tag value. so I just removed following snippet from the middle of line 4

```markup
|LD:tag_key|

```
