forked from signalfx/heroku-signalfx-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (106 loc) · 3.05 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/signalfx/golib/v3/datapoint"
"github.com/signalfx/golib/v3/sfxclient"
"github.com/signalfx/heroku-signalfx-collector/internal"
sfxwriter "github.com/signalfx/signalfx-go/writer"
)
func main() {
conf := internal.ConfigFromEnv()
if err := conf.Validate(); err != nil {
log.WithError(err).Error("Config was invalid")
os.Exit(1)
}
setupLogging(conf)
client := makeClient(conf)
dpChan := make(chan []*datapoint.Datapoint, 1)
datapointWriter := &sfxwriter.DatapointWriter{
SendFunc: func(ctx context.Context, dps []*datapoint.Datapoint) error {
err := client.AddDatapoints(ctx, dps)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Failed to dispatch datapoints to SignalFx")
return err
}
return nil
},
InputChan: dpChan,
}
datapointWriter.Start(context.Background())
listener, err := internal.NewListener(conf.IntervalSeconds, dpChan)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Failed to setup listener")
os.Exit(2)
}
if err := listener.Start(); err != nil {
log.WithError(err).Error("Failed to start listener")
os.Exit(3)
}
http.HandleFunc("/", listener.ProcessLogs)
if conf.SendInternalMetrics {
log.Infof("Sending internal metrics")
go sendInternalMetrics(conf.IntervalSeconds, dpChan, listener)
}
err = http.ListenAndServe(fmt.Sprintf(":%d", conf.Port), nil)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Failed to start SignalFx Collector")
os.Exit(4)
}
log.Infoln("Shutting Down")
}
func setupLogging(conf *internal.Config) {
// Output to stderr instead of stdout
log.SetOutput(os.Stderr)
// Only log the Info severity or above
log.SetLevel(log.InfoLevel)
if conf.Debug {
log.SetLevel(log.DebugLevel)
}
}
func makeClient(conf *internal.Config) *sfxclient.HTTPSink {
client := sfxclient.NewHTTPSink()
client.AuthToken = conf.AccessToken
// Prefer SFX_INGEST_URL over SFX_REALM
switch {
case conf.IngestURL != "":
client.DatapointEndpoint = fmt.Sprintf("%s/v2/datapoint", conf.IngestURL)
case conf.Realm != "":
client.DatapointEndpoint = fmt.Sprintf("https://ingest.%s.signalfx.com/v2/datapoint", conf.Realm)
default:
panic("ingest URL or realm should be set")
}
log.Infof("Sending datapoints to %s", client.DatapointEndpoint)
return client
}
func sendInternalMetrics(intervalSeconds int, dpChan chan<- []*datapoint.Datapoint, listener *internal.Listener) {
ticker := time.NewTicker(time.Duration(intervalSeconds) * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
dps := listener.InternalMetrics()
for i := range dps {
if dps[i].Dimensions == nil {
dps[i].Dimensions = map[string]string{}
}
dps[i].Dimensions["heroku_app"] = os.Getenv("HEROKU_APP_NAME")
dps[i].Dimensions["dyno_id"] = os.Getenv("HEROKU_DYNO_ID")
dps[i].Dimensions["source"] = "signalfx-heroku-collector"
}
dpChan <- dps
case <-context.Background().Done():
return
}
}
}