Skip to content

Commit efc9b1c

Browse files
committed
refactor(analytic): improved network stat #913
1 parent 789698a commit efc9b1c

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

internal/analytic/network.go

+56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analytic
22

33
import (
44
stdnet "net"
5+
"strings"
56

67
"github.com/shirou/gopsutil/v4/net"
78
"github.com/uozi-tech/cosy/logger"
@@ -46,6 +47,17 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
4647
continue
4748
}
4849

50+
// Skip common virtual interfaces by name pattern
51+
if isVirtualInterface(iface.Name) {
52+
continue
53+
}
54+
55+
// Handle container main interfaces like eth0 in container environments
56+
if isContainerInterface(iface.Name) {
57+
externalInterfaces[iface.Name] = true
58+
continue
59+
}
60+
4961
// Get addresses for this interface
5062
addrs, err := iface.Addrs()
5163
if err != nil {
@@ -106,6 +118,50 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
106118
}, nil
107119
}
108120

121+
// isVirtualInterface checks if the interface is a virtual one based on name patterns
122+
func isVirtualInterface(name string) bool {
123+
// Common virtual interface name patterns
124+
virtualPatterns := []string{
125+
"veth", "virbr", "vnet", "vmnet", "vboxnet", "docker",
126+
"br-", "bridge", "tun", "tap", "bond", "dummy",
127+
"vpn", "ipsec", "gre", "sit", "vlan", "virt",
128+
"wg", "vmk", "ib", "vxlan", "geneve", "ovs",
129+
"hyperv", "hyper-v", "awdl", "llw", "utun",
130+
"vpn", "zt", "zerotier", "wireguard",
131+
}
132+
133+
for _, pattern := range virtualPatterns {
134+
if strings.Contains(strings.ToLower(name), pattern) {
135+
return true
136+
}
137+
}
138+
139+
return false
140+
}
141+
142+
// isContainerInterface checks if this is a main container interface
143+
func isContainerInterface(name string) bool {
144+
// Common main container interface patterns
145+
// eth0 is usually the main interface inside containers
146+
// en0, en1 are common physical interfaces on macOS
147+
// ens/enp/eno are common physical interfaces on Linux
148+
containerPatterns := []string{
149+
"eth0", "en0", "en1",
150+
"ens", "enp", "eno",
151+
"eth1", "eth2", // Potential physical interfaces
152+
"wlan", "wifi", "wl", // Wireless interfaces
153+
"bond0", // Bonded interfaces that might be external
154+
}
155+
156+
for _, pattern := range containerPatterns {
157+
if strings.HasPrefix(strings.ToLower(name), pattern) {
158+
return true
159+
}
160+
}
161+
162+
return false
163+
}
164+
109165
// isRealExternalIP checks if an IP is a genuine external (public) IP
110166
func isRealExternalIP(ip stdnet.IP, ipNet *stdnet.IPNet) bool {
111167
// Skip if it's not a global unicast address

internal/analytic/record.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,32 @@ func recordCpu(now time.Time) {
6868
}
6969

7070
func recordNetwork(now time.Time) {
71-
// Get separate statistics for each interface
71+
// Get network statistics using GetNetworkStat which includes Ethernet interfaces
7272
networkStats, err := GetNetworkStat()
7373
if err != nil {
7474
logger.Error(err)
7575
return
7676
}
7777

78-
LastNetRecv = networkStats.BytesRecv
79-
LastNetSent = networkStats.BytesSent
78+
// Calculate usage since last record
79+
bytesRecv := networkStats.BytesRecv - LastNetRecv
80+
bytesSent := networkStats.BytesSent - LastNetSent
8081

82+
// Update records
8183
NetRecvRecord = append(NetRecvRecord, Usage[uint64]{
8284
Time: now,
83-
Usage: networkStats.BytesRecv - LastNetRecv,
85+
Usage: bytesRecv,
8486
})
8587
NetSentRecord = append(NetSentRecord, Usage[uint64]{
8688
Time: now,
87-
Usage: networkStats.BytesSent - LastNetSent,
89+
Usage: bytesSent,
8890
})
8991

92+
// Update last values
93+
LastNetRecv = networkStats.BytesRecv
94+
LastNetSent = networkStats.BytesSent
95+
96+
// Limit record size
9097
if len(NetRecvRecord) > 100 {
9198
NetRecvRecord = NetRecvRecord[1:]
9299
}

internal/transport/transport_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestCreatesTransportWithDefaultSettings(t *testing.T) {
1515
require.NoError(t, err)
1616
assert.NotNil(t, transport)
1717
assert.ObjectsAreEqual(http.ProxyFromEnvironment, transport.Proxy)
18-
assert.Equal(t, settings.ServerSettings.InsecureSkipVerify, transport.TLSClientConfig.InsecureSkipVerify)
18+
assert.Equal(t, settings.HTTPSettings.InsecureSkipVerify, transport.TLSClientConfig.InsecureSkipVerify)
1919
}
2020

2121
func TestCreatesTransportWithCustomProxy(t *testing.T) {

0 commit comments

Comments
 (0)