github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/gossip/status.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package gossip
    12  
    13  import (
    14  	"bytes"
    15  	"fmt"
    16  	"time"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/util/metric"
    19  )
    20  
    21  // Metrics contains gossip metrics used per node and server.
    22  type Metrics struct {
    23  	ConnectionsRefused *metric.Counter
    24  	BytesReceived      *metric.Counter
    25  	BytesSent          *metric.Counter
    26  	InfosReceived      *metric.Counter
    27  	InfosSent          *metric.Counter
    28  }
    29  
    30  func makeMetrics() Metrics {
    31  	return Metrics{
    32  		ConnectionsRefused: metric.NewCounter(MetaConnectionsRefused),
    33  		BytesReceived:      metric.NewCounter(MetaBytesReceived),
    34  		BytesSent:          metric.NewCounter(MetaBytesSent),
    35  		InfosReceived:      metric.NewCounter(MetaInfosReceived),
    36  		InfosSent:          metric.NewCounter(MetaInfosSent),
    37  	}
    38  }
    39  
    40  func (m Metrics) String() string {
    41  	return m.Snapshot().String()
    42  }
    43  
    44  // Snapshot returns a snapshot of the metrics.
    45  func (m Metrics) Snapshot() MetricSnap {
    46  	return MetricSnap{
    47  		ConnsRefused:  m.ConnectionsRefused.Count(),
    48  		BytesReceived: m.BytesReceived.Count(),
    49  		BytesSent:     m.BytesSent.Count(),
    50  		InfosReceived: m.InfosReceived.Count(),
    51  		InfosSent:     m.InfosSent.Count(),
    52  	}
    53  }
    54  
    55  func (m MetricSnap) String() string {
    56  	s := fmt.Sprintf("infos %d/%d sent/received, bytes %dB/%dB sent/received",
    57  		m.InfosSent, m.InfosReceived, m.BytesSent, m.BytesReceived)
    58  	if m.ConnsRefused > 0 {
    59  		s += fmt.Sprintf(", refused %d conns", m.ConnsRefused)
    60  	}
    61  	return s
    62  }
    63  
    64  func (c OutgoingConnStatus) String() string {
    65  	return fmt.Sprintf("%d: %s (%s: %s)",
    66  		c.NodeID, c.Address, roundSecs(time.Duration(c.AgeNanos)), c.MetricSnap)
    67  }
    68  
    69  func (c ClientStatus) String() string {
    70  	var buf bytes.Buffer
    71  	fmt.Fprintf(&buf, "gossip client (%d/%d cur/max conns)\n", len(c.ConnStatus), c.MaxConns)
    72  	for _, conn := range c.ConnStatus {
    73  		fmt.Fprintf(&buf, "  %s\n", conn)
    74  	}
    75  	return buf.String()
    76  }
    77  
    78  func (c ConnStatus) String() string {
    79  	return fmt.Sprintf("%d: %s (%s)", c.NodeID, c.Address, roundSecs(time.Duration(c.AgeNanos)))
    80  }
    81  
    82  func (s ServerStatus) String() string {
    83  	var buf bytes.Buffer
    84  	fmt.Fprintf(&buf, "gossip server (%d/%d cur/max conns, %s)\n",
    85  		len(s.ConnStatus), s.MaxConns, s.MetricSnap)
    86  	for _, conn := range s.ConnStatus {
    87  		fmt.Fprintf(&buf, "  %s\n", conn)
    88  	}
    89  	return buf.String()
    90  }
    91  
    92  func (c Connectivity) String() string {
    93  	var buf bytes.Buffer
    94  	fmt.Fprintf(&buf, "gossip connectivity\n")
    95  	if c.SentinelNodeID != 0 {
    96  		fmt.Fprintf(&buf, "  n%d [sentinel];\n", c.SentinelNodeID)
    97  	}
    98  	if len(c.ClientConns) > 0 {
    99  		fmt.Fprintf(&buf, " ")
   100  		for _, conn := range c.ClientConns {
   101  			fmt.Fprintf(&buf, " n%d -> n%d;", conn.SourceID, conn.TargetID)
   102  		}
   103  		fmt.Fprintf(&buf, "\n")
   104  	}
   105  	return buf.String()
   106  }