-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters