Skip to content

feat(tracker-client): add monitor udp command to tracker_checker - #1767

Merged
josecelano merged 21 commits into
torrust:developfrom
josecelano:1178-tracker-checker-udp-add-monitor-uptime-command
May 12, 2026
Merged

josecelano merged 21 commits into
torrust:developfrom
josecelano:1178-tracker-checker-udp-add-monitor-uptime-command

Conversation

@josecelano

@josecelano josecelano commented May 12, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add tracker_checker monitor udp command in tracker client
  • emit NDJSON probe events to stderr and final JSON summary to stdout
  • add support for --url, --interval, --timeout, --duration, and --info-hash
  • implement timeout/error handling and monitor statistics (total, timeouts, timeout_percent, latency fields)
  • update issue/spec and refactor-plan docs, including explicit deferral of success-path mock UDP integration test to the planned tracker-client repository split
  • refactor tracker_checker integration tests into two concern modules: configuration and monitor

Validation

  • cargo test -p torrust-tracker-client --test tracker_checker
  • linter all

Notes

  • Item 14 from the post-implementation plan is intentionally deferred, documented in the issue and closed refactor plan.

Usage Example

cargo run -p torrust-tracker-client --bin tracker_checker -- monitor udp \
  --url udp://udp1.torrust-tracker-demo.com:6969/announce \
  --interval 10 \
  --timeout 10 \
  --duration 60

The command emits per-probe NDJSON events to stderr and a final JSON summary to stdout.

Example Output

cargo run --quiet -p torrust-tracker-client --bin tracker_checker -- monitor udp --url udp://udp1.torrust-tracker-demo.com:6969/announce --interval 10 --timeout 10 --duration 60 2>/dev/null | jq
{
  "udp_trackers": [
    {
      "url": "udp://udp1.torrust-tracker-demo.com:6969/announce",
      "status": {
        "code": "ok",
        "message": "monitor completed",
        "stats": {
          "total": 6,
          "timeouts": 0,
          "timeout_percent": 0,
          "min_ms": 128,
          "max_ms": 145,
          "average_ms": 136,
          "last_ms": 137
        }
      }
    }
  ]
}

josecelano added 20 commits May 12, 2026 18:18
- Fix timeout_percent sample value: 33.3 -> 33 (integer, matches implementation)
- Add --info-hash row to CLI Options table
- Tick all completed Goals and Workflow Checkpoints
- Add unit test: all latency fields null when every probe times out
- Update refactor plan to mark items 1-4 as done
@josecelano
josecelano requested a review from a team as a code owner May 12, 2026 17:49
@josecelano josecelano self-assigned this May 12, 2026
@josecelano
josecelano requested a review from Copilot May 12, 2026 17:49
@josecelano josecelano linked an issue May 12, 2026 that may be closed by this pull request

Copilot AI 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.

Pull request overview

Adds a new tracker_checker monitor udp subcommand to the tracker client, emitting per-probe NDJSON events on stderr and a final JSON summary on stdout, and updates planning/docs around refactor plans and the Issue #1178 spec.

Changes:

  • Implement monitor udp command (CLI parsing + monitoring loop + JSON output + stats).
  • Refactor tracker_checker integration tests into separate configuration vs monitor modules.
  • Add refactor-plan documentation structure (template, lifecycle READMEs, skill doc) and update Issue #1178 spec/refactor plan docs.

Reviewed changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
project-words.txt Adds millis to the project word list.
docs/templates/REFACTOR-PLAN.md Introduces a refactor-plan document template.
docs/refactor-plans/open/README.md Documents lifecycle/usage for open refactor plans.
docs/refactor-plans/drafts/README.md Documents lifecycle/usage for draft refactor plans.
docs/refactor-plans/closed/README.md Documents lifecycle/usage for closed refactor plans.
docs/refactor-plans/closed/agent-docs-refactor-plan.md Adds a closed refactor plan documenting prior agent-doc changes.
docs/refactor-plans/closed/1178-monitor-udp-post-implementation-improvements.md Captures post-implementation improvements and explicit deferrals for monitor-udp work.
docs/issues/open/1178-tracker-checker-udp-add-monitor-uptime-command.md Updates the Issue #1178 spec with final behavior notes, options, verification, and risks.
console/tracker-client/tests/tracker_checker/monitor.rs Adds integration test coverage for monitor udp output/exit code (timeout-only sink).
console/tracker-client/tests/tracker_checker/configuration.rs Moves configuration error contract tests into a focused module.
console/tracker-client/tests/tracker_checker.rs Reorganizes integration tests into module files via #[path = ...].
console/tracker-client/src/console/clients/checker/monitor/udp.rs Implements the UDP monitor runtime, stats aggregation, and JSON emission.
console/tracker-client/src/console/clients/checker/monitor/mod.rs Exposes the new monitor module.
console/tracker-client/src/console/clients/checker/mod.rs Wires monitor module into checker client module tree.
console/tracker-client/src/console/clients/checker/app.rs Adds clap subcommand parsing for monitor udp and dispatches to monitor runner.
.github/skills/dev/planning/create-refactor-plan/SKILL.md Adds a planning skill describing how to write and manage refactor plans.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/templates/REFACTOR-PLAN.md Resolved
@codecov

