Repository navigation
feat(metrics): measure source CPU usage - #27168
gwenaskell wants to merge 3 commits into
Conversation
Adds measure_cpu_usage to sources. The source task, its build step and its output pumps carry the component counter, and the tasks sources spawn for connections and background work inherit it.
9a12bd0 to
e741257
Compare
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e741257af3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Docker and exec leave Vector-owned parsing and decoding workers outside the new CPU counter, causing source CPU usage to be underreported when measurement is enabled.
🤖 Bits Code Review · Commit e741257
There was a problem hiding this comment.
More details
The PR adds opt-in source CPU measurement across source tasks, output pumps, and selected workers, with documented exclusions for third-party execution. Docker and exec workers now inherit the source counter, resolving the earlier underreporting finding; no findings remain open.
🤖 Bits Code Review · Commit 1316fb4
bruceg
left a comment
There was a problem hiding this comment.
One substantial question about the added test, otherwise LGTM
| |s| s, | ||
| ) | ||
| .await; | ||
| assert_component_cpu_counter_positive(&metrics, &format!("{id}_source"), has_source_tags); |
There was a problem hiding this comment.
This test still passes if spawn_timed stops charging subtasks to the source. assert_component_cpu_counter_positive only checks > 0, and the source task's own polls (the select! loop and send_event) already add some nanoseconds to the counter. The doc comment above says the 1 ms burns in the spawned tasks are charged here, but nothing asserts it. component_cpu_usage_includes_sink_healthcheck_tasks pins the same kind of claim with a lower bound, so the source test could do the same: the 100 burns total 100 ms, which dwarfs what the source task spends itself. I think that bound could be flaky on a heavily loaded CI box, since busy_spin spins on wall-clock time, so a fraction of it would still dwarf the source's own polls. Did I miss another assertion that covers this?
The healthcheck test would then be a second copy of the min_ns block, so a shared helper looks worthwhile:
| assert_component_cpu_counter_positive(&metrics, &format!("{id}_source"), has_source_tags); | |
| assert_component_cpu_counter_at_least( | |
| &metrics, | |
| &format!("{id}_source"), | |
| has_source_tags, | |
| EVENT_COUNT as f64 * CPU_BURN_MS as f64 * 1e6, | |
| ); |
with assert_component_cpu_counter_at_least(metrics, id, has_tags, min_ns) built from the component_cpu_counter call and assert!(value >= min_ns, ...) in the healthcheck test, which would call it too.
| /// Defaults to `false`. Enable only for sources where CPU attribution is needed, as it adds a | ||
| /// `clock_gettime` call on every future poll. | ||
| /// | ||
| /// CPU spent on threads or tasks that a third-party library manages on its own is not |
There was a problem hiding this comment.
nit (TIOLI): this exclusion list names only third-party threads, but I think windows_event_log also has Vector-owned work outside the counter. with_subscription_blocking runs EvtNext/EvtRender in tokio::task::spawn_blocking, and that closure is not a future, so inherit_cpu_timer cannot wrap it. Rendering events is real CPU, and the option docs say the tasks a source spawns are counted. Did I miss a path that covers it? If not, a follow-up that times the closure, or a sentence here such as "the windows_event_log source renders events on blocking threads that are not counted", would keep the docs accurate.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c04043c8be
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| let span = info_span!("file_server"); | ||
| // Created here: the blocking thread does not carry the source's CPU counter. | ||
| let file_server = vector_lib::cpu_time::inherit_cpu_timer(file_server.run( |
There was a problem hiding this comment.
Include checkpoint persistence in file-source CPU accounting
With measure_cpu_usage: true on a file or kubernetes_logs source, checkpoint persistence remains outside the counter. Wrapping FileServer::run does not propagate measurement through lib/file-source/src/file_server.rs's spawn_in_current_span(checkpoint_writer(...)), and Checkpointer::write_checkpoints additionally serializes the checkpoint state inside an untimed spawn_blocking closure. For sources watching many actively changing files, this recurring state collection and JSON serialization can contribute substantial CPU that the new metric silently omits. Use the timed spawn helpers at both boundaries so checkpoint work is attributed to its source.
Useful? React with 👍 / 👎.
Summary
Extends the per-component
component_cpu_usage_ns_totalmetric (#25317) to sources, including the tasks they spawn.Follow-up to #27097 (sink CPU usage).
measure_cpu_usageoption. The source task is wrapped withcpu_timed, itsbuild()runs undercpu_scoped, and the per-output pumps (fanout to downstream components) are timed with the same counter, as transforms already count their fanout.http_server,splunk_hec,datadog_agent,opentelemetryHTTP,aws_kinesis_firehose,heroku_logs, ...) serve throughcrate::http::serve, whose connections already inherit the counter since feat(metrics): measure sink CPU usage #27097. UDP sources do their work in the source task.util::net::tcp(socket,syslog,fluent,logstash,statsd),util::unix_streamandutil::framestream(dnstap,socketunix)aws_sqs/aws_s3SQS workers and ack handlers,gcp_pubsubstreams,journald,kubernetes_logsreflectors,postgresql_metricsconnections,splunk_hecacks,windows_event_logkafkapartition consumers (JoinSet::spawn, throughinherit_cpu_timer, now public)fileandkubernetes_logsfile server: it runs on a blocking thread, so its future is wrapped beforespawn_blockingNot covered
vectorandopentelemetrygRPC sources:tonic's server spawns connections with a hardcodedTokioExecutor, and decompression and request handling run there. Covering them means serving thetonicroutes throughcrate::http::serve; left for a follow-up.kafkasource decompression and network I/O)docker_logsthrough bollard,kubewatchers internals,tonicclient connections ofgcp_pubsub)spawn_blockingcalls (host_metricsprocfs reads) and thestdinreader threadReferences
Related: #25317
Depends on: #27097
Vector configuration
How did you test this PR?
component_cpu_usage_emitted_source: the mock basic source burns CPU in aspawn_timedsubtask; the no-measure test also checks the source.cargo testoncpu_metricsand the unit tests of the touched sources; fullmake check-clippy.Does this PR include user facing changes?
no-changeloglabel to this PR.Contributor Guidelines
@vectordotdev/vectorto reach out to us regarding this PR.git merge origin masterandgit push.