forked from nightlyworker/dd_rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdd_rethinkdb_stats.go
146 lines (132 loc) · 3 KB
/
dd_rethinkdb_stats.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"strings"
"github.com/PagerDuty/godspeed"
log "github.com/Sirupsen/logrus"
r "gopkg.in/dancannon/gorethink.v2"
)
type RethinkStats struct {
addr []string
tags []string
verbose bool
stats chan []Stat
session *r.Session
g *godspeed.Godspeed
}
func NewRethinkStats(addr, tags, env string, verbose bool) *RethinkStats {
rs := &RethinkStats{
addr: strings.Split(addr, ","),
tags: strings.Split(tags, ","),
stats: make(chan []Stat),
verbose: verbose,
}
if rs.verbose {
log.Println("Connecting to DogStatsD..")
}
// DogStatsD
datadogHost := os.Getenv("DATADOG")
if datadogHost == "" {
datadogHost = "datadog"
}
if env == "" || env == "dev" {
datadogHost = "127.0.0.1"
}
g, _ := godspeed.New(datadogHost, 8125, false)
g.AddTags(rs.tags)
rs.g = g
go rs.procStats()
return rs
}
func (rs *RethinkStats) Close() {
close(rs.stats)
rs.g.Conn.Close()
rs.session.Close()
if rs.verbose {
log.Println("DogStatsD connection closed")
log.Println("RethinkDB connection closed")
}
}
func (rs *RethinkStats) Query() {
if rs.session == nil {
if rs.verbose {
log.Println("Connecting to RethinkDB..")
}
var err error
rs.session, err = r.Connect(r.ConnectOpts{
Addresses: rs.addr,
Database: "rethinkdb",
})
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to open database connection")
return
}
}
res, err := r.Table("stats").Run(rs.session)
defer res.Close()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to query stats table")
return
}
var stats []Stat
if res.All(&stats) != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to retrieve results")
return
}
rs.stats <- stats
}
func (rs *RethinkStats) procStats() {
var err error
for stats := range rs.stats {
if rs.verbose {
log.Println("Sending stats to DogStatsD..")
}
for _, stat := range stats {
switch stat.ID[0] {
case "cluster":
{
err = rs.g.Gauge("rethinkdb.read_docs_per_sec.cluster", stat.QueryEngine.ReadDocsPerSec, nil)
err = rs.g.Gauge("rethinkdb.written_docs_per_sec.cluster", stat.QueryEngine.WrittenDocsPerSec, nil)
}
case "server":
{
err = rs.g.Gauge(
fmt.Sprintf("rethinkdb.read_docs_per_sec.server.%s", stat.Server),
stat.QueryEngine.ReadDocsPerSec,
nil)
err = rs.g.Gauge(
fmt.Sprintf("rethinkdb.written_docs_per_sec.server.%s", stat.Server),
stat.QueryEngine.WrittenDocsPerSec,
nil)
}
case "table":
{
err = rs.g.Gauge(
fmt.Sprintf("rethinkdb.read_docs_per_sec.table.%s", stat.Table),
stat.QueryEngine.ReadDocsPerSec,
nil)
err = rs.g.Gauge(
fmt.Sprintf("rethinkdb.written_docs_per_sec.table.%s", stat.Table),
stat.QueryEngine.WrittenDocsPerSec,
nil)
}
case "table_server":
{
// replicas
}
}
}
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to send stats to DataDog")
}
}
}