Skip to content

Commit 9c9c96f

Browse files
committed
grafana dashboard in access log and user agent
1 parent 97597e2 commit 9c9c96f

File tree

6 files changed

+92
-48
lines changed

6 files changed

+92
-48
lines changed

graphite-clickhouse.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package main
22

33
import (
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 (
3128
const Version = "0.11.5"
3229

3330
func init() {
34-
version.Version = Version
31+
scope.Version = Version
3532
}
3633

3734
type 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
}

helper/clickhouse/clickhouse.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/lomik/graphite-clickhouse/helper/version"
1817
"github.com/lomik/graphite-clickhouse/pkg/dry"
1918
"github.com/lomik/graphite-clickhouse/pkg/scope"
2019
"github.com/lomik/zapwriter"
@@ -196,7 +195,7 @@ func reader(ctx context.Context, dsn string, query string, postBody io.Reader, g
196195
return
197196
}
198197

199-
req.Header.Add("User-Agent", fmt.Sprintf("graphite-clickhouse/%s (table:%s)", version.Version, scope.Table(ctx)))
198+
req.Header.Add("User-Agent", scope.ClickhouseUserAgent(ctx))
200199

201200
if gzip {
202201
req.Header.Add("Content-Encoding", "gzip")

pkg/scope/http_request.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package scope
2+
3+
import (
4+
"context"
5+
"encoding/binary"
6+
"fmt"
7+
"math/rand"
8+
"net/http"
9+
"regexp"
10+
)
11+
12+
var requestIdRegexp *regexp.Regexp = regexp.MustCompile("^[a-zA-Z0-9_.-]+$")
13+
var passHeaders = []string{
14+
"X-Dashboard-Id",
15+
"X-Grafana-Org-Id",
16+
"X-Panel-Id",
17+
}
18+
19+
func HttpRequest(r *http.Request) *http.Request {
20+
requestID := r.Header.Get("X-Request-Id")
21+
if requestID == "" || !requestIdRegexp.MatchString(requestID) {
22+
var b [16]byte
23+
binary.LittleEndian.PutUint64(b[:], rand.Uint64())
24+
binary.LittleEndian.PutUint64(b[8:], rand.Uint64())
25+
requestID = fmt.Sprintf("%x", b)
26+
}
27+
28+
ctx := r.Context()
29+
ctx = WithRequestID(ctx, requestID)
30+
31+
for _, h := range passHeaders {
32+
hv := r.Header.Get(h)
33+
if hv != "" {
34+
ctx = With(ctx, h, hv)
35+
}
36+
}
37+
38+
return r.WithContext(ctx)
39+
}
40+
41+
func Grafana(ctx context.Context) string {
42+
o, d, p := String(ctx, "X-Grafana-Org-Id"), String(ctx, "X-Dashboard-Id"), String(ctx, "X-Panel-Id")
43+
if o != "" || d != "" || p != "" {
44+
return fmt.Sprintf("Org:%s; Dashboard:%s; Panel:%s", o, d, p)
45+
}
46+
return ""
47+
}

pkg/scope/key.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package scope
22

3-
import "context"
3+
import (
4+
"context"
5+
"fmt"
6+
)
47

58
// key is type for context.Value keys
69
type scopeKey string
@@ -37,3 +40,12 @@ func WithTable(ctx context.Context, table string) context.Context {
3740
func Table(ctx context.Context) string {
3841
return String(ctx, "table")
3942
}
43+
44+
// ClickhouseUserAgent ...
45+
func ClickhouseUserAgent(ctx context.Context) string {
46+
grafana := Grafana(ctx)
47+
if grafana != "" {
48+
return fmt.Sprintf("Graphite-Clickhouse/%s (table:%s) Grafana(%s)", Version, Table(ctx), grafana)
49+
}
50+
return fmt.Sprintf("Graphite-Clickhouse/%s (table:%s)", Version, Table(ctx))
51+
}

pkg/scope/logger.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@ package scope
33
import (
44
"context"
55

6+
"github.com/lomik/zapwriter"
67
"go.uber.org/zap"
78
)
89

910
// Logger returns zap.Logger instance
1011
func Logger(ctx context.Context) *zap.Logger {
1112
logger := ctx.Value(scopeKey("logger"))
12-
if logger == nil {
13-
return zap.NewNop()
13+
var zapLogger *zap.Logger
14+
if logger != nil {
15+
if zl, ok := logger.(*zap.Logger); ok {
16+
zapLogger = zl
17+
}
1418
}
15-
if zapLogger, ok := logger.(*zap.Logger); ok {
16-
return zapLogger
19+
if zapLogger == nil {
20+
zapLogger = zapwriter.Default()
1721
}
1822

19-
return zap.NewNop()
23+
requestId := RequestID(ctx)
24+
if requestId != "" {
25+
zapLogger = zapLogger.With(zap.String("request_id", requestId))
26+
}
27+
28+
return zapLogger
2029
}
2130

2231
// WithLogger ...
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package version
1+
package scope
22

33
var Version string

0 commit comments

Comments
 (0)