Skip to content

feat(egress): add shared kernel drop telemetry core - #3094

Merged
Pangjiping merged 3 commits into
opensandbox-group:mainfrom
hpliStartAgain:feat/egress-drop-telemetry
Nov 26, 2026
Merged

Pangjiping merged 3 commits into
opensandbox-group:mainfrom
hpliStartAgain:feat/egress-drop-telemetry

Conversation

@hpliStartAgain

@hpliStartAgain hpliStartAgain commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #3085

Part 1 of 3 of kernel drop telemetry. Base: main at 3ac0ff4f9; there is no preceding telemetry PR. This is the reduced scope of #3094.

This adds the shared drop-telemetry package, definitions for drop metric instruments, dependency notices, and library-level tests. Drop instruments are not registered by runtime initialization in this part; an explicit RegisterDropMetrics API is exercised by library tests and wired only in part 2. It provides default-off script instrumentation, named-counter delta/reset accounting, NFLOG capability probing and a bounded receiver, and header-only TCP/UDP parsing. The environment parser remains a library API: this PR does not wire telemetry into either runtime profile, so setting the new variables does not enable telemetry, and enforcement behavior is unchanged. Sidecar integration, server allowlisting and usage docs follow in part 2; Fast Sandbox follows in part 3.

Configuration has three environment inputs: OPENSANDBOX_EGRESS_DROP_TELEMETRY, OPENSANDBOX_EGRESS_DROP_LOG_RATE, and OPENSANDBOX_EGRESS_DROP_COUNTER_POLL. NFLOG group is fixed at 3085 per network namespace, and the per-rule burst is derived as clamp(2 × N, 1, 1000) from the rate count N; the default remains 40 and 1/day gives 2. Direct Config overrides remain available to library fixtures.

Library kernel tests use a minimal standalone ruleset and do not depend on either profile renderer. Privileged CI runs packet/count/drop and live-parent receiver-close checks in private network namespaces, with an explicit NFLOG-unavailable skip reason.

Validation: Go1.26.11 full gofmt has no output; vet/build and droptelemetry/telemetry unit and race tests pass, including nonempty shared-attribute environments, closed labels with bidirectional reason-set equality and a copied read-only accessor, transient-error recovery and idempotent replacement release. The unchanged 26-test hermetic runner suite passed in the prior follow-up; it was not rerun for this configuration-only change. Both Linux private-netns kernel tests pass with -race and no skip. This part is +1760/-4 across 20 files.

Dependencies: go-nflog/v2 v2.3.0 → mdlayher/netlink → mdlayher/socket (MIT); original notices are copied into the image. Existing x/sys/x/net/x/sync selections are reused; go-cmp is test-only (BSD-3-Clause). No per-part binary-size measurement or container build was performed.

Accounting and samples are best effort. Shutdown orders the library Add before Close/Wait when a callback is observed; the one-second timeout fallback has no such happens-before guarantee, and deferred unbind may log a harmless error after Close. The implementation retains the reviewed behavior and limitations without a dependency upgrade.

Previously identified file-level overlap with still-open #3020: .github/workflows/egress-test.yml and components/egress/scripts/test_ci_privileged_go_test.py. No enforcement-anchor logic is included.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 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-25T03:50:39.865745Z 65ea860 PR opened
ℹ️ 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.

@github-actions github-actions Bot added component/egress component/server documentation Improvements or additions to documentation size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 25, 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: 65ea860dcf

ℹ️ 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 on lines +129 to +130
readCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
r.pollLocked(readCtx)

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 Read counters before finalizing lease-sensitive transactions

With counter or sample enabled during a table replacement, this synchronous read occurs after nftables.prepareStaticApplyLocked() or Fast Sandbox's writeRemainingLeasesFragment() has finalized the remaining DNS-lease timeouts. If the read delays submission past a lease's expiry, the wrapper still submits that lease with its old positive timeout, reintroducing an expired allow grant. The existing expiry checks cannot catch this because there is no re-planning after the read. Collect counters before lease planning, or revalidate and render the lease-sensitive batch after the wait, and add a regression covering expiry during counter collection.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 1d22240. Moved counter collection ahead of lease planning in both profiles, including sidecar replacement/removal and Fast Sandbox reset, first install, subject removal, and reload. The telemetry lock is held from the pre-planning read through submission, preventing periodic polling from delaying an already-planned transaction while preserving the successful-replacement baseline reset. Added counter/sample regressions for both profiles that expire a lease during counter collection and verify that expired elements are omitted and surviving timeouts use the post-read clock, plus a regression for polling exclusion during replacement.

