Splunk Search

https error percentage

harpr86
Explorer

Hi,

I am trying to get the error percentage of the https response request but its not working as expected.

 

index="john-doe-index"
| stats count AS Total count(eval(statusCode="2**")) as Success
| eval Failure = Total - Success | eval Percent_Failure = round((Failure/Total)*100)."%"
| stats count by Percent_Failure

 

 

showing the following result.

harpr86_0-1718154074955.png

 

I took the above query from previous answers, not sure why its not working on my end. because the ratio of 4xx , 2xx are high and result is showing 100% and count 1 all the time.

 

Thanks!

Labels (2)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

Several problems with this search.

  1. Success is 0.  You can verify this by removing everything after the first stats. (I will analyze this later.)
  2. You should not do stats by Percent_Failure because you will always get 1.

Why Success count is 0?  You need to explain what is the output in index=john-doe-index.  By explain, I mean illustrate some real events (anonymize as needed).  Short of that, you need to explain whether you have a field named statusCode AND if yes, how this code is extracted.  Second, you need to explain what "2**" signifies.  Is this a wildcard expression, or does your event record success events as literal 2**?  These details are necessary so volunteers do not have to read your mind.  Here, I will speculate that your codes are like 200, 203, and so on. (Not "2**".)

I am not sure when can count(eval(statusCode="2**")) give non-zero count but I know in most cases it will end up with zero.  If you really, really, really want to use eval, you can do something like

 

 

| stats count as Total count(eval(if(searchmatch("statusCode=2*"), "success", null()))) as Success
| eval Failure = Total - Success | eval Percent_Failure = round((Failure/Total)*100)."%"
| table Percent_Failure Total

 

 

(If you are using wildcard in search, there is no need for "2**".) This is a lot messier than simply

 

| eval success = if(searchmatch("statusCode=2*"), "success", null())
| stats count as Total count(success) as Success
| eval Failure = Total - Success | eval Percent_Failure = round((Failure/Total)*100)."%"
| table Percent_Failure Total

 

Here is a full emulation using _internal.

 

index="_internal"
| extract access-extractions
| rename status as statusCode
``` data emulation above ```

 

My result is

Percent_FailureTotal
98%257002

(Of course, in my emulation, most events do not have field status so they count as "failure" in this formula.) If you use | stats count by Percent_Failure instead of table, all you get is Total 1.

View solution in original post

harpr86
Explorer

Thanks for your detailed answer. it worked. Appreciated.

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Several problems with this search.

  1. Success is 0.  You can verify this by removing everything after the first stats. (I will analyze this later.)
  2. You should not do stats by Percent_Failure because you will always get 1.

Why Success count is 0?  You need to explain what is the output in index=john-doe-index.  By explain, I mean illustrate some real events (anonymize as needed).  Short of that, you need to explain whether you have a field named statusCode AND if yes, how this code is extracted.  Second, you need to explain what "2**" signifies.  Is this a wildcard expression, or does your event record success events as literal 2**?  These details are necessary so volunteers do not have to read your mind.  Here, I will speculate that your codes are like 200, 203, and so on. (Not "2**".)

I am not sure when can count(eval(statusCode="2**")) give non-zero count but I know in most cases it will end up with zero.  If you really, really, really want to use eval, you can do something like

 

 

| stats count as Total count(eval(if(searchmatch("statusCode=2*"), "success", null()))) as Success
| eval Failure = Total - Success | eval Percent_Failure = round((Failure/Total)*100)."%"
| table Percent_Failure Total

 

 

(If you are using wildcard in search, there is no need for "2**".) This is a lot messier than simply

 

| eval success = if(searchmatch("statusCode=2*"), "success", null())
| stats count as Total count(success) as Success
| eval Failure = Total - Success | eval Percent_Failure = round((Failure/Total)*100)."%"
| table Percent_Failure Total

 

Here is a full emulation using _internal.

 

index="_internal"
| extract access-extractions
| rename status as statusCode
``` data emulation above ```

 

My result is

Percent_FailureTotal
98%257002

(Of course, in my emulation, most events do not have field status so they count as "failure" in this formula.) If you use | stats count by Percent_Failure instead of table, all you get is Total 1.

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

Ready to make your IT operations smarter and more efficient? Discover how to automate Splunk alerts with Red ...