33
44package ethtool
55
6- import "github.com/safchain/ethtool"
6+ import (
7+ "strconv"
8+ "strings"
79
8- func Stats (netIfName string ) (map [string ]uint64 , error ) {
10+ "github.com/safchain/ethtool"
11+ "go.opentelemetry.io/otel/attribute"
12+
13+ "github.com/spidernet-io/spiderpool/pkg/rdmametrics/oteltype"
14+ )
15+
16+ func Stats (netIfName string ) ([]oteltype.Metrics , error ) {
917 tool , err := ethtool .NewEthtool ()
1018 if err != nil {
1119 return nil , err
@@ -15,13 +23,79 @@ func Stats(netIfName string) (map[string]uint64, error) {
1523 if err != nil {
1624 return nil , err
1725 }
26+
27+ res := make ([]oteltype.Metrics , 0 )
28+ for k , v := range stats {
29+ if strings .Contains (k , "vport" ) {
30+ res = append (res , oteltype.Metrics {Name : k , Value : int64 (v )})
31+ continue
32+ }
33+ if expectPriorityMetrics (k ) {
34+ name , priority , ok := extractNameWithPriority (k )
35+ if ok {
36+ res = append (res , oteltype.Metrics {
37+ Name : name ,
38+ Value : int64 (v ),
39+ Labels : []attribute.KeyValue {{
40+ Key : "priority" , Value : attribute .IntValue (priority ),
41+ }},
42+ })
43+ }
44+ }
45+ }
46+
1847 speed , err := tool .CmdGetMapped (netIfName )
1948 if err != nil {
2049 return nil , err
2150 }
51+
2252 // speed unknown = 4294967295
2353 if val , ok := speed ["speed" ]; ok && val != 4294967295 {
24- stats ["vport_speed_mbps" ] = val
54+ res = append (res , oteltype.Metrics {
55+ Name : "vport_speed_mbps" ,
56+ Value : int64 (val ),
57+ })
58+ }
59+ return res , nil
60+ }
61+
62+ func expectPriorityMetrics (s string ) bool {
63+ if len (s ) == 14 {
64+ if (strings .HasPrefix (s , "rx_prio" ) || strings .HasPrefix (s , "tx_prio" )) && strings .HasSuffix (s , "_pause" ) {
65+ return true
66+ }
67+ }
68+ if len (s ) == 17 {
69+ if (strings .HasPrefix (s , "rx_prio" ) || strings .HasPrefix (s , "tx_prio" )) && strings .HasSuffix (s , "_discards" ) {
70+ return true
71+ }
2572 }
26- return stats , nil
73+ return false
74+ }
75+
76+ func extractNameWithPriority (str string ) (name string , priority int , ok bool ) {
77+ parts := strings .Split (str , "_" )
78+ if len (parts ) != 3 {
79+ return "" , 0 , false
80+ }
81+
82+ name = parts [0 ] + "_" + parts [2 ]
83+
84+ rawPriority := parts [1 ]
85+ if len (rawPriority ) < 5 {
86+ return "" , 0 , false
87+ }
88+
89+ var err error
90+
91+ if strings .HasPrefix (rawPriority , "prio" ) {
92+ priority , err = strconv .Atoi (strings .TrimPrefix (rawPriority , "prio" ))
93+ if err != nil {
94+ return "" , 0 , false
95+ }
96+ } else {
97+ return "" , 0 , false
98+ }
99+
100+ return name , priority , true
27101}
0 commit comments