From 6a162c4be29709e62d755fa14cc820f52e8e9e5b Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Fri, 21 Apr 2017 18:50:25 -0500 Subject: [PATCH] Support LongQueryTime as config option --- cmd/server.go | 1 + config.go | 5 +++-- handler.go | 15 ++++++++++----- server.go | 4 +++- server/server.go | 1 + 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index d9863c47a..14ec2e7a3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -75,6 +75,7 @@ on the configured port.`, flags.StringVarP(&Server.Config.DataDir, "data-dir", "d", "~/.pilosa", "Directory to store pilosa data files.") flags.StringVarP(&Server.Config.Host, "bind", "b", ":10101", "Default URI on which pilosa should listen.") + flags.DurationVarP((*time.Duration)(&Server.Config.LongQueryTime), "long-query-time", "", 10*time.Second, "Threshold for logging long-running queries (0 to disable)") flags.IntVarP(&Server.Config.Cluster.ReplicaN, "cluster.replicas", "", 1, "Number of hosts each piece of data should be stored on.") flags.StringSliceVarP(&Server.Config.Cluster.Hosts, "cluster.hosts", "", []string{}, "Comma separated list of hosts in cluster.") flags.StringSliceVarP(&Server.Config.Cluster.InternalHosts, "cluster.internal-hosts", "", []string{}, "Comma separated list of hosts in cluster used for internal communication.") diff --git a/config.go b/config.go index a0b51a503..b0c0b9ee5 100644 --- a/config.go +++ b/config.go @@ -13,8 +13,9 @@ const ( // Config represents the configuration for the command. type Config struct { - DataDir string `toml:"data-dir"` - Host string `toml:"host"` + DataDir string `toml:"data-dir"` + Host string `toml:"host"` + LongQueryTime Duration `toml:"long-query-time"` Cluster struct { ReplicaN int `toml:"replicas"` diff --git a/handler.go b/handler.go index 542de01c1..3083d4695 100644 --- a/handler.go +++ b/handler.go @@ -41,6 +41,9 @@ type Handler struct { // The writer for any logging. LogOutput io.Writer + + // Threshold for logging long-running queries + LongQueryTime time.Duration } // externalPrefixFlag denotes endpoints that are intended to be exposed to clients. @@ -112,14 +115,16 @@ func (h *Handler) methodNotAllowedHandler(w http.ResponseWriter, r *http.Request func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t := time.Now() h.Router.ServeHTTP(w, r) - dif := time.Since(t).Seconds() + dif := time.Since(t) // Handle some stats tagging statsTags := make([]string, 0, 3) - if dif > 90 { - h.logger().Printf("%s %s %.03fs", r.Method, r.URL.String(), dif) - statsTags = append(statsTags, "longrunning") + fmt.Printf("long query time: %v\n", h.LongQueryTime) + + if h.LongQueryTime > 0 && dif > h.LongQueryTime { + h.logger().Printf("%s %s %.03fs", r.Method, r.URL.String(), float64(dif)) + statsTags = append(statsTags, "slow_query") } pathParts := strings.Split(r.URL.Path, "/") @@ -133,7 +138,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { statsTags = append(statsTags, "useragent:"+r.UserAgent()) stats := h.Index.Stats.WithTags(statsTags...) - stats.Histogram("http_"+endpointName, dif) + stats.Histogram("http_"+endpointName, float64(dif)) } // handleGetSchema handles GET /schema requests. diff --git a/server.go b/server.go index d8a2e458b..36aac1708 100644 --- a/server.go +++ b/server.go @@ -50,6 +50,9 @@ type Server struct { PollingInterval time.Duration MetricInterval time.Duration + // Threshold for logging long queries + LongQueryTime time.Duration + LogOutput io.Writer } @@ -71,7 +74,6 @@ func NewServer() *Server { } s.Handler.Index = s.Index - return s } diff --git a/server/server.go b/server/server.go index 7e4171d3d..9ce842376 100644 --- a/server/server.go +++ b/server/server.go @@ -174,6 +174,7 @@ func (m *Command) SetupServer() error { // Set configuration options. m.Server.AntiEntropyInterval = time.Duration(m.Config.AntiEntropy.Interval) + m.Server.Handler.LongQueryTime = time.Duration(m.Config.LongQueryTime) return nil }