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

How to convert individual container cpu usage to DQL?

Hillman
Guide

We are currently having a dashboard in gen2 UI that make use of builtin:containers.cpu.usagePercent to show not only the application pod CPU usage but also the pod count.

Hillman_0-1756879898526.png

However, when I try to convert it to gen3 dashboard, it shows the metrics is no longer available and unable to convert to DQL:

Hillman_1-1756879993922.png

According to the online documentation Kubernetes metrics migration guide — Dynatrace Docs, it shows a DQL that can be used to simulate the metrics

timeseries {
      // for total usage, user and system cpu usage are added
      userCpuUsage = avg(dt.containers.cpu.usage_user_time)
    , systemCpuUsage = avg(dt.containers.cpu.usage_system_time)
      // cpu logical counts are the fallback, if the throttling ratio doesn't exist
    , cpuLogicalCount = avg(dt.containers.cpu.logical_cores)
}
// filter statement ...
// leftOuter join allows the throttling ratio to be null
| join [
  timeseries {
      throttlingRatio = avg(dt.containers.cpu.throttling_ratio)
      // same filter statement as above ...
}
], on: { interval, timeframe}, fields: { throttlingRatio}, kind:leftOuter
| fieldsAdd
       // sum of system and user cpu usage
       numerator = userCpuUsage[] + systemCpuUsage[]
       // throttling ratio, or as a fallback cpu logical count.
     , denominator = coalesce(throttlingRatio, cpuLogicalCount)
     , nanoseconds_per_minute  = 60 * 1000 * 1000 * 1000
 | fields
       interval, timeframe
     , cpuUsagePercent = 100.0 * numerator[] / ( denominator[] * nanoseconds_per_minute)

I have tried with the DQL but it can only display the CPU usage percent in total. How can I revise it to display the cpu uage percent on a particular container/pod from a particular kubernetes namespace within a particular kubernetes cluster?

6 REPLIES 6

p_devulapalli
Leader

@Hillman You can try the below DQL and see if it helps , this will give you the CPU percent against the limits and requests

timeseries {
  cpu_usage = sum(dt.kubernetes.container.cpu_usage),
  requests_cpu = sum(dt.kubernetes.container.requests_cpu),
  limits_cpu = sum(dt.kubernetes.container.limits_cpu)
}, filter: {
  k8s.cluster.name == "XYZ" AND
  k8s.namespace.name == "ABC"    
  
}, by: { dt.entity.cloud_application_instance, k8s.container.name},

nonempty: true,
union: true

| fieldsAdd requests_cpu_percent = cpu_usage[] / requests_cpu[] * 100
| fieldsAdd limits_cpu_percent = cpu_usage[] / limits_cpu[] * 100

| fieldsRename `Name` = k8s.container.name, `CPU Requests %` = requests_cpu_percent, `CPU Limits %` = limits_cpu_percent

 

Phani Devulapalli

@p_devulapalli I have tried with your DQL and further filter it by container name. It ends up like this:

Hillman_0-1756952829346.png

Which does not match the gen2 version. The x-axis becomes time period instead of CPU usage. And the y-axis only displays the contain name. This is what we use in gen2 to display the chart:

Hillman_1-1756953011053.png

The DQL documentation only mention the generic language usage but does not mention what kind of types we can use. I am not sure how to filter with container group instance.

It also does not provide UI like the data explorer that we can choose from the drop down and do not need to learn a new language. I believe these kinds of limitations only hinter us from migrating to the new UI.

 

Try to add this summarize funcion to the end of the DQL and see if fits

| summarize CPU_Requests = avg(arrayAvg(`CPU Requests %`)), by:{Name}

Categorical visualization does not expect an array of values, so we need to aggregate it.

Regards

Site Reliability Engineer @ Kyndryl

@dannemca Thank you for your suggestion! The chart now resembles to the original one we have built.

Hillman_1-1757042935673.png

The DQL are now as follow:

timeseries {
  cpu_usage = sum(dt.kubernetes.container.cpu_usage),
  requests_cpu = sum(dt.kubernetes.container.requests_cpu),
  limits_cpu = sum(dt.kubernetes.container.limits_cpu)
}, filter: {
  k8s.cluster.name == "CLUSTER" AND
  k8s.namespace.name == "NAMESPACE" AND
  k8s.pod.name ~ "altcre-m*" AND
  k8s.container.name ~ "weblogic"
}, by: { k8s.pod.name, k8s.container.name},

nonempty: true,
union: true

| fieldsAdd requests_cpu_percent = cpu_usage[] / requests_cpu[] * 100
| fieldsAdd limits_cpu_percent = cpu_usage[] / limits_cpu[] * 100
| fieldsRename `Name` = k8s.pod.name, `CPU Requests %` = requests_cpu_percent, `CPU Limits %` = limits_cpu_percent
| summarize CPU_Requests = avg(arrayAvg(`CPU Requests %`)), by:{Name}

 

Then I have got 2 follow up questions. In the screenshot I have shown in the first post, the gen2 chart does put the labels (pod and container name) upon the bar itself instead of put them into the y-axis. That can help to save the space for the bar data to display. Does gen3 chart able to do that?

 

Another question is about the legend at the bottom. In gen2, it will show up to 3 labels and have an entry that shows there are more number of entries. In gen3, however, it only shows 1 label and it does not have the text to remind the users there are more entries. Instead, it only shows a rather non-obvious scroll bar. The bar is so near to the resize icon and it is very easy to misclick it. Is it possible to change that behaviour?

@Hillman Option to display the names on to the bar on Gen3 dashboards is not possible at this time .

The area of legend depends on the size of the view, you might need to adjust the size of the view a bit to adjust the visibility of legend or place them on the right side instead of bottom , I know this may not show up same what you had in Gen2 , alternatively you can turn off the legend as an option

Phani Devulapalli

Hillman
Guide

@p_devulapalli I have changed the chart type from "Categorical" to "Bar Chart", only display "CPU limits%" and add in "k8s.pod.name" as the "Split by" criteria and it looks better:

Hillman_0-1756978648434.png

However, I have 3 things that need to archive:

1. I want to display the bar in horizontal instead of vertical.

2. I want to split the bars from the 3 pods into individual columns instead of stacked column.

3. I do not care about the time series. I just want to display the overall (can be average / medium / 90% percentile) CPU usage % within the said time period of the dashboard, say last 2 hours, and the number of pods.

How can I archive that?

Featured Posts