-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace.go
53 lines (44 loc) · 1.27 KB
/
trace.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
package pipeline
import (
"context"
"fmt"
"io"
"os"
"time"
)
type (
// TracedStep decorates a step with tracing capabilities, logging the duration of the
// step execution, time of execution and its result.
TracedStep[I, O any] struct {
name string
step Step[I, O]
writer io.Writer
}
)
// NewTracedStep creates traced step that will log the execution time of the step to the stdout
func NewTracedStep[I, O any](name string, step Step[I, O]) TracedStep[I, O] {
return NewTracedStepWithWriter(name, step, os.Stdout)
}
// NewTracedStepWithWriter creates traced step that will log the execution time of the step to the writer
func NewTracedStepWithWriter[I, O any](name string, step Step[I, O], writer io.Writer) TracedStep[I, O] {
return TracedStep[I, O]{
name: name,
step: step,
writer: writer,
}
}
func (t TracedStep[I, O]) Draw(graph Graph) {
t.step.Draw(graph)
}
func (t TracedStep[I, O]) Run(ctx context.Context, in I) (O, error) {
start := time.Now()
res, err := t.step.Run(ctx, in)
var message string
if err == nil {
message = "Success"
} else {
message = fmt.Sprintf("Failure: %s", err.Error())
}
fmt.Fprintf(t.writer, "[STAGE] %s | %s | %s | %s\n", start.Format("2006-01-02 - 15:04:05"), t.name, time.Since(start), message)
return res, err
}