You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apl/aggregation-function/rate.mdx
+57-39Lines changed: 57 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -17,13 +17,13 @@ If you come from other query languages, this section explains how to adjust your
17
17
In Splunk SPL, the equivalent of the `rate` function can be achieved using the `timechart` command with a `per_second` option or by calculating the difference between successive values over time. In APL, the `rate` function simplifies this process by directly calculating the rate over a specified time interval.
18
18
19
19
<CodeGroup>
20
-
```splunk
21
-
| timechart per_second count by status
20
+
```splunk Splunk example
21
+
| timechart per_second count by resp_body_size_bytes
22
22
```
23
23
24
-
```kusto
24
+
```kusto APL equivalent
25
25
['sample-http-logs']
26
-
| summarize rate=count() by status, bin(_time, 1s)
26
+
| summarize rate(resp_body_size_bytes) by bin(_time, 1s)
27
27
```
28
28
</CodeGroup>
29
29
@@ -33,15 +33,14 @@ In Splunk SPL, the equivalent of the `rate` function can be achieved using the `
33
33
In ANSI SQL, calculating rates typically involves using window functions like `LAG` or `LEAD` to calculate the difference between successive rows in a time series. In APL, the `rate` function abstracts this complexity by allowing you to directly compute the rate over time without needing window functions.
34
34
35
35
<CodeGroup>
36
-
```sql
37
-
SELECT status, COUNT(*) / TIMESTAMPDIFF(SECOND, MIN(_time), MAX(_time)) AS rate
38
-
FROM http_logs
39
-
GROUP BY status;
36
+
```sql SQL example
37
+
SELECT resp_body_size_bytes, COUNT(*) / TIMESTAMPDIFF(SECOND, MIN(_time), MAX(_time)) AS rate
38
+
FROM http_logs;
40
39
```
41
40
42
-
```kusto
41
+
```kusto APL equivalent
43
42
['sample-http-logs']
44
-
| summarize rate=count() by status, bin(_time, 1s)
43
+
| summarize rate(resp_body_size_bytes) by bin(_time, 1s)
45
44
```
46
45
</CodeGroup>
47
46
@@ -53,88 +52,107 @@ GROUP BY status;
53
52
### Syntax
54
53
55
54
```kusto
56
-
rate(field, timeInterval)
55
+
rate(field)
57
56
```
58
57
59
58
### Parameters
60
59
61
-
-`field`: The numeric field that you want to calculate the rate for.
62
-
-`timeInterval`: The time interval (e.g., 1s, 1m, 1h) over which to calculate the rate.
60
+
-`field`: The numeric field for which you want to calculate the rate.
63
61
64
62
### Returns
65
63
66
-
Returns the rate of change or occurrence of the specified `field` over the specified `timeInterval`.
64
+
Returns the rate of change or occurrence of the specified `field` over the time interval specified in the query.
65
+
66
+
Specify the time interval in the query in the following way:
67
+
68
+
-`| summarize rate(field)` calculates the rate value of the field over the entire query window.
69
+
-`| summarize rate(field) by bin(_time, 1h)` calculates the rate value of the field over a one-hour time window.
70
+
-`| summarize rate(field) by bin_auto(_time)` calculates the rate value of the field bucketed by an automatic time window computed by `bin_auto()`.
71
+
72
+
<Tip>
73
+
74
+
Use two `summarize` statements to visualize the average rate over one minute per hour. For example:
75
+
76
+
```kusto
77
+
['sample-http-logs']
78
+
| summarize respBodyRate = rate(resp_body_size_bytes) by bin(_time, 1m)
79
+
| summarize avg(respBodyRate) by bin(_time, 1h)
80
+
```
81
+
82
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20summarize%20respBodyRate%20%3D%20rate(resp_body_size_bytes)%20by%20bin(_time%2C%201m)%20%7C%20summarize%20avg(respBodyRate)%20by%20bin(_time%2C%201h)%22%2C%20%22queryOptions%22%3A%7B%22quickRange%22%3A%226h%22%7D%7D)
83
+
84
+
</Tip>
67
85
68
86
## Use case examples
69
87
70
88
<Tabs>
71
89
<Tabtitle="Log analysis">
72
90
73
-
In this example, the `rate` aggregation calculates the rate of HTTP requests per second grouped by status.
91
+
In this example, the `rate` aggregation calculates the rate of HTTP response sizes per second.
74
92
75
93
**Query**
76
94
77
95
```kusto
78
96
['sample-http-logs']
79
-
| summarize rate=count() by status, bin(_time, 1s)
97
+
| summarize rate(resp_body_size_bytes) by bin(_time, 1s)
80
98
```
81
99
82
-
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B%27sample-http-logs%27%5D%20%7C%20summarize%20rate%3Dcount%28%29%20by%20status%2C%20bin%28_time%2C%201s%29%22%7D)
100
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20summarize%20rate(resp_body_size_bytes)%20by%20bin(_time%2C%201s)%22%7D)
83
101
84
102
**Output**
85
103
86
-
|status |rate |_time |
87
-
|--------|-------|--------------------|
88
-
|200 | 15| 2024-01-01 12:00:00|
89
-
|404 | 3 | 2024-01-01 12:00:00|
104
+
| rate |_time |
105
+
|-------|--------------------|
106
+
|854 kB| 2024-01-01 12:00:00|
107
+
|635 kB| 2024-01-01 12:00:01|
90
108
91
-
This query counts the number of requests per status code and calculates the rate of requests per second.
109
+
This query calculates the rate of HTTP response sizes per second.
92
110
93
111
</Tab>
94
112
<Tabtitle="OpenTelemetry traces">
95
113
96
-
This example calculates the rate of traces received per second for different services.
114
+
This example calculates the rate of span duration per second.
97
115
98
116
**Query**
99
117
100
118
```kusto
101
119
['otel-demo-traces']
102
-
| summarize rate=count() by ['service.name'], bin(_time, 1s)
120
+
| summarize rate(toint(duration)) by bin(_time, 1s)
103
121
```
104
122
105
-
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B%27otel-demo-traces%27%5D%20%7C%20summarize%20rate%3Dcount%28%29%20by%20%5B%27service.name%27%5D%2C%20bin%28_time%2C%201s%29%22%7D)
123
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B'otel-demo-traces'%5D%20%7C%20summarize%20rate(toint(duration))%20by%20bin(_time%2C%201s)%22%7D)
This query calculates the rate of traces per second for each service.
132
+
This query calculates the rate of span duration per second.
115
133
116
134
</Tab>
117
135
<Tabtitle="Security logs">
118
136
119
-
In this example, the `rate` aggregation calculates the rate of security events by HTTP status.
137
+
In this example, the `rate` aggregation calculates the rate of HTTP request duration per second which can be useful to detect an increate in malicious requests.
120
138
121
139
**Query**
122
140
123
141
```kusto
124
142
['sample-http-logs']
125
-
| summarize rate=count() by status, bin(_time, 1s)
143
+
| summarize rate(req_duration_ms) by bin(_time, 1s)
126
144
```
127
145
128
-
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B%27sample-http-logs%27%5D%20%7C%20summarize%20rate%3Dcount%28%29%20by%20status%2C%20bin%28_time%2C%201s%29%22%7D)
146
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20summarize%20rate(req_duration_ms)%20by%20bin(_time%2C%201s)%22%7D)
129
147
130
148
**Output**
131
149
132
-
|status |rate |_time |
133
-
|--------|-------|--------------------|
134
-
|401 | 8| 2024-01-01 12:00:00|
135
-
|403 | 2 | 2024-01-01 12:00:00|
150
+
| rate |_time |
151
+
|-------|--------------------|
152
+
|240.668 ms| 2024-01-01 12:00:00|
153
+
|264.17 ms| 2024-01-01 12:00:01|
136
154
137
-
This query calculates the rate of different security-related status codes over time.
155
+
This query calculates the rate of HTTP request duration per second.
138
156
139
157
</Tab>
140
158
</Tabs>
@@ -145,4 +163,4 @@ This query calculates the rate of different security-related status codes over t
145
163
-[**sum**](/apl/aggregation-function/sum): Returns the sum of values in a field. Use `sum` when you want to aggregate the total value, not its rate of change.
146
164
-[**avg**](/apl/aggregation-function/avg): Returns the average value of a field. Use `avg` when you want to know the mean value rather than how it changes over time.
147
165
-[**max**](/apl/aggregation-function/max): Returns the maximum value of a field. Use `max` when you need to find the peak value instead of how often or quickly something occurs.
148
-
-[**min**](/apl/aggregation-function/min): Returns the minimum value of a field. Use `min` when you're looking for the lowest value rather than a rate.
166
+
-[**min**](/apl/aggregation-function/min): Returns the minimum value of a field. Use `min` when you’re looking for the lowest value rather than a rate.
| Field Name |**string**| The name of the field to be reordered in the output. |
21
+
|[direction]|**string**| Optional. Specifies the sort order for the reordered fields. Can be one of: `asc`, `desc`, `granny-asc`, or `granny-desc`. `asc` or `desc` orders fields by field name in ascending or descending manner, respectively. `granny-asc` or `granny-desc` orders by ascending or descending, respectively, while secondarily sorting by the next numeric value. For example, `b50` comes before `b9` when granny-asc is specified.|
22
+
23
+
## Returns
24
+
25
+
A table with the specified fields reordered as requested, followed by any unspecified fields in their original order. `project-reorder` doesn‘t rename or remove fields from the dataset, therefore, all fields that existed in the dataset, appear in the result table.
26
+
27
+
## Examples
28
+
29
+
Reorder all fields in ascending order:
30
+
31
+
```kusto
32
+
['sample-http-logs']
33
+
| project-reorder * asc
34
+
```
35
+
36
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%20%22%5B%27sample-http-logs%27%5D%5Cn%7C%20project-reorder%20%2A%20asc%22%7D)
37
+
38
+
Reorder specific fields to the beginning:
39
+
40
+
```kusto
41
+
['sample-http-logs']
42
+
| project-reorder method, status, uri
43
+
```
44
+
45
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%20%22%5B%27sample-http-logs%27%5D%5Cn%7C%20project-reorder%20method%2C%20status%2C%20uri%22%7D)
46
+
47
+
Reorder fields using wildcards and sort in descending order:
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%20%22%5B%27github-push-event%27%5D%5Cn%7C%20project-reorder%20repo%2A%2C%20num_commits%2C%20push_id%2C%20ref%2C%20size%2C%20%5B%27id%27%5D%2C%20size_large%20desc%22%7D)
55
+
56
+
Reorder specific fields and keep others in original order:
57
+
58
+
```kusto
59
+
['otel-demo-traces']
60
+
| project-reorder trace_id, *, span_id // orders the trace_id then everything else, then span_id fields
61
+
```
62
+
63
+
[Run in Playground](https://play.axiom.co/axiom-play-qf1k/explorer?initForm=%7B%22apl%22%3A%20%22%5B%27otel-demo-traces%27%5D%5Cn%7C%20project-reorder%20trace_id%2C%20%2A%2C%20span_id%22%7D)
0 commit comments