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
120
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 3.30.0 to 3.31.0

dist/metrics/extension.d.ts

126

dist/index.js

@@ -49,13 +49,2 @@ "use strict";

};
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);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {

@@ -130,3 +119,2 @@ var m = typeof Symbol === "function" && o[Symbol.iterator];

var traceListener = new trace_1.TraceListener(finalConfig, handlerName);
var listeners = [metricsListener, traceListener];
// Only wrap the handler once unless forced

@@ -137,70 +125,66 @@ var _ddWrappedKey = "_ddWrapped";

}
var wrappedFunc = utils_1.wrap(handler, function (event, context) {
var e_1, _a;
utils_1.setColdStart();
utils_1.setLogLevel(finalConfig.debugLogging ? utils_1.LogLevel.DEBUG : utils_1.LogLevel.ERROR);
if (finalConfig.logger) {
utils_1.setLogger(finalConfig.logger);
}
currentMetricsListener = metricsListener;
currentTraceListener = traceListener;
try {
// Setup hook, (called once per handler invocation)
for (var listeners_1 = __values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()) {
var listener = listeners_1_1.value;
listener.onStartInvocation(event, context);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (listeners_1_1 && !listeners_1_1.done && (_a = listeners_1.return)) _a.call(listeners_1);
}
finally { if (e_1) throw e_1.error; }
}
if (finalConfig.enhancedMetrics) {
metrics_1.incrementInvocationsMetric(context);
}
}, function (event, context, error) { return __awaiter(_this, void 0, void 0, function () {
var listeners_2, listeners_2_1, listener, e_2_1;
var e_2, _a;
utils_1.setLogLevel(finalConfig.debugLogging ? utils_1.LogLevel.DEBUG : utils_1.LogLevel.ERROR);
if (finalConfig.logger) {
utils_1.setLogger(finalConfig.logger);
}
var promHandler = utils_1.promisifiedHandler(handler);
var wrappedFunc = function (event, context) { return __awaiter(_this, void 0, void 0, function () {
var _a, result, error, didError;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (finalConfig.enhancedMetrics && error) {
metrics_1.incrementErrorsMetric(context);
}
_b.label = 1;
utils_1.setColdStart();
currentMetricsListener = metricsListener;
currentTraceListener = traceListener;
return [4 /*yield*/, traceListener.onStartInvocation(event, context)];
case 1:
_b.trys.push([1, 6, 7, 8]);
listeners_2 = __values(listeners), listeners_2_1 = listeners_2.next();
_b.label = 2;
_b.sent();
return [4 /*yield*/, traceListener.onWrap(function (event, context) { return __awaiter(_this, void 0, void 0, function () {
var result, error, didError, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, metricsListener.onStartInvocation(event)];
case 1:
_a.sent();
if (finalConfig.enhancedMetrics) {
metrics_1.incrementInvocationsMetric(metricsListener, context);
}
didError = false;
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, promHandler(event, context)];
case 3:
result = (_a.sent());
return [3 /*break*/, 5];
case 4:
err_1 = _a.sent();
if (finalConfig.enhancedMetrics) {
metrics_1.incrementErrorsMetric(metricsListener, context);
}
err_1 = error;
didError = true;
return [3 /*break*/, 5];
case 5: return [4 /*yield*/, metricsListener.onCompleteInvocation()];
case 6:
_a.sent();
return [2 /*return*/, { result: result, error: error, didError: didError }];
}
});
}); })(event, context)];
case 2:
if (!!listeners_2_1.done) return [3 /*break*/, 5];
listener = listeners_2_1.value;
return [4 /*yield*/, listener.onCompleteInvocation()];
_a = _b.sent(), result = _a.result, error = _a.error, didError = _a.didError;
return [4 /*yield*/, traceListener.onCompleteInvocation()];
case 3:
_b.sent();
_b.label = 4;
case 4:
listeners_2_1 = listeners_2.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 8];
case 7:
try {
if (listeners_2_1 && !listeners_2_1.done && (_a = listeners_2.return)) _a.call(listeners_2);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 8:
currentMetricsListener = undefined;
currentTraceListener = undefined;
return [2 /*return*/];
if (didError) {
throw error;
}
return [2 /*return*/, result];
}
});
}); }, function (func) { return traceListener.onWrap(func); });
}); };
wrappedFunc[_ddWrappedKey] = true;

@@ -224,3 +208,3 @@ return wrappedFunc;

