diff --git a/monitor/build_info.go b/monitor/build_info.go index d65ae5a458..cc4248b717 100644 --- a/monitor/build_info.go +++ b/monitor/build_info.go @@ -1,5 +1,7 @@ package monitor +import "github.com/influxdata/influxdb/monitor/diagnostics" + // system captures build diagnostics type build struct { Version string @@ -8,7 +10,7 @@ type build struct { Time string } -func (b *build) Diagnostics() (*Diagnostic, error) { +func (b *build) Diagnostics() (*diagnostics.Diagnostics, error) { diagnostics := map[string]interface{}{ "Version": b.Version, "Commit": b.Commit, @@ -16,5 +18,5 @@ func (b *build) Diagnostics() (*Diagnostic, error) { "Build Time": b.Time, } - return DiagnosticFromMap(diagnostics), nil + return DiagnosticsFromMap(diagnostics), nil } diff --git a/monitor/diagnostics/diagnostics.go b/monitor/diagnostics/diagnostics.go new file mode 100644 index 0000000000..87aac80f45 --- /dev/null +++ b/monitor/diagnostics/diagnostics.go @@ -0,0 +1,41 @@ +package diagnostics // import "github.com/influxdata/influxdb/monitor/diagnostics" + +// Client is the interface modules implement if they register diagnostics with monitor. +type Client interface { + Diagnostics() (*Diagnostics, error) +} + +// The ClientFunc type is an adapter to allow the use of +// ordinary functions as Diagnostics clients. +type ClientFunc func() (*Diagnostics, error) + +// Diagnostics calls f(). +func (f ClientFunc) Diagnostics() (*Diagnostics, error) { + return f() +} + +// Diagnostics represents a table of diagnostic information. The first value +// is the name of the columns, the second is a slice of interface slices containing +// the values for each column, by row. This information is never written to an InfluxDB +// system and is display-only. An example showing, say, connections follows: +// +// source_ip source_port dest_ip dest_port +// 182.1.0.2 2890 127.0.0.1 38901 +// 174.33.1.2 2924 127.0.0.1 38902 +type Diagnostics struct { + Columns []string + Rows [][]interface{} +} + +// NewDiagnostic initialises a new Diagnostics with the specified columns. +func NewDiagnostics(columns []string) *Diagnostics { + return &Diagnostics{ + Columns: columns, + Rows: make([][]interface{}, 0), + } +} + +// AddRow appends the provided row to the Diagnostics' rows. +func (d *Diagnostics) AddRow(r []interface{}) { + d.Rows = append(d.Rows, r) +} diff --git a/monitor/go_runtime.go b/monitor/go_runtime.go index f45f4fce22..1a3252af06 100644 --- a/monitor/go_runtime.go +++ b/monitor/go_runtime.go @@ -2,12 +2,14 @@ package monitor import ( "runtime" + + "github.com/influxdata/influxdb/monitor/diagnostics" ) // goRuntime captures Go runtime diagnostics type goRuntime struct{} -func (g *goRuntime) Diagnostics() (*Diagnostic, error) { +func (g *goRuntime) Diagnostics() (*diagnostics.Diagnostics, error) { diagnostics := map[string]interface{}{ "GOARCH": runtime.GOARCH, "GOOS": runtime.GOOS, @@ -15,5 +17,5 @@ func (g *goRuntime) Diagnostics() (*Diagnostic, error) { "version": runtime.Version(), } - return DiagnosticFromMap(diagnostics), nil + return DiagnosticsFromMap(diagnostics), nil } diff --git a/monitor/network.go b/monitor/network.go index 80c0f6c9c3..29102d65bb 100644 --- a/monitor/network.go +++ b/monitor/network.go @@ -2,12 +2,14 @@ package monitor import ( "os" + + "github.com/influxdata/influxdb/monitor/diagnostics" ) // network captures network diagnostics type network struct{} -func (n *network) Diagnostics() (*Diagnostic, error) { +func (n *network) Diagnostics() (*diagnostics.Diagnostics, error) { h, err := os.Hostname() if err != nil { return nil, err @@ -17,5 +19,5 @@ func (n *network) Diagnostics() (*Diagnostic, error) { "hostname": h, } - return DiagnosticFromMap(diagnostics), nil + return DiagnosticsFromMap(diagnostics), nil } diff --git a/monitor/service.go b/monitor/service.go index c0627e697a..2b8f017f78 100644 --- a/monitor/service.go +++ b/monitor/service.go @@ -14,6 +14,7 @@ import ( "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/cluster" "github.com/influxdata/influxdb/models" + "github.com/influxdata/influxdb/monitor/diagnostics" "github.com/influxdata/influxdb/services/meta" ) @@ -25,46 +26,6 @@ const ( MonitorRetentionPolicyDuration = 7 * 24 * time.Hour ) -// DiagsClient is the interface modules implement if they register diags with monitor. -type DiagsClient interface { - Diagnostics() (*Diagnostic, error) -} - -// The DiagsClientFunc type is an adapter to allow the use of -// ordinary functions as Diagnostis clients. -type DiagsClientFunc func() (*Diagnostic, error) - -// Diagnostics calls f(). -func (f DiagsClientFunc) Diagnostics() (*Diagnostic, error) { - return f() -} - -// Diagnostic represents a table of diagnostic information. The first value -// is the name of the columns, the second is a slice of interface slices containing -// the values for each column, by row. This information is never written to an InfluxDB -// system and is display-only. An example showing, say, connections follows: -// -// source_ip source_port dest_ip dest_port -// 182.1.0.2 2890 127.0.0.1 38901 -// 174.33.1.2 2924 127.0.0.1 38902 -type Diagnostic struct { - Columns []string - Rows [][]interface{} -} - -// NewDiagnostic initialises a new Diagnostic with the specified columns. -func NewDiagnostic(columns []string) *Diagnostic { - return &Diagnostic{ - Columns: columns, - Rows: make([][]interface{}, 0), - } -} - -// AddRow appends the provided row to the Diagnostic's rows. -func (d *Diagnostic) AddRow(r []interface{}) { - d.Rows = append(d.Rows, r) -} - // Monitor represents an instance of the monitor system. type Monitor struct { // Build information for diagnostics. @@ -77,7 +38,7 @@ type Monitor struct { done chan struct{} mu sync.Mutex - diagRegistrations map[string]DiagsClient + diagRegistrations map[string]diagnostics.Client storeCreated bool storeEnabled bool @@ -109,7 +70,7 @@ type Monitor struct { func New(c Config) *Monitor { return &Monitor{ done: make(chan struct{}), - diagRegistrations: make(map[string]DiagsClient), + diagRegistrations: make(map[string]diagnostics.Client), storeEnabled: c.StoreEnabled, storeDatabase: c.StoreDatabase, storeInterval: time.Duration(c.StoreInterval), @@ -158,7 +119,7 @@ func (m *Monitor) SetLogger(l *log.Logger) { } // RegisterDiagnosticsClient registers a diagnostics client with the given name and tags. -func (m *Monitor) RegisterDiagnosticsClient(name string, client DiagsClient) { +func (m *Monitor) RegisterDiagnosticsClient(name string, client diagnostics.Client) { m.mu.Lock() defer m.mu.Unlock() m.diagRegistrations[name] = client @@ -287,11 +248,11 @@ func (m *Monitor) Statistics(tags map[string]string) ([]*Statistic, error) { // Diagnostics fetches diagnostic information for each registered // diagnostic client. It skips any clients that return an error when // retrieving their diagnostics. -func (m *Monitor) Diagnostics() (map[string]*Diagnostic, error) { +func (m *Monitor) Diagnostics() (map[string]*diagnostics.Diagnostics, error) { m.mu.Lock() defer m.mu.Unlock() - diags := make(map[string]*Diagnostic, len(m.diagRegistrations)) + diags := make(map[string]*diagnostics.Diagnostics, len(m.diagRegistrations)) for k, v := range m.diagRegistrations { d, err := v.Diagnostics() if err != nil { @@ -420,8 +381,8 @@ func (s *Statistic) valueNames() []string { return a } -// DiagnosticFromMap returns a Diagnostic from a map. -func DiagnosticFromMap(m map[string]interface{}) *Diagnostic { +// DiagnosticsFromMap returns a Diagnostics from a map. +func DiagnosticsFromMap(m map[string]interface{}) *diagnostics.Diagnostics { // Display columns in deterministic order. sortedKeys := make([]string, 0, len(m)) for k := range m { @@ -429,7 +390,7 @@ func DiagnosticFromMap(m map[string]interface{}) *Diagnostic { } sort.Strings(sortedKeys) - d := NewDiagnostic(sortedKeys) + d := diagnostics.NewDiagnostics(sortedKeys) row := make([]interface{}, len(sortedKeys)) for i, k := range sortedKeys { row[i] = m[k] diff --git a/monitor/statement_executor.go b/monitor/statement_executor.go index 3a1a94dff3..901f33c605 100644 --- a/monitor/statement_executor.go +++ b/monitor/statement_executor.go @@ -6,13 +6,14 @@ import ( "github.com/influxdata/influxdb/influxql" "github.com/influxdata/influxdb/models" + "github.com/influxdata/influxdb/monitor/diagnostics" ) // StatementExecutor translates InfluxQL queries to Monitor methods. type StatementExecutor struct { Monitor interface { Statistics(map[string]string) ([]*Statistic, error) - Diagnostics() (map[string]*Diagnostic, error) + Diagnostics() (map[string]*diagnostics.Diagnostics, error) } } diff --git a/monitor/system.go b/monitor/system.go index 1f113f1764..c22bcc9f74 100644 --- a/monitor/system.go +++ b/monitor/system.go @@ -3,6 +3,8 @@ package monitor import ( "os" "time" + + "github.com/influxdata/influxdb/monitor/diagnostics" ) var startTime time.Time @@ -14,7 +16,7 @@ func init() { // system captures system-level diagnostics type system struct{} -func (s *system) Diagnostics() (*Diagnostic, error) { +func (s *system) Diagnostics() (*diagnostics.Diagnostics, error) { diagnostics := map[string]interface{}{ "PID": os.Getpid(), "currentTime": time.Now().UTC(), @@ -22,5 +24,5 @@ func (s *system) Diagnostics() (*Diagnostic, error) { "uptime": time.Since(startTime).String(), } - return DiagnosticFromMap(diagnostics), nil + return DiagnosticsFromMap(diagnostics), nil } diff --git a/services/graphite/service.go b/services/graphite/service.go index 26ccbc2425..fc233f1994 100644 --- a/services/graphite/service.go +++ b/services/graphite/service.go @@ -14,7 +14,7 @@ import ( "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/cluster" - "github.com/influxdata/influxdb/monitor" + "github.com/influxdata/influxdb/monitor/diagnostics" "github.com/influxdata/influxdb/services/meta" "github.com/influxdata/influxdb/tsdb" ) @@ -76,7 +76,7 @@ type Service struct { done chan struct{} Monitor interface { - RegisterDiagnosticsClient(name string, client monitor.DiagsClient) + RegisterDiagnosticsClient(name string, client diagnostics.Client) DeregisterDiagnosticsClient(name string) } PointsWriter interface { @@ -374,16 +374,15 @@ func (s *Service) processBatches(batcher *tsdb.PointBatcher) { } // Diagnostics returns diagnostics of the graphite service. -func (s *Service) Diagnostics() (*monitor.Diagnostic, error) { +func (s *Service) Diagnostics() (*diagnostics.Diagnostics, error) { s.tcpConnectionsMu.Lock() defer s.tcpConnectionsMu.Unlock() - d := &monitor.Diagnostic{ + d := &diagnostics.Diagnostics{ Columns: []string{"local", "remote", "connect time"}, Rows: make([][]interface{}, 0, len(s.tcpConnections)), } for _, v := range s.tcpConnections { - _ = v d.Rows = append(d.Rows, []interface{}{v.conn.LocalAddr().String(), v.conn.RemoteAddr().String(), v.connectTime}) } return d, nil diff --git a/services/hh/service.go b/services/hh/service.go index 0b4434efd6..0a6e590c0d 100644 --- a/services/hh/service.go +++ b/services/hh/service.go @@ -14,7 +14,7 @@ import ( "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/monitor" + "github.com/influxdata/influxdb/monitor/diagnostics" "github.com/influxdata/influxdb/services/meta" ) @@ -46,7 +46,7 @@ type Service struct { MetaClient metaClient Monitor interface { - RegisterDiagnosticsClient(name string, client monitor.DiagsClient) + RegisterDiagnosticsClient(name string, client diagnostics.Client) DeregisterDiagnosticsClient(name string) } } @@ -188,11 +188,11 @@ func (s *Service) WriteShard(shardID, ownerID uint64, points []models.Point) err } // Diagnostics returns diagnostic information. -func (s *Service) Diagnostics() (*monitor.Diagnostic, error) { +func (s *Service) Diagnostics() (*diagnostics.Diagnostics, error) { s.mu.RLock() defer s.mu.RUnlock() - d := &monitor.Diagnostic{ + d := &diagnostics.Diagnostics{ Columns: []string{"node", "active", "last modified", "head", "tail"}, Rows: make([][]interface{}, 0, len(s.processors)), }