28
28
import java .util .concurrent .ExecutorService ;
29
29
import java .util .concurrent .Executors ;
30
30
31
+ import com .amazonaws .services .lambda .runtime .ClientContext ;
32
+ import com .amazonaws .services .lambda .runtime .CognitoIdentity ;
33
+ import com .amazonaws .services .lambda .runtime .Context ;
34
+ import com .amazonaws .services .lambda .runtime .LambdaLogger ;
35
+ import com .amazonaws .services .lambda .runtime .LambdaRuntime ;
31
36
import org .apache .commons .logging .Log ;
32
37
import org .apache .commons .logging .LogFactory ;
33
38
34
-
35
39
import org .springframework .cloud .function .context .FunctionCatalog ;
36
40
import org .springframework .cloud .function .context .catalog .SimpleFunctionRegistry .FunctionInvocationWrapper ;
37
41
import org .springframework .cloud .function .context .config .RoutingFunction ;
@@ -130,6 +134,8 @@ private void eventLoop(ConfigurableApplicationContext context) {
130
134
logger .debug ("Attempting to get new event" );
131
135
ResponseEntity <String > response = this .pollForData (rest , requestEntity );
132
136
137
+ Context clientContext = generateClientContext (response .getHeaders ());
138
+
133
139
if (logger .isDebugEnabled ()) {
134
140
logger .debug ("New Event received: " + response );
135
141
}
@@ -140,9 +146,9 @@ private void eventLoop(ConfigurableApplicationContext context) {
140
146
FunctionInvocationWrapper function = locateFunction (environment , functionCatalog , response .getHeaders ());
141
147
142
148
ByteArrayInputStream is = new ByteArrayInputStream (response .getBody ().getBytes (StandardCharsets .UTF_8 ));
143
- Message <?> requestMessage = AWSLambdaUtils .generateMessage (is , function .getInputType (), function .isSupplier (), mapper , null );
144
-
149
+ Message <?> requestMessage = AWSLambdaUtils .generateMessage (is , function .getInputType (), function .isSupplier (), mapper , clientContext );
145
150
Object functionResponse = function .apply (requestMessage );
151
+
146
152
byte [] responseBytes = AWSLambdaUtils .generateOutputFromObject (requestMessage , functionResponse , mapper , function .getOutputType ());
147
153
148
154
String invocationUrl = MessageFormat
@@ -157,12 +163,91 @@ private void eventLoop(ConfigurableApplicationContext context) {
157
163
}
158
164
}
159
165
catch (Exception e ) {
166
+ e .printStackTrace ();
160
167
this .propagateAwsError (requestId , e , mapper , runtimeApi , rest );
161
168
}
162
169
}
163
170
}
164
171
}
165
172
173
+ private Context generateClientContext (HttpHeaders headers ) {
174
+
175
+ Map <String , String > environment = System .getenv ();
176
+
177
+ Context context = new Context () {
178
+
179
+ @ Override
180
+ public int getRemainingTimeInMillis () {
181
+ long now = System .currentTimeMillis ();
182
+ if (!headers .containsKey ("Lambda-Runtime-Deadline-Ms" )) {
183
+ return 0 ;
184
+ }
185
+ int delta = (int ) (Long .parseLong (headers .getFirst ("Lambda-Runtime-Deadline-Ms" )) - now );
186
+ return delta > 0 ? delta : 0 ;
187
+ }
188
+
189
+ @ Override
190
+ public int getMemoryLimitInMB () {
191
+ if (!environment .containsKey ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" )) {
192
+ return 128 ;
193
+ }
194
+ return Integer .parseInt (environment .getOrDefault ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" , "128" ));
195
+ }
196
+
197
+ @ Override
198
+ public LambdaLogger getLogger () {
199
+ return LambdaRuntime .getLogger ();
200
+ }
201
+
202
+ @ Override
203
+ public String getLogStreamName () {
204
+ return environment .get ("LOG_STREAM_NAME" );
205
+ }
206
+
207
+ @ Override
208
+ public String getLogGroupName () {
209
+ return environment .get ("LOG_GROUP_NAME" );
210
+ }
211
+
212
+ @ Override
213
+ public String getInvokedFunctionArn () {
214
+ return headers .getFirst ("Lambda-Runtime-Invoked-Function-Arn" );
215
+ }
216
+
217
+ @ Override
218
+ public CognitoIdentity getIdentity () {
219
+ return null ;
220
+ }
221
+
222
+ @ Override
223
+ public String getFunctionVersion () {
224
+ return environment .get ("FUNCTION_VERSION" );
225
+ }
226
+
227
+ @ Override
228
+ public String getFunctionName () {
229
+ return environment .get ("FUNCTION_NAME" );
230
+ }
231
+
232
+ @ Override
233
+ public ClientContext getClientContext () {
234
+ return null ;
235
+ }
236
+
237
+ @ Override
238
+ public String getAwsRequestId () {
239
+ return headers .getFirst ("Lambda-Runtime-Aws-Request-Id" );
240
+ }
241
+
242
+ public String toString () {
243
+ return "FUNCTION NAME: " + getFunctionName () + ", FUNCTION VERSION: " + getFunctionVersion ()
244
+ + ", FUNCTION ARN: " + getInvokedFunctionArn () + ", FUNCTION MEM LIMIT: " + getMemoryLimitInMB ()
245
+ + ", FUNCTION DEADLINE: " + getRemainingTimeInMillis ();
246
+ }
247
+ };
248
+ return context ;
249
+ }
250
+
166
251
private void propagateAwsError (String requestId , Exception e , JsonMapper mapper , String runtimeApi , RestTemplate rest ) {
167
252
String errorMessage = e .getMessage ();
168
253
String errorType = e .getClass ().getSimpleName ();
0 commit comments