forked from rcrowley/go-metrics
/
cassandra.go
35 lines (32 loc) · 954 Bytes
/
cassandra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package metrics
import (
"time"
"github.com/gocql/gocql"
)
// Output each metric in the given registry to cassandra periodically by creating new session
// the given cassandrasyslogger.
func Cassandra(r Registry, d time.Duration, cassandra_cluster *gocql.ClusterConfig, query string, server string) {
for {
session, _ := cassandra_cluster.CreateSession()
r.Each(func(name string, i interface{}) {
switch metric := i.(type) {
case Counter:
if err := session.Query(query,
server, name, time.Now(), float32(metric.Count())).Exec() ; err != nil {
panic(err)
}
if metric.Get_reset_on_submit() {
metric.Clear()
}
case Gauge:
if err := session.Query(`INSERT INTO metrics2(server,metric, time, v) VALUES
(?, ?, ?, ?) USING TTL 1209600`,
server, name, time.Now(), float32(metric.Value())).Exec() ; err != nil {
panic(err)
}
}
})
session.Close()
time.Sleep(d)
}
}