Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(587)

Side by Side Diff: appengine/tsmon/middleware.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // Package tsmon adapts common/tsmon library to GAE environment. 5 // Package tsmon adapts common/tsmon library to GAE environment.
6 // 6 //
7 // It configures tsmon state with a monitor and store suitable for GAE 7 // It configures tsmon state with a monitor and store suitable for GAE
8 // environment and controls when metric flushes happen. 8 // environment and controls when metric flushes happen.
9 package tsmon 9 package tsmon
10 10
11 import ( 11 import (
12 "fmt" 12 "fmt"
13 "net/http"
14 "strings" 13 "strings"
15 "sync" 14 "sync"
16 "time" 15 "time"
17 16
18 "github.com/golang/protobuf/proto" 17 "github.com/golang/protobuf/proto"
19 "github.com/julienschmidt/httprouter"
20 "golang.org/x/net/context" 18 "golang.org/x/net/context"
21 19
22 "github.com/luci/gae/service/info" 20 "github.com/luci/gae/service/info"
23 gaeauth "github.com/luci/luci-go/appengine/gaeauth/client" 21 gaeauth "github.com/luci/luci-go/appengine/gaeauth/client"
24 "github.com/luci/luci-go/common/clock" 22 "github.com/luci/luci-go/common/clock"
25 gcps "github.com/luci/luci-go/common/gcloud/pubsub" 23 gcps "github.com/luci/luci-go/common/gcloud/pubsub"
26 "github.com/luci/luci-go/common/logging" 24 "github.com/luci/luci-go/common/logging"
27 "github.com/luci/luci-go/common/tsmon" 25 "github.com/luci/luci-go/common/tsmon"
28 "github.com/luci/luci-go/common/tsmon/monitor" 26 "github.com/luci/luci-go/common/tsmon/monitor"
29 "github.com/luci/luci-go/common/tsmon/store" 27 "github.com/luci/luci-go/common/tsmon/store"
30 "github.com/luci/luci-go/common/tsmon/target" 28 "github.com/luci/luci-go/common/tsmon/target"
31 » "github.com/luci/luci-go/server/middleware" 29 » "github.com/luci/luci-go/server/router"
32 ) 30 )
33 31
34 // State holds the configuration of the tsmon library for GAE. 32 // State holds the configuration of the tsmon library for GAE.
35 // 33 //
36 // Define it as a global variable and inject it in the request contexts using 34 // Define it as a global variable and inject it in the request contexts using
37 // State.Middleware(). 35 // State.Middleware().
38 // 36 //
39 // It will initialize itself from the tsmon state in the passed context on 37 // It will initialize itself from the tsmon state in the passed context on
40 // a first use, mutating it along the way. Assumes caller is consistently using 38 // a first use, mutating it along the way. Assumes caller is consistently using
41 // contexts configured with exact same tsmon state (in a vast majority of cases 39 // contexts configured with exact same tsmon state (in a vast majority of cases
(...skipping 12 matching lines...) Expand all
54 lastFlushed time.Time 52 lastFlushed time.Time
55 53
56 // testingMonitor is mocked monitor used in unit tests. 54 // testingMonitor is mocked monitor used in unit tests.
57 testingMonitor monitor.Monitor 55 testingMonitor monitor.Monitor
58 // testingSettings if not nil are used in unit tests. 56 // testingSettings if not nil are used in unit tests.
59 testingSettings *tsmonSettings 57 testingSettings *tsmonSettings
60 } 58 }
61 59
62 // Middleware returns a middleware that must be inserted into the chain to 60 // Middleware returns a middleware that must be inserted into the chain to
63 // enable tsmon metrics to be sent on App Engine. 61 // enable tsmon metrics to be sent on App Engine.
64 func (s *State) Middleware(h middleware.Handler) middleware.Handler { 62 func (s *State) Middleware() router.Handler {
65 » return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 63 » return func(c *router.Context) {
66 » » state, settings := s.checkSettings(c) 64 » » state, settings := s.checkSettings(c.Context)
67 » » h(c, rw, r, p) 65 » » c.Next()
68 if settings.Enabled { 66 if settings.Enabled {
69 » » » s.flushIfNeeded(c, state, settings) 67 » » » s.flushIfNeeded(c.Context, state, settings)
70 } 68 }
71 } 69 }
72 } 70 }
73 71
74 // checkSettings fetches tsmon settings and initializes, reinitializes or 72 // checkSettings fetches tsmon settings and initializes, reinitializes or
75 // deinitializes tsmon, as needed. 73 // deinitializes tsmon, as needed.
76 // 74 //
77 // Returns current tsmon state and settings. Panics if the context is using 75 // Returns current tsmon state and settings. Panics if the context is using
78 // unexpected tsmon state. 76 // unexpected tsmon state.
79 func (s *State) checkSettings(c context.Context) (*tsmon.State, *tsmonSettings) { 77 func (s *State) checkSettings(c context.Context) (*tsmon.State, *tsmonSettings) {
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 286 }
289 } 287 }
290 288
291 if err := state.Flush(c, mon); err != nil { 289 if err := state.Flush(c, mon); err != nil {
292 return err 290 return err
293 } 291 }
294 292
295 state.ResetGlobalCallbackMetrics(c) 293 state.ResetGlobalCallbackMetrics(c)
296 return nil 294 return nil
297 } 295 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698