Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement server stats and self-monitoring #1936

Merged
merged 26 commits into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7d82beb
Add basic stats type
otoolep Mar 12, 2015
8825580
Track server stats
otoolep Mar 12, 2015
228e238
Remove unnecessary err variable
otoolep Mar 12, 2015
e080664
Add "SHOW STATS" command
otoolep Mar 12, 2015
f730257
Implement self-monitoring
otoolep Mar 12, 2015
240b7a7
Add convenient Inc() method to Stats
otoolep Mar 13, 2015
6867d80
Use Inc() shorthand for Stats
otoolep Mar 13, 2015
bf28720
Add "name" to stats object
otoolep Mar 13, 2015
95f7a00
Fix test config for _internal database
otoolep Mar 13, 2015
f6ae0f9
Fix up statistics 'check-interval' in sample config
otoolep Mar 13, 2015
a8b928a
Add explanatory comments re statistics config
otoolep Mar 13, 2015
d95c945
Better names for statistics config options
otoolep Mar 13, 2015
a0e57a8
Allow stats to be snapshot
otoolep Mar 13, 2015
5240151
Actually start self-monitoring if requested
otoolep Mar 13, 2015
511be11
Final tweaks to stats unit tests
otoolep Mar 13, 2015
ea7b7be
Fix 'go vet' errors
otoolep Mar 13, 2015
ae3b3d5
Rename "metrics" to "stats"
otoolep Mar 13, 2015
f4b3e3d
Fix final 'go vet' errors
otoolep Mar 13, 2015
a99cd37
Update CHANGELOG
otoolep Mar 13, 2015
5aa49ef
Add GoDoc comments to Stats type
otoolep Mar 13, 2015
d627634
Ensure internal retention policy exists
otoolep Mar 13, 2015
43161bb
Small chance that a database exists
otoolep Mar 13, 2015
032cfaa
Launch self-monitoring in a go-routine
otoolep Mar 13, 2015
50d2470
Write totals, not diff, of internal stats
otoolep Mar 14, 2015
eec2cf6
Use a better ID for the server ID in stats
otoolep Mar 14, 2015
69530b7
Use a time.Ticker object for precise self-monitoring
otoolep Mar 14, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- [#1955](https://github.com/influxdb/influxdb/pull/1955): Prohibit creation of databases with no name. Thanks @dullgiulio
- [#1952](https://github.com/influxdb/influxdb/pull/1952): Handle delete statement with an error. Thanks again to @dullgiulio

### Features
- [#1936](https://github.com/influxdb/influxdb/pull/1936): Implement "SHOW STATS" and self-monitoring

## v0.9.0-rc11 [2015-03-13]

### Bugfixes
Expand Down
12 changes: 12 additions & 0 deletions cmd/influxd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ type Config struct {
RaftTracing bool `toml:"raft-tracing"`
} `toml:"logging"`

Statistics struct {
Enabled bool `toml:"enabled"`
Database string `toml:"database"`
RetentionPolicy string `toml:"retention-policy"`
WriteInterval Duration `toml:"write-interval"`
}

ContinuousQuery struct {
// when continuous queries are run we'll automatically recompute previous intervals
// in case lagged data came in. Set to zero if you never have lagged data. We do
Expand Down Expand Up @@ -166,6 +173,11 @@ func NewConfig() *Config {
c.ContinuousQuery.Disable = false
c.ReportingDisabled = false

c.Statistics.Enabled = false
c.Statistics.Database = "_internal"
c.Statistics.RetentionPolicy = "default"
c.Statistics.WriteInterval = Duration(1 * time.Minute)

// Detect hostname (or set to localhost).
if c.Hostname, _ = os.Hostname(); c.Hostname == "" {
c.Hostname = "localhost"
Expand Down
6 changes: 6 additions & 0 deletions cmd/influxd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ file = "influxdb.log"
write-tracing = true
raft-tracing = true

[statistics]
enabled = true
database = "_internal"
retention-policy = "default"
write-interval = "1m"

# Configure the admin server
[admin]
enabled = true
Expand Down
21 changes: 21 additions & 0 deletions cmd/influxd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ func Run(config *Config, join, version string, logWriter *os.File) (*messaging.B
log.Fatalf("failed to start %s Graphite server: %s", c.Protocol, err.Error())
}
}

// Start up self-monitoring if enabled.
if config.Statistics.Enabled {
database := config.Statistics.Database
policy := config.Statistics.RetentionPolicy
interval := time.Duration(config.Statistics.WriteInterval)

// Ensure database exists.
if err := s.CreateDatabaseIfNotExists(database); err != nil {
log.Fatalf("failed to create database %s for internal statistics: %s", database, err.Error())
}

// Ensure retention policy exists.
rp := influxdb.NewRetentionPolicy(policy)
if err := s.CreateRetentionPolicyIfNotExists(database, rp); err != nil {
log.Fatalf("failed to create retention policy for internal statistics: %s", err.Error())
}

s.StartSelfMonitoring(database, policy, interval)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing an interval := config.Statistics.WriteInterval?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good -- I just fixed that on my local copy and see you spotted it. Fix coming.

log.Printf("started self-monitoring at interval of %s", interval)
}
}

// unless disabled, start the loop to report anonymous usage stats every 24h
Expand Down
9 changes: 9 additions & 0 deletions etc/config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ dir = "/tmp/influxdb/development/state"
file = "/var/log/influxdb/influxd.log" # Leave blank to redirect logs to stderr.
write-tracing = false # If true, enables detailed logging of the write system.
raft-tracing = false # If true, enables detailed logging of Raft consensus.

# InfluxDB can store statistics about itself. This is useful for monitoring purposes.
# This feature is disabled by default, but if enabled, these statistics can be queried
# as any other data.
[statistics]
enabled = false
database = "_internal" # The database to which the data is written.
retention-policy = "default" # The retention policy within the database.
write-interval = "1m" # Period between writing the data.
23 changes: 23 additions & 0 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (*ShowFieldKeysStatement) node() {}
func (*ShowRetentionPoliciesStatement) node() {}
func (*ShowMeasurementsStatement) node() {}
func (*ShowSeriesStatement) node() {}
func (*ShowStatsStatement) node() {}
func (*ShowTagKeysStatement) node() {}
func (*ShowTagValuesStatement) node() {}
func (*ShowUsersStatement) node() {}
Expand Down Expand Up @@ -169,6 +170,7 @@ func (*ShowFieldKeysStatement) stmt() {}
func (*ShowMeasurementsStatement) stmt() {}
func (*ShowRetentionPoliciesStatement) stmt() {}
func (*ShowSeriesStatement) stmt() {}
func (*ShowStatsStatement) stmt() {}
func (*ShowTagKeysStatement) stmt() {}
func (*ShowTagValuesStatement) stmt() {}
func (*ShowUsersStatement) stmt() {}
Expand Down Expand Up @@ -1365,6 +1367,27 @@ func (s *ShowRetentionPoliciesStatement) RequiredPrivileges() ExecutionPrivilege
return ExecutionPrivileges{{Name: "", Privilege: ReadPrivilege}}
}

// ShowRetentionPoliciesStatement represents a command for displaying stats for a given server.
type ShowStatsStatement struct {
// Hostname or IP of the server for stats.
Host string
}

// String returns a string representation of a ShowStatsStatement.
func (s *ShowStatsStatement) String() string {
var buf bytes.Buffer
_, _ = buf.WriteString("SHOW STATS ")
if s.Host != "" {
_, _ = buf.WriteString(s.Host)
}
return buf.String()
}

// RequiredPrivileges returns the privilege(s) required to execute a ShowStatsStatement
func (s *ShowStatsStatement) RequiredPrivileges() ExecutionPrivileges {
return ExecutionPrivileges{{Name: "", Privilege: AllPrivileges}}
}

// ShowTagKeysStatement represents a command for listing tag keys.
type ShowTagKeysStatement struct {
// Data source that fields are extracted from.
Expand Down
17 changes: 17 additions & 0 deletions influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func (p *Parser) parseShowStatement() (Statement, error) {
return nil, newParseError(tokstr(tok, lit), []string{"POLICIES"}, pos)
case SERIES:
return p.parseShowSeriesStatement()
case STATS:
return p.parseShowStatsStatement()
case TAG:
tok, pos, lit := p.scanIgnoreWhitespace()
if tok == KEYS {
Expand Down Expand Up @@ -1172,6 +1174,21 @@ func (p *Parser) parseRetentionPolicy() (name string, dfault bool, err error) {
return
}

// parseShowStatsStatement parses a string and returns a ShowStatsStatement.
// This function assumes the "SHOW STATS" tokens have already been consumed.
func (p *Parser) parseShowStatsStatement() (*ShowStatsStatement, error) {
stmt := &ShowStatsStatement{}
var err error

if tok, _, _ := p.scanIgnoreWhitespace(); tok == ON {
stmt.Host, err = p.parseString()
} else {
p.unscan()
}

return stmt, err
}

// parseDropContinuousQueriesStatement parses a string and returns a DropContinuousQueryStatement.
// This function assumes the "DROP CONTINUOUS" tokens have already been consumed.
func (p *Parser) parseDropContinuousQueryStatement() (*DropContinuousQueryStatement, error) {
Expand Down
21 changes: 21 additions & 0 deletions influxql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,26 @@ func TestParser_ParseStatement(t *testing.T) {
stmt: newAlterRetentionPolicyStatement("policy1", "testdb", -1, 4, false),
},

// SHOW STATS
{
s: `SHOW STATS`,
stmt: &influxql.ShowStatsStatement{
Host: "",
},
},
{
s: `SHOW STATS ON 'servera'`,
stmt: &influxql.ShowStatsStatement{
Host: "servera",
},
},
{
s: `SHOW STATS ON '192.167.1.44'`,
stmt: &influxql.ShowStatsStatement{
Host: "192.167.1.44",
},
},

// Errors
{s: ``, err: `found EOF, expected SELECT at line 1, char 1`},
{s: `SELECT`, err: `found EOF, expected identifier, string, number, bool at line 1, char 8`},
Expand Down Expand Up @@ -798,6 +818,7 @@ func TestParser_ParseStatement(t *testing.T) {
{s: `SHOW RETENTION`, err: `found EOF, expected POLICIES at line 1, char 16`},
{s: `SHOW RETENTION POLICIES`, err: `found EOF, expected identifier at line 1, char 25`},
{s: `SHOW FOO`, err: `found FOO, expected CONTINUOUS, DATABASES, FIELD, MEASUREMENTS, RETENTION, SERIES, SERVERS, TAG, USERS at line 1, char 6`},
{s: `SHOW STATS ON`, err: `found EOF, expected string at line 1, char 15`},
{s: `DROP CONTINUOUS`, err: `found EOF, expected QUERY at line 1, char 17`},
{s: `DROP CONTINUOUS QUERY`, err: `found EOF, expected identifier at line 1, char 23`},
{s: `CREATE CONTINUOUS`, err: `found EOF, expected QUERY at line 1, char 19`},
Expand Down
2 changes: 2 additions & 0 deletions influxql/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const (
SERVERS
SHOW
SLIMIT
STATS
SOFFSET
TAG
TO
Expand Down Expand Up @@ -208,6 +209,7 @@ var tokens = [...]string{
SHOW: "SHOW",
SLIMIT: "SLIMIT",
SOFFSET: "SOFFSET",
STATS: "STATS",
TAG: "TAG",
TO: "TO",
USER: "USER",
Expand Down
Loading