-
Notifications
You must be signed in to change notification settings - Fork 928
chore: add additional network telemetry stats & events #13800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6ee3bfa
chore: add additional network telemetry stats & events
ethanndickson 6e0ec17
better connectionage
ethanndickson ae17bb3
send update events only when home derp changes
ethanndickson 612ae5f
better remote node id
ethanndickson d2d9c39
use netmap callback
ethanndickson af36c37
fixup
ethanndickson 3466ba6
fixup
ethanndickson dbffec4
test
ethanndickson b240507
set p2p setup
ethanndickson e2e4018
fixup
ethanndickson 0bebad4
watch for conn changes
ethanndickson 6a19849
fixup
ethanndickson 11f2a2b
use latest p2p setup time
ethanndickson fe7252a
ensure latest derpmap
ethanndickson 4b76942
nicer ping peer
ethanndickson 6214e2a
fix race
ethanndickson 60e88f7
relay -> udp addr
ethanndickson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"tailscale.com/types/key" | ||
tslogger "tailscale.com/types/logger" | ||
"tailscale.com/types/netlogtype" | ||
"tailscale.com/types/netmap" | ||
"tailscale.com/wgengine" | ||
"tailscale.com/wgengine/capture" | ||
"tailscale.com/wgengine/magicsock" | ||
|
@@ -262,17 +263,8 @@ func NewConn(options *Options) (conn *Conn, err error) { | |
) | ||
nodeUp.setAddresses(options.Addresses) | ||
nodeUp.setBlockEndpoints(options.BlockEndpoints) | ||
wireguardEngine.SetStatusCallback(nodeUp.setStatus) | ||
magicConn.SetDERPForcedWebsocketCallback(nodeUp.setDERPForcedWebsocket) | ||
if telemetryStore != nil { | ||
wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) { | ||
nodeUp.setNetInfo(ni) | ||
telemetryStore.setNetInfo(ni) | ||
}) | ||
} else { | ||
wireguardEngine.SetNetInfoCallback(nodeUp.setNetInfo) | ||
} | ||
|
||
ctx, ctxCancel := context.WithCancel(context.Background()) | ||
server := &Conn{ | ||
id: uuid.New(), | ||
closed: make(chan struct{}), | ||
|
@@ -290,13 +282,32 @@ func NewConn(options *Options) (conn *Conn, err error) { | |
configMaps: cfgMaps, | ||
nodeUpdater: nodeUp, | ||
telemetrySink: options.TelemetrySink, | ||
telemeteryStore: telemetryStore, | ||
telemetryStore: telemetryStore, | ||
createdAt: time.Now(), | ||
watchCtx: ctx, | ||
watchCancel: ctxCancel, | ||
} | ||
defer func() { | ||
if err != nil { | ||
_ = server.Close() | ||
} | ||
}() | ||
if server.telemetryStore != nil { | ||
server.wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) { | ||
server.telemetryStore.setNetInfo(ni) | ||
nodeUp.setNetInfo(ni) | ||
server.telemetryStore.pingPeer(server) | ||
}) | ||
server.wireguardEngine.AddNetworkMapCallback(func(nm *netmap.NetworkMap) { | ||
server.telemetryStore.updateNetworkMap(nm) | ||
server.telemetryStore.pingPeer(server) | ||
}) | ||
go server.watchConnChange() | ||
} else { | ||
server.wireguardEngine.SetNetInfoCallback(nodeUp.setNetInfo) | ||
} | ||
server.wireguardEngine.SetStatusCallback(nodeUp.setStatus) | ||
server.magicConn.SetDERPForcedWebsocketCallback(nodeUp.setDERPForcedWebsocket) | ||
|
||
netStack.GetTCPHandlerForFlow = server.forwardTCP | ||
|
||
|
@@ -351,11 +362,15 @@ type Conn struct { | |
wireguardEngine wgengine.Engine | ||
listeners map[listenKey]*listener | ||
clientType proto.TelemetryEvent_ClientType | ||
createdAt time.Time | ||
|
||
telemetrySink TelemetrySink | ||
// telemeteryStore will be nil if telemetrySink is nil. | ||
telemeteryStore *TelemetryStore | ||
telemetryWg sync.WaitGroup | ||
// telemetryStore will be nil if telemetrySink is nil. | ||
telemetryStore *TelemetryStore | ||
telemetryWg sync.WaitGroup | ||
|
||
watchCtx context.Context | ||
watchCancel func() | ||
|
||
trafficStats *connstats.Statistics | ||
} | ||
|
@@ -390,8 +405,8 @@ func (c *Conn) SetNodeCallback(callback func(node *Node)) { | |
|
||
// SetDERPMap updates the DERPMap of a connection. | ||
func (c *Conn) SetDERPMap(derpMap *tailcfg.DERPMap) { | ||
if c.configMaps.setDERPMap(derpMap) && c.telemeteryStore != nil { | ||
c.telemeteryStore.updateDerpMap(derpMap) | ||
if c.configMaps.setDERPMap(derpMap) && c.telemetryStore != nil { | ||
c.telemetryStore.updateDerpMap(derpMap) | ||
} | ||
} | ||
|
||
|
@@ -540,6 +555,7 @@ func (c *Conn) Closed() <-chan struct{} { | |
// Close shuts down the Wireguard connection. | ||
func (c *Conn) Close() error { | ||
c.logger.Info(context.Background(), "closing tailnet Conn") | ||
c.watchCancel() | ||
c.telemetryWg.Wait() | ||
c.configMaps.close() | ||
c.nodeUpdater.close() | ||
|
@@ -709,54 +725,34 @@ func (c *Conn) MagicsockServeHTTPDebug(w http.ResponseWriter, r *http.Request) { | |
c.magicConn.ServeHTTPDebug(w, r) | ||
} | ||
|
||
// SendConnectedTelemetry should be called when connection to a peer with the given IP is established. | ||
func (c *Conn) SendConnectedTelemetry(ip netip.Addr, application string) { | ||
if c.telemetrySink == nil { | ||
return | ||
} | ||
c.telemetryStore.markConnected(&ip, application) | ||
e := c.newTelemetryEvent() | ||
e.Status = proto.TelemetryEvent_CONNECTED | ||
e.Application = application | ||
pip, ok := c.wireguardEngine.PeerForIP(ip) | ||
if ok { | ||
e.NodeIdRemote = uint64(pip.Node.ID) | ||
} | ||
c.telemetryWg.Add(1) | ||
go func() { | ||
defer c.telemetryWg.Done() | ||
c.telemetrySink.SendTelemetryEvent(e) | ||
}() | ||
c.sendTelemetryBackground(e) | ||
} | ||
|
||
func (c *Conn) SendDisconnectedTelemetry(ip netip.Addr, application string) { | ||
func (c *Conn) SendDisconnectedTelemetry() { | ||
if c.telemetrySink == nil { | ||
return | ||
} | ||
e := c.newTelemetryEvent() | ||
e.Status = proto.TelemetryEvent_DISCONNECTED | ||
e.Application = application | ||
pip, ok := c.wireguardEngine.PeerForIP(ip) | ||
if ok { | ||
e.NodeIdRemote = uint64(pip.Node.ID) | ||
} | ||
c.telemetryWg.Add(1) | ||
go func() { | ||
defer c.telemetryWg.Done() | ||
c.telemetrySink.SendTelemetryEvent(e) | ||
}() | ||
c.sendTelemetryBackground(e) | ||
} | ||
|
||
func (c *Conn) SendSpeedtestTelemetry(throughputMbits float64) { | ||
if c.telemetrySink == nil { | ||
return | ||
} | ||
e := c.newTelemetryEvent() | ||
e.Status = proto.TelemetryEvent_CONNECTED | ||
e.ThroughputMbits = wrapperspb.Float(float32(throughputMbits)) | ||
c.telemetryWg.Add(1) | ||
go func() { | ||
defer c.telemetryWg.Done() | ||
c.telemetrySink.SendTelemetryEvent(e) | ||
}() | ||
e.Status = proto.TelemetryEvent_CONNECTED | ||
c.sendTelemetryBackground(e) | ||
} | ||
|
||
// nolint:revive | ||
|
@@ -769,31 +765,59 @@ func (c *Conn) sendPingTelemetry(pr *ipnstate.PingResult) { | |
latency := durationpb.New(time.Duration(pr.LatencySeconds * float64(time.Second))) | ||
if pr.Endpoint != "" { | ||
e.P2PLatency = latency | ||
e.P2PEndpoint = c.telemeteryStore.toEndpoint(pr.Endpoint) | ||
e.P2PEndpoint = c.telemetryStore.toEndpoint(pr.Endpoint) | ||
} else { | ||
e.DerpLatency = latency | ||
} | ||
e.Status = proto.TelemetryEvent_CONNECTED | ||
c.telemetryWg.Add(1) | ||
go func() { | ||
defer c.telemetryWg.Done() | ||
c.telemetrySink.SendTelemetryEvent(e) | ||
}() | ||
c.sendTelemetryBackground(e) | ||
} | ||
|
||
// The returned telemetry event will not have it's status set. | ||
func (c *Conn) newTelemetryEvent() *proto.TelemetryEvent { | ||
// Infallible | ||
id, _ := c.id.MarshalBinary() | ||
event := c.telemeteryStore.newEvent() | ||
event := c.telemetryStore.newEvent() | ||
event.ClientType = c.clientType | ||
event.Id = id | ||
selfNode := c.Node() | ||
event.NodeIdSelf = uint64(selfNode.ID) | ||
event.HomeDerp = strconv.Itoa(selfNode.PreferredDERP) | ||
event.ConnectionAge = durationpb.New(time.Since(c.createdAt)) | ||
return event | ||
} | ||
|
||
func (c *Conn) sendTelemetryBackground(e *proto.TelemetryEvent) { | ||
c.telemetryWg.Add(1) | ||
go func() { | ||
defer c.telemetryWg.Done() | ||
c.telemetrySink.SendTelemetryEvent(e) | ||
}() | ||
} | ||
|
||
// Watch for changes in the connection type (P2P<->DERP) and send telemetry events. | ||
func (c *Conn) watchConnChange() { | ||
ticker := time.NewTicker(time.Millisecond * 50) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we have to manually check if the connection has changed like this, P2PSetup can be ~50ms off it's true value. |
||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-c.watchCtx.Done(): | ||
return | ||
case <-ticker.C: | ||
} | ||
status := c.Status() | ||
peers := status.Peers() | ||
if len(peers) > 1 { | ||
// Not a CLI<->agent connection, stop watching | ||
return | ||
} else if len(peers) == 0 { | ||
continue | ||
} | ||
peer := status.Peer[peers[0]] | ||
// If the connection type has changed, send a telemetry event with the latest ping stats | ||
if c.telemetryStore.changedConntype(peer.CurAddr) { | ||
c.telemetryStore.pingPeer(c) | ||
} | ||
} | ||
} | ||
|
||
// PeerDiagnostics is a checklist of human-readable conditions necessary to establish an encrypted | ||
// tunnel to a peer via a Conn | ||
type PeerDiagnostics struct { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this callback to ensure we always have the latest Tailscale node data for a given peer.