# The loudest error in a client's logs may be harmless

> Five Splunk queries separate failures that fixed themselves from transactions that were lost, which tend to sit in the groups few people check.

Original: https://fdetimes.net/en/tools/splunk-queries-real-errors-vs-noise/

Call `spath` without a `path` and it automatically extracts fields from only the first 5,000 characters of the data. An error code at the end of a long JSON payload can be missing from the results while still sitting intact in the log.

For an FDE chasing an integration fault at a client site, that mistake is expensive: you report "no errors found" when the query never read that far. Splunk, part of Cisco since March 2024, answers exactly what you ask. The skill to practice is framing questions so that each result rules out a hypothesis.

The standard reference is the Search Reference, a catalog of SPL commands with syntax and examples. You do not need to learn all of it. A handful of commands is enough to get from the complaint "orders are going missing" to a list of specific orders.

## Narrow first, then count by order

Say a client sends orders through a gateway to an ERP system. The gateway writes text logs, with `request_id=`, `status=` and `client_ip=` on every line; the ERP's responses carry the same `request_id` and are logged as JSON under a different source type. The client says some orders sent in the past 30 minutes never reached the ERP.

Splunk's optimization documentation advises filtering as early as possible, specifying the index, source or source type, using the smallest time window and avoiding wildcards. So set a 30-minute window and run:

```spl
index=integration sourcetype=order_gateway
| rex "status=(?<status>\d+)"
| stats count by status
```

`rex` turns each named group in the regular expression into a field of the same name; if no source field is specified, it reads `_raw`. The query deliberately does not filter on the word "error". You need to see successful sends too, to know which failures resolved themselves.

In this example the log has 1,000 lines: 950 with status 200, 44 with 503 and six with 400. Going by the totals, anyone would want to dive into the 503s. But this table counts log lines, and the client is asking about orders. The next step is to group by `request_id` and keep only the IDs that never received a 200:

```spl
index=integration sourcetype=order_gateway
| rex "request_id=(?<request_id>\S+)"
| rex "status=(?<status>\d+)"
| stats count(eval(status=200)) as ok, values(status) as statuses by request_id
| where ok=0
```

The `ok` column counts each ID's successes, while `statuses` shows which codes that ID encountered. In the example, the 44 lines with 503 belong to only 11 `request_id` values, four lines each, and all 11 IDs also have a line with 200. The `where` line removes every one of them.

Exactly six IDs remain, each with only a 400. Within the window examined, the 503 group was resent successfully, while those six orders had not gone through. The first thing to do is send the client those six IDs to check against the ERP.

The `ok=0` table has two blind spots. An order the gateway never logged cannot appear in it, so ask the client for a list of orders sent and compare. An ID near the edge of the 30-minute window may still be retrying, or may have a 200 line that falls outside the window; widen the window and run the query again before drawing a conclusion.

**Key point:** The number of error lines measures how noisy the log is; the number of failed transactions measures the damage to the client.

## Why did the ERP reject them?

Now turn to the ERP's responses, but only for those six orders. Put the six IDs directly in the first line to filter early. If you know where the error code sits in the JSON, specify the path so that you do not depend on the 5,000-character automatic extraction:

```spl
index=integration sourcetype=erp_response
  ("R-1041" OR "R-1077" OR "R-1102" OR "R-1130" OR "R-1158" OR "R-1193")
| spath path=request_id
| spath path=response.error.code output=error_code
| stats count, values(request_id) as request_ids by error_code
```

If all six responses carry the same code, for example one reporting a missing tax ID, you have a tidy hypothesis: that field is mapped incorrectly. If each response has a different code, open the events one by one.

If the table is empty, rerun only the filter at the top. If there are events but no error code, check the path and whether the JSON is well formed, because `spath` requires valid JSON; a timestamp prefix before the opening brace is the first thing to suspect.

The 5,000-character limit can be changed through `extraction_cutoff` in `limits.conf`. But that is configuration on the client's system, so pass a `path` before asking anyone to change a server.

## When IDs are reused, stats merges too much

The counting above relies on one condition: every order has its own ID. Splunk's documentation recommends `stats` when the grouping is defined by a field, and says explicitly that `transaction` is less efficient for computing aggregate statistics. If the log has only the client's IP address, one IP may span several sessions, and `stats` will blend them into one.

That is where `transaction` belongs: it can use time spans or pauses to separate sessions that share an identifier. The query below works only if the gateway log contains `client_ip=`; at a client site, check for that field first:

```spl
index=integration sourcetype=order_gateway
| rex "client_ip=(?<client_ip>\S+)"
| transaction client_ip maxspan=10m maxpause=1m
```

