-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreceiver.go
109 lines (91 loc) · 2.76 KB
/
receiver.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
package main
import (
"github.com/noahhl/clamp"
"github.com/noahhl/go-batsd/gobatsd"
"fmt"
"runtime"
"time"
"os"
"os/signal"
"runtime/pprof"
)
var counterChannel chan gobatsd.Datapoint
var gaugeChannel chan gobatsd.Datapoint
var timerChannel chan gobatsd.Datapoint
const channelBufferSize = 4000000
const numIncomingMessageProcessors = 20
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
gobatsd.LoadConfig()
if gobatsd.ProfileCPU {
cpuprof, err := os.Create(fmt.Sprintf("cpuprof-%v", time.Now().Unix()))
if err != nil {
panic(err)
}
defer cpuprof.Close()
pprof.StartCPUProfile(cpuprof)
defer pprof.StopCPUProfile()
}
processingChannel := clamp.StartExplodingDualServer(":" + gobatsd.Config.Port)
clamp.StartStatsServer(":8124")
gobatsd.SetupDatastore()
fmt.Printf("Starting on port %v\n", gobatsd.Config.Port)
gobatsd.InitializeInternalMetrics()
gaugeChannel = make(chan gobatsd.Datapoint, channelBufferSize)
counterChannel = make(chan gobatsd.Datapoint, channelBufferSize)
timerChannel = make(chan gobatsd.Datapoint, channelBufferSize)
channels := map[string]chan gobatsd.Datapoint{"g": gaugeChannel, "c": counterChannel, "ms": timerChannel}
for i := 0; i < numIncomingMessageProcessors; i++ {
go func(processingChannel chan string) {
for {
message := <-processingChannel
d := gobatsd.ParseDatapointFromString(message)
if ch, ok := channels[d.Datatype]; ok {
ch <- d
}
}
}(processingChannel)
}
go func() {
c := time.Tick(1 * time.Second)
for {
<-c
clamp.StatsChannel <- clamp.Stat{"gaugeChannelSize", fmt.Sprintf("%v", len(gaugeChannel))}
clamp.StatsChannel <- clamp.Stat{"counterChannelSize", fmt.Sprintf("%v", len(counterChannel))}
clamp.StatsChannel <- clamp.Stat{"timerChannelSize", fmt.Sprintf("%v", len(timerChannel))}
}
}()
processDatatype("gauges", gaugeChannel, gobatsd.NewGauge)
processDatatype("timers", timerChannel, gobatsd.NewTimer)
processDatatype("counters", counterChannel, gobatsd.NewCounter)
terminate := make(chan os.Signal)
signal.Notify(terminate, os.Interrupt)
<-terminate
fmt.Printf("Server stopped")
}
func processDatatype(datatypeName string, ch chan gobatsd.Datapoint, metricCreator func(string) gobatsd.Metric) {
metrics := make(map[string]gobatsd.Metric)
go func() {
c := time.Tick(1 * time.Second)
for {
<-c
clamp.StatsChannel <- clamp.Stat{datatypeName, fmt.Sprintf("%v", len(metrics))}
gobatsd.InternalMetrics[datatypeName+"Known"].Update(float64(len(metrics)))
}
}()
go func() {
for {
select {
case d := <-ch:
if m, ok := metrics[d.Name]; ok {
m.Update(d.Value)
} else {
m := metricCreator(d.Name)
metrics[d.Name] = m
m.Update(d.Value)
}
gobatsd.InternalMetrics[datatypeName+"Processed"].Update(1)
}
}
}()
}