Skip to content

Commit

Permalink
refactor statsd
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Aug 2, 2017
1 parent fb404d9 commit 3022f13
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
1 change: 1 addition & 0 deletions cluster/calcium/create_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
ms[i].Success = true
}

go c.logMemoryAllocStats(opts)
return ms
}

Expand Down
12 changes: 12 additions & 0 deletions cluster/calcium/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
38 changes: 0 additions & 38 deletions g/statsd.go

This file was deleted.

5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
67 changes: 67 additions & 0 deletions stats/statsd.go
Original file line number Diff line number Diff line change
@@ -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}
}
17 changes: 0 additions & 17 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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}, "_")
Expand Down

0 comments on commit 3022f13

Please sign in to comment.