Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

datadog-lambda-js

Package Overview
Dependencies
Maintainers
1
Versions
117
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datadog-lambda-js - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

dist/dd-trace-js/docs/test.d.ts

28

dist/index.js

@@ -14,6 +14,7 @@ "use strict";

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -49,6 +50,6 @@ });

};
var __values = (this && this.__values) || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
return {
if (o && typeof o.length === "number") return {
next: function () {

@@ -59,2 +60,3 @@ if (o && i >= o.length) o = void 0;

};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};

@@ -90,2 +92,3 @@ var __read = (this && this.__read) || function (o, n) {

var logForwardingEnvVar = "DD_FLUSH_TO_LOG";
var experimentalDatadogTracingEnvVar = "DD_EXPERIMENTAL_TRACING";
var defaultSiteURL = "datadoghq.com";

@@ -103,2 +106,8 @@ exports.defaultConfig = {

var currentTraceListener;
// The tracer has to be initialized before tracer.wrap is called in client code, (which normally happens on require).
// Therefore, this file needs to be the first thing imported by the client, in their handler file.
var useDatadogTracing = getEnvValue(experimentalDatadogTracingEnvVar, "false").toLocaleLowerCase() === "true";
if (useDatadogTracing) {
trace_1.initDatadogTracer();
}
/**

@@ -120,5 +129,6 @@ * Wraps your AWS lambda handler functions to add tracing/metrics support

var metricsListener = new metrics_1.MetricsListener(new metrics_1.KMSService(), finalConfig);
var traceListener = new trace_1.TraceListener(finalConfig);
var handlerName = getEnvValue("_HANDLER", "handler");
var traceListener = new trace_1.TraceListener(finalConfig, handlerName);
var listeners = [metricsListener, traceListener];
return utils_1.wrap(handler, function (event) {
return utils_1.wrap(handler, function (event, context) {
var e_1, _a;

@@ -132,3 +142,3 @@ utils_1.setLogLevel(finalConfig.debugLogging ? utils_1.LogLevel.DEBUG : utils_1.LogLevel.ERROR);

var listener = listeners_1_1.value;
listener.onStartInvocation(event);
listener.onStartInvocation(event, context);
}

@@ -179,3 +189,3 @@ }

});
}); });
}); }, function (func) { return traceListener.onWrap(func); });
}

@@ -219,3 +229,3 @@ exports.datadog = datadog;

else {
config = __assign({}, exports.defaultConfig, userConfig);
config = __assign(__assign({}, exports.defaultConfig), userConfig);
}

@@ -222,0 +232,0 @@ if (config.apiKey === "") {

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -8,0 +9,0 @@ });

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -8,0 +9,0 @@ });

@@ -63,3 +63,3 @@ "use strict";

var distribution = new Distribution(this.name, this.points);
Object.assign(distribution, __assign({}, this, { points: __spread(this.points, metric.points) }));
Object.assign(distribution, __assign(__assign({}, this), { points: __spread(this.points, metric.points) }));
return distribution;

@@ -66,0 +66,0 @@ };

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -8,0 +9,0 @@ });

"use strict";
var __values = (this && this.__values) || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
return {
if (o && typeof o.length === "number") return {
next: function () {

@@ -11,2 +11,3 @@ if (o && i >= o.length) o = void 0;

};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};

@@ -13,0 +14,0 @@ Object.defineProperty(exports, "__esModule", { value: true });

@@ -1,3 +0,4 @@

export { TraceListener, TraceConfig } from "./listener";
export { initDatadogTracer } from "./datadog-tracer";
export { TraceConfig, TraceListener } from "./listener";
export { TraceHeaders } from "./trace-context-service";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var datadog_tracer_1 = require("./datadog-tracer");
exports.initDatadogTracer = datadog_tracer_1.initDatadogTracer;
var listener_1 = require("./listener");
exports.TraceListener = listener_1.TraceListener;
//# sourceMappingURL=index.js.map

@@ -0,1 +1,2 @@

import { Context } from "aws-lambda";
export interface TraceConfig {

@@ -10,8 +11,12 @@ /**

private config;
private handlerName;
private contextService;
private context?;
private coldstart;
readonly currentTraceHeaders: Partial<import("./trace-context-service").TraceHeaders>;
constructor(config: TraceConfig);
onStartInvocation(event: any): void;
constructor(config: TraceConfig, handlerName: string);
onStartInvocation(event: any, context: Context): void;
onCompleteInvocation(): Promise<void>;
onWrap<T = (...args: any[]) => any>(func: T): T;
}
//# sourceMappingURL=listener.d.ts.map
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -37,3 +38,7 @@ });

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var dd_trace_1 = __importDefault(require("dd-trace"));
var context_1 = require("./context");

@@ -43,5 +48,7 @@ var patch_http_1 = require("./patch-http");

var TraceListener = /** @class */ (function () {
function TraceListener(config) {
function TraceListener(config, handlerName) {
this.config = config;
this.handlerName = handlerName;
this.contextService = new trace_context_service_1.TraceContextService();
this.coldstart = true;
}

@@ -55,6 +62,7 @@ Object.defineProperty(TraceListener.prototype, "currentTraceHeaders", {

});
TraceListener.prototype.onStartInvocation = function (event) {
TraceListener.prototype.onStartInvocation = function (event, context) {
if (this.config.autoPatchHTTP) {
patch_http_1.patchHttp(this.contextService);
}
this.context = context;
this.contextService.rootTraceContext = context_1.extractTraceContext(event);

@@ -68,2 +76,3 @@ };

}
this.coldstart = false;
return [2 /*return*/];

@@ -73,2 +82,19 @@ });

};
TraceListener.prototype.onWrap = function (func) {
var rootTraceContext = this.contextService.currentTraceHeaders;
var spanContext = dd_trace_1.default.extract("http_headers", rootTraceContext);
var options = {};
if (this.context) {
options.tags = {
cold_start: this.coldstart,
function_arn: this.context.invokedFunctionArn,
request_id: this.context.awsRequestId,
resource_names: this.context.functionName,
};
}
if (spanContext !== null) {
options.childOf = spanContext;
}
return dd_trace_1.default.wrap(this.handlerName, options, func);
};
return TraceListener;

@@ -75,0 +101,0 @@ }());

@@ -80,3 +80,3 @@ "use strict";

else if (typeof arg2 === "object") {
options = __assign({}, options, arg2);
options = __assign(__assign({}, options), arg2);
}

@@ -91,5 +91,5 @@ return { options: options, callback: callback };

var traceHeaders = traceService.currentTraceHeaders;
headers = __assign({}, headers, traceHeaders);
return __assign({}, options, { headers: headers });
headers = __assign(__assign({}, headers), traceHeaders);
return __assign(__assign({}, options), { headers: headers });
}
//# sourceMappingURL=patch-http.js.map

