Splunk Search

How to edit my search to calculate the error ratio by a certain field?

Hung_Nguyen
Path Finder

My website has multiple widgets owned by various team and hosted on various CDN. I want to see the error rate by widget. I came up with this Splunk search

 index=tto*  Type=ERROR Message="Widget load failed" | stats count(eval(Type="ERROR")) as errors by widgetId

The table shows up as

Widget ID | count
WidgetA   |  45
WidgetB   |  90

I would like to modify it so that I would get the error rate (errors/total users), so it would look like this

Widget ID | count
WidgetA   |  1%
WidgetB   |  2%

I modified the search to count the total number of user

index=tto*  (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") | stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors 

however, I don't know how I can calculate the error rate. would love some advice. thank you

0 Karma
1 Solution

nmohammed
Contributor

hi Hung_Nguyen,

I think this should do it -

index=tto* (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") | stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors | eval ratio = round((errors/UsersCount)*100, 2) | table widgetId, ratio

View solution in original post

0 Karma

DalJeanis
SplunkTrust
SplunkTrust

try -

index=tto* (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") 
| stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors by widgetId
| eval ratio = round((errors/UsersCount)*100, 2) 
| table widgetId, UsersCount, errors, ratio
0 Karma

Hung_Nguyen
Path Finder

Hi Dal, the UserCount is 0 when I put in the "by widgetId" string but it would compute correctly without it.

0 Karma

nmohammed
Contributor

hi Hung_Nguyen,

I think this should do it -

index=tto* (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") | stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors | eval ratio = round((errors/UsersCount)*100, 2) | table widgetId, ratio

0 Karma

Hung_Nguyen
Path Finder

Hi Mason,

I get a table like one below. where I only see 1 row and the widgetId column is empty, and I should see 3 rows. thanks

widgetId | ratio
| 0.13

0 Karma

masonmorales
Influencer

That wasn't mine. 😉 Did you try the one I posted?

0 Karma

Hung_Nguyen
Path Finder

sorry Mason misread the name. I tried yours but I just get this table

Success | Failures | ratio
202019 | 267 | 0.13

I think I need to add the "By wigdetId" part

count(eval(Type="ERROR")) as errors by widgetId but if I add that it doesn't work as well

0 Karma

nmohammed
Contributor

in which field are you capturing the widgetId or "Widget ID"?

you were able to produce results using the widgetId field using your first query right ?
index=tto* Type=ERROR Message="Widget load failed" | stats count(eval(Type="ERROR")) as errors by widgetId

The table shows up as
Widget ID | count
WidgetA | 45
WidgetB | 90

can you try this ?

index=tto* (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") | stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors | eval ratio = round((errors/UsersCount)*100, 2) | table "Widget ID", ratio

0 Karma

Hung_Nguyen
Path Finder

Hi mohammed, yes the first query produced the right table, your query produces a table like one below. where I only see 1 row and the widgetId column is empty, and I should see 3 rows.

Success | Failures | ratio
202019 | 267 | 0.13

"Widget ID" and widgetId are the same. sorry typo

0 Karma

nmohammed
Contributor

hi Hung_Nguyen ,

in which field are you capturing the widgetId or "Widget ID"?

0 Karma

Hung_Nguyen
Path Finder

they are the same. I just rename widgetId to "Widget ID" for display purpose

0 Karma

nmohammed
Contributor

can you paste sample data ?

0 Karma

masonmorales
Influencer

You can use eval to calculate the percentage:

index=tto* (sourcetype=access method=GET uri_path="index.htm") OR (Type=ERROR Message="Widget load failed") | stats count(eval(uri_path="index.htm")) as UsersCount, count(eval(Type="ERROR")) as errors | eval error_rate_perc=round(errors/UsersCount*100)
0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...