/
conf.go
148 lines (117 loc) · 2.83 KB
/
conf.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
147
148
//The conf package is responsible for reading in data from the environment.
//This should be the only place in the l2met source that calls os.Getenv.
//Flag handling is also limited to this package.
//The conf package has a pattern:
//
// Variable Declaration
// Variable Initialization
//
//If you are adding new configuration, please follow the pattern and append.
package conf
import (
"errors"
"strconv"
"net/url"
"strings"
"flag"
"os"
"time"
)
var AppName string
func init() {
AppName = os.Getenv("APP_NAME")
if len(AppName) == 0 {
AppName = "l2met"
}
}
var Outlet string
func init() {
flag.StringVar(&Outlet, "outlet", "", "Type of outlet to start. Valid opts: librato, graphite, (blank).")
}
var (
BufferSize int
Concurrency int
FlushtInterval time.Duration
)
func init() {
b := envInt("REQUEST_BUFFER", 1000)
flag.IntVar(&BufferSize, "buffer", b, "Number of items to buffer prior to flush.")
c := envInt("CONCURRENCY", 100)
flag.IntVar(&Concurrency, "concurrency", c, "Number local routines to start.")
t := time.Millisecond * 200
flag.DurationVar(&FlushtInterval, "flush-interval", t, "Time to wait before flushing items in buffer.")
}
var (
UsingRedis bool
MaxRedisConns int
RedisHost string
RedisPass string
MaxPartitions uint64
)
func init() {
var err error
UsingRedis = false
rurl := os.Getenv("REDIS_URL")
if len(rurl) == 0 {
return
}
RedisHost, RedisPass, err = parseRedisUrl(rurl)
if err != nil {
return
}
UsingRedis = true
mr := Concurrency + 10
flag.IntVar(&MaxRedisConns, "max-redis-conns", mr, "Max number of redis connections to pool.")
flag.Uint64Var(&MaxPartitions, "max-partitions", uint64(1), "Max number of partitions.")
}
var UsingHttpOutlet bool
func init() {
flag.BoolVar(&UsingHttpOutlet, "http-outlet", true, "Enable HTTP Outlet.")
}
var NumOutletRetry int
func init() {
flag.IntVar(&NumOutletRetry, "outlet-retry", 2, "Number of times to retry outlet requests.")
}
var UsingReciever bool
func init() {
flag.BoolVar(&UsingReciever, "receiver", true, "Start a log receiver.")
}
var (
Verbose bool
Port int
)
func init() {
flag.BoolVar(&Verbose, "v", false, "Enable verbose stastics.")
p := envInt("PORT", 8080)
flag.IntVar(&Port, "port", p, "HTTP server will bind to this port.")
}
var Secrets []string
func init() {
Secrets = strings.Split(os.Getenv("SECRETS"), ":")
}
//Finally.
func init() {
flag.Parse()
}
//Helper Functions
func parseRedisUrl(s string) (string, string, error) {
u, err := url.Parse(s)
if err != nil {
return "", "", errors.New("Missing REDIS_URL")
}
var password string
if u.User != nil {
password, _ = u.User.Password()
}
return u.Host, password, nil
}
func envInt(name string, defaultVal int) int {
tmp := os.Getenv(name)
if len(tmp) != 0 {
n, err := strconv.Atoi(tmp)
if err == nil {
return n
}
}
return defaultVal
}