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
parent
8e100998fa
commit
5a61bdad70
|
@ -1,5 +1,7 @@
|
||||||
package monitor
|
package monitor
|
||||||
|
|
||||||
|
import "github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
|
|
||||||
// system captures build diagnostics
|
// system captures build diagnostics
|
||||||
type build struct {
|
type build struct {
|
||||||
Version string
|
Version string
|
||||||
|
@ -8,7 +10,7 @@ type build struct {
|
||||||
Time string
|
Time string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *build) Diagnostics() (*Diagnostic, error) {
|
func (b *build) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
diagnostics := map[string]interface{}{
|
diagnostics := map[string]interface{}{
|
||||||
"Version": b.Version,
|
"Version": b.Version,
|
||||||
"Commit": b.Commit,
|
"Commit": b.Commit,
|
||||||
|
@ -16,5 +18,5 @@ func (b *build) Diagnostics() (*Diagnostic, error) {
|
||||||
"Build Time": b.Time,
|
"Build Time": b.Time,
|
||||||
}
|
}
|
||||||
|
|
||||||
return DiagnosticFromMap(diagnostics), nil
|
return DiagnosticsFromMap(diagnostics), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -2,12 +2,14 @@ package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// goRuntime captures Go runtime diagnostics
|
// goRuntime captures Go runtime diagnostics
|
||||||
type goRuntime struct{}
|
type goRuntime struct{}
|
||||||
|
|
||||||
func (g *goRuntime) Diagnostics() (*Diagnostic, error) {
|
func (g *goRuntime) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
diagnostics := map[string]interface{}{
|
diagnostics := map[string]interface{}{
|
||||||
"GOARCH": runtime.GOARCH,
|
"GOARCH": runtime.GOARCH,
|
||||||
"GOOS": runtime.GOOS,
|
"GOOS": runtime.GOOS,
|
||||||
|
@ -15,5 +17,5 @@ func (g *goRuntime) Diagnostics() (*Diagnostic, error) {
|
||||||
"version": runtime.Version(),
|
"version": runtime.Version(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return DiagnosticFromMap(diagnostics), nil
|
return DiagnosticsFromMap(diagnostics), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,14 @@ package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// network captures network diagnostics
|
// network captures network diagnostics
|
||||||
type network struct{}
|
type network struct{}
|
||||||
|
|
||||||
func (n *network) Diagnostics() (*Diagnostic, error) {
|
func (n *network) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
h, err := os.Hostname()
|
h, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -17,5 +19,5 @@ func (n *network) Diagnostics() (*Diagnostic, error) {
|
||||||
"hostname": h,
|
"hostname": h,
|
||||||
}
|
}
|
||||||
|
|
||||||
return DiagnosticFromMap(diagnostics), nil
|
return DiagnosticsFromMap(diagnostics), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/influxdata/influxdb"
|
"github.com/influxdata/influxdb"
|
||||||
"github.com/influxdata/influxdb/cluster"
|
"github.com/influxdata/influxdb/cluster"
|
||||||
"github.com/influxdata/influxdb/models"
|
"github.com/influxdata/influxdb/models"
|
||||||
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
"github.com/influxdata/influxdb/services/meta"
|
"github.com/influxdata/influxdb/services/meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,46 +26,6 @@ const (
|
||||||
MonitorRetentionPolicyDuration = 7 * 24 * time.Hour
|
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.
|
// Monitor represents an instance of the monitor system.
|
||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
// Build information for diagnostics.
|
// Build information for diagnostics.
|
||||||
|
@ -77,7 +38,7 @@ type Monitor struct {
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
diagRegistrations map[string]DiagsClient
|
diagRegistrations map[string]diagnostics.Client
|
||||||
|
|
||||||
storeCreated bool
|
storeCreated bool
|
||||||
storeEnabled bool
|
storeEnabled bool
|
||||||
|
@ -109,7 +70,7 @@ type Monitor struct {
|
||||||
func New(c Config) *Monitor {
|
func New(c Config) *Monitor {
|
||||||
return &Monitor{
|
return &Monitor{
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
diagRegistrations: make(map[string]DiagsClient),
|
diagRegistrations: make(map[string]diagnostics.Client),
|
||||||
storeEnabled: c.StoreEnabled,
|
storeEnabled: c.StoreEnabled,
|
||||||
storeDatabase: c.StoreDatabase,
|
storeDatabase: c.StoreDatabase,
|
||||||
storeInterval: time.Duration(c.StoreInterval),
|
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.
|
// 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()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
m.diagRegistrations[name] = client
|
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
|
// Diagnostics fetches diagnostic information for each registered
|
||||||
// diagnostic client. It skips any clients that return an error when
|
// diagnostic client. It skips any clients that return an error when
|
||||||
// retrieving their diagnostics.
|
// retrieving their diagnostics.
|
||||||
func (m *Monitor) Diagnostics() (map[string]*Diagnostic, error) {
|
func (m *Monitor) Diagnostics() (map[string]*diagnostics.Diagnostics, error) {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
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 {
|
for k, v := range m.diagRegistrations {
|
||||||
d, err := v.Diagnostics()
|
d, err := v.Diagnostics()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -420,8 +381,8 @@ func (s *Statistic) valueNames() []string {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiagnosticFromMap returns a Diagnostic from a map.
|
// DiagnosticsFromMap returns a Diagnostics from a map.
|
||||||
func DiagnosticFromMap(m map[string]interface{}) *Diagnostic {
|
func DiagnosticsFromMap(m map[string]interface{}) *diagnostics.Diagnostics {
|
||||||
// Display columns in deterministic order.
|
// Display columns in deterministic order.
|
||||||
sortedKeys := make([]string, 0, len(m))
|
sortedKeys := make([]string, 0, len(m))
|
||||||
for k := range m {
|
for k := range m {
|
||||||
|
@ -429,7 +390,7 @@ func DiagnosticFromMap(m map[string]interface{}) *Diagnostic {
|
||||||
}
|
}
|
||||||
sort.Strings(sortedKeys)
|
sort.Strings(sortedKeys)
|
||||||
|
|
||||||
d := NewDiagnostic(sortedKeys)
|
d := diagnostics.NewDiagnostics(sortedKeys)
|
||||||
row := make([]interface{}, len(sortedKeys))
|
row := make([]interface{}, len(sortedKeys))
|
||||||
for i, k := range sortedKeys {
|
for i, k := range sortedKeys {
|
||||||
row[i] = m[k]
|
row[i] = m[k]
|
||||||
|
|
|
@ -6,13 +6,14 @@ import (
|
||||||
|
|
||||||
"github.com/influxdata/influxdb/influxql"
|
"github.com/influxdata/influxdb/influxql"
|
||||||
"github.com/influxdata/influxdb/models"
|
"github.com/influxdata/influxdb/models"
|
||||||
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StatementExecutor translates InfluxQL queries to Monitor methods.
|
// StatementExecutor translates InfluxQL queries to Monitor methods.
|
||||||
type StatementExecutor struct {
|
type StatementExecutor struct {
|
||||||
Monitor interface {
|
Monitor interface {
|
||||||
Statistics(map[string]string) ([]*Statistic, error)
|
Statistics(map[string]string) ([]*Statistic, error)
|
||||||
Diagnostics() (map[string]*Diagnostic, error)
|
Diagnostics() (map[string]*diagnostics.Diagnostics, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package monitor
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
)
|
)
|
||||||
|
|
||||||
var startTime time.Time
|
var startTime time.Time
|
||||||
|
@ -14,7 +16,7 @@ func init() {
|
||||||
// system captures system-level diagnostics
|
// system captures system-level diagnostics
|
||||||
type system struct{}
|
type system struct{}
|
||||||
|
|
||||||
func (s *system) Diagnostics() (*Diagnostic, error) {
|
func (s *system) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
diagnostics := map[string]interface{}{
|
diagnostics := map[string]interface{}{
|
||||||
"PID": os.Getpid(),
|
"PID": os.Getpid(),
|
||||||
"currentTime": time.Now().UTC(),
|
"currentTime": time.Now().UTC(),
|
||||||
|
@ -22,5 +24,5 @@ func (s *system) Diagnostics() (*Diagnostic, error) {
|
||||||
"uptime": time.Since(startTime).String(),
|
"uptime": time.Since(startTime).String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return DiagnosticFromMap(diagnostics), nil
|
return DiagnosticsFromMap(diagnostics), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/influxdata/influxdb"
|
"github.com/influxdata/influxdb"
|
||||||
"github.com/influxdata/influxdb/cluster"
|
"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/services/meta"
|
||||||
"github.com/influxdata/influxdb/tsdb"
|
"github.com/influxdata/influxdb/tsdb"
|
||||||
)
|
)
|
||||||
|
@ -76,7 +76,7 @@ type Service struct {
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
|
||||||
Monitor interface {
|
Monitor interface {
|
||||||
RegisterDiagnosticsClient(name string, client monitor.DiagsClient)
|
RegisterDiagnosticsClient(name string, client diagnostics.Client)
|
||||||
DeregisterDiagnosticsClient(name string)
|
DeregisterDiagnosticsClient(name string)
|
||||||
}
|
}
|
||||||
PointsWriter interface {
|
PointsWriter interface {
|
||||||
|
@ -374,16 +374,15 @@ func (s *Service) processBatches(batcher *tsdb.PointBatcher) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diagnostics returns diagnostics of the graphite service.
|
// Diagnostics returns diagnostics of the graphite service.
|
||||||
func (s *Service) Diagnostics() (*monitor.Diagnostic, error) {
|
func (s *Service) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
s.tcpConnectionsMu.Lock()
|
s.tcpConnectionsMu.Lock()
|
||||||
defer s.tcpConnectionsMu.Unlock()
|
defer s.tcpConnectionsMu.Unlock()
|
||||||
|
|
||||||
d := &monitor.Diagnostic{
|
d := &diagnostics.Diagnostics{
|
||||||
Columns: []string{"local", "remote", "connect time"},
|
Columns: []string{"local", "remote", "connect time"},
|
||||||
Rows: make([][]interface{}, 0, len(s.tcpConnections)),
|
Rows: make([][]interface{}, 0, len(s.tcpConnections)),
|
||||||
}
|
}
|
||||||
for _, v := range s.tcpConnections {
|
for _, v := range s.tcpConnections {
|
||||||
_ = v
|
|
||||||
d.Rows = append(d.Rows, []interface{}{v.conn.LocalAddr().String(), v.conn.RemoteAddr().String(), v.connectTime})
|
d.Rows = append(d.Rows, []interface{}{v.conn.LocalAddr().String(), v.conn.RemoteAddr().String(), v.connectTime})
|
||||||
}
|
}
|
||||||
return d, nil
|
return d, nil
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/influxdata/influxdb"
|
"github.com/influxdata/influxdb"
|
||||||
"github.com/influxdata/influxdb/models"
|
"github.com/influxdata/influxdb/models"
|
||||||
"github.com/influxdata/influxdb/monitor"
|
"github.com/influxdata/influxdb/monitor/diagnostics"
|
||||||
"github.com/influxdata/influxdb/services/meta"
|
"github.com/influxdata/influxdb/services/meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ type Service struct {
|
||||||
MetaClient metaClient
|
MetaClient metaClient
|
||||||
|
|
||||||
Monitor interface {
|
Monitor interface {
|
||||||
RegisterDiagnosticsClient(name string, client monitor.DiagsClient)
|
RegisterDiagnosticsClient(name string, client diagnostics.Client)
|
||||||
DeregisterDiagnosticsClient(name string)
|
DeregisterDiagnosticsClient(name string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,11 +188,11 @@ func (s *Service) WriteShard(shardID, ownerID uint64, points []models.Point) err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diagnostics returns diagnostic information.
|
// Diagnostics returns diagnostic information.
|
||||||
func (s *Service) Diagnostics() (*monitor.Diagnostic, error) {
|
func (s *Service) Diagnostics() (*diagnostics.Diagnostics, error) {
|
||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
d := &monitor.Diagnostic{
|
d := &diagnostics.Diagnostics{
|
||||||
Columns: []string{"node", "active", "last modified", "head", "tail"},
|
Columns: []string{"node", "active", "last modified", "head", "tail"},
|
||||||
Rows: make([][]interface{}, 0, len(s.processors)),
|
Rows: make([][]interface{}, 0, len(s.processors)),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue