-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrunner_metrics.go
234 lines (191 loc) · 6.35 KB
/
runner_metrics.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package tasks
import (
"bytes"
"context"
"runtime/pprof"
"sync"
"time"
"github.com/ydb-platform/nbs/cloud/tasks/common"
"github.com/ydb-platform/nbs/cloud/tasks/errors"
"github.com/ydb-platform/nbs/cloud/tasks/logging"
"github.com/ydb-platform/nbs/cloud/tasks/metrics"
)
////////////////////////////////////////////////////////////////////////////////
const (
checkTaskHangingPeriod = 15 * time.Second
)
////////////////////////////////////////////////////////////////////////////////
const (
// To limit printing frequency.
stackTracesPrintingCooldown = 10 * time.Minute
)
var stackTracesPrintedAtMutex sync.Mutex
var stackTracesPrintedAt time.Time
func printStackTraces() string {
stackTracesPrintedAtMutex.Lock()
defer stackTracesPrintedAtMutex.Unlock()
if time.Since(stackTracesPrintedAt) < stackTracesPrintingCooldown {
return "printing throttled"
}
var stackTraces bytes.Buffer
_ = pprof.Lookup("goroutine").WriteTo(&stackTraces, 1)
stackTracesPrintedAt = time.Now()
return stackTraces.String()
}
////////////////////////////////////////////////////////////////////////////////
type runnerMetrics interface {
OnExecutionStarted(execCtx ExecutionContext)
OnExecutionStopped()
OnExecutionError(err error)
OnError(err error)
}
////////////////////////////////////////////////////////////////////////////////
type taskMetrics struct {
publicErrorsCounter metrics.Counter
wrongGenerationErrorsCounter metrics.Counter
retriableErrorsCounter metrics.Counter
nonRetriableErrorsCounter metrics.Counter
nonCancellableErrorsCounter metrics.Counter
panicCounter metrics.Counter
isTaskHanging bool
inflightTasksGauge metrics.Gauge
taskID string
taskType string
}
////////////////////////////////////////////////////////////////////////////////
type runnerMetricsImpl struct {
registry metrics.Registry
hangingTaskTimeout time.Duration
exceptHangingTaskTypes []string
taskMetrics *taskMetrics
taskMetricsMutex sync.Mutex
onExecutionStopped func()
logger logging.Logger
}
func (m *runnerMetricsImpl) OnExecutionStarted(execCtx ExecutionContext) {
m.taskMetricsMutex.Lock()
defer m.taskMetricsMutex.Unlock()
subRegistry := m.registry.WithTags(map[string]string{
"type": execCtx.GetTaskType(),
})
m.taskMetrics = &taskMetrics{
publicErrorsCounter: subRegistry.Counter("errors/public"),
panicCounter: subRegistry.Counter("errors/panic"),
wrongGenerationErrorsCounter: subRegistry.Counter("errors/wrongGeneration"),
retriableErrorsCounter: subRegistry.Counter("errors/retriable"),
nonRetriableErrorsCounter: subRegistry.Counter("errors/nonRetriable"),
nonCancellableErrorsCounter: subRegistry.Counter("errors/nonCancellable"),
inflightTasksGauge: subRegistry.Gauge("inflightTasks"),
taskID: execCtx.GetTaskID(),
taskType: execCtx.GetTaskType(),
}
ctx, cancel := context.WithCancel(context.Background())
m.onExecutionStopped = cancel
// Should not report some tasks as hanging (NBS-4341).
if !common.Find(m.exceptHangingTaskTypes, execCtx.GetTaskType()) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(checkTaskHangingPeriod):
}
m.setTaskHanging(ctx, execCtx.IsHanging())
}
}()
m.setTaskHangingImpl(execCtx.IsHanging())
}
m.taskMetrics.inflightTasksGauge.Add(1)
}
func (m *runnerMetricsImpl) OnExecutionStopped() {
m.taskMetricsMutex.Lock()
defer m.taskMetricsMutex.Unlock()
if m.taskMetrics == nil {
// Nothing to do.
return
}
m.setTaskHangingImpl(false)
m.taskMetrics.inflightTasksGauge.Add(-1)
m.taskMetrics = nil
m.onExecutionStopped()
}
func (m *runnerMetricsImpl) OnExecutionError(err error) {
m.taskMetricsMutex.Lock()
defer m.taskMetricsMutex.Unlock()
if errors.IsPublic(err) {
m.taskMetrics.publicErrorsCounter.Inc()
} else if errors.IsPanicError(err) {
m.taskMetrics.panicCounter.Inc()
} else if errors.Is(err, errors.NewWrongGenerationError()) {
m.taskMetrics.wrongGenerationErrorsCounter.Inc()
} else if errors.Is(err, errors.NewInterruptExecutionError()) {
// InterruptExecutionError is not a failure.
} else if errors.Is(err, errors.NewEmptyNonCancellableError()) {
m.taskMetrics.nonCancellableErrorsCounter.Inc()
} else if errors.Is(err, errors.NewEmptyNonRetriableError()) {
e := errors.NewEmptyNonRetriableError()
errors.As(err, &e)
if !e.Silent {
m.taskMetrics.nonRetriableErrorsCounter.Inc()
}
} else if errors.Is(err, errors.NewEmptyRetriableError()) {
m.taskMetrics.retriableErrorsCounter.Inc()
} else if errors.Is(err, errors.NewEmptyDetailedError()) {
e := errors.NewEmptyDetailedError()
errors.As(err, &e)
if !e.Silent {
m.taskMetrics.nonRetriableErrorsCounter.Inc()
}
} else {
// All other execution errors should be interpreted as non retriable.
m.taskMetrics.nonRetriableErrorsCounter.Inc()
}
}
func (m *runnerMetricsImpl) OnError(err error) {
m.taskMetricsMutex.Lock()
defer m.taskMetricsMutex.Unlock()
if errors.Is(err, errors.NewWrongGenerationError()) {
if m.taskMetrics != nil {
m.taskMetrics.wrongGenerationErrorsCounter.Inc()
}
}
}
////////////////////////////////////////////////////////////////////////////////
func (m *runnerMetricsImpl) setTaskHangingImpl(value bool) {
prevValue := m.taskMetrics.isTaskHanging
m.taskMetrics.isTaskHanging = value
switch {
case !prevValue && value:
if m.logger != nil {
m.logger.Fmt().Infof(
"Task %v with id %v is hanging, stack traces %v",
m.taskMetrics.taskType,
m.taskMetrics.taskID,
printStackTraces(),
)
}
}
}
func (m *runnerMetricsImpl) setTaskHanging(ctx context.Context, value bool) {
m.taskMetricsMutex.Lock()
defer m.taskMetricsMutex.Unlock()
if ctx.Err() != nil {
return
}
m.setTaskHangingImpl(value)
}
////////////////////////////////////////////////////////////////////////////////
func newRunnerMetrics(
ctx context.Context,
registry metrics.Registry,
hangingTaskTimeout time.Duration,
exceptHangingTaskTypes []string,
) *runnerMetricsImpl {
return &runnerMetricsImpl{
registry: registry,
hangingTaskTimeout: hangingTaskTimeout,
exceptHangingTaskTypes: exceptHangingTaskTypes,
onExecutionStopped: func() {},
logger: logging.GetLogger(ctx),
}
}