Time functions

earliest(<value>)

Description

Returns the chronologically earliest seen occurrence of a value in a field.

Usage

You can use this function with the chart, mstats, stats, timechart, and tstats commands.

This function processes field values as strings.

Basic example

This example uses the sample data from the Search Tutorial. To try this example on your own Splunk instance, you must download the sample data and follow the instructions to get the tutorial data into Splunk. Use the time range All time when you run the search.

You run the following search to locate invalid user login attempts against a sshd (Secure Shell Daemon). You use the table command to see the values in the _time, source, and _raw fields.

The results appear on the Statistics tab and look something like this:

You extend the search using the earliest function.

The search returns the event with the _time value 2023-04-28 00:23:28, which is the event with the oldest timestamp.

earliest_time(<value>)

Description

Returns the UNIX time of the chronologically earliest-seen occurrence of a given field value.

Usage

You can use this function with the mstats, stats, and tstats commands.

This function processes field values as strings.

If you have metrics data, you can use earliest_time function in conjunction with the earliest, latest, and latest_time functions to calculate the rate of increase for a counter. Alternatively you can use the rate counter to do the same thing.

Basic example

The following search runs against metric data. It is designed to return the earliest UNIX time values on every minute for each metric_name that begins with deploy.

The results appear on the Statistics tab and look something like this:

latest(<value>)

Description

Returns the chronologically latest seen occurrence of a value in a field.

Usage

You can use this function with the chart, mstats, stats, timechart, and tstats commands.

This function processes field values as strings.

Basic example

This example uses the sample data from the Search Tutorial. To try this example on your own Splunk instance, you must download the sample data and follow the instructions to get the tutorial data into Splunk. Use the time range All time when you run the search.

You run the following search to locate invalid user login attempts against a specific sshd (Secure Shell Daemon). You use the table command to see the values in the _time, source, and _raw fields.

The results appear on the Statistics tab and look something like this:

You extend the search using the latest function.

The search returns the event with the _time value 2023-05-01 00:15:05, which is the event with the most recent timestamp.

latest_time(<value>)

Description

Returns the UNIX time of the chronologically latest-seen occurrence of a given field value.

Usage

You can use this function with the mstats, stats, and tstats commands.

This function processes field values as strings.

If you have metrics data, you can use latest_time function in conjunction with earliest, latest, and earliest_time functions to calculate the rate of increase for a counter. Alternatively, you can use the rate function counter to do the same thing.

Basic example

The following search runs against metric data. It is designed to return the latest UNIX time values in the past 60 minutes for metrics with names that begin with queue..

The results appear on the Statistics tab and look something like this:

per_day(<value>)

Description

Returns the values in a field or eval expression for each day.

Usage

You can use this function with the timechart command.

Basic examples

The following example returns the values for the field total for each day.

The following example returns the results of the eval expression eval(method="GET")) AS Views .

Extended example

This example uses the sample dataset from the Search Tutorial but should work with any format of Apache Web access log. Download the data set from this topic in the Search Tutorial and follow the instructions to upload it to your Splunk deployment.

This search uses the per_day() function and eval expressions to determine how many times the web pages were viewed and how many times items were purchased. The results appear on the Statistics tab.

To determine the number of Views and Purchases for each hour, minute, or second you can add the other time functions to the search. For example:

This screen image shows the result of the search. There are five columns. The first column contains dates, based on the event timestamps. The next column shows the number of views each day. The third column shows the number of views for each hour. The fourth column shows the number of views for each minute. The last column shows the number of purchases for each day.

Use the field format option to change the number formatting for the field values.

per_hour(<value>)

Description

Returns the values in a field or eval expression for each hour.

Usage

You can use this function with the timechart command.

Basic examples

The following example returns the values for the field total for each hour.

The following example returns the the results of the eval expression eval(method="POST")) AS Views .

per_minute(<value>)

Description

Returns the values in a field or eval expression for each minute.

Usage

You can use this function with the timechart command.

Basic examples

