Repository: iopipe/iopipe-js-core
Pull request #317 (2019-02-13 00:19:27)

Updating serverless to 1.32.0 and pinning version
Should resolve customer-support issue 18.  I'm doing the PR to get it in front of people, but I'll want to verify this on my local also. 

mrickard: I've added a test around setting a UUID on local awsRequestId in report.js in addition to pinning the version. (And I've added the JetBrains cruft directory to .gitignore.) 

@kolanos @adjohn @ewindisch : I've structured the local ID somewhat arbitrarily: 
`local-${functionName}|${uuid()}`.  I'd be happy to change this (and the test) to a different structure, if people prefer.  My thinking behind the pipe was to make testing a bit quicker, since we can just split on the pipe.  But this could change if a different structure works better for us and customers. 
mrickard: Oy. *So* close.

Patch:
diff --git a/.gitignore b/.gitignore
index 86dcaa4..8385646 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,4 +8,6 @@ stats.json
 package-lock.json
 yarn-error.log
 # ignore vim swap files
-.*.sw* 
+.*.sw*
+# ignore JetBrains-suite config files
+.idea
diff --git a/package.json b/package.json
index 25a44be..86c52e5 100644
--- a/package.json
+++ b/package.json
@@ -68,7 +68,7 @@
     "lodash.every": "^4.6.0",
     "nock": "^9.4.1",
     "pre-commit": "^1.2.2",
-    "serverless": "^1.24.1",
+    "serverless": "1.32.0",
     "webpack": "^3.2.0",
     "webpack-bundle-analyzer": "^2.9.1",
     "webpack-node-externals": "^1.6.0",
diff --git a/src/report.js b/src/report.js
index ad1bec8..c32e13d 100644
--- a/src/report.js
+++ b/src/report.js
@@ -2,6 +2,7 @@ import os from 'os';
 
 import { sendReport } from './sendReport';
 import strToBool from './util/strToBool';
+import uuid from './uuidv4';
 
 import {
   COLDSTART,
@@ -51,13 +52,12 @@ class Report {
     const {
       functionName,
       functionVersion,
-      awsRequestId,
       logGroupName,
       logStreamName,
       memoryLimitInMB
     } = this.context;
 
-    let { invokedFunctionArn } = this.context;
+    let { invokedFunctionArn, awsRequestId } = this.context;
 
     // Patch invokedFunctionArn in cases of SAM local invocations
     // or if invokedFunctionArn is missing
@@ -65,6 +65,14 @@ class Report {
       invokedFunctionArn = `arn:aws:lambda:local:0:function:${functionName}`;
     }
 
+    if (
+      process.env.AWS_SAM_LOCAL ||
+      awsRequestId === undefined ||
+      awsRequestId === 'id'
+    ) {
+      awsRequestId = `local|${uuid()}`;
+    }
+
     const pluginMetas = plugins
       .filter(plugin => typeof plugin !== 'undefined')
       .map(plugin => {


Test patch:
diff --git a/src/report.test.js b/src/report.test.js
index e2c92cd..c71a699 100644
--- a/src/report.test.js
+++ b/src/report.test.js
@@ -150,4 +150,19 @@ describe('Report creation', () => {
       'arn:aws:lambda:local:0:function:aws-lambda-mock-context'
     );
   });
+
+  test('patches the awsRequestId if SAM local is detected', () => {
+    process.env.AWS_SAM_LOCAL = true;
+    const localReport = new Report({ config, context: context() });
+    const requestId = String(localReport.report.aws.awsRequestId);
+
+    expect(requestId.substring(0, 6)).toBe(`local|`);
+    expect(requestId).toHaveLength(42);
+
+    delete process.env.AWS_SAM_LOCAL;
+    const normalReport = new Report({ config, context: context() });
+    expect(normalReport.report.aws.invokedFunctionArn).toBe(
+      'arn:aws:lambda:us-west-1:123456789012:function:aws-lambda-mock-context:$LATEST'
+    );
+  });
 });