codecov Bot commented May 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.53226% with 83 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.00%. Comparing base (244c94e) to head (f76c733).

Files with missing lines Patch % Lines
...-client/src/console/clients/checker/monitor/udp.rs 65.38% 64 Missing and 8 partials ⚠️
.../tracker-client/src/console/clients/checker/app.rs 72.50% 7 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1767      +/-   ##
===========================================
- Coverage    79.04%   79.00%   -0.05%     
===========================================
  Files          370      371       +1     
  Lines        27682    27928     +246     
  Branches     27682    27928     +246     
===========================================
+ Hits         21882    22065     +183     
- Misses        5517     5568      +51     
- Partials       283      295      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano

Copy link
Copy Markdown
Member Author

ACK f76c733

@josecelano
josecelano merged commit 8970b90 into torrust:develop May 12, 2026
17 of 18 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a post-merge review of the change from #1767 as it stands at e170b9620 on the default branch.

What still runs: the probe loop, the statistics and the output types in console/tracker-client/src/console/clients/checker/monitor/udp.rs survive almost unchanged, and since 36607df they write through unified::output instead of print macros. The monitor udp argument parsing and dispatch that the change added to checker/app.rs now live in console/tracker-client/src/console/clients/unified/check.rs (added by 1ee546e), reached as tracker_client check -- monitor udp; checker/app.rs and the tracker_checker binary no longer exist. The split of the checker integration tests into tests/tracker_checker/configuration.rs and tests/tracker_checker/monitor.rs survives, and the monitor tests now also cover the success path against a fake UDP tracker, which settles the deferral the description noted.

