Skip to content

Commit

Permalink
Merge pull request #21 from DataDog/darcy.rayner/add-ts-support
Browse files Browse the repository at this point in the history
Add typescript support
  • Loading branch information
DarcyRaynerDD authored Jan 27, 2020
2 parents ce4bb23 + 643ac40 commit d1fbd4a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-datadog",
"version": "0.12.0",
"version": "0.13.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",
Expand Down
1 change: 1 addition & 0 deletions src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Service from "serverless/classes/Service";

export enum RuntimeType {
NODE,
NODE_TS,
PYTHON,
UNSUPPORTED,
}
Expand Down
15 changes: 15 additions & 0 deletions src/templates/node-ts-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 typescriptTemplate(filePath: string, method: string) {
return `/* tslint:disable */
/* eslint-disable */
const { datadog } = require("datadog-lambda-js") as any;
import * as original from "../${filePath}";
export const ${method} = datadog(original.${method});`;
}
60 changes: 50 additions & 10 deletions src/wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -42,12 +42,33 @@ 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);",
}
`);
});
Expand Down Expand Up @@ -182,4 +203,23 @@ 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();
});
});
31 changes: 30 additions & 1 deletion src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Service from "serverless/classes/Service";
import util from "util";
import { HandlerInfo, RuntimeType } from "./layer";
import { nodeTemplate } from "./templates/node-js-template";
import { typescriptTemplate } from "./templates/node-ts-template";
import { pythonTemplate } from "./templates/python-template";
import { removeDirectory } from "./util";

Expand All @@ -22,6 +23,18 @@ export async function writeHandlers(service: Service, handlers: HandlerInfo[]) {
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 result = getWrapperText(handlerInfo);
if (result === undefined) {
return;
Expand Down Expand Up @@ -61,14 +74,17 @@ export function getWrapperText(handlerInfo: HandlerInfo) {
switch (handlerInfo.type) {
case RuntimeType.NODE:
return { text: nodeTemplate(filename, method), method };
case RuntimeType.NODE_TS:
return { text: typescriptTemplate(filename, method), method };
case RuntimeType.PYTHON:
return { text: pythonTemplate(filename, method), method };
}
}

export async function writeWrapperFunction(handlerInfo: HandlerInfo, wrapperText: string) {
const extension = handlerInfo.type === RuntimeType.PYTHON ? "py" : "js";
const extension = getHandlerExtension(handlerInfo.type);
const filename = `${handlerInfo.name}.${extension}`;

const pathname = path.join(datadogDirectory, filename);
await util.promisify(fs.writeFile)(pathname, wrapperText);
return pathname;
Expand All @@ -95,3 +111,16 @@ export async function addToExclusionList(service: any, files: string[]) {
}
pack.include.push(...files);
}

function getHandlerExtension(type: RuntimeType) {
switch (type) {
case RuntimeType.NODE:
return "js";
case RuntimeType.NODE_TS:
return "ts";
case RuntimeType.PYTHON:
return "py";
case RuntimeType.UNSUPPORTED:
return "";
}
}

0 comments on commit d1fbd4a

Please sign in to comment.