@hpliStartAgain
hpliStartAgain force-pushed the feat/egress-drop-telemetry branch from 01bb45c to fb68d29 Compare November 25, 2026 05:29
@hpliStartAgain hpliStartAgain changed the title feat(egress): add opt-in kernel drop telemetry feat(egress): add shared kernel drop telemetry core Nov 25, 2026
@github-actions github-actions Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 25, 2026
@hpliStartAgain

Copy link
Copy Markdown
Collaborator Author

The original change (+2370) exceeded the 2000-line review limit in CONTRIBUTING.md, so I split it into three stacked parts. This PR now carries part 1 only: the shared drop-telemetry library, metrics and dependencies, with nothing wired into either profile.

  • Part 2: sidecar wiring, server allowlist and docs (feat/egress-drop-telemetry-sidecar, +715/-5)
  • Part 3: Fast Sandbox wiring (feat/egress-drop-telemetry-fast-sandbox, +474/-28)

Stacked together, the three parts are byte-identical to the previously reviewed head 01bb45c4d. I'll open each follow-up after the previous one merges. The lease-planning fix from the earlier review thread lives in the profile wiring, so it lands in parts 2 and 3.

@hpliStartAgain

Copy link
Copy Markdown
Collaborator Author

An additional review of fb68d29 raised one medium and three low findings. All of them are fixed in 34a57ae; parts 2 and 3 are rebased on top of it.

Medium – drop metrics lost the shared attributes

  • egress.blocked.packets, egress.blocked.samples and egress.drop_telemetry.sampler_up now go through egressMetricOptWith, including the gauge observation, so sandbox_id and OPENSANDBOX_EGRESS_METRICS_EXTRA_ATTRS are kept. All three have descriptions.
  • New test: run in a subprocess with both env vars set and assert the shared attributes survive.
  • This part no longer registers the metrics. It only exposes telemetry.RegisterDropMetrics, so merging it leaves the exported metric set unchanged. Part 2 calls it when it wires up sidecars and adds the three rows to docs/guides/telemetry.md.

Low – label values were not a closed set

  • reason is normalized to the drop-reason set and cause to bind | ruleset | receive | disabled | "". Anything else is exported as unknown, following normalizeCredentialTransition.
  • telemetry.DropReasons() returns a copy of the accepted reason set. A cross-package test asserts that it equals the droptelemetry reason set in both directions, so neither side can drift.

Low – sampler_up stuck at 0 after a transient receive error

  • A valid sample after a transient error sets sampler_up back to 1. It stays 0 only once the receiver has stopped or its context was cancelled.
  • All reads and writes of Mode now happen under r.mu.
  • The release returned by BeginReplacement is wrapped in sync.OnceFunc, so a second call is a no-op instead of a panic.

Low – the privileged CI script lacked runner tests

  • ci-nft-go-test.sh now ties an NFLOG_UNAVAILABLE skip to the RUN section of the test that emitted it. A skip reason from another test, a missing PASS, or a plain skip is rejected.
  • test_ci_privileged_go_test.py gains tests for the script branch and the workflow wiring, plus negative controls.

Part 2 changes only to register the metrics and add the docs rows; part 3 changes only by the rebase. Apart from these fixes, the full stack is still identical to the previously reviewed 01bb45c4d.

@Pangjiping Pangjiping self-assigned this Nov 25, 2026

@Pangjiping Pangjiping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two questions on the new drop-telemetry env vars, inline on config.go.

@Pangjiping Pangjiping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@Pangjiping
Pangjiping merged commit 067a138 into opensandbox-group:main Nov 26, 2026
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/egress component/server documentation Improvements or additions to documentation ok-to-test size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants