forked from go-graphite/graphite-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphite-clickhouse.go
175 lines (139 loc) · 3.92 KB
/
graphite-clickhouse.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"runtime"
"runtime/debug"
"time"
"github.com/lomik/graphite-clickhouse/autocomplete"
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/find"
"github.com/lomik/graphite-clickhouse/index"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"github.com/lomik/graphite-clickhouse/prometheus"
"github.com/lomik/graphite-clickhouse/render"
"github.com/lomik/graphite-clickhouse/tagger"
"github.com/lomik/zapwriter"
"go.uber.org/zap"
_ "net/http/pprof"
)
// Version of graphite-clickhouse
const Version = "0.11.7"
func init() {
scope.Version = Version
}
type LogResponseWriter struct {
http.ResponseWriter
status int
}
func (w *LogResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *LogResponseWriter) Status() int {
if w.status == 0 {
return http.StatusOK
}
return w.status
}
func WrapResponseWriter(w http.ResponseWriter) *LogResponseWriter {
if wrapped, ok := w.(*LogResponseWriter); ok {
return wrapped
}
return &LogResponseWriter{ResponseWriter: w}
}
func Handler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer := WrapResponseWriter(w)
r = scope.HttpRequest(r)
start := time.Now()
handler.ServeHTTP(writer, r)
d := time.Since(start)
logger := scope.Logger(r.Context())
grafana := scope.Grafana(r.Context())
if grafana != "" {
logger = logger.With(zap.String("grafana", grafana))
}
logger.Info("access",
zap.Duration("time", d),
zap.String("method", r.Method),
zap.String("url", r.URL.String()),
zap.String("peer", r.RemoteAddr),
zap.Int("status", writer.Status()),
)
})
}
func main() {
rand.Seed(time.Now().UnixNano())
var err error
/* CONFIG start */
configFile := flag.String("config", "/etc/graphite-clickhouse/graphite-clickhouse.conf", "Filename of config")
printDefaultConfig := flag.Bool("config-print-default", false, "Print default config")
checkConfig := flag.Bool("check-config", false, "Check config and exit")
buildTags := flag.Bool("tags", false, "Build tags table")
pprof := flag.String("pprof", "", "Additional pprof listen addr for non-server modes (tagger, etc..)")
printVersion := flag.Bool("version", false, "Print version")
flag.Parse()
if *printVersion {
fmt.Print(Version)
return
}
if *printDefaultConfig {
if err = config.PrintDefaultConfig(); err != nil {
log.Fatal(err)
}
return
}
cfg, err := config.ReadConfig(*configFile)
if err != nil {
log.Fatal(err)
}
// config parsed successfully. Exit in check-only mode
if *checkConfig {
return
}
if err = zapwriter.ApplyConfig(cfg.Logging); err != nil {
log.Fatal(err)
}
runtime.GOMAXPROCS(cfg.Common.MaxCPU)
if cfg.Common.MemoryReturnInterval.Duration > 0 {
go func() {
t := time.NewTicker(cfg.Common.MemoryReturnInterval.Duration)
for {
<-t.C
debug.FreeOSMemory()
}
}()
}
/* CONFIG end */
if pprof != nil && *pprof != "" {
go func() { log.Fatal(http.ListenAndServe(*pprof, nil)) }()
}
/* CONSOLE COMMANDS start */
if *buildTags {
if err := tagger.Make(cfg); err != nil {
log.Fatal(err)
}
return
}
/* CONSOLE COMMANDS end */
http.Handle("/metrics/find/", Handler(find.NewHandler(cfg)))
http.Handle("/metrics/index.json", Handler(index.NewHandler(cfg)))
http.Handle("/render/", Handler(render.NewHandler(cfg)))
http.Handle("/tags/autoComplete/tags", Handler(autocomplete.NewTags(cfg)))
http.Handle("/tags/autoComplete/values", Handler(autocomplete.NewValues(cfg)))
http.HandleFunc("/debug/config", func(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
})
http.Handle("/", Handler(prometheus.NewHandler(cfg)))
log.Fatal(http.ListenAndServe(cfg.Common.Listen, nil))
}