forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
146 lines (125 loc) · 3.36 KB
/
Copy pathapi.go
File metadata and controls
146 lines (125 loc) · 3.36 KB
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
package api
import (
"crypto/tls"
"net"
"net/http"
"strings"
"time"
_ "net/http/pprof"
"github.com/raintank/metrictank/idx"
"github.com/raintank/metrictank/mdata"
"github.com/raintank/metrictank/mdata/cache"
"github.com/raintank/metrictank/stats"
"github.com/raintank/worldping-api/pkg/log"
"gopkg.in/macaron.v1"
)
var LogLevel int
var (
// metric api.get_target is how long it takes to get a target
getTargetDuration = stats.NewLatencyHistogram15s32("api.get_target")
// metric api.iters_to_points is how long it takes to decode points from a chunk iterator
itersToPointsDuration = stats.NewLatencyHistogram15s32("api.iters_to_points")
// metric api.requests_span.mem_and_cassandra is the timerange of requests hitting both in-memory and cassandra
reqSpanBoth = stats.NewMeter32("api.requests_span.mem_and_cassandra", false)
// metric api.requests_span.mem is the timerange of requests hitting only the ringbuffer
reqSpanMem = stats.NewMeter32("api.requests_span.mem", false)
)
type Server struct {
Addr string
SSL bool
certFile string
keyFile string
Macaron *macaron.Macaron
MetricIndex idx.MetricIndex
MemoryStore mdata.Metrics
BackendStore mdata.Store
Cache cache.Cache
shutdown chan struct{}
}
func (s *Server) BindMetricIndex(i idx.MetricIndex) {
s.MetricIndex = i
}
func (s *Server) BindMemoryStore(store mdata.Metrics) {
s.MemoryStore = store
}
func (s *Server) BindBackendStore(store mdata.Store) {
s.BackendStore = store
}
func (s *Server) BindCache(cache cache.Cache) {
s.Cache = cache
}
func NewServer() (*Server, error) {
m := macaron.New()
m.Use(macaron.Logger())
m.Use(macaron.Recovery())
// route pprof to where it belongs
m.Use(func(ctx *macaron.Context) {
if strings.HasPrefix(ctx.Req.URL.Path, "/debug/") {
http.DefaultServeMux.ServeHTTP(ctx.Resp, ctx.Req.Request)
}
})
return &Server{
Addr: Addr,
SSL: UseSSL,
certFile: certFile,
keyFile: keyFile,
shutdown: make(chan struct{}),
Macaron: m,
}, nil
}
func (s *Server) Run() {
s.RegisterRoutes()
proto := "http"
if s.SSL {
proto = "https"
}
log.Info("API Listening on: %v://%s/", proto, s.Addr)
// define our own listner so we can call Close on it
l, err := net.Listen("tcp", s.Addr)
if err != nil {
log.Fatal(4, "API failed to listen on %s, %s", s.Addr, err.Error())
}
go s.handleShutdown(l)
srv := http.Server{
Addr: s.Addr,
Handler: s.Macaron,
}
if s.SSL {
var cert tls.Certificate
cert, err = tls.LoadX509KeyPair(s.certFile, s.keyFile)
if err != nil {
log.Fatal(4, "API Failed to start server: %v", err)
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"http/1.1"},
}
tlsListener := tls.NewListener(tcpKeepAliveListener{l.(*net.TCPListener)}, srv.TLSConfig)
err = srv.Serve(tlsListener)
} else {
err = srv.Serve(tcpKeepAliveListener{l.(*net.TCPListener)})
}
if err != nil {
log.Info("API %s", err.Error())
}
}
func (s *Server) Stop() {
close(s.shutdown)
}
func (s *Server) handleShutdown(l net.Listener) {
<-s.shutdown
log.Info("API shutdown started.")
l.Close()
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}