Skip to content

Commit

Permalink
Merge pull request #6 from DataDog/darcy.rayner/fix-global-runtime-error
Browse files Browse the repository at this point in the history
Darcy.rayner/fix global runtime error
  • Loading branch information
DarcyRaynerDD authored Sep 30, 2019
2 parents cc1177a + 4b7185f commit 7d40908
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-datadog",
"version": "0.3.0",
"version": "0.4.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 All @@ -18,7 +18,7 @@
"@types/jest": "^24.0.13",
"@types/mock-fs": "true3.6.30",
"@types/node": "^12.0.4",
"@types/serverless": "true1.18.2",
"@types/serverless": "true1.18.3",
"jest": "^24.8.0",
"mock-fs": "true4.10.1",
"nock": "^10.0.6",
Expand Down
21 changes: 18 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as Serverless from "serverless";
import * as layers from "./layers.json";

import { getConfig, setEnvConfiguration } from "./env";
import { applyLayers, findHandlers } from "./layer";
import { applyLayers, findHandlers, HandlerInfo, RuntimeType } from "./layer";
import { enabledTracing } from "./tracing";
import { cleanupHandlers, writeHandlers } from "./wrapper";

Expand Down Expand Up @@ -50,10 +50,11 @@ module.exports = class ServerlessPlugin {
this.serverless.cli.log("Auto instrumenting functions with Datadog");
const config = getConfig(this.serverless.service);
setEnvConfiguration(config, this.serverless.service);

const handlers = findHandlers(this.serverless.service);
const defaultRuntime = this.serverless.service.provider.runtime;
const handlers = findHandlers(this.serverless.service, defaultRuntime);
if (config.addLayers) {
this.serverless.cli.log("Adding Lambda Layers to functions");
this.debugLogHandlers(handlers);
applyLayers(this.serverless.service.provider.region, handlers, layers);
} else {
this.serverless.cli.log("Skipping adding Lambda Layers, make sure you are packaging them yourself");
Expand All @@ -65,4 +66,18 @@ module.exports = class ServerlessPlugin {
this.serverless.cli.log("Cleaning up Datadog Handlers");
await cleanupHandlers();
}

private debugLogHandlers(handlers: HandlerInfo[]) {
for (const handler of handlers) {
if (handler.type === RuntimeType.UNSUPPORTED) {
if (handler.runtime === undefined) {
this.serverless.cli.log(`Unable to determine runtime for function ${handler.name}`);
} else {
this.serverless.cli.log(
`Unable to add Lambda Layers to function ${handler.name} with runtime ${handler.runtime}`,
);
}
}
}
}
};
40 changes: 40 additions & 0 deletions src/layer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,45 @@ describe("findHandlers", () => {
{
handler: { runtime: "nodejs8.10" },
type: RuntimeType.NODE,
runtime: "nodejs8.10",
},
{
handler: { runtime: "go1.10" },
type: RuntimeType.UNSUPPORTED,
runtime: "go1.10",
},
{
handler: { runtime: "nodejs10.x" },
type: RuntimeType.NODE,
runtime: "nodejs10.x",
},
{
handler: { runtime: "python2.7" },
type: RuntimeType.PYTHON,
runtime: "python2.7",
},
{
handler: { runtime: "python3.6" },
type: RuntimeType.PYTHON,
runtime: "python3.6",
},
{
handler: { runtime: "python3.7" },
type: RuntimeType.PYTHON,
runtime: "python3.7",
},
]);
});
it("uses the global runtime when one isn't specified", () => {
const mockService = createMockService("us-east-1", {
"func-a": {},
});
const result = findHandlers(mockService, "nodejs8.10");
expect(result).toMatchObject([
{
handler: {},
type: RuntimeType.NODE,
runtime: "nodejs8.10",
},
]);
});
Expand All @@ -63,6 +86,7 @@ describe("applyLayers", () => {
const handler = {
handler: { runtime: "nodejs10.x" },
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "nodejs10.x": "node:2" } },
Expand All @@ -77,6 +101,7 @@ describe("applyLayers", () => {
const handler = {
handler: { runtime: "nodejs10.x", layers: ["node:1"] } as any,
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "nodejs10.x": "node:2" } },
Expand All @@ -91,6 +116,7 @@ describe("applyLayers", () => {
const handler = {
handler: { runtime: "nodejs10.x", layers: ["node:1"] } as any,
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "nodejs10.x": "node:1" } },
Expand All @@ -105,6 +131,7 @@ describe("applyLayers", () => {
const handler = {
handler: { runtime: "nodejs10.x" } as any,
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "nodejs10.x": "node:1" } },
Expand All @@ -118,6 +145,7 @@ describe("applyLayers", () => {
const handler = {
handler: { runtime: "nodejs10.x" } as any,
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "python2.7": "python:2" } },
Expand All @@ -131,6 +159,18 @@ describe("applyLayers", () => {
const handler = {
handler: {} as any,
type: RuntimeType.NODE,
runtime: "nodejs10.x",
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "python2.7": "python:2" } },
};
applyLayers("us-east-1", [handler], layers);
expect(handler.handler).toEqual({});
});
it("only add layer when when supported runtime present", () => {
const handler = {
handler: {} as any,
type: RuntimeType.UNSUPPORTED,
} as HandlerInfo;
const layers: LayerJSON = {
regions: { "us-east-1": { "python2.7": "python:2" } },
Expand Down
19 changes: 14 additions & 5 deletions src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import Service from "serverless/classes/Service";
export enum RuntimeType {
NODE,
PYTHON,
UNSUPPORTED,
}

export interface HandlerInfo {
name: string;
type: RuntimeType;
handler: FunctionDefinition;
runtime?: string;
}

export interface LayerJSON {
Expand All @@ -38,16 +40,19 @@ export const runtimeLookup: { [key: string]: RuntimeType } = {
"python3.7": RuntimeType.PYTHON,
};

export function findHandlers(service: Service): HandlerInfo[] {
export function findHandlers(service: Service, defaultRuntime?: string): HandlerInfo[] {
const funcs = (service as any).functions as { [key: string]: FunctionDefinition };

return Object.entries(funcs)
.map(([name, handler]) => {
const { runtime } = handler;
let { runtime } = handler;
if (runtime === undefined) {
runtime = defaultRuntime;
}
if (runtime !== undefined && runtime in runtimeLookup) {
return { type: runtimeLookup[runtime], name, handler } as HandlerInfo;
return { type: runtimeLookup[runtime], runtime, name, handler } as HandlerInfo;
}
return undefined;
return { type: RuntimeType.UNSUPPORTED, runtime, name, handler } as HandlerInfo;
})
.filter((result) => result !== undefined) as HandlerInfo[];
}
Expand All @@ -59,7 +64,11 @@ export function applyLayers(region: string, handlers: HandlerInfo[], layers: Lay
}

for (const handler of handlers) {
const { runtime } = handler.handler;
if (handler.type === RuntimeType.UNSUPPORTED) {
continue;
}

const { runtime } = handler;
const layerARN = runtime !== undefined ? regionRuntimes[runtime] : undefined;
if (layerARN !== undefined) {
const currentLayers = getLayers(handler);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.6.8.tgz#e469b4bf9d1c9832aee4907ba8a051494357c12c"
integrity sha512-aX+gFgA5GHcDi89KG5keey2zf0WfZk/HAQotEamsK2kbey+8yGKcson0hbK8E+v0NArlCJQCqMP161YhV6ZXLg==

"@types/[email protected].2":
version "1.18.2"
resolved "https://registry.yarnpkg.com/@types/serverless/-/serverless-1.18.2.tgz#4054abce878bc4e29896c005cb8e88563fd0e4fc"
integrity sha512-bBbuNQkDUYZFjmT4KB/eiFKyoNbTcnSslVpeKH+3j1C9xhIkSS1sEZtiFb0TI9TnalxauGEnZRmQ0c6uzAY2Zg==
"@types/[email protected].3":
version "1.18.3"
resolved "https://registry.yarnpkg.com/@types/serverless/-/serverless-1.18.3.tgz#ac2fac6240b12fff587daaa05dd20b679e3ea534"
integrity sha512-8wrTc80rIhQWK3YHQOAQjYlZByeWhe8gtMn5bOedXdT2M8SyAYVeZIio1+pQIma1qbIpwg6H3YKkKDuJDLf36g==

"@types/stack-utils@^1.0.1":
version "1.0.1"
Expand Down

0 comments on commit 7d40908

Please sign in to comment.