@@ -1,6 +0,8 @@

import { Handler } from "aws-lambda";
import { Context, Handler } from "aws-lambda";
export declare type OnWrapFunc<T = (...args: any[]) => any> = (fn: T) => T;
/**
* Wraps a lambda handler function, adding an onStart and onComplete hook.
*/
export declare function wrap<TEvent, TResult>(handler: Handler<TEvent, TResult>, onStart: (event: TEvent) => void, onComplete: () => Promise<void>): Handler<TEvent, TResult>;
export declare function wrap<TEvent, TResult>(handler: Handler<TEvent, TResult>, onStart: (event: TEvent, context: Context) => void, onComplete: () => Promise<void>, onWrap?: OnWrapFunc): Handler<TEvent, TResult>;
export declare function promisifiedHandler<TEvent, TResult>(handler: Handler<TEvent, TResult>): (event: TEvent, context: Context) => Promise<TResult>;
//# sourceMappingURL=handler.d.ts.map
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());

@@ -42,48 +43,64 @@ });

*/
function wrap(handler, onStart, onComplete) {
function wrap(handler, onStart, onComplete, onWrap) {
var _this = this;
return function (event, context, callback) {
var promHandler = promisifiedHandler(handler);
return function (event, context) { return __awaiter(_this, void 0, void 0, function () {
var error_1, result, wrappedHandler, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, onStart(event, context)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
// Swallow the error and continue processing.
log_1.logError("Pre-lambda hook threw error", { innerError: error_1 });
return [3 /*break*/, 3];
case 3:
_a.trys.push([3, , 5, 9]);
wrappedHandler = onWrap !== undefined ? onWrap(promHandler) : promHandler;
return [4 /*yield*/, wrappedHandler(event, context)];
case 4:
result = _a.sent();
return [3 /*break*/, 9];
case 5:
_a.trys.push([5, 7, , 8]);
return [4 /*yield*/, onComplete()];
case 6:
_a.sent();
return [3 /*break*/, 8];
case 7:
error_2 = _a.sent();
// Swallow the error and continue processing.
log_1.logError("Post-lambda hook threw error", { innerError: error_2 });
return [3 /*break*/, 8];
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/, result];
}
});
}); };
}
exports.wrap = wrap;
function promisifiedHandler(handler) {
return function (event, context) {
// Lambda functions in node complete in one of two possible ways.
// 1. By calling the "callback" function with a result.
// 2. Returning a value directly from the function using a promise.
try {
onStart(event);
}
catch (error) {
// Swallow the error and continue processing.
log_1.logError("Pre-lambda hook threw error", { innerError: error });
}
// Handle the case where the result comes from a callback
var modifiedCallback = function (err, result) {
var cb = onComplete();
cb.catch(function (error) {
log_1.logError("Post-lambda hook threw error", { innerError: error });
}).then(function () {
callback(err, result);
});
};
var modifiedCallback = function () { };
var callbackProm = new Promise(function (resolve, reject) {
modifiedCallback = function (err, result) {
if (err !== undefined && err !== null) {
reject(err);
}
else {
resolve(result);
}
};
});
var promise = handler(event, context, modifiedCallback);
// Handle the case where the result comes from a promise
if (promise !== undefined) {
promise = promise.then(function (result) {
var cb = onComplete();
// If there is a reject with the onComplete, swallow it and return the result as normal.
return cb.then(function () { return result; }, function (error) {
log_1.logError("Post-lambda hook threw error", { innerError: error });
return result;
});
}, function (rejection) {
var cb = onComplete();
// Return the original reason for rejection, regardless of whether onComplete resolved or rejected.
return cb.then(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw rejection;
});
}); }, function (error) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
log_1.logError("Post-lambda hook threw error", { innerError: error });
throw rejection;
});
}); });
});
if (promise === undefined) {
promise = callbackProm;
}

