-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.ts
More file actions
495 lines (436 loc) · 16.8 KB
/
index.ts
File metadata and controls
495 lines (436 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { Context, Handler } from "aws-lambda";
import { HANDLER_STREAMING, STREAM_RESPONSE } from "./constants";
import {
incrementErrorsMetric,
incrementInvocationsMetric,
incrementBatchItemFailureMetric,
KMSService,
MetricsConfig,
MetricsListener,
MetricsQueue,
} from "./metrics";
import { TraceConfig, TraceListener } from "./trace";
import { subscribeToDC } from "./runtime";
import {
isBatchItemFailure,
batchItemFailureCount,
logDebug,
Logger,
LogLevel,
promisifiedHandler,
setSandboxInit,
setLogger,
setLogLevel,
isManagedInstancesMode,
} from "./utils";
import { getEnhancedMetricTags } from "./metrics/enhanced-metrics";
import { DatadogTraceHeaders } from "./trace/context/extractor";
import { SpanWrapper } from "./trace/span-wrapper";
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";
import { initDurableFunctionTracing } from "./trace/durable-function-patch";
// Backwards-compatible export, TODO deprecate in next major
export { DatadogTraceHeaders as TraceHeaders } from "./trace/context/extractor";
export const apiKeyEnvVar = "DD_API_KEY";
export const apiKeyKMSEnvVar = "DD_KMS_API_KEY";
export const apiKeySecretARNEnvVar = "DD_API_KEY_SECRET_ARN";
export const captureLambdaPayloadEnvVar = "DD_CAPTURE_LAMBDA_PAYLOAD";
export const captureLambdaPayloadMaxDepthEnvVar = "DD_CAPTURE_LAMBDA_PAYLOAD_MAX_DEPTH";
export const traceManagedServicesEnvVar = "DD_TRACE_MANAGED_SERVICES";
export const siteURLEnvVar = "DD_SITE";
export const logLevelEnvVar = "DD_LOG_LEVEL";
export const logForwardingEnvVar = "DD_FLUSH_TO_LOG";
export const logInjectionEnvVar = "DD_LOGS_INJECTION";
export const enhancedMetricsEnvVar = "DD_ENHANCED_METRICS";
export const datadogHandlerEnvVar = "DD_LAMBDA_HANDLER";
export const lambdaTaskRootEnvVar = "LAMBDA_TASK_ROOT";
export const mergeXrayTracesEnvVar = "DD_MERGE_XRAY_TRACES";
export const traceExtractorEnvVar = "DD_TRACE_EXTRACTOR";
export const defaultSiteURL = "datadoghq.com";
export const encodeAuthorizerContextEnvVar = "DD_ENCODE_AUTHORIZER_CONTEXT";
export const decodeAuthorizerContextEnvVar = "DD_DECODE_AUTHORIZER_CONTEXT";
export const coldStartTracingEnvVar = "DD_COLD_START_TRACING";
export const minColdStartTraceDurationEnvVar = "DD_MIN_COLD_START_DURATION";
export const coldStartTraceSkipLibEnvVar = "DD_COLD_START_TRACE_SKIP_LIB";
export const localTestingEnvVar = "DD_LOCAL_TESTING";
export const addSpanPointersEnvVar = "DD_TRACE_AWS_ADD_SPAN_POINTERS";
export const dataStreamsEnabledEnvVar = "DD_DATA_STREAMS_ENABLED";
interface GlobalConfig {
/**
* Whether to log extra information.
* @default false
*/
debugLogging: boolean;
/**
* Whether to force the `datadog()` wrapper to always wrap.
* @default false
*/
forceWrap: boolean;
/**
* Custom logger.
*/
logger?: Logger;
}
/**
* Configuration options for Datadog's lambda wrapper.
*/
export type Config = MetricsConfig & TraceConfig & GlobalConfig;
export const defaultConfig: Config = {
apiKey: "",
apiKeyKMS: "",
apiKeySecretARN: "",
autoPatchHTTP: true,
captureLambdaPayload: false,
captureLambdaPayloadMaxDepth: 10,
createInferredSpan: true,
debugLogging: false,
encodeAuthorizerContext: true,
decodeAuthorizerContext: true,
enhancedMetrics: true,
forceWrap: false,
injectLogContext: true,
logForwarding: false,
mergeDatadogXrayTraces: false,
shouldRetryMetrics: false,
siteURL: "",
minColdStartTraceDuration: 3,
coldStartTraceSkipLib: "",
localTesting: false,
addSpanPointers: true,
dataStreamsEnabled: false,
} as const;
export const _metricsQueue: MetricsQueue = new MetricsQueue();
let currentMetricsListener: MetricsListener | undefined;
let currentTraceListener: TraceListener | undefined;
// Skip cold start tracing subscription in managed instances mode
// In managed instances, the tracer library handles cold start independently
if (getEnvValue(coldStartTracingEnvVar, "true").toLowerCase() === "true" && !isManagedInstancesMode()) {
subscribeToDC();
}
// Initialize durable function tracing if SDK is present
initDurableFunctionTracing();
const initTime = Date.now();
/**
* Wraps your AWS lambda handler functions to add tracing/metrics support
* @param handler A lambda handler function.
* @param config Configuration options for datadog.
* @returns A wrapped handler function.
*
* ```javascript
* import { datadog } from 'datadog-lambda-js';
* function yourHandler(event) {}
* exports.yourHandler = datadog(yourHandler);
* ```
*/
export function datadog<TEvent, TResult>(
handler: Handler<TEvent, TResult> | any,
config?: Partial<Config>,
): Handler<TEvent, TResult> | any {
const finalConfig = getConfig(config);
const metricsListener = new MetricsListener(new KMSService(), finalConfig);
const traceListener = new TraceListener(finalConfig);
// Only wrap the handler once unless forced
const _ddWrappedKey = "_ddWrapped";
if ((handler as any)[_ddWrappedKey] !== undefined && !finalConfig.forceWrap) {
return handler;
}
setLogLevel(finalConfig.debugLogging ? LogLevel.DEBUG : LogLevel.ERROR);
if (finalConfig.logger) {
setLogger(finalConfig.logger);
}
const isResponseStreamFunction =
handler[HANDLER_STREAMING] !== undefined && handler[HANDLER_STREAMING] === STREAM_RESPONSE;
const promHandler: any = promisifiedHandler(handler);
let wrappedFunc: any;
wrappedFunc = async (...args: any[]) => {
const { event, context, responseStream } = extractArgs(isResponseStreamFunction, ...args);
const startTime = new Date();
setSandboxInit(initTime, startTime.getTime());
currentMetricsListener = metricsListener;
currentTraceListener = traceListener;
try {
await traceListener.onStartInvocation(event, context);
await metricsListener.onStartInvocation(event, context);
if (finalConfig.enhancedMetrics) {
incrementInvocationsMetric(metricsListener, context);
}
sendQueueMetrics(metricsListener);
} catch (err) {
if (err instanceof Error) {
logDebug("Failed to start listeners", err);
}
}
let result: TResult | undefined;
let localResult: TResult | undefined;
let error: any;
let didThrow = false;
try {
const traceListenerOnWrap = async (...localArgs: any[]) => {
const {
event: localEvent,
context: localContext,
responseStream: localResponseStream,
} = extractArgs(isResponseStreamFunction, ...localArgs);
if (isResponseStreamFunction) {
responseStream.once("drain", () => {
const firstDrainTime = new Date();
const timeToFirstByte = firstDrainTime.getTime() - startTime.getTime();
metricsListener.sendDistributionMetric(
"aws.lambda.enhanced.time_to_first_byte",
timeToFirstByte,
true,
...getEnhancedMetricTags(context),
);
});
}
try {
localResult = isResponseStreamFunction
? await promHandler(localEvent, localResponseStream, localContext)
: await promHandler(localEvent, localContext);
} finally {
const responseIs5xxError = traceListener.onEndingInvocation(
localEvent,
localResult,
isResponseStreamFunction,
);
if (responseIs5xxError) {
incrementErrorsMetric(metricsListener, context);
}
if (isBatchItemFailure(localResult)) {
incrementBatchItemFailureMetric(metricsListener, batchItemFailureCount(localResult), context);
}
}
return localResult;
};
result = isResponseStreamFunction
? await traceListener.onWrap(traceListenerOnWrap)(event, responseStream, context)
: await traceListener.onWrap(traceListenerOnWrap)(event, context);
} catch (err) {
didThrow = true;
error = err;
}
try {
if (didThrow && finalConfig.enhancedMetrics) {
incrementErrorsMetric(metricsListener, context);
}
await metricsListener.onCompleteInvocation();
await traceListener.onCompleteInvocation(error, result, event);
} catch (err) {
if (err instanceof Error) {
logDebug("Failed to complete listeners", err);
}
}
currentMetricsListener = undefined;
currentTraceListener = undefined;
if (didThrow) {
throw error;
}
return result as TResult;
};
(wrappedFunc as any)[_ddWrappedKey] = true;
if (isResponseStreamFunction) {
(wrappedFunc as any)[HANDLER_STREAMING] = STREAM_RESPONSE;
}
return wrappedFunc;
}
/**
*
* @param isResponseStreamFunction A boolean determining if a Lambda Function is Response Stream.
* @param args Spread arguments of a Lambda Function.
* @returns An object containing the context and the event of a Lambda Function.
*/
export function extractArgs<TEvent>(isResponseStreamFunction: boolean, ...args: any[]) {
const context: Context = isResponseStreamFunction ? args[2] : args.length > 0 ? (args[1] as Context) : {};
const event: TEvent = args[0];
const responseStream: any = isResponseStreamFunction ? args[1] : undefined;
return { context, event, responseStream };
}
/**
* Sends a Distribution metric asynchronously to the Datadog API.
* @param name The name of the metric to send.
* @param value The value of the metric
* @param metricTime The timestamp associated with this metric data point.
* @param tags The tags associated with the metric. Should be of the format "tag:value".
*/
export function sendDistributionMetricWithDate(name: string, value: number, metricTime: Date, ...tags: string[]) {
tags = [...tags, getRuntimeTag(), ...getDDTags()];
if (currentMetricsListener !== undefined) {
currentMetricsListener.sendDistributionMetricWithDate(name, value, metricTime, false, ...tags);
return;
}
_metricsQueue.push({ name, value, metricTime, tags });
}
/**
* Sends a Distribution metric asynchronously to the Datadog API.
* @param name The name of the metric to send.
* @param value The value of the metric
* @param tags The tags associated with the metric. Should be of the format "tag:value".
*/
export function sendDistributionMetric(name: string, value: number, ...tags: string[]) {
tags = [...tags, getRuntimeTag()];
if (currentMetricsListener !== undefined) {
currentMetricsListener.sendDistributionMetric(name, value, false, ...tags);
return;
}
_metricsQueue.push({ name, value, tags });
}
function sendQueueMetrics(listener: MetricsListener) {
// Reverse the queue to send metrics in order.
// This is necessary because the "queue" is a stack,
// and we want to send metrics in the order they were added.
_metricsQueue.reverse();
while (_metricsQueue.length > 0) {
const metric = _metricsQueue.pop()!; // This will always exist.
const { name, value, metricTime, tags } = metric;
if (metricTime !== undefined) {
listener.sendDistributionMetricWithDate(name, value, metricTime, false, ...tags);
continue;
}
listener.sendDistributionMetric(name, value, false, ...tags);
}
}
/**
* Retrieves the Datadog headers for the current trace.
*/
export function getTraceHeaders(): Partial<DatadogTraceHeaders> {
if (currentTraceListener === undefined) {
return {};
}
return currentTraceListener.currentTraceHeaders;
}
function getConfig(userConfig?: Partial<Config>): Config {
let config: Config;
if (userConfig === undefined) {
config = defaultConfig;
} else {
config = {
...defaultConfig,
...userConfig,
};
}
if (config.apiKey === "") {
config.apiKey = getEnvValue(apiKeyEnvVar, "");
}
if (config.siteURL === "") {
config.siteURL = getEnvValue(siteURLEnvVar, defaultSiteURL);
}
if (config.apiKeyKMS === "") {
config.apiKeyKMS = getEnvValue(apiKeyKMSEnvVar, "");
}
if (config.apiKeySecretARN === "") {
config.apiKeySecretARN = getEnvValue(apiKeySecretARNEnvVar, "");
}
if (userConfig === undefined || userConfig.injectLogContext === undefined) {
const result = getEnvValue(logInjectionEnvVar, "true").toLowerCase();
config.injectLogContext = result === "true";
}
if (userConfig === undefined || userConfig.debugLogging === undefined) {
const result = getEnvValue(logLevelEnvVar, "ERROR").toLowerCase();
config.debugLogging = result === "debug";
}
if (userConfig === undefined || userConfig.logForwarding === undefined) {
const result = getEnvValue(logForwardingEnvVar, "false").toLowerCase();
config.logForwarding = result === "true";
}
if (userConfig === undefined || userConfig.enhancedMetrics === undefined) {
const result = getEnvValue(enhancedMetricsEnvVar, "true").toLowerCase();
config.enhancedMetrics = result === "true";
}
if (userConfig === undefined || userConfig.mergeDatadogXrayTraces === undefined) {
const result = getEnvValue(mergeXrayTracesEnvVar, "false").toLowerCase();
config.mergeDatadogXrayTraces = result === "true";
}
if (userConfig === undefined || userConfig.captureLambdaPayload === undefined) {
const result = getEnvValue(captureLambdaPayloadEnvVar, "false").toLowerCase();
config.captureLambdaPayload = result === "true";
}
if (userConfig === undefined || userConfig.createInferredSpan === undefined) {
const result = getEnvValue(traceManagedServicesEnvVar, "true").toLowerCase();
config.createInferredSpan = result === "true";
}
if (userConfig === undefined || userConfig.encodeAuthorizerContext === undefined) {
const result = getEnvValue(encodeAuthorizerContextEnvVar, "true").toLowerCase();
config.encodeAuthorizerContext = result === "true";
}
if (userConfig === undefined || userConfig.decodeAuthorizerContext === undefined) {
const result = getEnvValue(decodeAuthorizerContextEnvVar, "true").toLowerCase();
config.decodeAuthorizerContext = result === "true";
}
if (userConfig === undefined || userConfig.minColdStartTraceDuration === undefined) {
config.minColdStartTraceDuration = Number(getEnvValue(minColdStartTraceDurationEnvVar, "3"));
}
if (userConfig === undefined || userConfig.minColdStartTraceDuration === undefined) {
config.coldStartTraceSkipLib = getEnvValue(coldStartTraceSkipLibEnvVar, "./opentracing/tracer");
}
if (userConfig === undefined || userConfig.captureLambdaPayloadMaxDepth === undefined) {
config.captureLambdaPayloadMaxDepth = Number(getEnvValue(captureLambdaPayloadMaxDepthEnvVar, "10"));
}
if (userConfig === undefined || userConfig.localTesting === undefined) {
const result = getEnvValue(localTestingEnvVar, "false").toLowerCase();
// TODO deprecate 1 for truthy, this shouldn't have been allowed
// but the extension allows it, so we must as well
// @ts-ignore-next-line
config.localTesting = result === "true" || result === "1";
}
if (userConfig === undefined || userConfig.addSpanPointers === undefined) {
const result = getEnvValue(addSpanPointersEnvVar, "true").toLowerCase();
config.addSpanPointers = result === "true";
}
if (userConfig === undefined || userConfig.dataStreamsEnabled === undefined) {
const result = getEnvValue(dataStreamsEnabledEnvVar, "false").toLowerCase();
const validEnabledValues = new Set(["true", "1", "yes", "y", "on"]);
config.dataStreamsEnabled = validEnabledValues.has(result);
}
return config;
}
export function getEnvValue(key: string, defaultValue: string): string {
const val = process.env[key];
return val !== undefined ? val : defaultValue;
}
function getRuntimeTag(): string {
const version = process.version;
return `dd_lambda_layer:datadog-node${version}`;
}
function getDDTags(): string[] {
const ddTags = getEnvValue("DD_TAGS", "").split(",");
const ddService = getEnvValue("DD_SERVICE", "");
if (ddService.length > 0) {
ddTags.push(`service:${ddService}`);
}
const ddEnv = getEnvValue("DD_ENV", "");
if (ddEnv.length > 0) {
ddTags.push(`env:${ddEnv}`);
}
return ddTags;
}
export async function emitTelemetryOnErrorOutsideHandler(
error: Error,
functionName: string,
startTime: number,
): Promise<void> {
if (getEnvValue("DD_TRACE_ENABLED", "true").toLowerCase() === "true") {
const options: SpanOptions = {
tags: {
service: "aws.lambda",
operation_name: "aws.lambda",
resource_names: functionName,
"resource.name": functionName,
"span.type": "serverless",
"error.status": 500,
"error.type": error.name,
"error.message": error.message,
"error.stack": error.stack,
},
startTime,
};
const tracerWrapper = new TracerWrapper();
const span = new SpanWrapper(tracerWrapper.startSpan("aws.lambda", options), {});
span.finish();
}
const config = getConfig();
if (config.enhancedMetrics) {
const metricsListener = new MetricsListener(new KMSService(), config);
await metricsListener.onStartInvocation(undefined);
incrementErrorsMetric(metricsListener);
await metricsListener.onCompleteInvocation();
}
}