-
Notifications
You must be signed in to change notification settings - Fork 17
/
tasks_enabled.go
182 lines (166 loc) · 5.16 KB
/
tasks_enabled.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !no_mon
package monitor
import (
"fmt"
"sort"
"github.com/spacemonkeygo/errors"
"github.com/spacemonkeygo/monotime"
)
const (
secondInMicroseconds = 1000000
microsecondInNanoseconds = 1000
)
// Start is a helper method for watching a task in a less error-prone way.
// Managing a task context yourself is tricky to get right - recover only works
// in deferred methods. Call out of a method that was deferred and it no longer
// works! See the example.
func (t *TaskMonitor) Start() func(*error) {
ctx := t.NewContext()
return func(e *error) { ctx.Finish(e, recover()) }
}
// NewContext creates a new context that is watching a live task. See Start
// or MonitorGroup.Task
func (t *TaskMonitor) NewContext() *TaskCtx {
c := &TaskCtx{start: monotime.Monotonic(), monitor: t}
t.mtx.Lock()
t.current += 1
t.total_started += 1
if t.current > t.highwater {
t.highwater = t.current
}
t.running[c] = true
t.mtx.Unlock()
return c
}
// Stats conforms to the Monitor interface
func (t *TaskMonitor) Stats(cb func(name string, val float64)) {
t.mtx.Lock()
current := t.current
highwater := t.highwater
total_started := t.total_started
total_completed := t.total_completed
success := t.success
panics := t.panics
error_counts := make(map[string]uint64, len(t.errors))
for error, count := range t.errors {
error_counts[error] = count
}
t.mtx.Unlock()
errors := make([]string, 0, len(error_counts))
for error := range error_counts {
errors = append(errors, error)
}
sort.Strings(errors)
cb("current", float64(current))
for _, error := range errors {
cb(fmt.Sprintf("error_%s", error), float64(error_counts[error]))
}
cb("highwater", float64(highwater))
cb("panics", float64(panics))
cb("success", float64(success))
if len(errors) > 0 {
t.error_timing.Stats(func(name string, val float64) {
if name != "count" {
// these values are in microseconds, convert to seconds
cb(fmt.Sprintf("time_error_%s", name), val/secondInMicroseconds)
}
})
}
if success > 0 {
t.success_timing.Stats(func(name string, val float64) {
if name != "count" {
// these values are in microseconds, convert to seconds
cb(fmt.Sprintf("time_success_%s", name), val/secondInMicroseconds)
}
})
}
if total_completed > 0 {
t.total_timing.Stats(func(name string, val float64) {
if name != "count" {
// these values are in microseconds, convert to seconds
cb(fmt.Sprintf("time_total_%s", name), val/secondInMicroseconds)
}
})
}
cb("total_completed", float64(total_completed))
cb("total_started", float64(total_started))
}
// Finish records a successful task completion. You must pass a pointer to
// the named error return value (or nil if there isn't one) and the result
// of recover() out of the method that was deferred for this to work right.
// Finish will re-panic any recovered panics (provided it wasn't a nil panic)
// after bookkeeping.
func (c *TaskCtx) Finish(err_ref *error, rec interface{}) {
duration_nanoseconds := int64(c.ElapsedTime())
var error_name string
var err error
if err_ref != nil {
err = *err_ref
}
if rec != nil {
var ok bool
err, ok = rec.(error)
if !ok || err == nil {
err = errors.PanicError.New("%v", rec)
}
}
if err != nil {
error_name = errors.GetClass(err).String()
max_len := Config.MaxErrorLength
if len(error_name) > max_len {
error_name = error_name[:max_len]
}
error_name = SanitizeName(error_name)
}
// we keep granularity on the order microseconds, which should keep
// sum_squared useful
duration_microseconds := int64(duration_nanoseconds /
microsecondInNanoseconds)
c.monitor.mtx.Lock()
c.monitor.current -= 1
c.monitor.total_completed += 1
delete(c.monitor.running, c)
if err != nil {
c.monitor.errors[error_name] += 1
if rec != nil {
c.monitor.panics += 1
}
c.monitor.error_timing.Add(duration_microseconds)
} else {
c.monitor.success_timing.Add(duration_microseconds)
c.monitor.success += 1
}
c.monitor.mtx.Unlock()
c.monitor.total_timing.Add(duration_microseconds)
// doh, we didn't actually want to stop the panic codepath.
// we have to repanic. Oh and great, panics can be nil. Welp!
if rec != nil {
panic(rec)
}
}
// Running returns a list of tasks that are currently running. Each TaskCtx
// can tell how long it's been since the task was started, though keep in mind
// that the task might finish between calling (*TaskMonitor).Running() and
// (*TaskCtx).ElapsedTime()
func (t *TaskMonitor) Running() (rv []*TaskCtx) {
t.mtx.Lock()
rv = make([]*TaskCtx, 0, len(t.running))
for task_ctx := range t.running {
rv = append(rv, task_ctx)
}
t.mtx.Unlock()
return rv
}