pipeline-info: increase safety of transforming stats (#13345) (#13850)

When a pipeline isn't fully initialized, we run the risk of attempting to
format pipeline info that isn't yet fully-shaped. By using safe-fallback
methods like `Hash#dig` and conditional-chaining, we can avoid the spurious
`NoMethodError` caused by sending `[]` to nil.

(cherry picked from commit e9455ca81e)
This commit is contained in:
Ry Biesemeyer 2022-03-07 11:02:18 -08:00 committed by GitHub
parent f0fa13d188
commit 5d0ad6ea38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,9 +21,9 @@ module LogStash; module Config;
# It is important that we iterate via the agent's pipelines vs. the # It is important that we iterate via the agent's pipelines vs. the
# metrics pipelines. This prevents race conditions as pipeline stats may be # metrics pipelines. This prevents race conditions as pipeline stats may be
# populated before the agent has it in its own pipelines state # populated before the agent has it in its own pipelines state
stats = metric_store.get_with_path("/stats/pipelines")[:stats][:pipelines] stats = metric_store.get_with_path("/stats/pipelines").dig(:stats, :pipelines) || {}
agent.running_pipelines.map do |pipeline_id, pipeline| agent.running_pipelines.map do |pipeline_id, pipeline|
p_stats = stats[pipeline_id] p_stats = stats.fetch(pipeline_id) { Hash.new }
# Don't record stats for system pipelines # Don't record stats for system pipelines
next nil if pipeline.system? next nil if pipeline.system?
# Don't emit stats for pipelines that have not yet registered any metrics # Don't emit stats for pipelines that have not yet registered any metrics