Support LongQueryTime as config option

This commit is contained in:
Alan Bernstein 2017-04-21 18:50:25 -05:00
parent a1ba58398d
commit 6a162c4be2
5 changed files with 18 additions and 8 deletions

View file

@ -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.")

View file

@ -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"`

View file

@ -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.

View file

@ -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
}

View file

@ -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
}