# Query Kuberentes workloads

**URL:** <https://community.dynatrace.com/t/query-kuberentes-workloads/48030>\
**Category:** DQL\
**Tags:** dql, kubernetes\
**Created:** [September 28, 2024, 5:43pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030 "2024-09-28T17:43:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamianJankowski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/damianjankowski/32/10540_2.png) [@DamianJankowski](https://community.dynatrace.com/u/DamianJankowski)\
**Post date:** [September 28, 2024, 5:43pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/1 "2024-09-28T17:43:44Z")

</div>

Hi,

Could you please help me better understand how to query entities and the relationships between them?

fetch dt.entity.service  
| filter in(id, classicEntitySelector(“type(service),fromRelationship.isServiceOf(type(CLOUD\_APPLICATION),entityId(\“CLOUD\_APPLICATION-D40C6BABEC087314\”))”))

This returns all services related to a specific cloud application. In my case, it includes:

-Requests executed in background threads of SpringBoot for dine-in-api-\*

-Web-server for dine-in-api-\*

What I want to achieve is to get the Kubernetes workload names, which are visible in the “services” application (either in the filter bar or at the top of the service page). I plan to use these names as variables in a dashboard to display metrics like response time or failure rate, split by each service.

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [September 29, 2024, 7:54pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/2 "2024-09-29T19:54:48Z")

</div>

You query is correct. We just need to make it “dynamic” and use filtering criteria “by name” instead of “by id”.  
In combination with getting metric you mention it will look like:

```markup
timeseries response_time=avg(dt.service.request.response_time), by: {dt.entity.service},
filter: in( dt.entity.service, 
  classicEntitySelector(concat("type(service),fromRelationship.isServiceOf(type(CLOUD_APPLICATION),entityName(",$Entity,"))")))
| fieldsAdd dt.entity.service.name = entityName(dt.entity.service)

```

As dimensions for this metric contains id of the service, you can add service name using _ **entityName** _ function

---

<div class="post-metadata">

**Author:** ![DamianJankowski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/damianjankowski/32/10540_2.png) [@DamianJankowski](https://community.dynatrace.com/u/DamianJankowski)\
**Post date:** [September 30, 2024, 9:49am UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/3 "2024-09-30T09:49:42Z")

</div>

Hi&nbsp;[@krzysztof\_hoja](https://community.dynatrace.com/user/25373)

thanks a lot.

I would say it is only a part of the desired solution, so first I would like to get the pure Kubernetes workload name:

 ![Image 2024-09-30 at 11.42.25.png](https://global.discourse-cdn.com/dynatrace/original/2X/6/676d34f4927dacb02e0e771a3c128f296b0a03bf.png)

Kubernetes workload name is going to be a variable,

then I would like to query based on that name (e.g. to get 5xx timeseries) to get all related services.

btw. is it possible to trim service-id from your query as I am getting sth like:&nbsp;SERVICE-86915F82C40F74B8•payments-api PaymentLinksController

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [September 30, 2024, 10:08am UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/4 "2024-09-30T10:08:56Z")

</div>

To get list of workload names to use in variable definition:

```markup
fetch dt.entity.cloud_application
| fields entity.name
| dedup entity.name

```

Of course you can control what fields are in final results using _ **fields** _ of _ **fieldsRemove** _ commands.

```markup
timeseries response_time=avg(dt.service.request.response_time), by: {dt.entity.service},
filter: in( dt.entity.service, 
  classicEntitySelector(concat("type(service),fromRelationship.isServiceOf(type(CLOUD_APPLICATION),entityName(",$Entity,"))")))
| fields dt.entity.service = entityName(dt.entity.service), response_time, interval, timeframe

```

---

<div class="post-metadata">

**Author:** ![DamianJankowski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/damianjankowski/32/10540_2.png) [@DamianJankowski](https://community.dynatrace.com/u/DamianJankowski)\
**Post date:** [September 30, 2024, 12:52pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/5 "2024-09-30T12:52:08Z")

</div>

[@krzysztof\_hoja](https://community.dynatrace.com/user/25373)&nbsp;thanks, second query indeed is solving the issue, thanks a lot.

is there an other option to list workloads?

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [September 30, 2024, 4:50pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/6 "2024-09-30T16:50:29Z")

</div>

If you want data related variable, one fed by DQL results is the easiest one to use.

---

<div class="post-metadata">

**Author:** ![DamianJankowski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/damianjankowski/32/10540_2.png) [@DamianJankowski](https://community.dynatrace.com/u/DamianJankowski)\
**Post date:** [September 30, 2024, 8:55pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/7 "2024-09-30T20:55:07Z")

</div>

Hmm… the workload name (without a lot of trash) should be usable as a variable to create a tile that queries all related services

 ![Image 2024-09-30 at 22.52.02.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/e/9e3ea61d769cccd77efc7b31f6beab99b77c432f.png)

 ![Image 2024-09-30 at 22.54.35.png](https://global.discourse-cdn.com/dynatrace/original/2X/f/fa4210cefd8e18af6e1182faf865676bcc3a97ea.png)

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [October 1, 2024, 10:25am UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/8 "2024-10-01T10:25:17Z")

</div>

I am not sure what is your question here…

Query retrieving e.g. response time takes workload name ($Entity matching name of CLOUD\_APPLICATION).

---

<div class="post-metadata">

**Author:** ![DamianJankowski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/damianjankowski/32/10540_2.png) [@DamianJankowski](https://community.dynatrace.com/u/DamianJankowski)\
**Post date:** [October 1, 2024, 4:51pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/9 "2024-10-01T16:51:23Z")

</div>

[@krzysztof\_hoja](https://community.dynatrace.com/user/25373)&nbsp;I would like a query that returns only the Kubernetes workloads and another one that returns all entities belonging to a specific workload. So the question is - how to achieve that?

---

<div class="post-metadata">

**Author:** ![agravier](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/agravier/32/29303_2.png) [@agravier](https://community.dynatrace.com/u/agravier)\
**Post date:** [March 26, 2025, 7:09pm UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/10 "2025-03-26T19:09:22Z")

</div>

Hi [@krzysztof\_hoja](https://community.dynatrace.com/user/25373)&nbsp;, thanks for sharing!

Do you know if there’s an alternative approach to correlate metrics like service response time with total CPU consumption of the pods belonging to its associated workloads?

It works with classicEntitySelector&nbsp;but I can’t iterate on each entity without coding or a workflow :

```markup
fetch dt.entity.cloud_application
| filter in( id, classicEntitySelector("type(CLOUD_APPLICATION),toRelationships.isServiceOf(type(SERVICE),entityId(SERVICE-B82BE29035866C21))"))
| fieldsAdd serviceID = "SERVICE-B82BE29035866C21"

```

![AurelienGravier_0-1743015647565.png](https://global.discourse-cdn.com/dynatrace/optimized/3X/f/b/fbce25231f7c82cdbd9b75c5e91d35dbd7493408_2_690x41.png)

I’m looking for a way to build an iterative view ideally suitable for rendering in a notebook table, where each row would contain:  
- Service name  
- Avg response time  
- Total CPU usage of its workload’s pods (aggregated)

Would love to know if you’ve explored something similar, or have any tips on composing the entity graph to support this use case.

Thanks you very much.  
Regard Aurélien.

---

<div class="post-metadata">

**Author:** ![agravier](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/agravier/32/29303_2.png) [@agravier](https://community.dynatrace.com/u/agravier)\
**Post date:** [March 28, 2025, 6:16am UTC](https://community.dynatrace.com/t/query-kuberentes-workloads/48030/11 "2025-03-28T06:16:07Z")

</div>

Hello&nbsp;[@krzysztof\_hoja](https://community.dynatrace.com/user/25373)&nbsp;,

I eventually figured out how to do this iteratively using the belongs\_to method :

```markup
fetch dt.entity.service

| filter isnotnull(clustered_by)

// Get namespaces from service
| fieldsAdd namespaceId = belongs_to[dt.entity.cloud_application_namespace]
| expand namespaceId
| fieldsAdd namespace = entityAttr(namespaceId, "entity.name", type:"dt.entity.cloud_application_namespace")
// Get workloads from service
| fieldsAdd workloadId = belongs_to[dt.entity.cloud_application]
| expand workloadId
| fieldsAdd workloadName = entityAttr(workloadId, "entity.name", type:"dt.entity.cloud_application")

// get service median response time
| lookup [
timeseries responseTime_p50_timeseries = percentile(dt.service.request.response_time, 50), by:{dt.entity.service}, bins: 40
], sourceField:id, lookupField:dt.entity.service, fields:{responseTime_p50_timeseries}, executionOrder:leftFirst
| fieldsAdd responseTime = arrayLast(responseTime_p50_timeseries)

// Add CPU % Usage per workload
// Only work for workload with a limit set
| lookup [
timeseries by:{dt.entity.cloud_application}, {
cpu_usage = sum(dt.kubernetes.container.cpu_usage, default:0, rollup:avg),
cpu_limit = sum(dt.kubernetes.container.limits_cpu, rollup:avg)
}
| fieldsAdd cpuPercent = (arrayAvg(cpu_usage) / arrayAvg(cpu_limit)) * 100

] , sourceField:workloadId, lookupField:dt.entity.cloud_application, fields:{cpuPercent}
| fieldsAdd cpuPercent = if(isNull(cpuPercent), "no limit", else: cpuPercent)

....

```

 ![AurelienGravier_0-1743142370828.png](https://global.discourse-cdn.com/dynatrace/original/1X/6a46d8ba836a7298294ee940d040e9ed2405d317.png)

Thank you !