@@ -93,3 +110,3 @@ return promise;

}
exports.wrap = wrap;
exports.promisifiedHandler = promisifiedHandler;
//# sourceMappingURL=handler.js.map

@@ -34,3 +34,3 @@ "use strict";

else {
console.debug(JSON.stringify(__assign({}, metadata, { status: "debug", message: message })));
console.debug(JSON.stringify(__assign(__assign({}, metadata), { status: "debug", message: message })));
}

@@ -48,3 +48,3 @@ }

else {
console.error(JSON.stringify(__assign({}, metadata, { status: "error", message: message })));
console.error(JSON.stringify(__assign(__assign({}, metadata), { status: "error", message: message })));
}

@@ -51,0 +51,0 @@ }

{
"name": "datadog-lambda-js",
"version": "0.3.0",
"version": "0.4.0",
"description": "Lambda client library that supports hybrid tracing in node js",

@@ -14,3 +14,4 @@ "main": "dist/index.js",

"test:watch": "jest --watch",
"lint": "tslint --project tsconfig.json"
"lint": "tslint --project tsconfig.json",
"check-formatting": "prettier --check src/**"
},

@@ -22,3 +23,3 @@ "devDependencies": {

"@types/jest": "^24.0.13",
"@types/nock": "^10.0.3",
"@types/nock": "^11.1.0",
"@types/node": "^12.0.4",

@@ -28,3 +29,3 @@ "@types/promise-retry": "^1.1.3",

"jest": "^24.8.0",
"nock": "^10.0.6",
"nock": "^11.3.4",
"prettier": "^1.17.1",

@@ -38,2 +39,3 @@ "ts-jest": "^24.0.2",

"bignumber.js": "^9.0.0",
"dd-trace": "0.16.0-beta.1",
"promise-retry": "^1.1.1",

@@ -55,4 +57,3 @@ "shimmer": "^1.2.1"

"coverageReporters": [
"json",
"text-summary"
"lcovonly"
],

@@ -59,0 +60,0 @@ "testRegex": "(src\\/).*(\\.spec\\.ts)$",

# datadog-lambda-layer-js
![CircleCI](https://img.shields.io/circleci/build/github/DataDog/datadog-lambda-layer-js)
[![Code Coverage](https://img.shields.io/codecov/c/github/DataDog/datadog-lambda-layer-js)](https://codecov.io/gh/DataDog/datadog-lambda-layer-js)
[![NPM](https://img.shields.io/npm/v/datadog-lambda-js)](https://www.npmjs.com/package/datadog-lambda-js)
[![Slack](https://img.shields.io/badge/slack-%23serverless-blueviolet?logo=slack)](https://datadoghq.slack.com/channels/serverless/)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/DataDog/datadog-lambda-layer-js/blob/master/LICENSE)
Datadog's Lambda node client library enables distributed tracing between serverful and serverless environments, as well as letting you send custom metrics to the Datadog API.

@@ -53,3 +59,3 @@

environment:
DATADOG_API_KEY: xxx
DD_API_KEY: xxx
```

@@ -73,2 +79,6 @@

### DD_FLUSH_TO_LOG
If you have the Datadog Lambda Log forwarder enabled and are sending custom metrics, set this to true so your metrics will be sent via logs, (instead of being sent at the end of your lambda invocation).
## Usage

@@ -79,3 +89,3 @@

```typescript
import { datadog } from "datadog-lambda-js";
const { datadog } = require("datadog-lambda-js");

@@ -89,5 +99,5 @@ async function myHandler(event, context) {

// Wrap your handler function like this
exports.exports.myHandler = datadog(myHandler);
module.exports.myHandler = datadog(myHandler);
/* OR with manual configuration options
exports.exports.myHandler = datadog(myHandler, {
module.exports.myHandler = datadog(myHandler, {
apiKey: "my-api-key"

@@ -105,3 +115,3 @@ });

```typescript
import { sendDistributionMetric } from "datadog-lambda-js";
const { sendDistributionMetric } = require("datadog-lambda-js");

@@ -135,4 +145,4 @@ sendDistributionMetric(

```typescript
import http from "http";
import { sendDistributionMetric } from "datadog-lambda-js";
const https = require("https");
const { datadog, getTraceHeaders } = require("datadog-lambda-js");

@@ -146,3 +156,3 @@ async function myHandler(event, context) {

// Explicitly disable auto patching
exports.exports.myHandler = datadog(myHandler, { autoPatchHTTP: false });
module.exports.myHandler = datadog(myHandler, { autoPatchHTTP: false });
```

@@ -149,0 +159,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc