Splunk Search

How to search count of multiple logs?

vishal_pcap
Explorer

How can I write a query like following? 

index=my_app
| eval userError="Error while fetching User"
| eval addressError = "Did not find address of user"
| stats count(userError) as totalUserErrors, count(addressError) as totalAddressErrors

Expected output: 

Error while fetching User 50
Did not find address of user 30
Labels (3)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Given that your event don't appear to have any structured fields, you could try counting matches of the _raw field

| stats count(eval(match(_raw,"Error while fetching Users"))) as userError count(eval(match(_raw,"No User address Found"))) as addressError

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Is this what you are trying to do?

index=my_app
| stats count(eval(userError=="Error while fetching User")) as totalUserErrors, count(eval(addressError=="Did not find address of user")) as totalAddressErrors
0 Karma

vishal_pcap
Explorer

The query doesn't return anything 😞  I wanted to have a tabular output - error message and number of times it appeared 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Please share some of your events (in a code block using the </> formatting button), anonymised of course.

Also, share your current search, so we can see what you have tried so far..

0 Karma

vishal_pcap
Explorer

So when I search the following query in splunk, it returns 50k+ records

index=xxxeks_prod_app cluster_name="xxxx-xxxxx-prod-eks-cluster-v1" container_name="xx*-service" "Error while fetching Users"

and I want to see the multiple error logs and their count (for the duration I have selected e.g. 30 minutes)
Here's what I have tried but it returned 0 count

index=xxxeks_prod_app cluster_name="xxxx-xxxxx-prod-eks-cluster-v1" container_name="xx*-service" | stats count(eval(userError=="Error while fetching Users")) as totalUserErrors
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It looks like the errorError field has not been extracted. 

0 Karma

vishal_pcap
Explorer

How to extract that? 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Please share some of your events (in a code block using the </> formatting button), anonymised of course.

0 Karma

vishal_pcap
Explorer

oh yes, 

1/16/23
7:15:44.624 AM	
2023-01-16 07:15:44 AM [http-nio-8080-exec-8] [trace_id:  / span_id: ] ERROR jobTraceId= commandTraceId=   {X-B3-ParentSpanId=xxxxxx, X-B3-SpanId=xxxxx, X-B3-TraceId=xxxxx, X-Span-Export=false, parentId=xxxxx, spanExportable=false, spanId=xxxx, traceId=xxxxxxxxxxxx} com.demo.controller.UserController - Error while fetching Users participant and plan info details=Could not find any User for the userId=202961636 java.lang.IllegalArgumentException: Could not find any User for userId=202961636
	at com.demo.service.UserServiceV2.lambda$prepareUserInfo$4(UserServiceV2.java:520) ~[demo-data-rest-1.0.22.12.40.jar:?]
	at java.util.Optional.orElseThrow(Unknown Source) ~[?:?]
	at com.demo.service.UserServiceV2.prepareUserInfo(UserServiceV2.java:520) ~[demo-data-rest-1.0.22.12.40.jar:?]

host = ip-11-000-00-00.us-west-2.compute.internalsource = /var/log/containers/demo-v2-service-55f87cc4v2-v2service-78f8e0f8ff9689627faa4718f34578bd511913596cbf57.logsourcetype = kube:container:demo-v2-service

 

and 

2023-01-16 07:21:28 AM [http-nio-8080-exec-63] [trace_id:  / span_id: ] ERROR com.demo.service.PersonApiChunkService - Error while handling user: bae9877cf5ab433xx39fda32ffd9833exx6bf2 com.demo.exception.ResourceNotFoundException: No User address Found for personId=329813370 deviceId=7440501_P_192

host = ip-11-000-00-00.us-west-2.compute.internalsource = /var/log/containers/demo-v2-service-55f87cc4v2-v2service-78f8e0f8ff9689627faa4718f34578bd511913596cbf57.logsourcetype = kube:container:demo-v2-service
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Given that your event don't appear to have any structured fields, you could try counting matches of the _raw field

| stats count(eval(match(_raw,"Error while fetching Users"))) as userError count(eval(match(_raw,"No User address Found"))) as addressError

vishal_pcap
Explorer

This works! 🙂 Thank you very much,  I have also figured out one more way to do this: 

| eval errorType=case(
   match(_raw, "Error while fetching Users"), "Error while fetching Users",
   match(_raw, "No User address Found"), "No User address Found"
) | stats count by errorType | table errorType, count

This gives me the table structure I wanted, the error message and count.  

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...

Design, Compete, Win: Submit Your Best Splunk Dashboards for a .conf26 Pass

Hello Splunkers,  We’re excited to kick off a Splunk Dashboard contest! We know that dashboards are a primary ...

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...