diff --git a/cluster/calcium/create_container.go b/cluster/calcium/create_container.go index bc4ca028e..789f59ce5 100644 --- a/cluster/calcium/create_container.go +++ b/cluster/calcium/create_container.go @@ -191,6 +191,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec ms[i].Success = true } + go c.logMemoryAllocStats(opts) return ms } diff --git a/cluster/calcium/resource.go b/cluster/calcium/resource.go index 7a6154e95..4ca87739a 100644 --- a/cluster/calcium/resource.go +++ b/cluster/calcium/resource.go @@ -5,12 +5,22 @@ import ( "sort" "sync" + "gitlab.ricebook.net/platform/core/stats" + log "github.com/Sirupsen/logrus" "gitlab.ricebook.net/platform/core/types" "gitlab.ricebook.net/platform/core/utils" ) +func (c *calcium) logMemoryAllocStats(opts *types.DeployOptions) { + cpuandmem, _, err := c.getCPUAndMem(opts.Podname, opts.Nodename, 1.0) + if err != nil { + log.Errorf("Get cpu and mem stats failed %v", err) + } + stats.Client.SendMemCap(cpuandmem, false) +} + func (c *calcium) allocMemoryPodResource(opts *types.DeployOptions) ([]types.NodeInfo, error) { lock, err := c.Lock(opts.Podname, 30) if err != nil { @@ -22,6 +32,8 @@ func (c *calcium) allocMemoryPodResource(opts *types.DeployOptions) ([]types.Nod if err != nil { return nil, err } + + stats.Client.SendMemCap(cpuandmem, true) nodesInfo := getNodesInfo(cpuandmem) // Load deploy status diff --git a/g/statsd.go b/g/statsd.go deleted file mode 100644 index 0393410ac..000000000 --- a/g/statsd.go +++ /dev/null @@ -1,38 +0,0 @@ -package g - -import ( - "fmt" - - statsdlib "github.com/CMGS/statsd" - log "github.com/Sirupsen/logrus" -) - -type StatsDClient struct { - Addr string -} - -func (self *StatsDClient) Close() error { - return nil -} - -func (self *StatsDClient) Send(data map[string]float64, endpoint, tag string) error { - remote, err := statsdlib.New(self.Addr) - if err != nil { - log.Errorf("Connect statsd failed: %v", err) - return err - } - defer remote.Close() - defer remote.Flush() - for k, v := range data { - key := fmt.Sprintf("eru-core.%s.%s.%s", endpoint, tag, k) - remote.Gauge(key, v) - } - return nil -} - -var Statsd = StatsDClient{} -var Hostname string - -func NewStatsdClient(addr string) { - Statsd = StatsDClient{addr} -} diff --git a/main.go b/main.go index 3faed0705..361391cb1 100644 --- a/main.go +++ b/main.go @@ -13,9 +13,9 @@ import ( log "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" "gitlab.ricebook.net/platform/core/cluster/calcium" - "gitlab.ricebook.net/platform/core/g" "gitlab.ricebook.net/platform/core/rpc" "gitlab.ricebook.net/platform/core/rpc/gen" + "gitlab.ricebook.net/platform/core/stats" "gitlab.ricebook.net/platform/core/types" "gitlab.ricebook.net/platform/core/versioninfo" "google.golang.org/grpc" @@ -82,8 +82,7 @@ func serve() { log.Fatal(err) } - g.NewStatsdClient(config.Statsd) - g.Hostname = os.Getenv("HOSTNAME") + stats.NewStatsdClient(config.Statsd) cluster, err := calcium.New(config) if err != nil { diff --git a/stats/statsd.go b/stats/statsd.go new file mode 100644 index 000000000..3618505b6 --- /dev/null +++ b/stats/statsd.go @@ -0,0 +1,67 @@ +package stats + +import ( + "fmt" + "os" + "strings" + + "gitlab.ricebook.net/platform/core/types" + + statsdlib "github.com/CMGS/statsd" + log "github.com/Sirupsen/logrus" +) + +const ( + MEMSTATS = "eru-core.%s.%s.%s" + + BEFORE = "before" + AFTER = "after" +) + +type statsdClient struct { + Addr string + Hostname string +} + +func (s *statsdClient) Close() error { + return nil +} + +func (s *statsdClient) send(data map[string]float64, endpoint, tag string) error { + remote, err := statsdlib.New(s.Addr) + if err != nil { + log.Errorf("Connect statsd failed: %v", err) + return err + } + defer remote.Close() + defer remote.Flush() + for k, v := range data { + key := fmt.Sprintf(MEMSTATS, endpoint, tag, k) + remote.Gauge(key, v) + } + return nil +} + +func (s *statsdClient) SendMemCap(cpumemmap map[string]types.CPUAndMem, before bool) { + data := map[string]float64{} + for node, cpuandmem := range cpumemmap { + data[node] = float64(cpuandmem.MemCap) + } + + cleanHost := strings.Replace(s.Hostname, ".", "-", -1) + tag := BEFORE + if !before { + tag = AFTER + } + err := s.send(data, cleanHost, tag) + if err != nil { + log.Errorf("Error occured while sending data to statsd: %v", err) + } +} + +var Client = statsdClient{} + +func NewStatsdClient(addr string) { + hostname, _ := os.Hostname() + Client = statsdClient{addr, hostname} +} diff --git a/utils/utils.go b/utils/utils.go index 251817173..660fe2ce1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,11 +8,8 @@ import ( "os" "strings" - log "github.com/Sirupsen/logrus" engineapi "github.com/docker/docker/client" "github.com/docker/docker/pkg/stdcopy" - "gitlab.ricebook.net/platform/core/g" - "gitlab.ricebook.net/platform/core/types" "golang.org/x/net/context" ) @@ -152,20 +149,6 @@ func MakeCommandLineArgs(s string) []string { return r } -func SendMemCap(cpumemmap map[string]types.CPUAndMem, tag string) { - data := map[string]float64{} - for node, cpuandmem := range cpumemmap { - data[node] = float64(cpuandmem.MemCap) - } - clean_host := strings.Replace(g.Hostname, ".", "-", -1) - - err := g.Statsd.Send(data, clean_host, tag) - if err != nil { - log.Errorf("Error occured while sending data to statsd: %v", err) - } - return -} - // MakeContainerName joins appname, entrypoint, ident using '_' func MakeContainerName(appname, entrypoint, ident string) string { return strings.Join([]string{appname, entrypoint, ident}, "_")