From 0c9b211891409e94188c8960c6ed141646f55090 Mon Sep 17 00:00:00 2001 From: DarcyRaynerDD Date: Mon, 24 Feb 2020 16:52:46 -0500 Subject: [PATCH 1/5] Add es6 mode to support webpacking --- src/env.ts | 3 + src/index.ts | 15 +++- src/layer.spec.ts | 134 +++++++++++++++++++++++++---- src/layer.ts | 42 ++++++++- src/templates/node-es6-template.ts | 14 +++ src/util.ts | 12 +++ src/wrapper.spec.ts | 73 ++++++++-------- src/wrapper.ts | 31 ++----- 8 files changed, 243 insertions(+), 81 deletions(-) create mode 100644 src/templates/node-es6-template.ts diff --git a/src/env.ts b/src/env.ts index 3617acd7..884292a6 100644 --- a/src/env.ts +++ b/src/env.ts @@ -21,6 +21,9 @@ export interface Configuration { logLevel: string; // Whether the log forwarder integration is enabled by default flushMetricsToLogs: boolean; + // When set, the plugin will always write wrapper handlers in the given format. Otherwise, will try + // to infer the handler type either from the extension, or presence of webpack. + nodeHandlerType?: 'es6' | 'node' | 'typescript'; } const apiKeyEnvVar = "DD_API_KEY"; diff --git a/src/index.ts b/src/index.ts index 0887d962..943de91c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,20 @@ module.exports = class ServerlessPlugin { const config = getConfig(this.serverless.service); setEnvConfiguration(config, this.serverless.service); const defaultRuntime = this.serverless.service.provider.runtime; - const handlers = findHandlers(this.serverless.service, defaultRuntime); + let defaultNodeRuntime: RuntimeType.NODE | RuntimeType.NODE_ES6 | RuntimeType.NODE_TS | undefined; + switch (config.nodeHandlerType) { + case "es6": + defaultNodeRuntime = RuntimeType.NODE_ES6; + break; + case "typescript": + defaultNodeRuntime = RuntimeType.NODE_TS; + break; + case "node": + defaultNodeRuntime = RuntimeType.NODE; + break; + } + + const handlers = findHandlers(this.serverless.service, defaultRuntime, defaultNodeRuntime); if (config.addLayers) { this.serverless.cli.log("Adding Lambda Layers to functions"); this.debugLogHandlers(handlers); diff --git a/src/layer.spec.ts b/src/layer.spec.ts index 4b5b0c41..1eddfbbd 100644 --- a/src/layer.spec.ts +++ b/src/layer.spec.ts @@ -10,13 +10,20 @@ import { HandlerInfo, LayerJSON, RuntimeType, applyLayers, findHandlers } from " import { FunctionDefinition } from "serverless"; import Service from "serverless/classes/Service"; +import fs from "fs"; +import mock from "mock-fs"; -function createMockService(region: string, funcs: { [funcName: string]: Partial }): Service { - const service: Partial & { functions: any } = { +function createMockService( + region: string, + funcs: { [funcName: string]: Partial }, + plugins?: string[], +): Service { + const service: Partial & { functions: any; plugins: any } = { provider: { region } as any, getAllFunctionsNames: () => Object.keys(funcs), getFunction: (name) => funcs[name] as FunctionDefinition, functions: funcs as any, + plugins, }; return service as Service; } @@ -24,50 +31,50 @@ function createMockService(region: string, funcs: { [funcName: string]: Partial< describe("findHandlers", () => { it("finds all node and python layers with matching layers", () => { const mockService = createMockService("us-east-1", { - "func-a": { runtime: "nodejs8.10" }, - "func-b": { runtime: "go1.10" }, - "func-c": { runtime: "nodejs10.x" }, - "func-d": { runtime: "python2.7" }, - "func-e": { runtime: "python3.6" }, - "func-f": { runtime: "python3.7" }, - "func-g": { runtime: "python3.8" }, - "func-h": { runtime: "nodejs12.x" }, + "func-a": { handler: "myfile.handler", runtime: "nodejs8.10" }, + "func-b": { handler: "myfile.handler", runtime: "go1.10" }, + "func-c": { handler: "myfile.handler", runtime: "nodejs10.x" }, + "func-d": { handler: "myfile.handler", runtime: "python2.7" }, + "func-e": { handler: "myfile.handler", runtime: "python3.6" }, + "func-f": { handler: "myfile.handler", runtime: "python3.7" }, + "func-g": { handler: "myfile.handler", runtime: "python3.8" }, + "func-h": { handler: "myfile.handler", runtime: "nodejs12.x" }, }); const result = findHandlers(mockService); expect(result).toMatchObject([ { - handler: { runtime: "nodejs8.10" }, + handler: { handler: "myfile.handler", runtime: "nodejs8.10" }, type: RuntimeType.NODE, runtime: "nodejs8.10", }, { - handler: { runtime: "go1.10" }, + handler: { handler: "myfile.handler", runtime: "go1.10" }, type: RuntimeType.UNSUPPORTED, runtime: "go1.10", }, { - handler: { runtime: "nodejs10.x" }, + handler: { handler: "myfile.handler", runtime: "nodejs10.x" }, type: RuntimeType.NODE, runtime: "nodejs10.x", }, { - handler: { runtime: "python2.7" }, + handler: { handler: "myfile.handler", runtime: "python2.7" }, type: RuntimeType.PYTHON, runtime: "python2.7", }, { - handler: { runtime: "python3.6" }, + handler: { handler: "myfile.handler", runtime: "python3.6" }, type: RuntimeType.PYTHON, runtime: "python3.6", }, { - handler: { runtime: "python3.7" }, + handler: { handler: "myfile.handler", runtime: "python3.7" }, type: RuntimeType.PYTHON, runtime: "python3.7", }, { - handler: { runtime: "python3.8" }, + handler: { handler: "myfile.handler", runtime: "python3.8" }, type: RuntimeType.PYTHON, runtime: "python3.8", }, @@ -80,7 +87,7 @@ describe("findHandlers", () => { }); it("uses the global runtime when one isn't specified", () => { const mockService = createMockService("us-east-1", { - "func-a": {}, + "func-a": { handler: "myfile.handler" }, }); const result = findHandlers(mockService, "nodejs8.10"); expect(result).toMatchObject([ @@ -91,6 +98,97 @@ describe("findHandlers", () => { }, ]); }); + + it("uses typescript runtime when ts file is detected", () => { + mock({ + "mylambda.ts": "", + }); + const mockService = createMockService("us-east-1", { + "func-a": { handler: "mylambda.handler", runtime: "nodejs8.10" }, + }); + const result = findHandlers(mockService, "nodejs8.10"); + + expect(result).toMatchObject([ + { + handler: { handler: "mylambda.handler", runtime: "nodejs8.10" }, + type: RuntimeType.NODE_TS, + runtime: "nodejs8.10", + }, + ]); + }); + it("uses es6 runtime when es.js file is detected", () => { + mock({ + "mylambda.es.js": "", + }); + const mockService = createMockService("us-east-1", { + "func-a": { handler: "mylambda.handler", runtime: "nodejs8.10" }, + }); + const result = findHandlers(mockService, "nodejs8.10"); + + expect(result).toMatchObject([ + { + handler: { handler: "mylambda.handler", runtime: "nodejs8.10" }, + type: RuntimeType.NODE_ES6, + runtime: "nodejs8.10", + }, + ]); + }); + it("uses es6 runtime when .mjs file is detected", () => { + mock({ + "mylambda.mjs": "", + }); + const mockService = createMockService("us-east-1", { + "func-a": { handler: "mylambda.handler", runtime: "nodejs8.10" }, + }); + const result = findHandlers(mockService, "nodejs8.10"); + + expect(result).toMatchObject([ + { + handler: { handler: "mylambda.handler", runtime: "nodejs8.10" }, + type: RuntimeType.NODE_ES6, + runtime: "nodejs8.10", + }, + ]); + }); + it("uses default node runtime when provided", () => { + mock({ + "mylambda.js": "", + }); + const mockService = createMockService("us-east-1", { + "func-a": { handler: "mylambda.handler", runtime: "nodejs8.10" }, + }); + const result = findHandlers(mockService, "nodejs8.10", RuntimeType.NODE_TS); + + expect(result).toMatchObject([ + { + handler: { handler: "mylambda.handler", runtime: "nodejs8.10" }, + type: RuntimeType.NODE_TS, + runtime: "nodejs8.10", + }, + ]); + }); + it("uses es6 runtime by default when webpack detected", () => { + mock({ + "mylambda.js": "", + }); + const plugins = ["serverless-plugin-datadog", "serverless-webpack"]; + const mockService = createMockService( + "us-east-1", + { + "func-a": { handler: "mylambda.handler", runtime: "nodejs8.10" }, + }, + plugins, + ); + const result = findHandlers(mockService, "nodejs8.10"); + + expect(result).toMatchObject([ + { + handler: { handler: "mylambda.handler", runtime: "nodejs8.10" }, + type: RuntimeType.NODE_ES6, + runtime: "nodejs8.10", + }, + ]); + }); }); describe("applyLayers", () => { diff --git a/src/layer.ts b/src/layer.ts index bc06cb62..339ae199 100644 --- a/src/layer.ts +++ b/src/layer.ts @@ -8,10 +8,13 @@ import { FunctionDefinition } from "serverless"; import Service from "serverless/classes/Service"; +import { getHandlerPath } from "./util"; +import fs from "fs"; export enum RuntimeType { NODE, NODE_TS, + NODE_ES6, PYTHON, UNSUPPORTED, } @@ -43,7 +46,11 @@ export const runtimeLookup: { [key: string]: RuntimeType } = { "python3.8": RuntimeType.PYTHON, }; -export function findHandlers(service: Service, defaultRuntime?: string): HandlerInfo[] { +export function findHandlers( + service: Service, + defaultRuntime?: string, + defaultNodeRuntime?: RuntimeType.NODE_ES6 | RuntimeType.NODE_TS | RuntimeType.NODE, +): HandlerInfo[] { const funcs = (service as any).functions as { [key: string]: FunctionDefinition }; return Object.entries(funcs) @@ -53,7 +60,30 @@ export function findHandlers(service: Service, defaultRuntime?: string): Handler runtime = defaultRuntime; } if (runtime !== undefined && runtime in runtimeLookup) { - return { type: runtimeLookup[runtime], runtime, name, handler } as HandlerInfo; + const handlerInfo = { type: runtimeLookup[runtime], runtime, name, handler } as HandlerInfo; + if (handlerInfo.type === RuntimeType.NODE) { + const handlerPath = getHandlerPath(handlerInfo); + if (handlerPath == undefined) { + return; + } + + if (defaultNodeRuntime == undefined) { + if ( + fs.existsSync(`./${handlerPath.filename}.es.js`) || + fs.existsSync(`./${handlerPath.filename}.mjs`) || + hasWebpackPlugin(service) + ) { + handlerInfo.type = RuntimeType.NODE_ES6; + } + if (fs.existsSync(`./${handlerPath.filename}.ts`)) { + handlerInfo.type = RuntimeType.NODE_TS; + } + } else { + handlerInfo.type = defaultNodeRuntime; + } + } + + return handlerInfo; } return { type: RuntimeType.UNSUPPORTED, runtime, name, handler } as HandlerInfo; }) @@ -94,3 +124,11 @@ function getLayers(handler: HandlerInfo) { function setLayers(handler: HandlerInfo, layers: string[]) { (handler.handler as any).layers = layers; } + +function hasWebpackPlugin(service: Service) { + const plugins: string[] | undefined = (service as any).plugins; + if (plugins == undefined) { + return false; + } + return plugins.find((plugin) => plugin === "serverless-webpack") !== undefined; +} diff --git a/src/templates/node-es6-template.ts b/src/templates/node-es6-template.ts new file mode 100644 index 00000000..3752e717 --- /dev/null +++ b/src/templates/node-es6-template.ts @@ -0,0 +1,14 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed + * under the Apache License Version 2.0. + * + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2019 Datadog, Inc. + */ + +export function es6Template(filePath: string, method: string) { + return `/* eslint-disable */ + const { datadog } = require("datadog-lambda-js"); + import * as original from "../${filePath}"; + export const ${method} = datadog(original.${method});`; + } \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index a75eb233..7be6e772 100644 --- a/src/util.ts +++ b/src/util.ts @@ -9,6 +9,7 @@ import * as fs from "fs"; import { promisify } from "util"; +import { HandlerInfo } from "layer"; const exists = promisify(fs.exists); const readdir = promisify(fs.readdir); @@ -32,3 +33,14 @@ export async function removeDirectory(path: string) { await rmdir(path); } } + +export function getHandlerPath(handlerInfo: HandlerInfo) { + const handlerfile = handlerInfo.handler.handler; + const parts = handlerfile.split("."); + if (parts.length < 2) { + return; + } + const method = parts[parts.length - 1]; + const filename = parts.slice(0, -1).join("."); + return { method, filename }; +} diff --git a/src/wrapper.spec.ts b/src/wrapper.spec.ts index 61d89ace..c20699f5 100644 --- a/src/wrapper.spec.ts +++ b/src/wrapper.spec.ts @@ -24,13 +24,13 @@ describe("getWrapperText", () => { }, }); expect(wrapperText).toMatchInlineSnapshot(` - Object { - "method": "myhandler", - "text": "from datadog_lambda.wrapper import datadog_lambda_wrapper - from mydir.func import myhandler as myhandler_impl - myhandler = datadog_lambda_wrapper(myhandler_impl)", - } - `); + Object { + "method": "myhandler", + "text": "from datadog_lambda.wrapper import datadog_lambda_wrapper + from mydir.func import myhandler as myhandler_impl + myhandler = datadog_lambda_wrapper(myhandler_impl)", + } + `); }); it("renders the node template correctly", () => { const wrapperText = getWrapperText({ @@ -42,19 +42,40 @@ describe("getWrapperText", () => { handler: "my.myhandler", }, }); + expect(wrapperText).toMatchInlineSnapshot(` + Object { + "method": "myhandler", + "text": "const { datadog } = require(\\"datadog-lambda-js\\"); + const original = require(\\"../my\\"); + module.exports.myhandler = datadog(original.myhandler);", + } + `); + }); + it("renders the node ts template correctly", () => { + const wrapperText = getWrapperText({ + name: "my-lambda", + type: RuntimeType.NODE_TS, + handler: { + name: "", + package: {} as any, + handler: "my.myhandler", + }, + }); expect(wrapperText).toMatchInlineSnapshot(` Object { "method": "myhandler", - "text": "const { datadog } = require(\\"datadog-lambda-js\\"); - const original = require(\\"../my\\"); - module.exports.myhandler = datadog(original.myhandler);", + "text": "/* tslint:disable */ + /* eslint-disable */ + const { datadog } = require(\\"datadog-lambda-js\\") as any; + import * as original from \\"../my\\"; + export const myhandler = datadog(original.myhandler);", } `); }); - it("renders the node ts template correctly", () => { + it("renders the node es template correctly", () => { const wrapperText = getWrapperText({ name: "my-lambda", - type: RuntimeType.NODE_TS, + type: RuntimeType.NODE_ES6, handler: { name: "", package: {} as any, @@ -64,11 +85,10 @@ describe("getWrapperText", () => { expect(wrapperText).toMatchInlineSnapshot(` Object { "method": "myhandler", - "text": "/* tslint:disable */ - /* eslint-disable */ - const { datadog } = require(\\"datadog-lambda-js\\") as any; - import * as original from \\"../my\\"; - export const myhandler = datadog(original.myhandler);", + "text": "/* eslint-disable */ + const { datadog } = require(\\"datadog-lambda-js\\"); + import * as original from \\"../my\\"; + export const myhandler = datadog(original.myhandler);", } `); }); @@ -203,23 +223,4 @@ describe("writeHandlers", () => { ]); expect(fs.existsSync(`${datadogDirectory}/my-lambda.py`)).toBeTruthy(); }); - it("uses the typescript template when the handler file is ts", async () => { - mock({ - "mylambda.ts": "", - }); - const service = {} as any; - await writeHandlers(service, [ - { - name: "my-lambda", - type: RuntimeType.NODE, - handler: { - name: "my-lambda", - - package: {} as any, - handler: "mylambda.myhandler", - }, - }, - ]); - expect(fs.existsSync(`${datadogDirectory}/my-lambda.ts`)).toBeTruthy(); - }); }); diff --git a/src/wrapper.ts b/src/wrapper.ts index 1eb73d63..548d6649 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -12,9 +12,10 @@ import Service from "serverless/classes/Service"; import util from "util"; import { HandlerInfo, RuntimeType } from "./layer"; import { nodeTemplate } from "./templates/node-js-template"; +import { es6Template } from "./templates/node-es6-template"; import { typescriptTemplate } from "./templates/node-ts-template"; import { pythonTemplate } from "./templates/python-template"; -import { removeDirectory } from "./util"; +import { removeDirectory, getHandlerPath } from "./util"; export const datadogDirectory = "datadog_handlers"; @@ -22,19 +23,7 @@ export async function writeHandlers(service: Service, handlers: HandlerInfo[]) { await cleanupHandlers(); await util.promisify(fs.mkdir)(datadogDirectory); - const promises = handlers.map(async (handlerInfo) => { - const handlerPath = getHandlerPath(handlerInfo); - if (handlerPath === undefined) { - return; - } - - if (handlerInfo.type === RuntimeType.NODE && fs.existsSync(`./${handlerPath.filename}.ts`)) { - handlerInfo = { - ...handlerInfo, - type: RuntimeType.NODE_TS, - }; - } - + const promises = handlers.map(async (handlerInfo) => { const result = getWrapperText(handlerInfo); if (result === undefined) { return; @@ -74,6 +63,8 @@ export function getWrapperText(handlerInfo: HandlerInfo) { switch (handlerInfo.type) { case RuntimeType.NODE: return { text: nodeTemplate(filename, method), method }; + case RuntimeType.NODE_ES6: + return { text: es6Template(filename, method), method }; case RuntimeType.NODE_TS: return { text: typescriptTemplate(filename, method), method }; case RuntimeType.PYTHON: @@ -90,16 +81,7 @@ export async function writeWrapperFunction(handlerInfo: HandlerInfo, wrapperText return pathname; } -export function getHandlerPath(handlerInfo: HandlerInfo) { - const handlerfile = handlerInfo.handler.handler; - const parts = handlerfile.split("."); - if (parts.length < 2) { - return; - } - const method = parts[parts.length - 1]; - const filename = parts.slice(0, -1).join("."); - return { method, filename }; -} + export async function addToExclusionList(service: any, files: string[]) { if (service.package === undefined) { @@ -114,6 +96,7 @@ export async function addToExclusionList(service: any, files: string[]) { function getHandlerExtension(type: RuntimeType) { switch (type) { + case RuntimeType.NODE_ES6: case RuntimeType.NODE: return "js"; case RuntimeType.NODE_TS: From 0274cf0eabf629f8c77de323faa4e5d2ea0968ec Mon Sep 17 00:00:00 2001 From: DarcyRaynerDD Date: Mon, 24 Feb 2020 17:00:31 -0500 Subject: [PATCH 2/5] Add serverless-webpack instructions to readme --- README.md | 30 +++++++++++++++++++++++++++++- src/index.ts | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c98462d..fba09737 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,34 @@ custom: You can use your own version of those libraries by setting 'addLayers' to false in the datadog configuration block. Just make sure to bundle those libaries with your Lambda functions. +### How do I use this with serverless-webpack? + +Make sure serverless-datadog is above the serverless-webpack entry in your serverless.yml + +```yaml +plugins: + - serverless-plugin-datadog + - serverless-webpack +``` + +When using serverless webpack, the plugin will assume you are using es6 module format. If that's not the case, you can manually configure `nodeModuleType`. + +```yaml +custom: + datadog: + nodeModuleType: "node" # 'typescript' | 'es6' +``` + +### How do I use this with serverless-typescript? + +Make sure serverless-datadog is above the serverless-typescript entry in your serverless.yml. The plugin will detect automatically .ts files. + +```yaml +plugins: + - serverless-plugin-datadog + - serverless-typescript +``` + ## Opening Issues If you encounter a bug with this package, we want to hear about it. Before opening a new issue, search the existing issues to avoid duplicates. @@ -96,4 +124,4 @@ If you find an issue with this package and have a fix, please feel free to open Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. -This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2019 Datadog, Inc. \ No newline at end of file +This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2019 Datadog, Inc. diff --git a/src/index.ts b/src/index.ts index 943de91c..4c9ad71e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,7 +52,7 @@ module.exports = class ServerlessPlugin { setEnvConfiguration(config, this.serverless.service); const defaultRuntime = this.serverless.service.provider.runtime; let defaultNodeRuntime: RuntimeType.NODE | RuntimeType.NODE_ES6 | RuntimeType.NODE_TS | undefined; - switch (config.nodeHandlerType) { + switch (config.nodeModuleType) { case "es6": defaultNodeRuntime = RuntimeType.NODE_ES6; break; From 2da3cd1b2919b9ea46e90528622117cfb84ec48e Mon Sep 17 00:00:00 2001 From: DarcyRaynerDD Date: Mon, 24 Feb 2020 17:03:22 -0500 Subject: [PATCH 3/5] Fix lint issues --- src/env.ts | 2 +- src/layer.ts | 9 ++++----- src/templates/node-es6-template.ts | 4 ++-- src/util.ts | 2 +- src/wrapper.ts | 8 +++----- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/env.ts b/src/env.ts index 884292a6..22d916f5 100644 --- a/src/env.ts +++ b/src/env.ts @@ -23,7 +23,7 @@ export interface Configuration { flushMetricsToLogs: boolean; // When set, the plugin will always write wrapper handlers in the given format. Otherwise, will try // to infer the handler type either from the extension, or presence of webpack. - nodeHandlerType?: 'es6' | 'node' | 'typescript'; + nodeModuleType?: "es6" | "node" | "typescript"; } const apiKeyEnvVar = "DD_API_KEY"; diff --git a/src/layer.ts b/src/layer.ts index 339ae199..fc908d92 100644 --- a/src/layer.ts +++ b/src/layer.ts @@ -5,11 +5,10 @@ * This product includes software developed at Datadog (https://www.datadoghq.com/). * Copyright 2019 Datadog, Inc. */ - +import fs from "fs"; import { FunctionDefinition } from "serverless"; import Service from "serverless/classes/Service"; import { getHandlerPath } from "./util"; -import fs from "fs"; export enum RuntimeType { NODE, @@ -63,11 +62,11 @@ export function findHandlers( const handlerInfo = { type: runtimeLookup[runtime], runtime, name, handler } as HandlerInfo; if (handlerInfo.type === RuntimeType.NODE) { const handlerPath = getHandlerPath(handlerInfo); - if (handlerPath == undefined) { + if (handlerPath === undefined) { return; } - if (defaultNodeRuntime == undefined) { + if (defaultNodeRuntime === undefined) { if ( fs.existsSync(`./${handlerPath.filename}.es.js`) || fs.existsSync(`./${handlerPath.filename}.mjs`) || @@ -127,7 +126,7 @@ function setLayers(handler: HandlerInfo, layers: string[]) { function hasWebpackPlugin(service: Service) { const plugins: string[] | undefined = (service as any).plugins; - if (plugins == undefined) { + if (plugins === undefined) { return false; } return plugins.find((plugin) => plugin === "serverless-webpack") !== undefined; diff --git a/src/templates/node-es6-template.ts b/src/templates/node-es6-template.ts index 3752e717..e0627011 100644 --- a/src/templates/node-es6-template.ts +++ b/src/templates/node-es6-template.ts @@ -7,8 +7,8 @@ */ export function es6Template(filePath: string, method: string) { - return `/* eslint-disable */ + return `/* eslint-disable */ const { datadog } = require("datadog-lambda-js"); import * as original from "../${filePath}"; export const ${method} = datadog(original.${method});`; - } \ No newline at end of file +} diff --git a/src/util.ts b/src/util.ts index 7be6e772..17ad02cc 100644 --- a/src/util.ts +++ b/src/util.ts @@ -8,8 +8,8 @@ import * as fs from "fs"; -import { promisify } from "util"; import { HandlerInfo } from "layer"; +import { promisify } from "util"; const exists = promisify(fs.exists); const readdir = promisify(fs.readdir); diff --git a/src/wrapper.ts b/src/wrapper.ts index 548d6649..7e43d36b 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -11,11 +11,11 @@ import path from "path"; import Service from "serverless/classes/Service"; import util from "util"; import { HandlerInfo, RuntimeType } from "./layer"; -import { nodeTemplate } from "./templates/node-js-template"; import { es6Template } from "./templates/node-es6-template"; +import { nodeTemplate } from "./templates/node-js-template"; import { typescriptTemplate } from "./templates/node-ts-template"; import { pythonTemplate } from "./templates/python-template"; -import { removeDirectory, getHandlerPath } from "./util"; +import { getHandlerPath, removeDirectory } from "./util"; export const datadogDirectory = "datadog_handlers"; @@ -23,7 +23,7 @@ export async function writeHandlers(service: Service, handlers: HandlerInfo[]) { await cleanupHandlers(); await util.promisify(fs.mkdir)(datadogDirectory); - const promises = handlers.map(async (handlerInfo) => { + const promises = handlers.map(async (handlerInfo) => { const result = getWrapperText(handlerInfo); if (result === undefined) { return; @@ -81,8 +81,6 @@ export async function writeWrapperFunction(handlerInfo: HandlerInfo, wrapperText return pathname; } - - export async function addToExclusionList(service: any, files: string[]) { if (service.package === undefined) { service.package = {}; From caf09e0fd94e362ba8933be1d155e200e2e0d18a Mon Sep 17 00:00:00 2001 From: DarcyRaynerDD Date: Mon, 24 Feb 2020 17:16:36 -0500 Subject: [PATCH 4/5] Add note about webpack externals --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fba09737..cb0df870 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ custom: nodeModuleType: "node" # 'typescript' | 'es6' ``` +If you have the addLayers option enabled, you may also want to add 'datadog-lambda-js' and 'dd-trace' to the [externals](https://webpack.js.org/configuration/externals/) section of your webpack config. + ### How do I use this with serverless-typescript? Make sure serverless-datadog is above the serverless-typescript entry in your serverless.yml. The plugin will detect automatically .ts files. From 9a9d9ebf2d76cc3952be687afbaea86218b19720 Mon Sep 17 00:00:00 2001 From: DarcyRaynerDD Date: Mon, 24 Feb 2020 17:31:13 -0500 Subject: [PATCH 5/5] Bump version to 0.16.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4158d2ce..2a4863f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-plugin-datadog", - "version": "0.15.0", + "version": "0.16.0", "description": "Serverless plugin to automatically instrument python and node functions with datadog tracing", "main": "dist/index.js", "repository": "https://github.com/DataDog/serverless-plugin-datadog",