Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log carbonapi uuid and request headers #175

Merged
merged 7 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/finder"
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
"github.com/lomik/graphite-clickhouse/helper/headers"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"github.com/lomik/graphite-clickhouse/pkg/where"
"go.uber.org/zap"
)

type Handler struct {
Expand All @@ -40,6 +42,14 @@ func NewValues(config *config.Config) *Handler {

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := scope.Logger(r.Context()).Named("autocomplete")
carbonapiUUID := r.Header.Get("X-Ctx-Carbonapi-Uuid")
if carbonapiUUID != "" {
logger = logger.With(zap.String("carbonapi_uuid", carbonapiUUID))
}
requestHeaders := headers.GetHeaders(&r.Header, h.config.Common.HeadersToLog)
if len(requestHeaders) > 0 {
logger = logger.With(zap.Any("request_headers", requestHeaders))
}
r = r.WithContext(scope.WithLogger(r.Context(), logger))

// Don't process, if the tagged table is not set
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Common struct {
TargetBlacklist []string `toml:"target-blacklist" json:"target-blacklist" comment:"daemon returns empty response if query matches any of regular expressions" commented:"true"`
Blacklist []*regexp.Regexp `toml:"-" json:"-"` // compiled TargetBlacklist
MemoryReturnInterval time.Duration `toml:"memory-return-interval" json:"memory-return-interval" comment:"daemon will return the freed memory to the OS when it>0"`
HeadersToLog []string `toml:"headers-to-log" json:"headers-to-log" comment:"additional request headers to log"`
}

// IndexReverseRule contains rules to use direct or reversed request to index table
Expand Down
2 changes: 2 additions & 0 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ It's possible to set multiple loggers. See `Config` description in [config.go](h
# target-blacklist = []
# daemon will return the freed memory to the OS when it>0
memory-return-interval = "0s"
# additional request headers to log
headers-to-log = []

[clickhouse]
# see https://clickhouse.tech/docs/en/interfaces/http
Expand Down
10 changes: 10 additions & 0 deletions find/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
v3pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
"github.com/lomik/graphite-clickhouse/helper/headers"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"go.uber.org/zap"
)

type Handler struct {
Expand All @@ -23,6 +25,14 @@ func NewHandler(config *config.Config) *Handler {

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := scope.Logger(r.Context()).Named("metrics-find")
carbonapiUUID := r.Header.Get("X-Ctx-Carbonapi-Uuid")
if carbonapiUUID != "" {
logger = logger.With(zap.String("carbonapi_uuid", carbonapiUUID))
}
requestHeaders := headers.GetHeaders(&r.Header, h.config.Common.HeadersToLog)
if len(requestHeaders) > 0 {
logger = logger.With(zap.Any("request_headers", requestHeaders))
}
r = r.WithContext(scope.WithLogger(r.Context(), logger))
r.ParseMultipartForm(1024 * 1024)

Expand Down
21 changes: 13 additions & 8 deletions graphite-clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lomik/graphite-clickhouse/capabilities"
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/find"
"github.com/lomik/graphite-clickhouse/helper/headers"
"github.com/lomik/graphite-clickhouse/index"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"github.com/lomik/graphite-clickhouse/prometheus"
Expand Down Expand Up @@ -59,7 +60,7 @@ func WrapResponseWriter(w http.ResponseWriter) *LogResponseWriter {
return &LogResponseWriter{ResponseWriter: w}
}

func Handler(handler http.Handler) http.Handler {
func Handler(handler http.Handler, cfg *config.Config) http.Handler {
msaf1980 marked this conversation as resolved.
Show resolved Hide resolved
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer := WrapResponseWriter(w)

Expand All @@ -83,6 +84,10 @@ func Handler(handler http.Handler) http.Handler {
if carbonapiUUID != "" {
logger = logger.With(zap.String("carbonapi_uuid", carbonapiUUID))
}
requestHeaders := headers.GetHeaders(&r.Header, cfg.Common.HeadersToLog)
if len(requestHeaders) > 0 {
logger = logger.With(zap.Any("request_headers", requestHeaders))
}

var peer string
if peer = r.Header.Get("X-Real-Ip"); peer == "" {
Expand Down Expand Up @@ -184,12 +189,12 @@ func main() {
/* CONSOLE COMMANDS end */

mux := http.NewServeMux()
mux.Handle("/_internal/capabilities/", Handler(capabilities.NewHandler(cfg)))
mux.Handle("/metrics/find/", Handler(find.NewHandler(cfg)))
mux.Handle("/metrics/index.json", Handler(index.NewHandler(cfg)))
mux.Handle("/render/", Handler(render.NewHandler(cfg)))
mux.Handle("/tags/autoComplete/tags", Handler(autocomplete.NewTags(cfg)))
mux.Handle("/tags/autoComplete/values", Handler(autocomplete.NewValues(cfg)))
mux.Handle("/_internal/capabilities/", Handler(capabilities.NewHandler(cfg), cfg))
mux.Handle("/metrics/find/", Handler(find.NewHandler(cfg), cfg))
mux.Handle("/metrics/index.json", Handler(index.NewHandler(cfg), cfg))
mux.Handle("/render/", Handler(render.NewHandler(cfg), cfg))
mux.Handle("/tags/autoComplete/tags", Handler(autocomplete.NewTags(cfg), cfg))
mux.Handle("/tags/autoComplete/values", Handler(autocomplete.NewValues(cfg), cfg))
mux.HandleFunc("/debug/config", func(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
Expand All @@ -199,7 +204,7 @@ func main() {
w.Write(b)
})

mux.Handle("/", Handler(prometheus.NewHandler(cfg)))
mux.Handle("/", Handler(prometheus.NewHandler(cfg), cfg))

log.Fatal(http.ListenAndServe(cfg.Common.Listen, mux))
}
17 changes: 17 additions & 0 deletions helper/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package headers

import "net/http"

func GetHeaders(header *http.Header, keys []string) map[string]string {
if len(keys) > 0 {
headers := make(map[string]string)
for _, key := range keys {
value := header.Get(key)
if len(value) > 0 {
headers[key] = value
}
}
return headers
}
return nil
}
9 changes: 9 additions & 0 deletions render/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/finder"
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
"github.com/lomik/graphite-clickhouse/helper/headers"
"github.com/lomik/graphite-clickhouse/pkg/alias"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"github.com/lomik/graphite-clickhouse/render/data"
Expand All @@ -33,6 +34,14 @@ func NewHandler(config *config.Config) *Handler {

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := scope.Logger(r.Context()).Named("render")
carbonapiUUID := r.Header.Get("X-Ctx-Carbonapi-Uuid")
if carbonapiUUID != "" {
logger = logger.With(zap.String("carbonapi_uuid", carbonapiUUID))
}
requestHeaders := headers.GetHeaders(&r.Header, h.config.Common.HeadersToLog)
if len(requestHeaders) > 0 {
logger = logger.With(zap.Any("request_headers", requestHeaders))
}
r = r.WithContext(scope.WithLogger(r.Context(), logger))

var err error
Expand Down