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

Grail code help

derija
Helper

Here is my code related to area trying to get the percentage of 5xx. I seem to have all the data here but the simple math is not working out...Any help?

| summarize { dur_msAvg=avg(duration), dur_msMax=max(duration), dur_msMin=min(duration), dur_medMin=median(duration),
failedRequests300 = countIf(http.response.status_code >= 300 and http.response.status_code < 400),
failedRequests400 = countIf(http.response.status_code >= 400 and http.response.status_code <500),
failedRequests500 = countIf(http.response.status_code >= 500),
successfulRequests = countIf(http.response.status_code == 200),
allRequests = count(),
rate5xx = countIf( toDouble((failedRequests500 / allRequests) * 100) <> 0) },
by:{service.entity.name, endpoint.name, appName}

 

output:

derija_0-1760368990140.png

2025-10-13_08-19-48.jpg

 

4 REPLIES 4

fstekelenburg
DynaMight Pro
DynaMight Pro

Perhaps (since it involves percentage) you could calculate the percentage directly, like this?

rate5xx = toDouble(failedRequests500) / toDouble(allRequests) * 100

Remarks:
- toDouble() ensures you're doing floating-point division.
- I don't think you need to wrap the percentage calculation in countIf — that’s only for counting events that match a condition.



 

Kind regards, Frans Stekelenburg                 Certified Dynatrace Associate | Cegeka.com, Dynatrace Partner

Thanks for the option when I just try straight math whether I use toDouble or not I get

derija_0-1760373595830.png

 

Here is the fix...I had to END the summarize, then include the field...subtle but I missed it..

 

|summarize by:{service.entity.name, endpoint.name, appName},
{ dur_msAvg=avg(duration), dur_msMax=max(duration), dur_msMin=min(duration), dur_medMin=median(duration),
failedRequests300 = countIf(http.response.status_code >= 300 and http.response.status_code < 400),
failedRequests400 = countIf(http.response.status_code >= 400 and http.response.status_code <500),
failedRequests500 = countIf(http.response.status_code >= 500),
successfulRequests = countIf(http.response.status_code == 200),
allRequests = count()}
|fieldsAdd rate5xx = if(allRequests > 0, toDouble(failedRequests500) / toDouble(allRequests) * 100)
|fieldsAdd rate4xx = if(allRequests > 0, toDouble(failedRequests400) / toDouble(allRequests) * 100)

ZackE
Participant

The issue is that you're trying to reference fields created within the same summarize command and that isn't possible. You will have to do something like this:

rate5xx = toDouble(countIf(http.response.status_code >= 500) / count() * 100)

EDIT: I see that you found a solution.

Featured Posts