| OLD | NEW |
| 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 | 5 package tsmon |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 "net/http/httptest" | 9 "net/http/httptest" |
| 10 "testing" | 10 "testing" |
| 11 "time" | 11 "time" |
| 12 | 12 |
| 13 "github.com/golang/protobuf/proto" | 13 "github.com/golang/protobuf/proto" |
| 14 "github.com/julienschmidt/httprouter" | 14 "github.com/julienschmidt/httprouter" |
| 15 "github.com/luci/gae/service/datastore" | 15 "github.com/luci/gae/service/datastore" |
| 16 "github.com/luci/luci-go/common/tsmon" | 16 "github.com/luci/luci-go/common/tsmon" |
| 17 "github.com/luci/luci-go/common/tsmon/field" | 17 "github.com/luci/luci-go/common/tsmon/field" |
| 18 "github.com/luci/luci-go/common/tsmon/store" | 18 "github.com/luci/luci-go/common/tsmon/store" |
| 19 "github.com/luci/luci-go/common/tsmon/store/storetest" | 19 "github.com/luci/luci-go/common/tsmon/store/storetest" |
| 20 "github.com/luci/luci-go/common/tsmon/target" | 20 "github.com/luci/luci-go/common/tsmon/target" |
| 21 "github.com/luci/luci-go/common/tsmon/types" | 21 "github.com/luci/luci-go/common/tsmon/types" |
| 22 "github.com/luci/luci-go/server/router" |
| 22 "golang.org/x/net/context" | 23 "golang.org/x/net/context" |
| 23 | 24 |
| 24 . "github.com/smartystreets/goconvey/convey" | 25 . "github.com/smartystreets/goconvey/convey" |
| 25 ) | 26 ) |
| 26 | 27 |
| 27 func TestMiddleware(t *testing.T) { | 28 func TestMiddleware(t *testing.T) { |
| 28 metric := &storetest.FakeMetric{"m", "", []field.Field{}, types.Cumulati
veIntType} | 29 metric := &storetest.FakeMetric{"m", "", []field.Field{}, types.Cumulati
veIntType} |
| 29 | 30 |
| 30 » f := func(c context.Context, rw http.ResponseWriter, r *http.Request, p
httprouter.Params) { | 31 » initialize := func(c context.Context, w http.ResponseWriter, r *http.Req
uest, p httprouter.Params) router.Handler { |
| 31 » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeFalse) | 32 » » return func(ctx *router.Context) { |
| 33 » » » ctx.Context = c |
| 34 » » » ctx.Writer = w |
| 35 » » » ctx.Request = r |
| 36 » » » ctx.Params = p |
| 37 » » } |
| 38 » } |
| 32 | 39 |
| 33 » » tsmon.Register(c, metric) | 40 » f := func(c *router.Context) { |
| 34 » » So(tsmon.Store(c).Incr(c, metric, time.Time{}, []interface{}{},
int64(1)), ShouldBeNil) | 41 » » So(store.IsNilStore(tsmon.Store(c.Context)), ShouldBeFalse) |
| 42 |
| 43 » » tsmon.Register(c.Context, metric) |
| 44 » » So(tsmon.Store(c.Context).Incr(c.Context, metric, time.Time{}, [
]interface{}{}, int64(1)), ShouldBeNil) |
| 35 } | 45 } |
| 36 | 46 |
| 37 Convey("Creates instance entity", t, func() { | 47 Convey("Creates instance entity", t, func() { |
| 38 c, _ := buildGAETestContext() | 48 c, _ := buildGAETestContext() |
| 39 state, monitor := buildTestState() | 49 state, monitor := buildTestState() |
| 40 ds := datastore.Get(c) | 50 ds := datastore.Get(c) |
| 41 | 51 |
| 42 exists, err := ds.Exists(ds.NewKey("Instance", instanceEntityID(
c), 0, nil)) | 52 exists, err := ds.Exists(ds.NewKey("Instance", instanceEntityID(
c), 0, nil)) |
| 43 So(err, ShouldBeNil) | 53 So(err, ShouldBeNil) |
| 44 So(exists.All(), ShouldBeFalse) | 54 So(exists.All(), ShouldBeFalse) |
| 45 | 55 |
| 46 rec := httptest.NewRecorder() | 56 rec := httptest.NewRecorder() |
| 47 » » state.Middleware(f)(c, rec, &http.Request{}, nil) | 57 » » router.ChainHandlers(initialize(c, rec, &http.Request{}, nil), s
tate.Middleware(), f)() |
| 48 So(rec.Code, ShouldEqual, http.StatusOK) | 58 So(rec.Code, ShouldEqual, http.StatusOK) |
| 49 | 59 |
| 50 exists, err = ds.Exists(ds.NewKey("Instance", instanceEntityID(c
), 0, nil)) | 60 exists, err = ds.Exists(ds.NewKey("Instance", instanceEntityID(c
), 0, nil)) |
| 51 So(err, ShouldBeNil) | 61 So(err, ShouldBeNil) |
| 52 So(exists.All(), ShouldBeTrue) | 62 So(exists.All(), ShouldBeTrue) |
| 53 | 63 |
| 54 // Shouldn't flush since the instance entity doesn't have a task
number yet. | 64 // Shouldn't flush since the instance entity doesn't have a task
number yet. |
| 55 So(len(monitor.Cells), ShouldEqual, 0) | 65 So(len(monitor.Cells), ShouldEqual, 0) |
| 56 }) | 66 }) |
| 57 | 67 |
| 58 Convey("Flushes after 2 minutes", t, func() { | 68 Convey("Flushes after 2 minutes", t, func() { |
| 59 c, clock := buildGAETestContext() | 69 c, clock := buildGAETestContext() |
| 60 state, monitor := buildTestState() | 70 state, monitor := buildTestState() |
| 61 | 71 |
| 62 ds := datastore.Get(c) | 72 ds := datastore.Get(c) |
| 63 | 73 |
| 64 i := instance{ | 74 i := instance{ |
| 65 ID: instanceEntityID(c), | 75 ID: instanceEntityID(c), |
| 66 TaskNum: 0, | 76 TaskNum: 0, |
| 67 LastUpdated: clock.Now().Add(-2 * time.Minute).UTC(), | 77 LastUpdated: clock.Now().Add(-2 * time.Minute).UTC(), |
| 68 } | 78 } |
| 69 So(ds.Put(&i), ShouldBeNil) | 79 So(ds.Put(&i), ShouldBeNil) |
| 70 | 80 |
| 71 state.lastFlushed = clock.Now().Add(-2 * time.Minute) | 81 state.lastFlushed = clock.Now().Add(-2 * time.Minute) |
| 72 | 82 |
| 73 rec := httptest.NewRecorder() | 83 rec := httptest.NewRecorder() |
| 74 » » state.Middleware(f)(c, rec, &http.Request{}, nil) | 84 » » router.ChainHandlers(initialize(c, rec, &http.Request{}, nil), s
tate.Middleware(), f)() |
| 75 So(rec.Code, ShouldEqual, http.StatusOK) | 85 So(rec.Code, ShouldEqual, http.StatusOK) |
| 76 | 86 |
| 77 So(len(monitor.Cells), ShouldEqual, 1) | 87 So(len(monitor.Cells), ShouldEqual, 1) |
| 78 So(monitor.Cells[0][0].Name, ShouldEqual, "m") | 88 So(monitor.Cells[0][0].Name, ShouldEqual, "m") |
| 79 So(monitor.Cells[0][0].Value, ShouldEqual, int64(1)) | 89 So(monitor.Cells[0][0].Value, ShouldEqual, int64(1)) |
| 80 | 90 |
| 81 // Flushing should update the LastUpdated time. | 91 // Flushing should update the LastUpdated time. |
| 82 inst, err := getOrCreateInstanceEntity(c) | 92 inst, err := getOrCreateInstanceEntity(c) |
| 83 So(err, ShouldBeNil) | 93 So(err, ShouldBeNil) |
| 84 So(inst.LastUpdated, ShouldResemble, clock.Now().Round(time.Seco
nd)) | 94 So(inst.LastUpdated, ShouldResemble, clock.Now().Round(time.Seco
nd)) |
| 85 | 95 |
| 86 // The value should still be set. | 96 // The value should still be set. |
| 87 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter
face{}{}) | 97 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter
face{}{}) |
| 88 So(err, ShouldBeNil) | 98 So(err, ShouldBeNil) |
| 89 So(value, ShouldEqual, int64(1)) | 99 So(value, ShouldEqual, int64(1)) |
| 90 }) | 100 }) |
| 91 | 101 |
| 92 Convey("Resets cumulative metrics", t, func() { | 102 Convey("Resets cumulative metrics", t, func() { |
| 93 c, clock := buildGAETestContext() | 103 c, clock := buildGAETestContext() |
| 94 state, monitor := buildTestState() | 104 state, monitor := buildTestState() |
| 95 | 105 |
| 96 state.lastFlushed = clock.Now().Add(-2 * time.Minute) | 106 state.lastFlushed = clock.Now().Add(-2 * time.Minute) |
| 97 | 107 |
| 98 rec := httptest.NewRecorder() | 108 rec := httptest.NewRecorder() |
| 99 » » state.Middleware(func(c context.Context, rw http.ResponseWriter,
r *http.Request, p httprouter.Params) { | 109 » » router.ChainHandlers( |
| 100 » » » f(c, rw, r, p) | 110 » » » initialize(c, rec, &http.Request{}, nil), |
| 101 | 111 » » » state.Middleware(), |
| 102 » » » // Override the TaskNum here - it's created just before
this handler runs | 112 » » » f, |
| 103 » » » // and used just after. | 113 » » » func(c *router.Context) { |
| 104 » » » tar := tsmon.Store(c).DefaultTarget().(*target.Task) | 114 » » » » // Override the TaskNum here - it's created just
before this handler runs |
| 105 » » » tar.TaskNum = proto.Int32(int32(0)) | 115 » » » » // and used just after. |
| 106 » » » tsmon.Store(c).SetDefaultTarget(tar) | 116 » » » » tar := tsmon.Store(c.Context).DefaultTarget().(*
target.Task) |
| 107 » » })(c, rec, &http.Request{}, nil) | 117 » » » » tar.TaskNum = proto.Int32(int32(0)) |
| 118 » » » » tsmon.Store(c.Context).SetDefaultTarget(tar) |
| 119 » » » }, |
| 120 » » )() |
| 108 So(rec.Code, ShouldEqual, http.StatusOK) | 121 So(rec.Code, ShouldEqual, http.StatusOK) |
| 109 | 122 |
| 110 So(len(tsmon.GetState(c).RegisteredMetrics), ShouldEqual, 1) | 123 So(len(tsmon.GetState(c).RegisteredMetrics), ShouldEqual, 1) |
| 111 So(len(monitor.Cells), ShouldEqual, 0) | 124 So(len(monitor.Cells), ShouldEqual, 0) |
| 112 | 125 |
| 113 // Value should be reset. | 126 // Value should be reset. |
| 114 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter
face{}{}) | 127 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter
face{}{}) |
| 115 So(err, ShouldBeNil) | 128 So(err, ShouldBeNil) |
| 116 So(value, ShouldBeNil) | 129 So(value, ShouldBeNil) |
| 117 }) | 130 }) |
| 118 | 131 |
| 119 Convey("Dynamic enable and disable works", t, func() { | 132 Convey("Dynamic enable and disable works", t, func() { |
| 120 c, _ := buildGAETestContext() | 133 c, _ := buildGAETestContext() |
| 121 state, _ := buildTestState() | 134 state, _ := buildTestState() |
| 122 | 135 |
| 123 // Enabled. Store is not nil. | 136 // Enabled. Store is not nil. |
| 124 rec := httptest.NewRecorder() | 137 rec := httptest.NewRecorder() |
| 125 » » state.Middleware(func(c context.Context, rw http.ResponseWriter,
r *http.Request, p httprouter.Params) { | 138 » » router.ChainHandlers( |
| 126 » » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeFalse) | 139 » » » initialize(c, rec, &http.Request{}, nil), |
| 127 » » })(c, rec, &http.Request{}, nil) | 140 » » » state.Middleware(), |
| 141 » » » func(c *router.Context) { |
| 142 » » » » So(store.IsNilStore(tsmon.Store(c.Context)), Sho
uldBeFalse) |
| 143 » » » }, |
| 144 » » )() |
| 128 So(rec.Code, ShouldEqual, http.StatusOK) | 145 So(rec.Code, ShouldEqual, http.StatusOK) |
| 129 | 146 |
| 130 // Disabled. Store is nil. | 147 // Disabled. Store is nil. |
| 131 state.testingSettings.Enabled = false | 148 state.testingSettings.Enabled = false |
| 132 » » state.Middleware(func(c context.Context, rw http.ResponseWriter,
r *http.Request, p httprouter.Params) { | 149 » » router.ChainHandlers( |
| 133 » » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeTrue) | 150 » » » initialize(c, rec, &http.Request{}, nil), |
| 134 » » })(c, rec, &http.Request{}, nil) | 151 » » » state.Middleware(), |
| 152 » » » func(c *router.Context) { |
| 153 » » » » So(store.IsNilStore(tsmon.Store(c.Context)), Sho
uldBeTrue) |
| 154 » » » }, |
| 155 » » )() |
| 135 So(rec.Code, ShouldEqual, http.StatusOK) | 156 So(rec.Code, ShouldEqual, http.StatusOK) |
| 136 }) | 157 }) |
| 137 } | 158 } |
| OLD | NEW |