if (currentMetricsListener !== undefined) {
currentMetricsListener.sendDistributionMetricWithDate.apply(currentMetricsListener, __spread([name, value, metricTime], tags));
currentMetricsListener.sendDistributionMetricWithDate.apply(currentMetricsListener, __spread([name, value, metricTime, false], tags));
}

@@ -245,3 +229,3 @@ else {

if (currentMetricsListener !== undefined) {
currentMetricsListener.sendDistributionMetric.apply(currentMetricsListener, __spread([name, value], tags));
currentMetricsListener.sendDistributionMetric.apply(currentMetricsListener, __spread([name, value, false], tags));
}

@@ -248,0 +232,0 @@ else {

@@ -13,5 +13,4 @@ import { APIMetric } from "./model";

sendMetrics(metrics: APIMetric[]): Promise<void>;
private post;
private getUrl;
}
//# sourceMappingURL=api.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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -7,3 +43,2 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

exports.APIClient = void 0;
var https_1 = __importDefault(require("https"));
var querystring_1 = __importDefault(require("querystring"));

@@ -22,33 +57,29 @@ var url_1 = require("url");

APIClient.prototype.sendMetrics = function (metrics) {
return this.post(this.getUrl("api/v1/distribution_points"), { series: metrics });
};
APIClient.prototype.post = function (url, body) {
var _this = this;
var bodyJSON = JSON.stringify(body);
var buffer = Buffer.from(bodyJSON);
utils_1.logDebug("sending payload with body " + bodyJSON);
return new Promise(function (resolve, reject) {
var options = {
headers: { "content-type": "application/json" },
host: url.host,
method: "POST",
path: "" + url.pathname + url.search,
protocol: url.protocol,
};
var request = https_1.default.request(options, function (response) {
if (response.statusCode === undefined || response.statusCode < 200 || response.statusCode > 299) {
if (response.statusCode === 403) {
utils_1.logDebug("authorization failed with api key of length " + _this.apiKey.length + " characters");
}
reject("Invalid status code " + response.statusCode);
return __awaiter(this, void 0, void 0, function () {
var e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, utils_1.post(this.getUrl("api/v1/distribution_points"), { series: metrics })];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
e_1 = _a.sent();
if (!utils_1.isHTTPError(e_1)) {
utils_1.logDebug("Failed to send metrics " + e_1);
throw e_1;
}
if (e_1.type === utils_1.HTTPErrorType.BadAuth) {
utils_1.logDebug("authorization failed with api key of length " + this.apiKey.length + " characters");
}
if (e_1.type === utils_1.HTTPErrorType.FailedSend) {
utils_1.logDebug("Failed to send metrics " + e_1.message);
throw "Failed to send metrics: " + e_1.message;
}
throw e_1.message;
case 3: return [2 /*return*/];
}
else {
resolve();
}
});
request.on("error", function (error) {
reject("Failed to send metrics: " + error);
});
request.write(buffer);
request.end();
});

@@ -55,0 +86,0 @@ };

import { Context } from "aws-lambda";
import { MetricsListener } from "./listener";
/**

@@ -9,4 +10,4 @@ * Uses process.version to create a runtime tag

export declare function getEnhancedMetricTags(context: Context): string[];
export declare function incrementInvocationsMetric(context: Context): void;
export declare function incrementErrorsMetric(context: Context): void;
export declare function incrementInvocationsMetric(listener: MetricsListener, context: Context): void;
export declare function incrementErrorsMetric(listener: MetricsListener, context: Context): void;
//# sourceMappingURL=enhanced-metrics.d.ts.map

@@ -27,3 +27,2 @@ "use strict";

var process_version_1 = require("../utils/process-version");
var metric_log_1 = require("./metric-log");
var ENHANCED_LAMBDA_METRICS_NAMESPACE = "aws.lambda.enhanced";

@@ -78,14 +77,14 @@ // Same tag strings added to normal Lambda integration metrics

*/
function incrementEnhancedMetric(metricName, context) {
function incrementEnhancedMetric(listener, metricName, context) {
// Always write enhanced metrics to standard out
metric_log_1.writeMetricToStdout(ENHANCED_LAMBDA_METRICS_NAMESPACE + "." + metricName, 1, new Date(), getEnhancedMetricTags(context));
listener.sendDistributionMetric.apply(listener, __spread(["aws.lambda.enhanced." + metricName, 1, true], getEnhancedMetricTags(context)));
}
function incrementInvocationsMetric(context) {
incrementEnhancedMetric("invocations", context);
function incrementInvocationsMetric(listener, context) {
incrementEnhancedMetric(listener, "invocations", context);
}
exports.incrementInvocationsMetric = incrementInvocationsMetric;
function incrementErrorsMetric(context) {
incrementEnhancedMetric("errors", context);
function incrementErrorsMetric(listener, context) {
incrementEnhancedMetric(listener, "errors", context);
}
exports.incrementErrorsMetric = incrementErrorsMetric;
//# sourceMappingURL=enhanced-metrics.js.map

@@ -42,7 +42,9 @@ import { KMSService } from "./kms-service";

private apiKey;
private statsDClient?;
private isAgentRunning?;
constructor(kmsClient: KMSService, config: MetricsConfig);
onStartInvocation(_: any): void;
onStartInvocation(_: any): Promise<void>;
onCompleteInvocation(): Promise<void>;
sendDistributionMetricWithDate(name: string, value: number, metricTime: Date, ...tags: string[]): void;
sendDistributionMetric(name: string, value: number, ...tags: string[]): void;
sendDistributionMetricWithDate(name: string, value: number, metricTime: Date, forceAsync: boolean, ...tags: string[]): void;
sendDistributionMetric(name: string, value: number, forceAsync: boolean, ...tags: string[]): void;
private createProcessor;

@@ -49,0 +51,0 @@ private getAPIKey;

@@ -66,2 +66,4 @@ "use strict";

var processor_1 = require("./processor");
var hot_shots_1 = require("hot-shots");
var extension_1 = require("./extension");
var metricsBatchSendIntervalMS = 10000; // 10 seconds

@@ -72,9 +74,33 @@ var MetricsListener = /** @class */ (function () {

this.config = config;
this.isAgentRunning = undefined;
this.apiKey = this.getAPIKey(config);
}
MetricsListener.prototype.onStartInvocation = function (_) {
if (this.config.logForwarding) {
return;
}
this.currentProcessor = this.createProcessor(this.config, this.apiKey);
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(this.isAgentRunning === undefined)) return [3 /*break*/, 2];
_a = this;
return [4 /*yield*/, extension_1.isAgentRunning()];
case 1:
_a.isAgentRunning = _b.sent();
utils_1.logDebug("Extension present: " + this.isAgentRunning);
_b.label = 2;
case 2:
if (this.config.logForwarding) {
utils_1.logDebug("logForwarding configured");
return [2 /*return*/];
}
if (this.isAgentRunning) {
utils_1.logDebug("Using StatsD client");
this.statsDClient = new hot_shots_1.StatsD({ host: "127.0.0.1" });
return [2 /*return*/];
}
this.currentProcessor = this.createProcessor(this.config, this.apiKey);
return [2 /*return*/];
}
});
});
};

@@ -84,6 +110,7 @@ MetricsListener.prototype.onCompleteInvocation = function () {

var processor, error_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
_a.trys.push([0, 8, , 9]);
if (!(this.currentProcessor !== undefined)) return [3 /*break*/, 4];

@@ -106,10 +133,33 @@ return [4 /*yield*/, this.currentProcessor];

_a.label = 4;
case 4: return [3 /*break*/, 6];
case 4:
if (!(this.statsDClient !== undefined)) return [3 /*break*/, 7];
utils_1.logDebug("Flushing statsD");
// Make sure all stats are flushed to extension
return [4 /*yield*/, new Promise(function (resolve, reject) {
var _a;
(_a = _this.statsDClient) === null || _a === void 0 ? void 0 : _a.close(function (error) {
if (error !== undefined) {
reject(error);
}
resolve();
});
})];
case 5:
// Make sure all stats are flushed to extension
_a.sent();
this.statsDClient = undefined;
utils_1.logDebug("Flushing Extension");
return [4 /*yield*/, extension_1.flushExtension()];
case 6:
_a.sent();
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
error_1 = _a.sent();
// This can fail for a variety of reasons, from the API not being reachable,
// to KMS key decryption failing.
console.log(error_1);
utils_1.logError("failed to flush metrics", { innerError: error_1 });
return [3 /*break*/, 6];
case 6:
return [3 /*break*/, 9];
case 9:
this.currentProcessor = undefined;

@@ -121,8 +171,13 @@ return [2 /*return*/];

};
MetricsListener.prototype.sendDistributionMetricWithDate = function (name, value, metricTime) {
MetricsListener.prototype.sendDistributionMetricWithDate = function (name, value, metricTime, forceAsync) {
var _a;
var tags = [];
for (var _i = 3; _i < arguments.length; _i++) {
tags[_i - 3] = arguments[_i];
for (var _i = 4; _i < arguments.length; _i++) {
tags[_i - 4] = arguments[_i];
}
if (this.config.logForwarding) {
if (this.isAgentRunning) {
(_a = this.statsDClient) === null || _a === void 0 ? void 0 : _a.distribution(name, value, undefined, tags);
return;
}
if (this.config.logForwarding || forceAsync) {
metric_log_1.writeMetricToStdout(name, value, metricTime, tags);

@@ -141,8 +196,8 @@ return;

};
MetricsListener.prototype.sendDistributionMetric = function (name, value) {
MetricsListener.prototype.sendDistributionMetric = function (name, value, forceAsync) {
var tags = [];
for (var _i = 2; _i < arguments.length; _i++) {
tags[_i - 2] = arguments[_i];
for (var _i = 3; _i < arguments.length; _i++) {
tags[_i - 3] = arguments[_i];
}
this.sendDistributionMetricWithDate.apply(this, __spread([name, value, new Date(Date.now())], tags));
this.sendDistributionMetricWithDate.apply(this, __spread([name, value, new Date(Date.now()), forceAsync], tags));
};

@@ -149,0 +204,0 @@ MetricsListener.prototype.createProcessor = function (config, apiKey) {

@@ -6,4 +6,4 @@ import { Context, Handler } from "aws-lambda";

*/
export declare function wrap<TEvent, TResult>(handler: Handler<TEvent, TResult>, onStart: (event: TEvent, context: Context) => void, onComplete: (event: TEvent, context: Context, error?: Error) => Promise<void>, onWrap?: OnWrapFunc): Handler<TEvent, TResult>;
export declare function wrap<TEvent, TResult>(handler: Handler<TEvent, TResult>, onStart: (event: TEvent, context: Context) => Promise<void>, onComplete: (event: TEvent, context: Context, error?: Error) => 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
export { didFunctionColdStart, getColdStartTag, setColdStart } from "./cold-start";
export { wrap } from "./handler";
export { wrap, promisifiedHandler } from "./handler";
export { Timer } from "./timer";
export { logError, logDebug, Logger, setLogLevel, setLogger, LogLevel } from "./log";
export { get, post, HTTPErrorType, HTTPError, isHTTPError } from "./request";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogLevel = exports.setLogger = exports.setLogLevel = exports.logDebug = exports.logError = exports.Timer = exports.wrap = exports.setColdStart = exports.getColdStartTag = exports.didFunctionColdStart = void 0;
exports.isHTTPError = exports.HTTPErrorType = exports.post = exports.get = exports.LogLevel = exports.setLogger = exports.setLogLevel = exports.logDebug = exports.logError = exports.Timer = exports.promisifiedHandler = exports.wrap = exports.setColdStart = exports.getColdStartTag = exports.didFunctionColdStart = void 0;
var cold_start_1 = require("./cold-start");

@@ -10,2 +10,3 @@ Object.defineProperty(exports, "didFunctionColdStart", { enumerable: true, get: function () { return cold_start_1.didFunctionColdStart; } });

Object.defineProperty(exports, "wrap", { enumerable: true, get: function () { return handler_1.wrap; } });
Object.defineProperty(exports, "promisifiedHandler", { enumerable: true, get: function () { return handler_1.promisifiedHandler; } });
var timer_1 = require("./timer");

@@ -19,2 +20,7 @@ Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return timer_1.Timer; } });

Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return log_1.LogLevel; } });
var request_1 = require("./request");
Object.defineProperty(exports, "get", { enumerable: true, get: function () { return request_1.get; } });
Object.defineProperty(exports, "post", { enumerable: true, get: function () { return request_1.post; } });
Object.defineProperty(exports, "HTTPErrorType", { enumerable: true, get: function () { return request_1.HTTPErrorType; } });
Object.defineProperty(exports, "isHTTPError", { enumerable: true, get: function () { return request_1.isHTTPError; } });
//# sourceMappingURL=index.js.map
{
"name": "datadog-lambda-js",
"version": "3.30.0",
"version": "3.31.0",
"description": "Lambda client library that supports hybrid tracing in node js",

@@ -37,2 +37,3 @@ "main": "dist/index.js",

"bignumber.js": "^9.0.0",
"hot-shots": "true7.7.1",
"promise-retry": "^2.0.1",

@@ -39,0 +40,0 @@ "serialize-error": "^7.0.1",

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