Move monitor.Diagnostics to its own package

I was trying to create a Diagnostics Client in the tsdb package, but
IIRC importing `monitor` caused an import cycle of:
tsdb -> monitor -> cluster -> tsdb.

Moving Diagnostics to its own package will allow further use of
diagnostics.Client without running into import cycles.
pull/5682/head
Mark Rushakoff 2016-02-13 14:56:15 -08:00
parent 8e100998fa
commit 5a61bdad70
9 changed files with 76 additions and 66 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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]

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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

View File

@ -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)),
}