@@ -24,6 +24,7 @@ type LsOpts struct {
24
24
HotReloadingPaths []string
25
25
EnableDnsServer string
26
26
LocalstackIP string
27
+ InitLogLevel string
27
28
}
28
29
29
30
func GetEnvOrDie (env string ) string {
@@ -36,12 +37,14 @@ func GetEnvOrDie(env string) string {
36
37
37
38
func InitLsOpts () * LsOpts {
38
39
return & LsOpts {
40
+ // required
39
41
RuntimeEndpoint : GetEnvOrDie ("LOCALSTACK_RUNTIME_ENDPOINT" ),
40
42
RuntimeId : GetEnvOrDie ("LOCALSTACK_RUNTIME_ID" ),
41
43
// optional with default
42
44
InteropPort : GetenvWithDefault ("LOCALSTACK_INTEROP_PORT" , "9563" ),
43
45
InitTracingPort : GetenvWithDefault ("LOCALSTACK_RUNTIME_TRACING_PORT" , "9564" ),
44
46
User : GetenvWithDefault ("LOCALSTACK_USER" , "sbx_user1051" ),
47
+ InitLogLevel : GetenvWithDefault ("LOCALSTACK_INIT_LOG_LEVEL" , "debug" ),
45
48
// optional or empty
46
49
CodeArchives : os .Getenv ("LOCALSTACK_CODE_ARCHIVES" ),
47
50
HotReloadingPaths : strings .Split (GetenvWithDefault ("LOCALSTACK_HOT_RELOADING_PATHS" , "" ), "," ),
@@ -78,22 +81,33 @@ func main() {
78
81
// we're setting this to the same value as in the official RIE
79
82
debug .SetGCPercent (33 )
80
83
84
+ // configuration parsing
81
85
lsOpts := InitLsOpts ()
82
86
UnsetLsEnvs ()
83
87
84
- // set up logging (logrus)
85
- //log.SetFormatter(&log.JSONFormatter{})
86
- //log.SetLevel(log.TraceLevel)
87
- log .SetLevel (log .DebugLevel )
88
+ // set up logging
88
89
log .SetReportCaller (true )
90
+ switch lsOpts .InitLogLevel {
91
+ case "debug" :
92
+ log .SetLevel (log .DebugLevel )
93
+ case "trace" :
94
+ log .SetFormatter (& log.JSONFormatter {})
95
+ log .SetLevel (log .TraceLevel )
96
+ default :
97
+ log .Fatal ("Invalid value for LOCALSTACK_INIT_LOG_LEVEL" )
98
+ }
99
+
100
+ // enable dns server
101
+ dnsServerContext , stopDnsServer := context .WithCancel (context .Background ())
102
+ go RunDNSRewriter (lsOpts , dnsServerContext )
89
103
90
104
// download code archive if env variable is set
91
105
if err := DownloadCodeArchives (lsOpts .CodeArchives ); err != nil {
92
106
log .Fatal ("Failed to download code archives" )
93
107
}
94
- // enable dns server
95
- dnsServerContext , stopDnsServer := context . WithCancel ( context . Background ())
96
- go RunDNSRewriter ( lsOpts , dnsServerContext )
108
+
109
+ // parse CLI args
110
+ bootstrap , handler := getBootstrap ( os . Args )
97
111
98
112
// Switch to non-root user and drop root privileges
99
113
if IsRootUser () && lsOpts .User != "" {
@@ -108,23 +122,40 @@ func main() {
108
122
UserLogger ().Debugln ("Process running as non-root user." )
109
123
}
110
124
111
- // parse CLI args
112
- opts , args := getCLIArgs ()
113
- bootstrap , handler := getBootstrap (args , opts )
114
125
logCollector := NewLogCollector ()
126
+
127
+ // file watcher for hot-reloading
115
128
fileWatcherContext , cancelFileWatcher := context .WithCancel (context .Background ())
129
+
130
+ // build sandbox
116
131
sandbox := rapidcore .
117
132
NewSandboxBuilder (bootstrap ).
133
+ //SetTracer(tracer).
118
134
AddShutdownFunc (func () {
119
- log .Debugln ("Closing contexts " )
135
+ log .Debugln ("Stopping file watcher " )
120
136
cancelFileWatcher ()
137
+ log .Debugln ("Stopping DNS server" )
121
138
stopDnsServer ()
122
139
}).
123
- AddShutdownFunc (func () { os .Exit (0 ) }).
124
140
SetExtensionsFlag (true ).
125
141
SetInitCachingFlag (true ).
126
142
SetTailLogOutput (logCollector )
127
143
144
+ // xray daemon
145
+ if shouldRunXrayDaemon () {
146
+ xrayConfig := initConfig (
147
+ "http://" + GetEnvOrDie ("LOCALSTACK_HOSTNAME" )+ ":4566" ,
148
+ GetEnvOrDie ("LOCALSTACK_LAMBDA_FUNCTION_ARN" ),
149
+ )
150
+ d := initDaemon (xrayConfig )
151
+ sandbox .AddShutdownFunc (func () {
152
+ d .stop ()
153
+ })
154
+ runDaemon (d ) // async
155
+
156
+ defer d .close () // synchronous wait for all receivers to be finished
157
+ }
158
+
128
159
defaultInterop := sandbox .InteropServer ()
129
160
interopServer := NewCustomInteropServer (lsOpts , defaultInterop , logCollector )
130
161
sandbox .SetInteropServer (interopServer )
@@ -136,7 +167,7 @@ func main() {
136
167
go sandbox .Create ()
137
168
138
169
// get timeout
139
- invokeTimeoutEnv := GetEnvOrDie ("AWS_LAMBDA_FUNCTION_TIMEOUT" )
170
+ invokeTimeoutEnv := GetEnvOrDie ("AWS_LAMBDA_FUNCTION_TIMEOUT" ) // TODO: collect all AWS_* env parsing
140
171
invokeTimeoutSeconds , err := strconv .Atoi (invokeTimeoutEnv )
141
172
if err != nil {
142
173
log .Fatalln (err )
@@ -153,3 +184,13 @@ func main() {
153
184
log .Fatal ("Failed to start debug server" )
154
185
}
155
186
}
187
+
188
+ func shouldRunXrayDaemon () bool {
189
+ xrayAddress := os .Getenv ("AWS_XRAY_DAEMON_ADDRESS" )
190
+ traceHeaer := os .Getenv ("_X_AMZN_TRACE_ID" )
191
+ if xrayAddress != "" && traceHeaer != "" {
192
+ // no point in running the daemon if we don't actually sample the request
193
+ return strings .Contains (traceHeaer , "Sampled=1" )
194
+ }
195
+ return false
196
+ }
0 commit comments