Skip to content

feat(metrics): measure source CPU usage - #27168

Open
gwenaskell wants to merge 3 commits into
masterfrom
yoenn.burban/source-cpu-usage
Open

gwenaskell wants to merge 3 commits into
masterfrom
yoenn.burban/source-cpu-usage

Conversation

@gwenaskell

@gwenaskell gwenaskell commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extends the per-component component_cpu_usage_ns_total metric (#25317) to sources, including the tasks they spawn.

Follow-up to #27097 (sink CPU usage).

  • Sources: new measure_cpu_usage option. The source task is wrapped with cpu_timed, its build() runs under cpu_scoped, and the per-output pumps (fanout to downstream components) are timed with the same counter, as transforms already count their fanout.
  • Covered without changes: HTTP sources (http_server, splunk_hec, datadog_agent, opentelemetry HTTP, aws_kinesis_firehose, heroku_logs, ...) serve through crate::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.
  • Spawn sites switched to inherit the counter:
    • per-connection tasks of util::net::tcp (socket, syslog, fluent, logstash, statsd), util::unix_stream and util::framestream (dnstap, socket unix)
    • per-source background tasks: aws_sqs / aws_s3 SQS workers and ack handlers, gcp_pubsub streams, journald, kubernetes_logs reflectors, postgresql_metrics connections, splunk_hec acks, windows_event_log
    • kafka partition consumers (JoinSet::spawn, through inherit_cpu_timer, now public)
    • file and kubernetes_logs file server: it runs on a blocking thread, so its future is wrapped before spawn_blocking

Not covered

  • vector and opentelemetry gRPC sources: tonic's server spawns connections with a hardcoded TokioExecutor, and decompression and request handling run there. Covering them means serving the tonic routes through crate::http::serve; left for a follow-up.
  • librdkafka native threads (kafka source decompression and network I/O)
  • tasks spawned by third-party clients (docker_logs through bollard, kube watchers internals, tonic client connections of gcp_pubsub)
  • short spawn_blocking calls (host_metrics procfs reads) and the stdin reader thread

References

Related: #25317
Depends on: #27097

Vector configuration

sources:
  in:
    type: socket
    mode: tcp
    address: 0.0.0.0:9000
    decoding:
      codec: json
    measure_cpu_usage: true

How did you test this PR?

  • New topology test component_cpu_usage_emitted_source: the mock basic source burns CPU in a spawn_timed subtask; the no-measure test also checks the source.
  • cargo test on cpu_metrics and the unit tests of the touched sources; full make check-clippy.

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on our guidelines.
  • No. A maintainer will apply the no-changelog label to this PR.

Contributor Guidelines

  • Please read our Vector contributor resources.
  • Do not hesitate to use @vectordotdev/vector to reach out to us regarding this PR.
  • Before pushing, follow our pre-push guidance.
  • After a review is requested, please avoid force pushes to help us review incrementally.
    • Feel free to push as many commits as you want. They will be squashed into one before merging.
    • For example, you can run git merge origin master and git push.

@github-actions github-actions Bot added docs review on hold The documentation team reviews PRs only after a PR is approved by the COSE team. domain:topology Anything related to Vector's topology code domain:sources Anything related to Vector's sources domain:external docs Anything related to Vector's external, public documentation labels Nov 13, 2026
@pront pront added the meta:awaiting author Pull requests that are awaiting their author. label Nov 19, 2026
Base automatically changed from yoenn.burban/sink-cpu-usage to master November 23, 2026 10:44
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.
@gwenaskell
gwenaskell force-pushed the yoenn.burban/source-cpu-usage branch from 9a12bd0 to e741257 Compare November 23, 2026 14:54
@gwenaskell
gwenaskell marked this pull request as ready for review November 23, 2026 16:01
@gwenaskell
gwenaskell requested review from a team as code owners November 23, 2026 16:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T09:54:43.771832Z c04043c New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-official datadog-official Bot added domain:internal-observability Anything related to monitoring/observing Vector domain:metrics Anything related to Vector's metrics events platform:kubernetes Anything `kubernetes` platform related source:aws_s3 Anything `aws_s3` source related source:aws_sqs Anything `aws_sqs` source related source:file Anything `file` source related source:gcp_pubsub Anything `gcp_pubsub` source related source:journald Anything `journald` source related source:kafka Anything `kafka` source related source:kubernetes_logs Anything `kubernetes_logs` source related source:postgresql_metrics Anything `postgresql_metrics` source related source:splunk_hec Anything `splunk_hec` source related source:windows_event_log labels Nov 23, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/config/source.rs Resolved

@datadoghq-integration datadoghq-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

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.

Open Bits AI session

🤖 Bits Code Review · Commit e741257

Comment thread src/topology/builder.rs Resolved

@datadoghq-integration datadoghq-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

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.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 1316fb4

@gwenaskell
gwenaskell requested a review from bruceg November 23, 2026 18:12

@bruceg bruceg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One substantial question about the added test, otherwise LGTM

Comment thread src/topology/test/cpu_metrics.rs Outdated
|s| s,
)
.await;
assert_component_cpu_counter_positive(&metrics, &format!("{id}_source"), has_source_tags);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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.

Comment thread src/config/source.rs
/// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/sources/file.rs

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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs review on hold The documentation team reviews PRs only after a PR is approved by the COSE team. domain:external docs Anything related to Vector's external, public documentation domain:internal-observability Anything related to monitoring/observing Vector domain:metrics Anything related to Vector's metrics events domain:sources Anything related to Vector's sources domain:topology Anything related to Vector's topology code meta:awaiting author Pull requests that are awaiting their author. platform:kubernetes Anything `kubernetes` platform related source:aws_s3 Anything `aws_s3` source related source:aws_sqs Anything `aws_sqs` source related source:file Anything `file` source related source:gcp_pubsub Anything `gcp_pubsub` source related source:journald Anything `journald` source related source:kafka Anything `kafka` source related source:kubernetes_logs Anything `kubernetes_logs` source related source:postgresql_metrics Anything `postgresql_metrics` source related source:splunk_hec Anything `splunk_hec` source related source:windows_event_log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants