forked from sokil/statsd-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
113 lines (98 loc) · 3.56 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"github.com/GoMetric/statsd-http-proxy/proxy"
)
// Version is a current git commit hash and tag
// Injected by compilation flag
var Version = "Unknown"
// BuildNumber is a current commit hash
// Injected by compilation flag
var BuildNumber = "Unknown"
// BuildDate is a date of build
// Injected by compilation flag
var BuildDate = "Unknown"
// HTTP connection params
const defaultHTTPHost = "127.0.0.1"
const defaultHTTPPort = 8825
const defaultHTTPReadTimeout = 1
const defaultHTTPWriteTimeout = 1
const defaultHTTPIdleTimeout = 1
// StatsD connection params
const defaultStatsDHost = "127.0.0.1"
const defaultStatsDPort = 8125
func main() {
// declare command line options
var httpHost = flag.String("http-host", defaultHTTPHost, "HTTP Host")
var httpPort = flag.Int("http-port", defaultHTTPPort, "HTTP Port")
var httpReadTimeout = flag.Int("http-timeout-read", defaultHTTPReadTimeout, "The maximum duration in seconds for reading the entire request, including the body")
var httpWriteTimeout = flag.Int("http-timeout-write", defaultHTTPWriteTimeout, "The maximum duration in seconds before timing out writes of the respons")
var httpIdleTimeout = flag.Int("http-timeout-idle", defaultHTTPIdleTimeout, "The maximum amount of time in seconds to wait for the next request when keep-alives are enabled")
var tlsCert = flag.String("tls-cert", "", "TLS certificate to enable HTTPS")
var tlsKey = flag.String("tls-key", "", "TLS private key to enable HTTPS")
var statsdHost = flag.String("statsd-host", defaultStatsDHost, "StatsD Host")
var statsdPort = flag.Int("statsd-port", defaultStatsDPort, "StatsD Port")
var metricPrefix = flag.String("metric-prefix", "", "Prefix of metric name")
var tokenSecret = flag.String("jwt-secret", "", "Secret to encrypt JWT")
var verbose = flag.Bool("verbose", false, "Verbose")
var version = flag.Bool("version", false, "Show version")
var httpRouterName = flag.String("http-router-name", "HttpRouter", "Type of HTTP router. Allowed values are GorillaMux and HttpRouter. Do not use in production.")
var statsdClientName = flag.String("statsd-client-name", "GoMetric", "Type of StatsD client. Allowed values are Cactus and GoMetric. Do not use in production.")
var profilerHTTPort = flag.Int("profiler-http-port", 0, "Start profiler localhost")
// get flags
flag.Parse()
// show version and exit
if *version {
fmt.Printf(
"StatsD HTTP Proxy v.%s, build %s from %s\n",
Version,
BuildNumber,
BuildDate,
)
os.Exit(0)
}
// log build version
log.Printf(
"Starting StatsD HTTP Proxy v.%s, build %s from %s\n",
Version,
BuildNumber,
BuildDate,
)
// start profiler
if *profilerHTTPort > 0 {
// enable block profiling
runtime.SetBlockProfileRate(1)
// start debug server
profilerHTTPAddress := fmt.Sprintf("localhost:%d", *profilerHTTPort)
go func() {
log.Println("Profiler started at " + profilerHTTPAddress)
log.Println("Open 'http://" + profilerHTTPAddress + "/debug/pprof/' in you browser or use 'go tool pprof http://" + profilerHTTPAddress + "/debug/pprof/heap' from console")
log.Println("See details about pprof in https://golang.org/pkg/net/http/pprof/")
log.Println(http.ListenAndServe(profilerHTTPAddress, nil))
}()
}
// start proxy server
proxyServer := proxy.NewServer(
*httpHost,
*httpPort,
*httpReadTimeout,
*httpWriteTimeout,
*httpIdleTimeout,
*statsdHost,
*statsdPort,
*tlsCert,
*tlsKey,
*metricPrefix,
*tokenSecret,
*verbose,
*httpRouterName,
*statsdClientName,
)
proxyServer.Listen()
}