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

Query Kuberentes workloads

DamianJankowski
Observer

 

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.

10 REPLIES 10

krzysztof_hoja
Dynatrace Champion
Dynatrace Champion

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:

 

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

 

Hi @krzysztof_hoja 

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

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: SERVICE-86915F82C40F74B8•payments-api PaymentLinksController

 

 

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

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.

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

 

@krzysztof_hoja thanks, second query indeed is solving the issue, thanks a lot. 

is there an other option to list workloads? 

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

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.pngImage 2024-09-30 at 22.54.35.png

 

I am not sure what is your question here....

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

@krzysztof_hoja 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? 


Hi @krzysztof_hoja , 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 but I can't iterate on each entity without coding or a workflow :

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

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.

Observability consultant - Dynatrace Associate/Pro/Services certified

Hello @krzysztof_hoja ,

I eventually figured out how to do this iteratively using the belongs_to method :

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

Thank you !

 

Observability consultant - Dynatrace Associate/Pro/Services certified

Featured Posts