|
|
|
@ -16,29 +16,28 @@ func metadataLoop(d *DeviceManager) {
|
|
|
|
|
|
|
|
|
|
func syncMetrics(d *DeviceManager) {
|
|
|
|
|
logrus.Debug("metadata sync executing")
|
|
|
|
|
devices, err := d.ListAllDevices()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Warn(errors.Wrap(err, "failed to list devices - metrics cannot be recorded"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peers, err := d.wg.ListPeers()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Warn(errors.Wrap(err, "failed to list peers - metrics cannot be recorded"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
|
for _, device := range devices {
|
|
|
|
|
if peer.PublicKey.String() == device.PublicKey {
|
|
|
|
|
// if the peer is connected we can update their metrics
|
|
|
|
|
// importantly, we'll ignore peers that we know about
|
|
|
|
|
// but aren't connected at the moment.
|
|
|
|
|
// they may actually be connected to another replica.
|
|
|
|
|
if peer.Endpoint != nil {
|
|
|
|
|
if device, err := d.GetByPublicKey(peer.PublicKey.String()); err == nil {
|
|
|
|
|
device.Endpoint = peer.Endpoint.IP.String()
|
|
|
|
|
device.ReceiveBytes = peer.ReceiveBytes
|
|
|
|
|
device.TransmitBytes = peer.TransmitBytes
|
|
|
|
|
if !peer.LastHandshakeTime.IsZero() {
|
|
|
|
|
device.LastHandshakeTime = &peer.LastHandshakeTime
|
|
|
|
|
}
|
|
|
|
|
if peer.Endpoint != nil {
|
|
|
|
|
device.Endpoint = peer.Endpoint.IP.String()
|
|
|
|
|
}
|
|
|
|
|
if err := d.SaveDevice(device); err != nil {
|
|
|
|
|
logrus.Debug(errors.Wrap(err, "failed to update device metadata"))
|
|
|
|
|
logrus.Error(errors.Wrap(err, "failed to save device during metadata sync"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|