Join query question.

Microsoft

Hello all,

My POD containers are logging their state and I need to find out how long the Containers are in the “Waiting” state.

Consider the below table structure 

ContainerName

Status

Time Generated

C1

Running

2018-11-13T19:31:17.000

C1

Running

2018-11-13T19:32:17.000

C2

Running

2018-11-13T19:31:17.000

C2

Running

2018-11-13T19:29:17.000

C1

Waiting

2018-11-13T19:28:17.000

C1

Waiting

2018-11-13T19:29:17.000

C2

Waiting

2018-11-13T19:25:17.000

C2

Waiting

2018-11-13T19:26:17.000

 

Essentially, I need to find out the difference between the Waiting “Time generated”  with the next Running “Time generated” value.   

Please keep in mind a Container can be in “Waiting” state and in Running state in multiple times (within any specific time-range), for example

[ t1:Waiting -> t2:Waiting-> t3:Running -> t4:Running -> t5:Running -> t6:Waiting -> t7:Running -> t8:Running ->  t9:Running ->  t10:Waiting ], here t1, t2, t3 are time points.
How to figure out this as query.

Thanks,

3 Replies

Hi,

There are various ways to approach this, here's one example:

datatable (Container:string, Status:string, TimeGenerated:datetime)
[
  "C1", "Running", datetime('2018-11-13T19:27:16.000'),
  "C1", "Running", datetime('2018-11-13T19:32:17.000'),
  "C2", "Running", datetime('2018-11-13T19:31:10.000'),
  "C2", "Waiting", datetime('2018-11-13T19:29:17.000'),
  "C1", "Waiting", datetime('2018-11-13T19:28:14.000'),
  "C1", "Waiting", datetime('2018-11-13T19:29:15.000'),
  "C1", "Waiting", datetime('2018-11-13T19:30:19.000'),
  "C2", "Waiting", datetime('2018-11-13T19:25:12.000'),
  "C2", "Running", datetime('2018-11-13T19:26:10.000'),
]
| sort by Container asc, TimeGenerated asc
| extend Status_changed = (Container != prev(Container) or Status != prev(Status))
| where Status_changed == true
| extend Waiting_time = iff(Status=="Running" and prev(Status)=="Waiting", tostring(TimeGenerated-prev(TimeGenerated)), "null")

I've created a data table with records similar to what you suggest (not exactly the same).

First  - I sort the data by container and time of event.

Then - I calculate whether a row indicated a change in status for this specific container (from waiting to running or the other way).

I then keep only the records in which the status has changed - the others aren't needed for the calculation of waiting time.

Finally - I calculate the duration from the most recent waiting period, until the current running period.

 

HTH,

Noa

Adding @Keiko Harada
Are you using AKS?

Not sure which solution you have on boarded. If its ContainerInsights solution , then the Table you can look at is KubePodInventory Table instead of the ContainerTable in the query. 

 

Here is the sample query which you can use to calculate  how long container in waiting state

ContainerTable
| where ContainerStatus =~ "waiting"
| summarize arg_max(TimeGenerated, ContainerStatus) by ContainerID
| project LastWaitingTime= TimeGenerated, ContainerID
| join kind=inner (
ContainerTable
| where ContainerStatus =~ "running"
| summarize arg_max(TimeGenerated, ContainerStatus) by ContainerID
| project LastRunningTime= TimeGenerated, ContainerID
) on ContainerID
| project ContainerID, LastRunningTime, LastWaitingTime, TotalWaitTime = (LastWaitingTime - LastRunningTime)

 

Please feel free to ping me if you need any further help on this.