forked from atlassian/gostatsd
/
main.go
72 lines (66 loc) · 1.96 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
package main
import (
"context"
"fmt"
"math/rand"
"os"
"runtime/pprof"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/hligit/gostatsd"
"github.com/hligit/gostatsd/pkg/fakesocket"
"github.com/hligit/gostatsd/pkg/statsd"
)
var (
// BuildDate is the date when the binary was built.
BuildDate string
// GitCommit is the commit hash that built the binary.
GitCommit string
// Version is the version.
Version string
)
func main() {
rand.Seed(time.Now().UnixNano())
s := newServer()
s.AddFlags(pflag.CommandLine)
pflag.Parse()
if s.Version {
fmt.Printf("Version: %s - Commit: %s - Date: %s\n", Version, GitCommit, BuildDate)
os.Exit(0)
}
if s.Benchmark != 0 {
server := statsd.Server{
DefaultTags: gostatsd.DefaultTags,
ExpiryIntervalCounter: gostatsd.DefaultExpiryInterval,
ExpiryIntervalGauge: gostatsd.DefaultExpiryInterval,
ExpiryIntervalSet: gostatsd.DefaultExpiryInterval,
ExpiryIntervalTimer: gostatsd.DefaultExpiryInterval,
FlushInterval: gostatsd.DefaultFlushInterval,
MaxReaders: gostatsd.DefaultMaxReaders,
MaxWorkers: gostatsd.DefaultMaxWorkers,
MaxQueueSize: gostatsd.DefaultMaxQueueSize,
PercentThreshold: gostatsd.DefaultPercentThreshold,
ReceiveBatchSize: gostatsd.DefaultReceiveBatchSize,
Viper: viper.New(),
}
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(s.Benchmark)*time.Second)
defer cancelFunc()
if s.CPUProfile {
f, e := os.Create("profile.pprof")
if e != nil {
log.Fatal(e)
}
defer f.Close()
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
err := server.RunWithCustomSocket(ctx, fakesocket.Factory)
if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
log.Errorf("statsd run failed: %v", err)
}
} else if err := s.Run(); err != nil {
log.Fatalf("%v\n", err)
}
}