/
metrics.go
348 lines (281 loc) · 9.55 KB
/
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package flow
import (
"context"
"net"
"net/http"
"strings"
"time"
"github.com/direktiv/direktiv/pkg/flow/grpc"
"github.com/direktiv/direktiv/pkg/metrics"
"github.com/direktiv/direktiv/pkg/util"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type metricsServer struct {
*server
listener net.Listener
http *http.Server
router *mux.Router
}
var (
metricsWf = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "workflows",
Help: "Total number of workflows.",
},
[]string{"direktiv_namespace", "direktiv_tenant"},
)
metricsWfUpdated = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "updated_total",
Help: "Total number of workflows updated.",
},
[]string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsCloudEventsReceived = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "cloudevents_received",
Help: "Total number of cloudevents received.",
},
[]string{"direktiv_namespace", "ce_type", "ce_source", "direktiv_tenant"},
)
metricsCloudEventsCaptured = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "cloudevents_captured",
Help: "Total number of cloudevents captured.",
},
[]string{"direktiv_namespace", "ce_type", "ce_source", "direktiv_tenant"},
)
metricsWfInvoked = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "invoked_total",
Help: "Total number of workflows invoked.",
},
[]string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsWfSuccess = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "success_total",
Help: "Total number of workflows successfully finished.",
},
[]string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsWfFail = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "failed_total",
Help: "Total number of workflows failed.",
},
[]string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsWfPending = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "pending_total",
Help: "Total number of workflows pending.",
},
[]string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsWfDuration = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "total_milliseconds",
Help: "Total time workflow has been actively executing.",
}, []string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant"},
)
metricsWfStateDuration = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "direktiv",
Subsystem: "states",
Name: "milliseconds",
Help: "Average time each state spends in execution.",
}, []string{"direktiv_namespace", "direktiv_workflow", "state", "direktiv_tenant"},
)
metricsWfOutcome = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "direktiv",
Subsystem: "workflows",
Name: "outcomes",
Help: "Results of each workflow instance.",
}, []string{"direktiv_namespace", "direktiv_workflow", "direktiv_tenant", "direktiv_instance_status", "direktiv_errcode"},
)
)
func reportStateEnd(namespace, workflow, state string, t time.Time) {
ms := time.Now().Sub(t).Milliseconds()
metricsWfStateDuration.WithLabelValues(namespace, GetInodePath(workflow), state, namespace).Observe(float64(ms))
}
func setupPrometheusEndpoint() {
prometheus.MustRegister(metricsWfInvoked)
prometheus.MustRegister(metricsWfSuccess)
prometheus.MustRegister(metricsWfFail)
prometheus.MustRegister(metricsWfDuration)
prometheus.MustRegister(metricsWfStateDuration)
prometheus.Unregister(prometheus.NewGoCollector())
prometheus.Unregister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
prometheus.MustRegister(metricsWf)
prometheus.MustRegister(metricsWfUpdated)
prometheus.MustRegister(metricsWfPending)
prometheus.MustRegister(metricsWfOutcome)
prometheus.MustRegister(metricsCloudEventsReceived)
prometheus.MustRegister(metricsCloudEventsCaptured)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
// WorkflowMetrics - Gets the Workflow metrics of a given Workflow Revision Ref
// if ref is not set in the request, it will be automatically be set to latest
func (flow *flow) WorkflowMetrics(ctx context.Context, req *grpc.WorkflowMetricsRequest) (*grpc.WorkflowMetricsResponse, error) {
flow.sugar.Debugf("Handling gRPC request: %s", this())
d, err := flow.traverseToRef(ctx, flow.db.Namespace, req.GetNamespace(), req.GetPath(), req.GetRef())
if err != nil {
return nil, err
}
resp, err := flow.metrics.GetMetrics(&metrics.GetMetricsArgs{
Namespace: d.namespace(),
Workflow: d.path,
Revision: d.rev().ID.String(),
Since: req.SinceTimestamp.AsTime(),
})
if err != nil {
return nil, err
}
out := new(grpc.WorkflowMetricsResponse)
out.TotalInstancesRun = resp.TotalInstancesRun
out.TotalInstanceMilliseconds = resp.TotalInstanceMilliSeconds
out.SuccessfulExecutions = resp.SuccessfulExecutions
out.FailedExecutions = resp.FailedExecutions
out.SampleSize = resp.TotalInstancesRun
out.MeanInstanceMilliseconds = resp.MeanInstanceMilliSeconds
out.ErrorCodes = resp.ErrorCodes
out.ErrorCodesRepresentation = resp.ErrorCodesRepresentation
var sr, fr float32
sr = float32(resp.SuccessRate)
fr = float32(resp.FailureRate)
out.SuccessRate = sr
out.FailureRate = fr
states := make([]*grpc.State, 0)
for _, s := range resp.States {
thisState := s
is := new(grpc.State)
x := thisState.Name
is.Name = x
is.Invokers = thisState.Invokers
is.InvokersRepresentation = thisState.InvokersRepresentation
is.TotalExecutions = thisState.TotalExecutions
is.TotalMilliseconds = thisState.TotalMilliSeconds
is.TotalSuccesses = thisState.TotalSuccesses
is.TotalFailures = thisState.TotalFailures
is.TotalRetries = thisState.TotalRetries
is.Outcomes = &grpc.Outcomes{
Success: thisState.Outcomes.EndStates.Success,
Failure: thisState.Outcomes.EndStates.Failure,
Transitions: s.Outcomes.Transitions,
}
var fr, sr float32
sr = float32(thisState.MeanOutcomes.EndStates.Success)
fr = float32(thisState.MeanOutcomes.EndStates.Failure)
is.MeanOutcomes = &grpc.MeanOutcomes{
Success: sr,
Failure: fr,
Transitions: s.MeanOutcomes.Transitions,
}
is.MeanExecutionsPerInstance = thisState.MeanExecutionsPerInstance
is.MeanMillisecondsPerInstance = thisState.MeanMilliSecondsPerInstance
sr2 := float32(thisState.SuccessRate)
fr2 := float32(thisState.FailureRate)
ar := float32(thisState.MeanRetries)
is.SuccessRate = sr2
is.FailureRate = fr2
is.MeanRetries = ar
is.UnhandledErrors = thisState.UnhandledErrors
is.UnhandledErrorsRepresentation = thisState.UnhandledErrorsRepresentation
states = append(states, is)
}
out.States = states
return out, nil
}
func (engine *engine) metricsCompleteState(ctx context.Context, im *instanceMemory, nextState, errCode string, retrying bool) {
ns, err := engine.InstanceNamespace(ctx, im)
if err != nil {
engine.sugar.Error(err)
return
}
workflow := GetInodePath(im.in.As)
reportStateEnd(ns.Name, workflow, im.logic.GetID(), im.in.Edges.Runtime.StateBeginTime)
if im.Step() == 0 {
return
}
// if im.Status() != util.InstanceStatusPending {
// return
// }
args := new(metrics.InsertRecordArgs)
args.Namespace = ns.Name
args.Workflow = workflow
args.Revision = im.in.Edges.Revision.ID.String()
args.Instance = im.ID().String()
caller := engine.InstanceCaller(ctx, im)
if caller != nil {
args.Invoker = caller.InstanceID
}
flow := im.Flow()
args.State = flow[len(flow)-1]
d := time.Now().Sub(im.StateBeginTime())
args.WorkflowMilliSeconds = d.Milliseconds()
args.ErrorCode = errCode
args.Transition = nextState
args.Next = metrics.NextTransition
if nextState == "" {
args.Next = metrics.NextEnd
} else if retrying {
args.Next = metrics.NextRetry
}
if im.Step() == 1 {
args.Invoker = "start"
}
err = engine.metrics.InsertRecord(args)
if err != nil {
engine.sugar.Error(err)
}
}
func (engine *engine) metricsCompleteInstance(ctx context.Context, im *instanceMemory) {
ns, err := engine.InstanceNamespace(ctx, im)
if err != nil {
engine.sugar.Error(err)
return
}
t := im.StateBeginTime()
namespace := ns.Name
workflow := GetInodePath(im.in.As)
// Trim workflow revision until revisions are fully implemented.
if divider := strings.LastIndex(workflow, ":"); divider > 0 {
workflow = workflow[0:divider]
}
now := time.Now()
empty := time.Time{}
if im.Status() == util.InstanceStatusFailed || im.Status() == util.InstanceStatusCrashed {
metricsWfFail.WithLabelValues(namespace, workflow, namespace).Inc()
} else {
metricsWfSuccess.WithLabelValues(namespace, workflow, namespace).Inc()
}
metricsWfOutcome.WithLabelValues(namespace, workflow, namespace, im.in.Status, im.in.ErrorCode).Inc()
metricsWfPending.WithLabelValues(namespace, workflow, namespace).Dec()
if t != empty {
ms := now.Sub(t).Milliseconds()
metricsWfDuration.WithLabelValues(namespace, workflow, namespace).Observe(float64(ms))
}
}