Skip to content

Commit

Permalink
Add metrics and test TestUtil_MapToLabels (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
huikang committed Aug 2, 2022
1 parent b912871 commit fe86bd0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
8 changes: 4 additions & 4 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (m *Memberlist) handleConn(conn net.Conn) {
defer conn.Close()
m.logger.Printf("[DEBUG] memberlist: Stream connection %s", LogConn(conn))

metrics.IncrCounter([]string{"memberlist", "tcp", "accept"}, 1)
metrics.IncrCounterWithLabels([]string{"memberlist", "tcp", "accept"}, 1, m.metricLabels)

conn.SetDeadline(time.Now().Add(m.config.TCPTimeout))

Expand Down Expand Up @@ -869,7 +869,7 @@ func (m *Memberlist) rawSendMsgPacket(a Address, node *Node, msg []byte) error {
msg = buf.Bytes()
}

metrics.IncrCounter([]string{"memberlist", "udp", "sent"}, float32(len(msg)))
metrics.IncrCounterWithLabels([]string{"memberlist", "udp", "sent"}, float32(len(msg)), m.metricLabels)
_, err := m.transport.WriteToAddress(msg, a)
return err
}
Expand Down Expand Up @@ -898,7 +898,7 @@ func (m *Memberlist) rawSendMsgStream(conn net.Conn, sendBuf []byte, streamLabel
}

// Write out the entire send buffer
metrics.IncrCounter([]string{"memberlist", "tcp", "sent"}, float32(len(sendBuf)))
metrics.IncrCounterWithLabels([]string{"memberlist", "tcp", "sent"}, float32(len(sendBuf)), m.metricLabels)

if n, err := conn.Write(sendBuf); err != nil {
return err
Expand Down Expand Up @@ -953,7 +953,7 @@ func (m *Memberlist) sendAndReceiveState(a Address, join bool) ([]pushNodeState,
}
defer conn.Close()
m.logger.Printf("[DEBUG] memberlist: Initiating push/pull sync with: %s %s", a.Name, conn.RemoteAddr())
metrics.IncrCounter([]string{"memberlist", "tcp", "connect"}, 1)
metrics.IncrCounterWithLabels([]string{"memberlist", "tcp", "connect"}, 1, m.metricLabels)

// Send our state
if err := m.sendLocalState(conn, join, m.config.Label); err != nil {
Expand Down
14 changes: 0 additions & 14 deletions net_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,3 @@ func setUDPRecvBuf(c *net.UDPConn) error {
}
return err
}

func mapToLabels(m map[string]string) []metrics.Label {
if len(m) == 0 {
return nil
}
out := make([]metrics.Label, 0, len(m))
for k, v := range m {
out = append(out, metrics.Label{
Name: k,
Value: v,
})
}
return out
}
15 changes: 15 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

metrics "github.com/armon/go-metrics"
"github.com/hashicorp/go-msgpack/codec"
"github.com/sean-/seed"
)
Expand Down Expand Up @@ -324,3 +325,17 @@ func ensurePort(s string, port int) string {
s = net.JoinHostPort(s, strconv.Itoa(port))
return s
}

func mapToLabels(m map[string]string) []metrics.Label {
if len(m) == 0 {
return nil
}
out := make([]metrics.Label, 0, len(m))
for k, v := range m {
out = append(out, metrics.Label{
Name: k,
Value: v,
})
}
return out
}
27 changes: 27 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,30 @@ func TestCompressDecompressPayload(t *testing.T) {
t.Fatalf("bad payload: %v", decomp)
}
}

func TestUtil_MapToLabels(t *testing.T) {
tests := []struct {
name string
labels map[string]string
}{
{"empty labels", nil},
{
name: "multiple labels",
labels: map[string]string{
"name1": "value1",
"name2": "value2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := mapToLabels(tt.labels)
countLabels := 0
for _, kv := range got {
require.Equal(t, tt.labels[kv.Name], kv.Value)
countLabels++
}
require.Equal(t, len(tt.labels), countLabels)
})
}
}

0 comments on commit fe86bd0

Please sign in to comment.