wgengine/magicsock,types/logger: add latency logs for initial peer contacts (#19916)

In order to allow us to measure the performance effects of client-side netmap
caching, both with and without the feature enabled, add logs to record how long
it takes after a client restart or profile switch for the node to establish
contact with peers, relative to the first uncached netmap.

We do this by keeping track of a timestamp when the connection is constructed,
and logging a record for "new" peer contacts that records how long (in
microseconds) it took from the time the peer was recorded as a candidate.  The
message includes whether the contact was via DERP or direct, and whether a
cached netmap was in use at the time.

This builds on and extends the counters from #19699, but here we include new
contacts whether or not a cached netmap is in use, so that we can establish a
baseline for comparison.

Updates #12639
Updates tailscale/projects#27

Change-Id: I4f6d050e221f3881848d05a0425c4a5d1a59294c
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
This commit is contained in:
M. J. Fromberger
2026-06-02 07:34:17 -07:00
committed by GitHub
parent c898aeb0d8
commit a3bec699dc
3 changed files with 46 additions and 15 deletions
+12 -5
View File
@@ -139,11 +139,18 @@ func (de *endpoint) setBestAddrLocked(v addrQuality) {
if v.epAddr != de.bestAddr.epAddr {
de.probeUDPLifetime.resetCycleEndpointLocked()
// Reaching here, if we are using data from a cached netmap and we are
// upgrading from an invalid (missing) address to a valid one, increment
// the counter for peers established.
if !de.bestAddr.ap.IsValid() && v.ap.IsValid() && de.c.usingCachedNetmap.Load() {
metricCachedPeerContactDirect.Add(1)
// Reaching here, if we are upgrading from an invalid (missing) address
// to a valid one, record metrics:
if !de.bestAddr.ap.IsValid() && v.ap.IsValid() {
// If we are using data from a cached netmap, increment the counter for peers established.
isCached := de.c.usingCachedNetmap.Load()
if isCached {
metricCachedPeerContactDirect.Add(1)
}
// Regardless whether the netmap is cached, record how long it has
// been since the endpoint was initialized.
de.c.logf("magicsock: new contact: peer=%s usec=%d cached=%v via=direct",
de.publicKey.ShortString(), int64(mono.Since(de.c.initializedAt)/time.Microsecond), isCached)
}
}
de.bestAddr = v