-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspect.go
110 lines (89 loc) · 2.11 KB
/
aspect.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
package graphproc
import (
"time"
)
//Aspect :
type Aspect struct {
aspect func(name string, mesg []byte, scope *Scope) error
time time.Duration
value int64
action AspectAction
}
//Scope :
type Scope struct {
err error
start time.Time
end time.Time
}
//AspectAction :
type AspectAction int
//Aspect action :
const (
RETRY AspectAction = iota + 1
CANCEL
CONTINUE
DEALDLINE
TIMEOUT
ERROR
START
END
)
//NewAspect :
func NewAspect(f func(string, []byte, *Scope) error, t time.Duration, v int64, action AspectAction) Aspect {
var a Aspect
a.aspect = f
a.time = t
a.value = v
a.action = action
return a
}
//Execute :
func (a *Aspect) Execute(name string, mesg []byte, scope *Scope) AspectAction {
if a.aspect == nil {
return ERROR
}
scope.err = a.aspect(name, mesg, scope)
return a.action
}
//Trace : trace aspect that sends trace events
func Trace(name string, mesg []byte, scope *Scope) error {
var message string
var errmesg string
if scope.err != nil {
errmesg = scope.err.Error()
}
if errmesg != "" {
message = " : error = " + errmesg
}
var event Gevent
event.id = MESSAGE
event.strval = "Trace: " + name + message
SendEvent(event)
return nil
}
//StartTime : aspect that sends start time as an event
func StartTime(name string, mesg []byte, scope *Scope) error {
var event Gevent
scope.start = time.Now()
event.id = MESSAGE
event.strval = "Start: " + scope.start.String()
SendEvent(event)
return nil
}
//EndTime : aspect that sends the end time as an event
func EndTime(name string, mesg []byte, scope *Scope) error {
var event Gevent
scope.end = time.Now()
event.id = MESSAGE
event.strval = "End: " + scope.end.String()
SendEvent(event)
return nil
}
//DurationTime : aspect to calculate the duration and send an event. Uses the times from StartTime and EndTime
func DurationTime(name string, mesg []byte, scope *Scope) error {
var event Gevent
event.id = MESSAGE
event.strval = "Duration: " + scope.end.Sub(scope.start).String()
SendEvent(event)
return nil
}