`maxspan` limits the total duration and `maxpause` limits the gap between two consecutive events. The results gain `duration` and `eventcount`, so a session that is unusually long or has only one event will stand out.

Input events must be in descending time order, so do not re-sort before this command. The 10-minute and one-minute values also need tuning to the rhythm of the real system.

## Group similar lines when you don't know what to look for

On your first day at a client site, you often do not know what the logs contain. `cluster` groups events to reveal which groups are common and which are rare. The example in Splunk's documentation filters on the word `error` before grouping. Here it is better to drop that filter, because a rejected order may not be logged with the word "error":

```spl
index=integration sourcetype=order_gateway
| cluster t=0.9 showcount=t
| sort - cluster_count
| head 20
```

This query puts the largest groups first, showing you what normal logs look like. Splunk treats anomalies as events that fall outside large groups or belong to groups with few events, so next switch to `sort cluster_count` to bring the small groups to the top. Rare failures such as the six rejected orders above are usually easier to spot in this order.

## No lines at all? Check the way in

If the application sends logs through the HTTP Event Collector (HEC), silence may mean the events never arrived. Look at the code HEC returns: code 4 (HTTP 403) is an invalid token, code 1 (403) a disabled token, code 7 (400) an incorrect index and code 9 (503) a busy server.

Two codes share HTTP 403 but lead to different actions: fix the token in the configuration, or ask the administrator why the token was disabled. On Splunk Enterprise, HEC errors are logged in `splunkd.log` and metrics are in the `_introspection` index. If you do not have access, ask the client's administrator for an extract covering the window you are investigating.

## Practice at home, with a story for the interview

Splunk Enterprise has a 60-day trial that requires no credit card and allows up to 500 MB of data a day. The Search Tutorial comes with data from a fictional online game store: web access logs, secure-format logs, sales logs and a CSV price list. That is enough to practice linking browsing behavior to sales.

Do not choose Splunk Free as a monitoring platform for a client. It too is limited to 500 MB a day, has no alerting, no users or roles, and blocks searches after three license warnings in a rolling 30-day window. It is better suited to a small set of logs for self-study.

When reading job ads, look for phrases such as "log analysis", "troubleshooting" or "production support", even where Splunk is not named. On your CV, instead of writing "knows SPL", describe one investigation on practice data: the initial hypothesis, the query that disproved it and the final conclusion. State clearly that it was sample data.

The next time a results table comes back empty, do not rush to write "no errors". Rerun the query against an error you know occurred; if that does not show up either, the query is the first thing to fix.

**Try this week:**

- Install the Splunk Enterprise trial, load the Search Tutorial data and rewrite the queries using rex, stats, transaction and cluster for the web access logs.
- Create a JSON document longer than 5,000 characters, put the field you want at the end and compare the results of spath with and without path.

## Sources

- [Welcome to the Search Reference](https://help.splunk.com/en/splunk-enterprise/spl-search-reference/10.0/introduction)

- [Cisco Completes Acquisition of Splunk](https://newsroom.cisco.com/c/r/newsroom/en/us/a/y2024/m03/cisco-completes-acquisition-of-splunk.html)

- [Splunk Enterprise | Splunk](https://www.splunk.com/en_us/products/splunk-enterprise.html)

- [What you need for this tutorial](https://help.splunk.com/en/splunk-enterprise/search/search-tutorial/10.2/part-1-getting-started)

- [About Splunk Free](https://help.splunk.com/en/splunk-enterprise/administer/admin-manual/10.4/configure-splunk-licenses/about-splunk-free)

- [Quick tips for optimization](https://help.splunk.com/en/splunk-enterprise/search/search-manual/10.4/optimize-searches/quick-tips-for-optimization)

- [rex | Splunk Enterprise](https://help.splunk.com/en/splunk-enterprise/search/spl-search-reference/10.0/search-commands/rex)

- [spath | Splunk Enterprise](https://help.splunk.com/en/splunk-enterprise/spl-search-reference/10.0/search-commands/spath)

- [About transactions | Splunk Enterprise](https://help.splunk.com/en/splunk-enterprise/manage-knowledge-objects/knowledge-management-manual/9.4/transactions)

- [About transactions](https://help.splunk.com/en/splunk-enterprise/search/search-manual/10.4/group-and-correlate-events/about-transactions)

- [transaction | Splunk Enterprise](https://help.splunk.com/en/splunk-enterprise/search/spl-search-reference/10.0/search-commands/transaction)

- [cluster](https://help.splunk.com/en/splunk-enterprise/search/spl-search-reference/10.2/search-commands/cluster)

- [Troubleshoot HTTP Event Collector](https://help.splunk.com/en/splunk-enterprise/get-started/get-data-in/10.4/get-data-with-http-event-collector/troubleshoot-http-event-collector)
