-
Notifications
You must be signed in to change notification settings - Fork 1
/
emitter.go
121 lines (104 loc) · 3.58 KB
/
emitter.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
/*
Package cube periodically sends expvars to a Cube Collector. This is handy
if you want to create dashboards for visualizing statistics about
long-running programs.
You must first install and run a Cube collector. See
http://square.github.com/cube/
This module is plug-and-play. Example usage:
package main
import (
"expvar"
"flags"
"github.com/sburnett/cube"
)
func main() {
flags.Parse() // You must parse flags before starting the exporter.
go cube.Run("myevents") // Runs forever, so run it in a goroutine.
// Now create and use expvars.
}
*/
package cube
import (
"bytes"
"expvar"
"flag"
"fmt"
"log"
"net/http"
"strings"
"time"
)
var flagCollectorHost string
var flagCollectorPort int
var flagExportInterval string
var flagExportToCube bool
func init() {
flag.StringVar(&flagCollectorHost, "cube_collector_host", "localhost", "Export variables to this Cube collector.")
flag.IntVar(&flagCollectorPort, "cube_collector_port", 1080, "Use this port when connecting to the Cube collector.")
flag.StringVar(&flagExportInterval, "cube_export_interval", "10s", "Export variables to Cube once every interval.")
flag.BoolVar(&flagExportToCube, "cube_export", true, "Whether or not to export variables to Cube.")
}
// Periodically export variables from expvar to a Cube collector. This function
// never exits under normal circumstances, so you probably want to run it in a
// goroutine.
//
// You can control the collector hostname and port and how often we export to
// Cube using the cube_collector_host, cube_collector_port and
// cube_export_interval flags.
func Run(collectionType string) {
if !flagExportToCube {
return
}
putUrl := fmt.Sprintf("http://%s:%d/1.0/event/put", flagCollectorHost, flagCollectorPort)
log.Printf("Exporting expvars to %s with event type %s", putUrl, collectionType)
interval, err := time.ParseDuration(flagExportInterval)
if err != nil {
log.Fatalf("Error parsing duration %v: %v", flagExportInterval, err)
}
log.Printf("Exporting variables every %v", interval)
exportCounter := expvar.NewInt("CubeExports")
for now := range time.Tick(interval) {
if err := ExportVariablesWithTimestamp(collectionType, putUrl, now); err != nil {
log.Printf("Error exporting variables for %v", now)
}
exportCounter.Add(1)
}
}
// Export expvars to Cube right now. Use the current system time as the timestamp
// for the submitted event. This function sends variables once and returns.
//
// You shouldn't need this function under normal circumstances. Use Run()
// instead.
func ExportVariables(collectionType string, putUrl string) error {
return ExportVariablesWithTimestamp(collectionType, putUrl, time.Now())
}
// Export expvars to Cube right now. Use the provided timestamp for the
// submitted event. This function sends variables once and returns.
//
// You shouldn't need this function under normal circumstances. Use Run()
// instead.
func ExportVariablesWithTimestamp(collectionType string, putUrl string, timestamp time.Time) error {
variables := make([]string, 0)
expvar.Do(func(entry expvar.KeyValue) {
variables = append(variables, fmt.Sprintf("%q: %s", entry.Key, entry.Value))
})
request := fmt.Sprintf(
`[
{
"type": "%s",
"time": "%s",
"data": { %s }
}
]`,
collectionType,
timestamp.Format(time.ANSIC),
strings.Join(variables, ","))
response, err := http.Post(putUrl, "application/json", bytes.NewBufferString(request))
if err != nil {
log.Printf("Error POSTing events to Cube collector: %v", err)
log.Printf("The request we tried to post: %v", request)
return err
}
defer response.Body.Close()
return nil
}