The following example returns the values for the field total for each minute.

The following example returns the the results of the eval expression eval(method="GET")) AS Views .

per_second(<value>)

Description

Returns the values in a field or eval expression for each second.

Usage

You can use this function with the timechart command.

Basic examples

The following example returns the values for the field kb for each second.

rate(<value>)

Description

Returns the per-second rate change of the value in a field. The rate function represents the following formula:

(latest(<value>) - earliest(<value>)) / (latest_time(<value>) - earliest_time(<value>))

The rate function also handles the largest value reset if there is at least one reset.

Usage

You can use this function with the mstats, stats, and tstats commands.

  • Provides the per-second rate change for an accumulating counter metric. Accumulating counter metrics report the total counter value since the last counter reset. See Investigate counter metrics in Metrics
  • Requires the earliest and latest values of the field to be numerical, and the earliest_time and latest_time values to be different.
  • Requires at least two metric data points in the search time range.
  • Should be used to provide rate information about single, rather than multiple, counters.

Basic example

The following search runs against metric data. It provides the hourly hit rate for a metric that provides measurements of incoming web traffic. It uses the processor filter to ensure that it is not reporting on multiple metric series (name and processor combinations).

The resulting chart shows you that the counter hit rate for the traffic.incoming metric spiked at 1 pm, 4 pm, and 11 am, but otherwise remained stable.

An image of a line graph. It represents the hit rate for a metric titled traffic.incoming. It shows that the counter rate for the metric spiked at 1 pm, 4 pm, and 11 am.

rate_avg(<value>)

Description

Computes the per metric time series rates for an accumulating counter metric. Returns the averages of those rates.

For a detailed explanation of metric time series, see Perform statistical calculations on metric time series in Metrics.

Usage

You can use this function with the mstats command.

  • To ensure accurate results, Splunk software uses the latest value of a metric measurement from the previous timespan as the starting basis for a rate computation.
  • When you calculate the average rates for accumulating counter metrics, the cleanest way to do it is to split the counter metric rate calculations out by metric time series and then compute the average rate across all of the metric time series.
  • Unlike rate, the rate_avg function can calculate rates even when there is only a single metric data point per time series per timespan. It can pull in data across timespans to calculate rates when necessary.
  • The rate_avg function does not support prestats=true. It needs the final list of dimensions to split by.

Basic example

In your _metrics index, you have data for the metric spl.intr.resource_usage.PerProcess.data.elapsed. This is an accumulating counter metric. It contains a number of metric time series.

The following example search uses the rate_avg function to calculate the rate(X) for each spl.mlog.thruput.thruput.total_k_processed time series in the time range. Then it gets the average rate across all of the time series. Lastly, it splits the results by time, so they can be plotted on a chart.

rate_sum(<value>)

Description

Computes the per metric time series rates for an accumulating counter metric. Returns the aggregate of those rates.

For a detailed explanation of metric time series, see Perform statistical calculations on metric time series in Metrics.

Usage

You can use this function with the mstats command.

  • To ensure accurate results, Splunk software uses the latest value of a metric measurement from the previous timespan as the starting basis for a rate computation.
  • When you calculate the aggregated rates for accumulating counter metrics, the cleanest way to do it is to split the counter metric rate calculations out by metric time series and then compute the aggregate rate across all of the metric time series.
  • Unlike rate, the rate_sum function can calculate rates even when there is only a single metric data point per time series per timespan. It can pull in data across timespans to calculate rates when necessary.
  • The rate_sum function does not support prestats=true. It needs the final list of dimensions to split by.

Basic example

In your _metrics index, you have data for the metric spl.intr.resource_usage.PerProcess.data.elapsed. This is an accumulating counter metric. It contains a number of metric time series.

The following example search uses the rate_sum function to calculate the rate(X) for each spl.mlog.thruput.thruput.total_k_processed time series in the time range. Then it gets the aggregate rate across all of the time series. Lastly, it splits the results by time, so they can be plotted on a chart.