github.com/slackhq/nebula@v1.9.0/message_metrics.go (about) 1 package nebula 2 3 import ( 4 "fmt" 5 6 "github.com/rcrowley/go-metrics" 7 "github.com/slackhq/nebula/header" 8 ) 9 10 //TODO: this can probably move into the header package 11 12 type MessageMetrics struct { 13 rx [][]metrics.Counter 14 tx [][]metrics.Counter 15 16 rxUnknown metrics.Counter 17 txUnknown metrics.Counter 18 } 19 20 func (m *MessageMetrics) Rx(t header.MessageType, s header.MessageSubType, i int64) { 21 if m != nil { 22 if t >= 0 && int(t) < len(m.rx) && s >= 0 && int(s) < len(m.rx[t]) { 23 m.rx[t][s].Inc(i) 24 } else if m.rxUnknown != nil { 25 m.rxUnknown.Inc(i) 26 } 27 } 28 } 29 func (m *MessageMetrics) Tx(t header.MessageType, s header.MessageSubType, i int64) { 30 if m != nil { 31 if t >= 0 && int(t) < len(m.tx) && s >= 0 && int(s) < len(m.tx[t]) { 32 m.tx[t][s].Inc(i) 33 } else if m.txUnknown != nil { 34 m.txUnknown.Inc(i) 35 } 36 } 37 } 38 39 func newMessageMetrics() *MessageMetrics { 40 gen := func(t string) [][]metrics.Counter { 41 return [][]metrics.Counter{ 42 { 43 metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.handshake_ixpsk0", t), nil), 44 }, 45 nil, 46 {metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.recv_error", t), nil)}, 47 {metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.lighthouse", t), nil)}, 48 { 49 metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.test_request", t), nil), 50 metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.test_response", t), nil), 51 }, 52 {metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.close_tunnel", t), nil)}, 53 } 54 } 55 return &MessageMetrics{ 56 rx: gen("rx"), 57 tx: gen("tx"), 58 59 rxUnknown: metrics.GetOrRegisterCounter("messages.rx.other", nil), 60 txUnknown: metrics.GetOrRegisterCounter("messages.tx.other", nil), 61 } 62 } 63 64 // Historically we only recorded recv_error, so this is backwards compat 65 func newMessageMetricsOnlyRecvError() *MessageMetrics { 66 gen := func(t string) [][]metrics.Counter { 67 return [][]metrics.Counter{ 68 nil, 69 nil, 70 {metrics.GetOrRegisterCounter(fmt.Sprintf("messages.%s.recv_error", t), nil)}, 71 } 72 } 73 return &MessageMetrics{ 74 rx: gen("rx"), 75 tx: gen("tx"), 76 } 77 } 78 79 func newLighthouseMetrics() *MessageMetrics { 80 gen := func(t string) [][]metrics.Counter { 81 h := make([][]metrics.Counter, len(NebulaMeta_MessageType_name)) 82 used := []NebulaMeta_MessageType{ 83 NebulaMeta_HostQuery, 84 NebulaMeta_HostQueryReply, 85 NebulaMeta_HostUpdateNotification, 86 NebulaMeta_HostPunchNotification, 87 NebulaMeta_HostUpdateNotificationAck, 88 } 89 for _, i := range used { 90 h[i] = []metrics.Counter{metrics.GetOrRegisterCounter(fmt.Sprintf("lighthouse.%s.%s", t, i.String()), nil)} 91 } 92 return h 93 } 94 return &MessageMetrics{ 95 rx: gen("rx"), 96 tx: gen("tx"), 97 98 rxUnknown: metrics.GetOrRegisterCounter("lighthouse.rx.other", nil), 99 txUnknown: metrics.GetOrRegisterCounter("lighthouse.tx.other", nil), 100 } 101 }