Skip to content

Commit

Permalink
Merge pull request #83 from influxdb/usage-client
Browse files Browse the repository at this point in the history
Change 'enterprise-client' to 'usage-client'
  • Loading branch information
Nathaniel Cook committed Dec 14, 2015
2 parents d9f362d + 7b63a4e commit d959bd8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 129 deletions.
1 change: 0 additions & 1 deletion cmd/kapacitord/run/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type Config struct {

Hostname string `toml:"hostname"`
DataDir string `toml:"data_dir"`
Token string `toml:"token"`
}

// NewConfig returns an instance of Config with reasonable defaults.
Expand Down
6 changes: 3 additions & 3 deletions cmd/kapacitord/run/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewServer(c *Config, buildInfo *BuildInfo, logService logging.Interface) (*
// append StatsService and ReportingService last so all stats are ready
// to be reported
s.appendStatsService(c.Stats)
s.appendReportingService(c.Reporting, c.Token)
s.appendReportingService(c.Reporting)

return s, nil
}
Expand Down Expand Up @@ -306,10 +306,10 @@ func (s *Server) appendStatsService(c stats.Config) {
}
}

func (s *Server) appendReportingService(c reporting.Config, token string) {
func (s *Server) appendReportingService(c reporting.Config) {
if c.Enabled {
l := s.LogService.NewLogger("[reporting] ", log.LstdFlags)
srv := reporting.NewService(c, token, l)
srv := reporting.NewService(c, l)

s.Services = append(s.Services, srv)
}
Expand Down
12 changes: 1 addition & 11 deletions etc/kapacitor/kapacitor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
hostname = "localhost"
# Directory for storing a small amount of metadata about the server.
data_dir = "/var/lib/kapacitor"
# Your Enterprise token. By using Enterprise you can
# send all internal statistics to the Enterprise
# endpoints which will store and report on the
# activity of your instances.
token = ""

[http]
# HTTP API Server for Kapacitor
Expand Down Expand Up @@ -140,12 +135,7 @@ token = ""
# Send anonymous usage statistics
# every 12 hours to Enterprise.
enabled = true
enterprise-url = "https://enterprise.influxdata.com"
# The interval at which to send all
# internal statistics to Enterprise.
# If no token is specified this
# setting has no effect.
stats-interval = "1m0s"
url = "https://usage.influxdata.com"

##################################
# Input Methods, same as InfluxDB
Expand Down
15 changes: 5 additions & 10 deletions services/reporting/config.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package reporting

import (
"time"

"github.com/influxdb/enterprise-client/v1"
"github.com/influxdb/influxdb/toml"
"github.com/influxdb/usage-client/v1"
)

type Config struct {
Enabled bool `toml:"enabled"`
EnterpriseURL string `toml:"enterprise-url"`
StatsInterval toml.Duration `toml:"stats-interval"`
Enabled bool `toml:"enabled"`
URL string `toml:"url"`
}

func NewConfig() Config {
return Config{
Enabled: true,
EnterpriseURL: client.URL,
StatsInterval: toml.Duration(time.Minute),
Enabled: true,
URL: client.URL,
}
}
106 changes: 13 additions & 93 deletions services/reporting/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (
"sync"
"time"

"github.com/influxdb/enterprise-client/v1"
"github.com/influxdb/kapacitor"
"github.com/influxdb/usage-client/v1"
)

const reportingInterval = time.Hour * 12

// Sends periodic information to Enterprise.
// If not registered with Enterprise just
// registers the server on startup and sends anonymous
// stats every 12 hours.
//
// If registered with Enterprise also sends
// all expvar statistics at the Config.StatsInterval.
// Sends anonymous usage information every 12 hours.
type Service struct {
tags client.Tags

Expand All @@ -31,21 +25,19 @@ type Service struct {
version string
product string

statsInterval time.Duration
statsTicker *time.Ticker
usageTicker *time.Ticker
closing chan struct{}
logger *log.Logger
wg sync.WaitGroup
statsTicker *time.Ticker
usageTicker *time.Ticker
closing chan struct{}
logger *log.Logger
wg sync.WaitGroup
}

func NewService(c Config, token string, l *log.Logger) *Service {
client := client.New(token)
client.URL = c.EnterpriseURL
func NewService(c Config, l *log.Logger) *Service {
client := client.New("")
client.URL = c.URL
return &Service{
client: client,
logger: l,
statsInterval: time.Duration(c.StatsInterval),
client: client,
logger: l,
}
}

Expand All @@ -67,30 +59,9 @@ func (s *Service) Open() error {
s.tags["arch"] = runtime.GOARCH
s.tags["os"] = runtime.GOOS

// Check for enterprise token
if s.client.Token == "" {
r := client.Registration{
ClusterID: s.clusterID,
Product: s.product,
}
u, _ := s.client.RegistrationURL(r)
s.logger.Println("E! No Enterprise token configured, please register at", u)
} else {
// Send periodic stats
s.statsTicker = time.NewTicker(s.statsInterval)
s.wg.Add(1)
go s.stats()
}

// Register server on startup
err := s.registerServer()
if err != nil {
s.logger.Println("E! error registering server:", err)
}

// Send anonymous usage stats on startup
s.usageTicker = time.NewTicker(reportingInterval)
err = s.sendUsageReport()
err := s.sendUsageReport()
if err != nil {
s.logger.Println("E! error sending usage stats:", err)
}
Expand Down Expand Up @@ -130,37 +101,6 @@ func (s *Service) usage() {
}
}

func (s *Service) stats() {
defer s.wg.Done()
for {
select {
case <-s.closing:
return
case <-s.statsTicker.C:
err := s.sendStatsReport()
if err != nil {
s.logger.Println("E! error while sending stats report:", err)
}
}
}
}

// Register this server with Enterprise.
func (s *Service) registerServer() error {
server := client.Server{
ClusterID: s.clusterID,
ServerID: s.serverID,
Host: s.hostname,
Version: s.version,
Product: s.product,
}
resp, err := s.client.Save(server)
if resp != nil {
resp.Body.Close()
}
return err
}

// Send anonymous usage report.
func (s *Service) sendUsageReport() error {
data := client.UsageData{
Expand All @@ -186,23 +126,3 @@ func (s *Service) sendUsageReport() error {
}
return err
}

// Send all internal stats.
func (s *Service) sendStatsReport() error {
data, err := kapacitor.GetStatsData()
if err != nil {
return err
}
stats := client.Stats{
ClusterID: s.clusterID,
ServerID: s.serverID,
Product: s.product,
Data: data,
}

resp, err := s.client.Save(stats)
if resp != nil {
resp.Body.Close()
}
return err
}
27 changes: 16 additions & 11 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

"github.com/influxdb/enterprise-client/v1"
"github.com/twinj/uuid"
)

Expand Down Expand Up @@ -116,13 +115,19 @@ func NewStatistics(name string, tags map[string]string) *expvar.Map {
return statMap
}

type StatsData struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Values map[string]interface{} `json:"values"`
}

// Return all stats data from the expvars.
func GetStatsData() ([]client.StatsData, error) {
allData := make([]client.StatsData, 0)
func GetStatsData() ([]StatsData, error) {
allData := make([]StatsData, 0)
// Add Global expvars
globalData := client.StatsData{
globalData := StatsData{
Name: "kapacitor",
Values: make(client.Values),
Values: make(map[string]interface{}),
}

allData = append(allData, globalData)
Expand All @@ -142,9 +147,9 @@ func GetStatsData() ([]client.StatsData, error) {
globalData.Values[kv.Key] = f
}
case *expvar.Map:
data := client.StatsData{
Tags: make(client.Tags),
Values: make(client.Values),
data := StatsData{
Tags: make(map[string]string),
Values: make(map[string]interface{}),
}

v.Do(func(subKV expvar.KeyValue) {
Expand Down Expand Up @@ -191,7 +196,7 @@ func GetStatsData() ([]client.StatsData, error) {
}
})

// If a registered client has no field data, don't include it in the results
// If no field data, don't include it in the results
if len(data.Values) == 0 {
return
}
Expand All @@ -204,13 +209,13 @@ func GetStatsData() ([]client.StatsData, error) {
globalData.Values[UptimeVarName] = Uptime().Seconds()

// Add Go memstats.
data := client.StatsData{
data := StatsData{
Name: "runtime",
}

var rt runtime.MemStats
runtime.ReadMemStats(&rt)
data.Values = client.Values{
data.Values = map[string]interface{}{
"Alloc": int64(rt.Alloc),
"TotalAlloc": int64(rt.TotalAlloc),
"Sys": int64(rt.Sys),
Expand Down

0 comments on commit d959bd8

Please sign in to comment.