feat(timestamp-stack): add routing timestamp instrumentation - #30
Open
YuanYuYuan wants to merge 7 commits into
Open
feat(timestamp-stack): add routing timestamp instrumentation#30YuanYuYuan wants to merge 7 commits into
YuanYuYuan wants to merge 7 commits into
Conversation
YuanYuYuan
force-pushed
the
feat/routing-timestamps
branch
from
May 29, 2026 04:31
a764445 to
fc1e09c
Compare
This was referenced Jun 2, 2026
YuanYuYuan
force-pushed
the
feat/routing-timestamps
branch
from
June 9, 2026 10:34
9498eaa to
088aa2c
Compare
…pCallback The name SessionTimestampCallback better reflects the scope (session-level, registered once at open time) and matches the naming convention expected by the language bindings (Python, C).
Required by the language bindings (Python, C) which need to store and inspect TsStackContext values in callback wrappers.
…_ts_stack_timestamp Codec errors during HLC serialization are transient and non-fatal; panicking would crash the session. Log a warning and return an empty buffer so the intercepted message still gets delivered.
…lisherBuilder Allow callers to set a publisher-level default so every put/delete is instrumented without repeating the option at each call site. Per-put override via AdvancedPublicationBuilder::timestamp_instrumentation still takes precedence.
Add three examples demonstrating TsStack usage: - z_timestamp_instrumentation: basic Send/Route/Receive walkthrough - z_latency_collector: p50/p95/p99 per-hop latency stats - z_proprietary_token: custom 24-byte hardware-ns token via SessionTimestampCallback (data-layer moat pattern) Update all examples for the InstrumentationTimestamp enum API (UHLC/Custom variants) introduced in the ZS rebase.
YuanYuYuan
force-pushed
the
feat/routing-timestamps
branch
from
June 16, 2026 08:34
088aa2c to
fff0190
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in timestamp instrumentation for measuring end-to-end message latency in Zenoh. Messages can carry a
TsStackwire extension that accumulatesInterceptionrecords at up to three points along a message's path: Send, Route, and Receive.The feature is entirely
#[cfg(feature = "unstable")]-gated. Uninstrumented messages carry zero overhead —push_ts_interceptionis a no-op whenext_ts_stackisNone.Sister PRs
API Design
Rust (
zenohcrate,feature = "unstable")All public types live in
zenoh::timestamp_stack.Configuring instrumentation — choose which points to record per-message:
Session-level custom clock — registered once at open time, applied to every instrumented message:
SessionTimestampCallbackisArc<dyn Fn(TsStackContext) -> Vec<u8> + Send + Sync>. Returning an emptyVecskips stamping that point. When no callback is registered, Zenoh uses a lazily-initialized UHLC.Attaching instrumentation —
timestamp_instrumentation(Option<TimestampInstrumentation>)is available on:Reading timestamps off a received
Sample,ReplyError, orQuery:TimestampStack::records()returns records in wire order (Send → Route(s) → Receive).rec.is_custom()is a convenience predicate over the enum variant.AdvancedPublisher(zenoh-ext):Python (
zenoh-python)All types are importable directly from
zenoh:Configuring instrumentation:
Session-level custom clock:
Attaching instrumentation —
timestamp_instrumentation=kwarg on:Reading timestamps:
InterceptionPointis a pyclass enum with valuesSEND,ROUTE,RECEIVE,UNKNOWN.C (
zenoh-c,ZENOHC_BUILD_WITH_UNSTABLE_API=ON)Types:
z_owned_timestamp_instrumentation_t/z_loaned_timestamp_instrumentation_tz_interception_point_tZ_INTERCEPTION_POINT_SEND,Z_INTERCEPTION_POINT_ROUTE,Z_INTERCEPTION_POINT_RECEIVE,Z_INTERCEPTION_POINT_UNKNOWNz_loaned_timestamp_stack_tz_loaned_timestamp_stack_record_tz_owned_session_ts_callback_t/z_moved_session_ts_callback_tConfiguring instrumentation:
Session-level custom clock:
Attaching instrumentation —
timestamp_instrumentationfield on the relevant_options_tstruct:Reading timestamps:
Wire Protocol
A new
TsStackextension (ID0x7) is added toPush,Request, andResponsemessages. It carries:conf_flags— bitmask of enabled interception points (set by the sender, unchanged in transit)stack— orderedVec<Interception>, each with aflagsbyte (point ID +IS_CUSTOM_TSbit) and a length-prefixedtimestampbyte vectorThe codec bounds
timestampbytes tou16::MAX(65 535) and caps stack depth at 64 to prevent memory exhaustion from crafted wire input.Implementation Points
session.rs(resolve_put,resolve_get) andbuilders/reply.rsrouting/dispatcher/pubsub.rsper-subscriber (inside fan-out loop) andqueries.rsWeakSession::send_push_consumeandadminspace.rsRoute stamping happens inside the per-subscriber loop so each subscriber gets a timestamp that reflects queueing delay up to that point in fan-out.
Tests
zenoh/tests/timestamp_stack.rs: no-instrumentation baseline, single-point (Send / Receive), all-points ordering, custom callback byte verification, callback context correctness, per-message stack independence, query/reply flows,is_customflag, route-only instrumentation,ReplyErrorpropagation, multiple-subscriber fan-out, and publisher API pathzenoh/tests/timestamp_instrumentation.rs: full pub/sub lifecycle, multiple-session scenarios, callback interaction with instrumentation flags, and end-to-end latency measurement flowsAll 73 tests pass on Rust 1.93.0.
Breaking Changes
None. All new types and methods are behind
feature = "unstable".Robustness Notes
get_ts_stack_timestampno longer panics on UHLC serialization failure: theexpect()was replaced with atracing::warn!and graceful empty return.push_ts_interceptionusesdebug_assert!+ silent skip for unknown point IDs.u16::MAXbytes per timestamp to prevent memory exhaustion from crafted wire input.Known Limitations
QueryCleanuptimeout responses carryext_ts_stack: None— not instrumented (rationale documented in code)🏷️ Label-Based Checklist
Based on the labels applied to this PR, please complete these additional requirements:
Labels:
new feature🆕 New Feature Requirements
Since this PR adds a new feature:
examples/z_timestamp_instrumentation.rsaddedfeature = "unstable", zero overhead when inactivepush_ts_interceptionis a no-op whenext_ts_stackisNone)Consider: Can this feature be split into smaller, incremental PRs?
Instructions:
- [ ]to- [x])This checklist updates automatically when labels change, but preserves your checked boxes.