Skip to content

Commit e0acfe7

Browse files
andseldonoghuc
andauthored
Exposes average batch metrics at 1, 5 and 15 minutes time window. (#18460)
Updates stats API response to expose also 1m, 5m and 15m average batch metrics. Changed the response map returned by refine_batch_metrics method as result of API query to _node/stats so tha contains the average values of last 1, 5 and 15 minutes for event_count and batch_size. These data is published once they are available from the metric collector. Co-authored-by: Cas Donoghue <[email protected]>
1 parent cfa4fb9 commit e0acfe7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

docs/static/spec/openapi/logstash-api.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,16 @@ paths:
821821
current: 78
822822
average:
823823
lifetime: 115
824+
last_1_minute: 120
825+
last_5_minutes: 110
826+
last_15_minutes: 112
824827
byte_size:
825828
current: 32767
826829
average:
827830
lifetime: 14820
831+
last_1_minute: 15234
832+
last_5_minutes: 14012
833+
last_15_minutes: 14567
828834
events:
829835
duration_in_millis: 365495
830836
in: 216610

logstash-core/lib/logstash/api/commands/stats.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,34 @@ def refine_batch_metrics(stats)
176176
# current is a tuple of [event_count, byte_size] store the reference locally to avoid repeatedly
177177
# reading and retrieve unrelated values
178178
current_data_point = stats[:batch][:current]
179-
{
179+
# FlowMetric (from stats[:batch][:event_count][:average]) returns a composite object containing lifetime/last_1_minute/etc values. In order to get the map of sub-metrics we must use `.value`.
180+
# See: https://github.com/elastic/logstash/blob/279171b79c1f3be5fc85e6e2e4092281e504a6f9/logstash-core/src/main/java/org/logstash/instrument/metrics/ExtendedFlowMetric.java#L89
181+
event_count_average_flow_metric = stats[:batch][:event_count][:average].value
182+
event_count_average_lifetime = event_count_average_flow_metric["lifetime"] ? event_count_average_flow_metric["lifetime"].round : 0
183+
byte_size_average_flow_metric = stats[:batch][:byte_size][:average].value
184+
byte_size_average_lifetime = byte_size_average_flow_metric["lifetime"] ? byte_size_average_flow_metric["lifetime"].round : 0
185+
result = {
180186
:event_count => {
181187
# current_data_point is an instance of org.logstash.instrument.metrics.gauge.LazyDelegatingGauge so need to invoke getValue() to obtain the actual value
182188
:current => current_data_point.value[0],
183189
:average => {
184-
# average return a FlowMetric which and we need to invoke getValue to obtain the map with metric details.
185-
:lifetime => stats[:batch][:event_count][:average].value["lifetime"] ? stats[:batch][:event_count][:average].value["lifetime"].round : 0
190+
:lifetime => event_count_average_lifetime
186191
}
187192
},
188193
:byte_size => {
189194
:current => current_data_point.value[1],
190195
:average => {
191-
:lifetime => stats[:batch][:byte_size][:average].value["lifetime"] ? stats[:batch][:byte_size][:average].value["lifetime"].round : 0
196+
:lifetime => byte_size_average_lifetime
192197
}
193198
}
194199
}
200+
# Enrich byte_size and event_count averages with the last 1, 5, 15 minutes averages if available
201+
[:last_1_minute, :last_5_minutes, :last_15_minutes].each do |window|
202+
key = window.to_s
203+
result[:event_count][:average][window] = event_count_average_flow_metric[key]&.round if event_count_average_flow_metric[key]
204+
result[:byte_size][:average][window] = byte_size_average_flow_metric[key]&.round if byte_size_average_flow_metric[key]
205+
end
206+
result
195207
end
196208
private :refine_batch_metrics
197209

0 commit comments

Comments
 (0)