/
agent.go
178 lines (148 loc) · 3.86 KB
/
agent.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
package iopipe
import (
"context"
"fmt"
"os"
"strconv"
"sync"
"time"
)
// Config is the config object passed to agent initialization
type Config struct {
Debug *bool
Enabled *bool
PluginInstantiators []PluginInstantiator
Reporter Reporter
TimeoutWindow *time.Duration
Token *string
}
// Agent is the IOpipe instance
type Agent struct {
*Config
plugins []Plugin
}
var (
defaultConfigDebug = false
defaultConfigEnabled = true
defaultConfigTimeoutWindow = time.Duration(150 * time.Millisecond)
defaultReporter = sendReport
)
// NewAgent returns a new IOpipe instance with config
func NewAgent(config Config) *Agent {
if config.PluginInstantiators == nil {
config.PluginInstantiators = []PluginInstantiator{}
}
var plugins []Plugin
pluginInstantiators := config.PluginInstantiators
if pluginInstantiators != nil {
plugins = make([]Plugin, len(pluginInstantiators))
for index, pluginInstantiator := range pluginInstantiators {
plugins[index] = pluginInstantiator()
}
}
a := &Agent{
plugins: plugins,
}
a.preSetup()
// Debug
debug := &defaultConfigDebug
envDebug := os.Getenv("IOPIPE_DEBUG")
if envDebug != "" {
debug = strToBool(envDebug)
}
if config.Debug != nil {
debug = config.Debug
}
if *debug {
enableDebugMode()
}
// Enabled
enabled := &defaultConfigEnabled
envEnabled := os.Getenv("IOPIPE_ENABLED")
if envEnabled != "" {
enabled = strToBool(envEnabled)
}
if config.Enabled != nil {
enabled = config.Enabled
}
// Reporter
reporter := defaultReporter
if config.Reporter != nil {
reporter = config.Reporter
}
// TimeoutWindow
timeoutWindow := &defaultConfigTimeoutWindow
envTimeoutWindow := os.Getenv("IOPIPE_TIMEOUT_WINDOW")
envTimeoutWindowInt, err := strconv.Atoi(envTimeoutWindow)
if err == nil {
envTimeoutWindowDuration := time.Duration(time.Duration(envTimeoutWindowInt) * time.Millisecond)
timeoutWindow = &envTimeoutWindowDuration
}
if config.TimeoutWindow != nil {
timeoutWindow = config.TimeoutWindow
}
// Token
envToken := os.Getenv("IOPIPE_TOKEN")
token := &envToken
if config.Token != nil {
token = config.Token
}
a.Config = &Config{
Debug: debug,
Enabled: enabled,
PluginInstantiators: pluginInstantiators,
Reporter: reporter,
TimeoutWindow: timeoutWindow,
Token: token,
}
a.postSetup()
return a
}
// WrapHandler wraps the handler with the IOpipe agent
func (a *Agent) WrapHandler(handler interface{}) interface{} {
logger.Debug(fmt.Sprintf("%s wrapped with IOpipe decorator", getFuncName(handler)))
if a.Enabled != nil && !*a.Enabled {
logger.Debug("IOpipe agent disabled, skipping reporting")
return handler
}
if a.Token != nil && *a.Token == "" {
logger.Debug("Your function is decorated with iopipe, but a valid token was not found. Set the IOPIPE_TOKEN environment variable with your IOpipe project token.")
return handler
}
return wrapHandler(handler, a)
}
// preSetup runs the PreSetup hooks
func (a *Agent) preSetup() {
var wg sync.WaitGroup
wg.Add(len(a.plugins))
for _, plugin := range a.plugins {
go func(plugin Plugin) {
defer wg.Done()
if plugin != nil {
plugin.PreSetup(a)
}
}(plugin)
}
wg.Wait()
}
// postSetup runs the PostSetup hooks
func (a *Agent) postSetup() {
var wg sync.WaitGroup
wg.Add(len(a.plugins))
for _, plugin := range a.plugins {
go func(plugin Plugin) {
defer wg.Done()
if plugin != nil {
plugin.PostSetup(a)
}
}(plugin)
}
wg.Wait()
}
// wrapHandler decorates the handler with the handler wrapper
func wrapHandler(handler interface{}, agentInstance *Agent) lambdaHandler {
return func(context context.Context, payload interface{}) (interface{}, error) {
handlerWrapper := NewHandlerWrapper(handler, agentInstance)
return handlerWrapper.Invoke(context, payload)
}
}