[Major] An announce that the tracker refuses is counted as a successful probe. console/tracker-client/src/console/clients/checker/monitor/udp.rs:286 maps any Ok(_response) to ProbeOutcome::Ok, and Client::send_announce_request returns whatever response parses (console/tracker-client/src/console/clients/udp/checker.rs:154-160), including Response::Error. The UDP server answers a refused announce with an error response (packages/udp-server/src/handlers/mod.rs:109-129), for example when it runs in listed mode and the probed info-hash is not whitelisted. Against such a tracker every probe is written as "status":"ok" with an elapsed_ms, enters min/max/average, and the summary reports timeouts: 0: the monitor reports full health for a tracker that rejects every announce. A duplicated connect response read in place of the announce response is counted the same way. The connect exchange already treats a response of the wrong type as an error (console/tracker-client/src/console/clients/udp/checker.rs:95-98); the announce exchange does not. Smallest fix: in run_probe, count only Response::AnnounceIpv4(_) | Response::AnnounceIpv6(_) as Ok, and turn Response::Error (with the tracker's message) and any other response type into ProbeOutcome::Error.

[Minor] The monitor writes the tracker URL unmasked. console/tracker-client/src/console/clients/checker/monitor/udp.rs:137 and :163 take config.url.to_string() for the url of every probe event on stderr and of the summary on stdout, and the resolution errors at :308 and :313 embed it again. Since 72e1c5e the check command writes the same kind of URL through redacted_entry (console/tracker-client/src/console/clients/checker/checks/udp.rs:42-44, console/tracker-client/src/console/clients/checker/config.rs:175-208) because its path can hold a key, and the tracker CLI I/O contract says that wherever a tracker URL is written its key is written as **** (console/tracker-client/docs/contracts/tracker-cli-io-contract.md:48-55). With --url udp://tracker.example:6969/announce/SECRET the key appears on every probe line and in the summary of a command whose default run lasts a day with its stderr captured. The HTTP key the contract names cannot reach this command, whose URL must use udp, so the exposure needs a tracker that takes a key in a UDP URL path; that limits the rating to Minor. Smallest fix: compute redacted_entry(config.url.as_str()) once in run_monitor and use it for the events, the summary and the error text, keeping config.url for the network.

[Minor] --timeout bounds each socket wait, not the probe. console/tracker-client/src/console/clients/checker/monitor/udp.rs:256 passes config.timeout to Client::new, and UdpClient applies it separately to bind, connect, the writable wait, send, the readable wait and receive (packages/tracker-client/src/udp/client.rs:89, :112, :134, :139, :166, :171). A probe makes two request/response exchanges, so with --timeout 10 a tracker that answers the connect request after 9 s and the announce after 9 s gives "status":"ok","elapsed_ms":18000, while the flag is documented as the probe timeout (console/tracker-client/src/console/clients/unified/check.rs:64) and the issue spec says a probe with no response within --timeout is a timeout (docs/issues/closed/1178-tracker-checker-udp-add-monitor-uptime-command/ISSUE.md:119, :131). Smallest fix: wrap the connect-and-announce sequence of run_probe in one tokio::time::timeout(config.timeout, …) and map its expiry to ProbeOutcome::Timeout.

[Nit] Name resolution blocks the probe outside the timeout. resolve_socket_addr (console/tracker-client/src/console/clients/checker/monitor/udp.rs:305-314) calls the blocking Url::socket_addrs inside the async probe on every iteration. When the resolver stalls, each probe waits for the system resolver's own timeout whatever --timeout says, and a Ctrl+C pressed meanwhile takes effect only after the lookup returns, because the select! at :176-181 cannot poll its signal branch while the probe future is blocked in that call. Smallest fix: resolve with tokio::net::lookup_host on the URL's host and port, inside the same per-probe timeout.

[Nit] A Ctrl+C can be lost between the two waits. Each select! creates a fresh tokio::signal::ctrl_c() (console/tracker-client/src/console/clients/checker/monitor/udp.rs:177, :230). Once the first one exists the process no longer ends on SIGINT, and a listener created later does not see a signal delivered before it existed, so a Ctrl+C that arrives while no listener is alive, for example while a probe event is being written to a stalled stderr consumer, is dropped and the monitor keeps running. An error from registering the listener is also taken as an interrupt. Smallest fix: create one ctrl_c future before the loop, pin it, and select on a mutable reference to it in both places.

[Nit] The delay between probes is added after each probe, so the probe rate falls while the tracker is down. console/tracker-client/src/console/clients/checker/monitor/udp.rs:226-227 sleeps interval after the probe ends, so a timed-out probe lengthens its cycle by the timeout. With --interval 10 --timeout 10, a tracker that is down for half of a run is probed every 20 s while down and about every 10 s while up, so timeout_percent comes out near 33 rather than 50. This meets the acceptance criterion that --interval controls the delay between probes, but the spec's background asks for an announce at a fixed interval (docs/issues/closed/1178-tracker-checker-udp-add-monitor-uptime-command/ISSUE.md:39), which is what an uptime percentage needs. Smallest fix: sleep until started_at + sequence × interval, capped by the remaining duration, instead of sleeping for interval.

[Nit] The monitor ignores --format. run returns into run_command before it uses output_format (console/tracker-client/src/console/clients/unified/check.rs:84-86), so tracker_client check --format text -- monitor udp … writes the compact summary, while the contract says --format text pretty-prints the check result (console/tracker-client/docs/contracts/tracker-cli-io-contract.md:34-35). Smallest fix: pass output_format to run_command and render the summary with output::render_json(&output, output_format.is_pretty()).

Checked and found sound: the --url validation (scheme udp, an explicit port, and an empty host fails to parse), the lower bounds on --interval, --timeout and --duration, the statistics arithmetic (average over successful probes only, timeout_percent guarded against zero probes, no reachable overflow), the mapping of the client's timeout variants to timeout, the stop checks before each probe and before each sleep, the exit codes (0 for a completed or interrupted run, 1 when the summary cannot be written), the one-record-per-line stderr output, and the configuration and monitor integration tests, which drive tracker_client check -- and assert both output channels.

This branch was previously deployed

1 inactive deployment
coverage — f76c7331 Deployed May 12, 2026 by josecelano via Generate Coverage Report #1259
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracker Checker (UDP): add command to monitor uptime

3 participants