Skip to content

Commit 612ae5f

Browse files
committed
better remote node id
1 parent ae17bb3 commit 612ae5f

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

cli/ssh.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/coder/coder/v2/codersdk/workspacesdk"
3838
"github.com/coder/coder/v2/cryptorand"
3939
"github.com/coder/coder/v2/pty"
40-
"github.com/coder/coder/v2/tailnet"
4140
"github.com/coder/retry"
4241
"github.com/coder/serpent"
4342
)
@@ -438,7 +437,7 @@ func (r *RootCmd) ssh() *serpent.Command {
438437
}
439438

440439
err = sshSession.Wait()
441-
conn.SendDisconnectedTelemetry(tailnet.TelemetryApplicationSSH)
440+
conn.SendDisconnectedTelemetry()
442441
if err != nil {
443442
if exitErr := (&gossh.ExitError{}); errors.As(err, &exitErr) {
444443
// Clear the error since it's not useful beyond

codersdk/workspacesdk/agentconn.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,3 @@ func (c *AgentConn) apiClient() *http.Client {
380380
func (c *AgentConn) GetPeerDiagnostics() tailnet.PeerDiagnostics {
381381
return c.Conn.GetPeerDiagnostics(c.opts.AgentID)
382382
}
383-
384-
func (c *AgentConn) SendDisconnectedTelemetry(application string) {
385-
c.Conn.SendDisconnectedTelemetry(c.agentAddress(), application)
386-
}

tailnet/conn.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -728,14 +728,10 @@ func (c *Conn) SendConnectedTelemetry(ip netip.Addr, application string) {
728728
if c.telemetrySink == nil {
729729
return
730730
}
731-
c.telemetryStore.markConnected(time.Since(c.createdAt))
731+
c.telemetryStore.markConnected(&ip, c.createdAt, application)
732+
c.telemetryStore.updateRemoteNodeID(c.wireguardEngine)
732733
e := c.newTelemetryEvent()
733734
e.Status = proto.TelemetryEvent_CONNECTED
734-
e.Application = application
735-
pip, ok := c.wireguardEngine.PeerForIP(ip)
736-
if ok {
737-
e.NodeIdRemote = uint64(pip.Node.ID)
738-
}
739735
c.telemetryWg.Add(1)
740736
go func() {
741737
defer c.telemetryWg.Done()
@@ -749,6 +745,7 @@ func (c *Conn) sendUpdatedTelemetry() {
749745
if c.telemetrySink == nil {
750746
return
751747
}
748+
c.telemetryStore.updateRemoteNodeID(c.wireguardEngine)
752749
e := c.newTelemetryEvent()
753750
e.Status = proto.TelemetryEvent_CONNECTED
754751
c.telemetryWg.Add(1)
@@ -758,17 +755,13 @@ func (c *Conn) sendUpdatedTelemetry() {
758755
}()
759756
}
760757

761-
func (c *Conn) SendDisconnectedTelemetry(ip netip.Addr, application string) {
758+
func (c *Conn) SendDisconnectedTelemetry() {
762759
if c.telemetrySink == nil {
763760
return
764761
}
765762
e := c.newTelemetryEvent()
766763
e.Status = proto.TelemetryEvent_DISCONNECTED
767-
e.Application = application
768-
pip, ok := c.wireguardEngine.PeerForIP(ip)
769-
if ok {
770-
e.NodeIdRemote = uint64(pip.Node.ID)
771-
}
764+
c.telemetryStore.updateRemoteNodeID(c.wireguardEngine)
772765
c.telemetryWg.Add(1)
773766
go func() {
774767
defer c.telemetryWg.Done()
@@ -781,8 +774,8 @@ func (c *Conn) SendSpeedtestTelemetry(throughputMbits float64) {
781774
return
782775
}
783776
e := c.newTelemetryEvent()
784-
e.Status = proto.TelemetryEvent_CONNECTED
785777
e.ThroughputMbits = wrapperspb.Float(float32(throughputMbits))
778+
e.Status = proto.TelemetryEvent_CONNECTED
786779
c.telemetryWg.Add(1)
787780
go func() {
788781
defer c.telemetryWg.Done()

tailnet/telemetry.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"google.golang.org/protobuf/types/known/timestamppb"
1313
"google.golang.org/protobuf/types/known/wrapperspb"
1414
"tailscale.com/tailcfg"
15+
"tailscale.com/wgengine"
1516

1617
"github.com/coder/coder/v2/cryptorand"
1718
"github.com/coder/coder/v2/tailnet/proto"
@@ -33,8 +34,13 @@ type TelemetryStore struct {
3334
cleanNetCheck *proto.Netcheck
3435
nodeID uint64
3536
homeDerp int32
37+
application string
3638

39+
// nil if not connected
3740
connSetupTime *durationpb.Duration
41+
connectedIP *netip.Addr
42+
// 0 if not connected
43+
connectedNodeID uint64
3844
}
3945

4046
func newTelemetryStore() (*TelemetryStore, error) {
@@ -58,19 +64,37 @@ func (b *TelemetryStore) newEvent() *proto.TelemetryEvent {
5864
DerpMap: DERPMapToProto(b.cleanDerpMap),
5965
LatestNetcheck: b.cleanNetCheck,
6066
NodeIdSelf: b.nodeID,
67+
NodeIdRemote: b.connectedNodeID,
6168
HomeDerp: b.homeDerp,
6269
ConnectionSetup: b.connSetupTime,
70+
Application: b.application,
6371

6472
// TODO(ethanndickson):
6573
P2PSetup: &durationpb.Duration{},
6674
}
6775
}
6876

69-
func (b *TelemetryStore) markConnected(connSetupTime time.Duration) {
77+
func (b *TelemetryStore) markConnected(ip *netip.Addr, connCreatedAt time.Time, application string) {
7078
b.mu.Lock()
7179
defer b.mu.Unlock()
7280

73-
b.connSetupTime = durationpb.New(connSetupTime)
81+
b.connSetupTime = durationpb.New(time.Since(connCreatedAt))
82+
b.connectedIP = ip
83+
b.application = application
84+
}
85+
86+
func (b *TelemetryStore) updateRemoteNodeID(engine wgengine.Engine) {
87+
b.mu.Lock()
88+
defer b.mu.Unlock()
89+
90+
if b.connectedIP == nil {
91+
return
92+
}
93+
94+
pip, ok := engine.PeerForIP(*b.connectedIP)
95+
if ok {
96+
b.connectedNodeID = uint64(pip.Node.ID)
97+
}
7498
}
7599

76100
// Given a DERPMap, anonymise all IPs and hostnames.
@@ -97,7 +121,7 @@ func (b *TelemetryStore) updateDerpMap(cur *tailcfg.DERPMap) {
97121
b.cleanDerpMap = cleanMap
98122
}
99123

100-
// Update the telemetry store with the current node state.
124+
// Update the telemetry store with the current self node state.
101125
// Returns true if the home DERP has changed.
102126
func (b *TelemetryStore) updateByNode(n *Node) bool {
103127
b.mu.Lock()

0 commit comments

Comments
 (0)