11package main
22
33import (
4- "encoding/binary"
54 "encoding/json"
65 "flag"
76 "fmt"
87 "log"
98 "math/rand"
109 "net/http"
11- "regexp"
1210 "runtime"
1311 "time"
1412
1513 "github.com/lomik/graphite-clickhouse/autocomplete"
1614 "github.com/lomik/graphite-clickhouse/config"
1715 "github.com/lomik/graphite-clickhouse/find"
18- "github.com/lomik/graphite-clickhouse/helper/version"
1916 "github.com/lomik/graphite-clickhouse/index"
2017 "github.com/lomik/graphite-clickhouse/pkg/scope"
2118 "github.com/lomik/graphite-clickhouse/prometheus"
@@ -31,7 +28,7 @@ import (
3128const Version = "0.11.5"
3229
3330func init () {
34- version .Version = Version
31+ scope .Version = Version
3532}
3633
3734type LogResponseWriter struct {
@@ -58,43 +55,23 @@ func WrapResponseWriter(w http.ResponseWriter) *LogResponseWriter {
5855 return & LogResponseWriter {ResponseWriter : w }
5956}
6057
61- var requestIdRegexp * regexp.Regexp = regexp .MustCompile ("^[a-zA-Z0-9_.-]+$" )
62- var passHeaders = []string {
63- "X-Dashboard-Id" ,
64- "X-Grafana-Org-Id" ,
65- "X-Panel-Id" ,
66- }
67-
68- func Handler (logger * zap.Logger , handler http.Handler ) http.Handler {
58+ func Handler (handler http.Handler ) http.Handler {
6959 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
7060 writer := WrapResponseWriter (w )
7161
72- requestID := r .Header .Get ("X-Request-Id" )
73- if requestID == "" || ! requestIdRegexp .MatchString (requestID ) {
74- var b [16 ]byte
75- binary .LittleEndian .PutUint64 (b [:], rand .Uint64 ())
76- binary .LittleEndian .PutUint64 (b [8 :], rand .Uint64 ())
77- requestID = fmt .Sprintf ("%x" , b )
78- }
62+ r = scope .HttpRequest (r )
7963
80- logger := logger .With (zap .String ("request_id" , requestID ))
64+ start := time .Now ()
65+ handler .ServeHTTP (writer , r )
66+ d := time .Since (start )
8167
82- ctx := r .Context ()
83- ctx = scope .WithLogger (ctx , logger )
84- ctx = scope .WithRequestID (ctx , requestID )
68+ logger := scope .Logger (r .Context ())
8569
86- for _ , h := range passHeaders {
87- hv := r .Header .Get (h )
88- if hv != "" {
89- ctx = scope .With (ctx , h , hv )
90- }
70+ grafana := scope .Grafana (r .Context ())
71+ if grafana != "" {
72+ logger = logger .With (zap .String ("grafana" , grafana ))
9173 }
9274
93- r = r .WithContext (ctx )
94-
95- start := time .Now ()
96- handler .ServeHTTP (writer , r )
97- d := time .Since (start )
9875 logger .Info ("access" ,
9976 zap .Duration ("time" , d ),
10077 zap .String ("method" , r .Method ),
@@ -166,11 +143,11 @@ func main() {
166143
167144 /* CONSOLE COMMANDS end */
168145
169- http .Handle ("/metrics/find/" , Handler (zapwriter . Default (), find .NewHandler (cfg )))
170- http .Handle ("/metrics/index.json" , Handler (zapwriter . Default (), index .NewHandler (cfg )))
171- http .Handle ("/render/" , Handler (zapwriter . Default (), render .NewHandler (cfg )))
172- http .Handle ("/tags/autoComplete/tags" , Handler (zapwriter . Default (), autocomplete .NewTags (cfg )))
173- http .Handle ("/tags/autoComplete/values" , Handler (zapwriter . Default (), autocomplete .NewValues (cfg )))
146+ http .Handle ("/metrics/find/" , Handler (find .NewHandler (cfg )))
147+ http .Handle ("/metrics/index.json" , Handler (index .NewHandler (cfg )))
148+ http .Handle ("/render/" , Handler (render .NewHandler (cfg )))
149+ http .Handle ("/tags/autoComplete/tags" , Handler (autocomplete .NewTags (cfg )))
150+ http .Handle ("/tags/autoComplete/values" , Handler (autocomplete .NewValues (cfg )))
174151 http .HandleFunc ("/debug/config" , func (w http.ResponseWriter , r * http.Request ) {
175152 b , err := json .MarshalIndent (cfg , "" , " " )
176153 if err != nil {
@@ -180,7 +157,7 @@ func main() {
180157 w .Write (b )
181158 })
182159
183- http .Handle ("/" , Handler (zapwriter . Default (), prometheus .NewHandler (cfg )))
160+ http .Handle ("/" , Handler (prometheus .NewHandler (cfg )))
184161
185162 log .Fatal (http .ListenAndServe (cfg .Common .Listen , nil ))
186163}
0 commit comments