datadog-lambda-js
Advanced tools
Comparing version 4.67.0 to 4.68.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.datadogLambdaVersion = void 0; | ||
exports.datadogLambdaVersion = "4.67.0"; | ||
exports.datadogLambdaVersion = "4.68.0"; | ||
//# sourceMappingURL=constants.js.map |
@@ -9,2 +9,3 @@ import { Handler } from "aws-lambda"; | ||
export declare const captureLambdaPayloadEnvVar = "DD_CAPTURE_LAMBDA_PAYLOAD"; | ||
export declare const traceManagedServicesEnvVar = "DD_TRACE_MANAGED_SERVICES"; | ||
export declare const siteURLEnvVar = "DD_SITE"; | ||
@@ -11,0 +12,0 @@ export declare const logLevelEnvVar = "DD_LOG_LEVEL"; |
@@ -75,6 +75,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getEnvValue = exports.getTraceHeaders = exports.sendDistributionMetric = exports.sendDistributionMetricWithDate = exports.datadog = exports.defaultConfig = exports.defaultSiteURL = exports.traceExtractorEnvVar = exports.mergeXrayTracesEnvVar = exports.lambdaTaskRootEnvVar = exports.datadogHandlerEnvVar = exports.enhancedMetricsEnvVar = exports.logInjectionEnvVar = exports.logForwardingEnvVar = exports.logLevelEnvVar = exports.siteURLEnvVar = exports.captureLambdaPayloadEnvVar = exports.apiKeyKMSEnvVar = exports.apiKeyEnvVar = void 0; | ||
exports.getEnvValue = exports.getTraceHeaders = exports.sendDistributionMetric = exports.sendDistributionMetricWithDate = exports.datadog = exports.defaultConfig = exports.defaultSiteURL = exports.traceExtractorEnvVar = exports.mergeXrayTracesEnvVar = exports.lambdaTaskRootEnvVar = exports.datadogHandlerEnvVar = exports.enhancedMetricsEnvVar = exports.logInjectionEnvVar = exports.logForwardingEnvVar = exports.logLevelEnvVar = exports.siteURLEnvVar = exports.traceManagedServicesEnvVar = exports.captureLambdaPayloadEnvVar = exports.apiKeyKMSEnvVar = exports.apiKeyEnvVar = void 0; | ||
var metrics_1 = require("./metrics"); | ||
var trace_1 = require("./trace"); | ||
var trigger_1 = require("./trace/trigger"); | ||
var utils_1 = require("./utils"); | ||
@@ -84,2 +83,3 @@ exports.apiKeyEnvVar = "DD_API_KEY"; | ||
exports.captureLambdaPayloadEnvVar = "DD_CAPTURE_LAMBDA_PAYLOAD"; | ||
exports.traceManagedServicesEnvVar = "DD_TRACE_MANAGED_SERVICES"; | ||
exports.siteURLEnvVar = "DD_SITE"; | ||
@@ -100,2 +100,3 @@ exports.logLevelEnvVar = "DD_LOG_LEVEL"; | ||
captureLambdaPayload: false, | ||
createInferredSpan: true, | ||
debugLogging: false, | ||
@@ -128,4 +129,3 @@ enhancedMetrics: true, | ||
var metricsListener = new metrics_1.MetricsListener(new metrics_1.KMSService(), finalConfig); | ||
var handlerName = getEnvValue(exports.datadogHandlerEnvVar, getEnvValue("_HANDLER", "handler")); | ||
var traceListener = new trace_1.TraceListener(finalConfig, handlerName); | ||
var traceListener = new trace_1.TraceListener(finalConfig); | ||
// Only wrap the handler once unless forced | ||
@@ -175,3 +175,2 @@ var _ddWrappedKey = "_ddWrapped"; | ||
return [4 /*yield*/, traceListener.onWrap(function (localEvent, localContext) { return __awaiter(_this, void 0, void 0, function () { | ||
var statusCode; | ||
return __generator(this, function (_a) { | ||
@@ -186,16 +185,3 @@ switch (_a.label) { | ||
case 2: | ||
if (traceListener.currentSpan && finalConfig.captureLambdaPayload) { | ||
(0, utils_1.tagObject)(traceListener.currentSpan, "function.request", localEvent); | ||
(0, utils_1.tagObject)(traceListener.currentSpan, "function.response", localResult); | ||
} | ||
if (traceListener.triggerTags) { | ||
statusCode = (0, trigger_1.extractHTTPStatusCodeTag)(traceListener.triggerTags, localResult); | ||
if (statusCode) { | ||
// Store the status tag in the listener to send to Xray on invocation completion | ||
traceListener.triggerTags["http.status_code"] = statusCode; | ||
if (traceListener.currentSpan) { | ||
traceListener.currentSpan.setTag("http.status_code", statusCode); | ||
} | ||
} | ||
} | ||
traceListener.onEndingInvocation(localEvent, localResult, finalConfig.captureLambdaPayload); | ||
return [7 /*endfinally*/]; | ||
@@ -335,5 +321,9 @@ case 3: return [2 /*return*/, localResult]; | ||
if (userConfig === undefined || userConfig.captureLambdaPayload === undefined) { | ||
var result = getEnvValue(exports.captureLambdaPayloadEnvVar, "false").toLocaleLowerCase(); | ||
var result = getEnvValue(exports.captureLambdaPayloadEnvVar, "false").toLowerCase(); | ||
config.captureLambdaPayload = result === "true"; | ||
} | ||
if (userConfig === undefined || userConfig.createInferredSpan === undefined) { | ||
var result = getEnvValue(exports.traceManagedServicesEnvVar, "true").toLowerCase(); | ||
config.createInferredSpan = result === "true"; | ||
} | ||
return config; | ||
@@ -340,0 +330,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Context, SQSEvent } from "aws-lambda"; | ||
import { Context, EventBridgeEvent, KinesisStreamEvent, SNSEvent, SQSEvent } from "aws-lambda"; | ||
import { SampleMode, Source } from "./constants"; | ||
@@ -37,2 +37,6 @@ import { TraceExtractor } from "./listener"; | ||
export declare function readTraceFromSQSEvent(event: SQSEvent): TraceContext | undefined; | ||
export declare function readTraceFromSNSSQSEvent(event: SQSEvent): TraceContext | undefined; | ||
export declare function readTraceFromKinesisEvent(event: KinesisStreamEvent): TraceContext | undefined; | ||
export declare function readTraceFromEventbridgeEvent(event: EventBridgeEvent<any, any>): TraceContext | undefined; | ||
export declare function readTraceFromSNSEvent(event: SNSEvent): TraceContext | undefined; | ||
export declare function readTraceFromLambdaContext(context: any): TraceContext | undefined; | ||
@@ -42,9 +46,3 @@ export declare function readTraceFromHTTPEvent(event: any): TraceContext | undefined; | ||
export declare function readTraceContextFromXray(): TraceContext | undefined; | ||
export declare function parseXrayTraceContextHeader(header: string): { | ||
xrayTraceID: string; | ||
xraySampled: string; | ||
xrayParentID: string; | ||
} | undefined; | ||
export declare function readStepFunctionContextFromEvent(event: any): StepFunctionContext | undefined; | ||
export declare function convertTraceContext(traceHeader: XRayTraceHeader): TraceContext | undefined; | ||
export declare function convertToSampleMode(xraySampled: number): SampleMode; | ||
@@ -51,0 +49,0 @@ export declare function convertToAPMTraceID(xrayTraceID: string): string | undefined; |
@@ -30,3 +30,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertToAPMParentID = exports.convertToAPMTraceID = exports.convertToSampleMode = exports.convertTraceContext = exports.readStepFunctionContextFromEvent = exports.parseXrayTraceContextHeader = exports.readTraceContextFromXray = exports.readTraceFromEvent = exports.readTraceFromHTTPEvent = exports.readTraceFromLambdaContext = exports.readTraceFromSQSEvent = exports.readTraceFromAppSyncEvent = exports.sendXraySubsegment = exports.generateXraySubsegment = exports.addXrayMetadata = exports.addLambdaFunctionTagsToXray = exports.addStepFunctionContextToXray = exports.addTraceContextToXray = exports.extractTraceContext = void 0; | ||
exports.convertToAPMParentID = exports.convertToAPMTraceID = exports.convertToSampleMode = exports.readStepFunctionContextFromEvent = exports.readTraceContextFromXray = exports.readTraceFromEvent = exports.readTraceFromHTTPEvent = exports.readTraceFromLambdaContext = exports.readTraceFromSNSEvent = exports.readTraceFromEventbridgeEvent = exports.readTraceFromKinesisEvent = exports.readTraceFromSNSSQSEvent = exports.readTraceFromSQSEvent = exports.readTraceFromAppSyncEvent = exports.sendXraySubsegment = exports.generateXraySubsegment = exports.addXrayMetadata = exports.addLambdaFunctionTagsToXray = exports.addStepFunctionContextToXray = exports.addTraceContextToXray = exports.extractTraceContext = void 0; | ||
var bignumber_js_1 = require("bignumber.js"); | ||
@@ -185,5 +185,4 @@ var crypto_1 = require("crypto"); | ||
function readTraceFromSQSEvent(event) { | ||
if (event.Records[0].messageAttributes && | ||
event.Records[0].messageAttributes._datadog && | ||
event.Records[0].messageAttributes._datadog.stringValue) { | ||
var _a, _b, _c, _d; | ||
if ((_d = (_c = (_b = (_a = event.Records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.messageAttributes) === null || _c === void 0 ? void 0 : _c._datadog) === null || _d === void 0 ? void 0 : _d.stringValue) { | ||
var traceHeaders = event.Records[0].messageAttributes._datadog.stringValue; | ||
@@ -193,11 +192,134 @@ try { | ||
var traceID = traceData[constants_1.traceIDHeader]; | ||
if (typeof traceID !== "string") { | ||
var parentID = traceData[constants_1.parentIDHeader]; | ||
var sampledHeader = traceData[constants_1.samplingPriorityHeader]; | ||
if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") { | ||
return; | ||
} | ||
var sampleMode = parseInt(sampledHeader, 10); | ||
var trace = { | ||
parentID: parentID, | ||
sampleMode: sampleMode, | ||
source: constants_1.Source.Event, | ||
traceID: traceID, | ||
}; | ||
(0, utils_1.logDebug)("extracted trace context from sqs event", { trace: trace, event: event }); | ||
return trace; | ||
} | ||
catch (err) { | ||
if (err instanceof Error) { | ||
(0, utils_1.logError)("Error parsing SQS message trace data", err); | ||
} | ||
return; | ||
} | ||
} | ||
return; | ||
} | ||
exports.readTraceFromSQSEvent = readTraceFromSQSEvent; | ||
function readTraceFromSNSSQSEvent(event) { | ||
var _a, _b; | ||
if ((_b = (_a = event === null || event === void 0 ? void 0 : event.Records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.body) { | ||
try { | ||
var parsedBody = JSON.parse(event.Records[0].body); | ||
if (parsedBody.MessageAttributes && | ||
parsedBody.MessageAttributes._datadog && | ||
parsedBody.MessageAttributes._datadog.Value) { | ||
var traceData = JSON.parse(parsedBody.MessageAttributes._datadog.Value); | ||
var traceID = traceData[constants_1.traceIDHeader]; | ||
var parentID = traceData[constants_1.parentIDHeader]; | ||
var sampledHeader = traceData[constants_1.samplingPriorityHeader]; | ||
if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") { | ||
return; | ||
} | ||
var sampleMode = parseInt(sampledHeader, 10); | ||
var trace = { | ||
parentID: parentID, | ||
sampleMode: sampleMode, | ||
source: constants_1.Source.Event, | ||
traceID: traceID, | ||
}; | ||
(0, utils_1.logDebug)("extracted trace context from SNS SQS event", { trace: trace, event: event }); | ||
return trace; | ||
} | ||
} | ||
catch (err) { | ||
if (err instanceof Error) { | ||
(0, utils_1.logError)("Error parsing SNS SQS message trace data", err); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
exports.readTraceFromSNSSQSEvent = readTraceFromSNSSQSEvent; | ||
function readTraceFromKinesisEvent(event) { | ||
var _a, _b, _c; | ||
if ((_c = (_b = (_a = event === null || event === void 0 ? void 0 : event.Records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.kinesis) === null || _c === void 0 ? void 0 : _c.data) { | ||
try { | ||
var parsedBody = JSON.parse(Buffer.from(event.Records[0].kinesis.data, "base64").toString("ascii")); | ||
if (parsedBody && parsedBody._datadog) { | ||
var traceData = parsedBody._datadog; | ||
var traceID = traceData[constants_1.traceIDHeader]; | ||
var parentID = traceData[constants_1.parentIDHeader]; | ||
var sampledHeader = traceData[constants_1.samplingPriorityHeader]; | ||
if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") { | ||
return; | ||
} | ||
var sampleMode = parseInt(sampledHeader, 10); | ||
var trace = { | ||
parentID: parentID, | ||
sampleMode: sampleMode, | ||
source: constants_1.Source.Event, | ||
traceID: traceID, | ||
}; | ||
(0, utils_1.logDebug)("extracted trace context from Kinesis event", { trace: trace }); | ||
return trace; | ||
} | ||
} | ||
catch (err) { | ||
if (err instanceof Error) { | ||
(0, utils_1.logError)("Error parsing Kinesis message trace data", err); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
exports.readTraceFromKinesisEvent = readTraceFromKinesisEvent; | ||
function readTraceFromEventbridgeEvent(event) { | ||
var _a; | ||
if ((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a._datadog) { | ||
try { | ||
var traceData = event.detail._datadog; | ||
var traceID = traceData[constants_1.traceIDHeader]; | ||
var parentID = traceData[constants_1.parentIDHeader]; | ||
if (typeof parentID !== "string") { | ||
var sampledHeader = traceData[constants_1.samplingPriorityHeader]; | ||
if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") { | ||
return; | ||
} | ||
var sampleMode = parseInt(sampledHeader, 10); | ||
var trace = { | ||
parentID: parentID, | ||
sampleMode: sampleMode, | ||
source: constants_1.Source.Event, | ||
traceID: traceID, | ||
}; | ||
(0, utils_1.logDebug)("extracted trace context from Eventbridge event", { trace: trace, event: event }); | ||
return trace; | ||
} | ||
catch (err) { | ||
if (err instanceof Error) { | ||
(0, utils_1.logError)("Error parsing Eventbridge trace data", err); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
exports.readTraceFromEventbridgeEvent = readTraceFromEventbridgeEvent; | ||
function readTraceFromSNSEvent(event) { | ||
var _a, _b, _c, _d; | ||
if ((_d = (_c = (_b = (_a = event === null || event === void 0 ? void 0 : event.Records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.Sns) === null || _c === void 0 ? void 0 : _c.MessageAttributes) === null || _d === void 0 ? void 0 : _d._datadog.Value) { | ||
try { | ||
var traceData = JSON.parse(event.Records[0].Sns.MessageAttributes._datadog.Value); | ||
var traceID = traceData[constants_1.traceIDHeader]; | ||
var parentID = traceData[constants_1.parentIDHeader]; | ||
var sampledHeader = traceData[constants_1.samplingPriorityHeader]; | ||
if (typeof sampledHeader !== "string") { | ||
if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") { | ||
return; | ||
@@ -212,3 +334,3 @@ } | ||
}; | ||
(0, utils_1.logDebug)("extracted trace context from sqs event", { trace: trace, event: event }); | ||
(0, utils_1.logDebug)("extracted trace context from SNS event", { trace: trace, event: event }); | ||
return trace; | ||
@@ -218,3 +340,3 @@ } | ||
if (err instanceof Error) { | ||
(0, utils_1.logError)("Error parsing SQS message trace data", err); | ||
(0, utils_1.logError)("Error parsing SNS SQS message trace data", err); | ||
} | ||
@@ -224,5 +346,4 @@ return; | ||
} | ||
return; | ||
} | ||
exports.readTraceFromSQSEvent = readTraceFromSQSEvent; | ||
exports.readTraceFromSNSEvent = readTraceFromSNSEvent; | ||
function readTraceFromLambdaContext(context) { | ||
@@ -323,2 +444,8 @@ var _a; | ||
} | ||
if ((0, event_type_guards_1.isSNSEvent)(event)) { | ||
return readTraceFromSNSEvent(event); | ||
} | ||
if ((0, event_type_guards_1.isSNSSQSEvent)(event)) { | ||
return readTraceFromSNSSQSEvent(event); | ||
} | ||
if ((0, event_type_guards_1.isAppSyncResolverEvent)(event)) { | ||
@@ -330,2 +457,8 @@ return readTraceFromAppSyncEvent(event); | ||
} | ||
if ((0, event_type_guards_1.isKinesisStreamEvent)(event)) { | ||
return readTraceFromKinesisEvent(event); | ||
} | ||
if ((0, event_type_guards_1.isEventBridgeEvent)(event)) { | ||
return readTraceFromEventbridgeEvent(event); | ||
} | ||
return; | ||
@@ -374,11 +507,5 @@ } | ||
var _b = __read(root.split("="), 2), xrayTraceID = _b[1]; | ||
if (xrayTraceID === undefined) { | ||
return; | ||
} | ||
var _c = __read(parent.split("="), 2), xrayParentID = _c[1]; | ||
if (xrayParentID === undefined) { | ||
return; | ||
} | ||
var _d = __read(sampled.split("="), 2), xraySampled = _d[1]; | ||
if (xraySampled === undefined) { | ||
if (xraySampled === undefined || xrayParentID === undefined || xrayTraceID === undefined) { | ||
return; | ||
@@ -392,3 +519,2 @@ } | ||
} | ||
exports.parseXrayTraceContextHeader = parseXrayTraceContextHeader; | ||
function readStepFunctionContextFromEvent(event) { | ||
@@ -443,17 +569,2 @@ if (typeof event !== "object") { | ||
exports.readStepFunctionContextFromEvent = readStepFunctionContextFromEvent; | ||
function convertTraceContext(traceHeader) { | ||
var sampleMode = convertToSampleMode(traceHeader.sampled); | ||
var traceID = convertToAPMTraceID(traceHeader.traceID); | ||
var parentID = convertToAPMParentID(traceHeader.parentID); | ||
if (traceID === undefined || parentID === undefined) { | ||
return; | ||
} | ||
return { | ||
parentID: parentID, | ||
sampleMode: sampleMode, | ||
source: constants_1.Source.Xray, | ||
traceID: traceID, | ||
}; | ||
} | ||
exports.convertTraceContext = convertTraceContext; | ||
function convertToSampleMode(xraySampled) { | ||
@@ -460,0 +571,0 @@ return xraySampled === 1 ? constants_1.SampleMode.USER_KEEP : constants_1.SampleMode.USER_REJECT; |
@@ -15,2 +15,6 @@ import { Context } from "aws-lambda"; | ||
/** | ||
* Whether to create inferred spans for managed services | ||
*/ | ||
createInferredSpan: boolean; | ||
/** | ||
* Whether to automatically patch console.log with Datadog's tracing ids. | ||
@@ -31,3 +35,2 @@ */ | ||
private config; | ||
private handlerName; | ||
private contextService; | ||
@@ -37,9 +40,21 @@ private context?; | ||
private tracerWrapper; | ||
triggerTags?: { | ||
[key: string]: string; | ||
}; | ||
private inferrer; | ||
private inferredSpan?; | ||
private wrappedCurrentSpan?; | ||
private triggerTags?; | ||
private lambdaSpanParentContext?; | ||
get currentTraceHeaders(): Partial<import("./trace-context-service").TraceHeaders>; | ||
get currentSpan(): any; | ||
constructor(config: TraceConfig, handlerName: string); | ||
constructor(config: TraceConfig); | ||
onStartInvocation(event: any, context: Context): void; | ||
/** | ||
* onEndingInvocation runs after the user function has returned | ||
* but before the wrapped function has returned | ||
* this is needed to apply tags to the lambda span | ||
* before it is flushed to logs or extension | ||
* | ||
* @param event | ||
* @param result | ||
* @param shouldTagPayload | ||
*/ | ||
onEndingInvocation(event: any, result: any, shouldTagPayload?: boolean): void; | ||
onCompleteInvocation(): Promise<void>; | ||
@@ -46,0 +61,0 @@ onWrap<T = (...args: any[]) => any>(func: T): T; |
@@ -61,8 +61,10 @@ "use strict"; | ||
var tracer_wrapper_1 = require("./tracer-wrapper"); | ||
var span_inferrer_1 = require("./span-inferrer"); | ||
var span_wrapper_1 = require("./span-wrapper"); | ||
var TraceListener = /** @class */ (function () { | ||
function TraceListener(config, handlerName) { | ||
function TraceListener(config) { | ||
this.config = config; | ||
this.handlerName = handlerName; | ||
this.tracerWrapper = new tracer_wrapper_1.TracerWrapper(); | ||
this.contextService = new trace_context_service_1.TraceContextService(this.tracerWrapper); | ||
this.inferrer = new span_inferrer_1.SpanInferrer(this.tracerWrapper); | ||
} | ||
@@ -76,10 +78,4 @@ Object.defineProperty(TraceListener.prototype, "currentTraceHeaders", { | ||
}); | ||
Object.defineProperty(TraceListener.prototype, "currentSpan", { | ||
get: function () { | ||
return this.tracerWrapper.currentSpan; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
TraceListener.prototype.onStartInvocation = function (event, context) { | ||
var _a, _b; | ||
var tracerInitialized = this.tracerWrapper.isTracerAvailable; | ||
@@ -101,11 +97,62 @@ if (this.config.injectLogContext) { | ||
} | ||
var rootTraceHeaders = this.contextService.extractHeadersFromContext(event, context, this.config.traceExtractor); | ||
// The aws.lambda span needs to have a parented to the Datadog trace context from the | ||
// incoming event if available or the X-Ray trace context if hybrid tracing is enabled | ||
var parentSpanContext; | ||
if (this.contextService.traceSource === constants_2.Source.Event || this.config.mergeDatadogXrayTraces) { | ||
parentSpanContext = rootTraceHeaders ? (_a = this.tracerWrapper.extract(rootTraceHeaders)) !== null && _a !== void 0 ? _a : undefined : undefined; | ||
(0, utils_1.logDebug)("Attempting to find parent for the aws.lambda span"); | ||
} | ||
else { | ||
(0, utils_1.logDebug)("Didn't attempt to find parent for aws.lambda span", { | ||
mergeDatadogXrayTraces: this.config.mergeDatadogXrayTraces, | ||
traceSource: this.contextService.traceSource, | ||
}); | ||
} | ||
if (this.config.createInferredSpan) { | ||
this.inferredSpan = this.inferrer.createInferredSpan(event, context, parentSpanContext); | ||
} | ||
this.lambdaSpanParentContext = ((_b = this.inferredSpan) === null || _b === void 0 ? void 0 : _b.span) || parentSpanContext; | ||
this.context = context; | ||
this.triggerTags = (0, trigger_1.extractTriggerTags)(event, context); | ||
this.contextService.rootTraceContext = (0, context_1.extractTraceContext)(event, context, this.config.traceExtractor); | ||
this.stepFunctionContext = (0, context_1.readStepFunctionContextFromEvent)(event); | ||
}; | ||
/** | ||
* onEndingInvocation runs after the user function has returned | ||
* but before the wrapped function has returned | ||
* this is needed to apply tags to the lambda span | ||
* before it is flushed to logs or extension | ||
* | ||
* @param event | ||
* @param result | ||
* @param shouldTagPayload | ||
*/ | ||
TraceListener.prototype.onEndingInvocation = function (event, result, shouldTagPayload) { | ||
if (shouldTagPayload === void 0) { shouldTagPayload = false; } | ||
// Guard clause if something has gone horribly wrong | ||
// so we won't crash user code. | ||
if (!this.tracerWrapper.currentSpan) | ||
return; | ||
this.wrappedCurrentSpan = new span_wrapper_1.SpanWrapper(this.tracerWrapper.currentSpan, {}); | ||
if (shouldTagPayload) { | ||
(0, utils_1.tagObject)(this.tracerWrapper.currentSpan, "function.request", event); | ||
(0, utils_1.tagObject)(this.tracerWrapper.currentSpan, "function.response", result); | ||
} | ||
if (this.triggerTags) { | ||
var statusCode = (0, trigger_1.extractHTTPStatusCodeTag)(this.triggerTags, result); | ||
// Store the status tag in the listener to send to Xray on invocation completion | ||
this.triggerTags["http.status_code"] = statusCode; | ||
if (this.tracerWrapper.currentSpan) { | ||
this.tracerWrapper.currentSpan.setTag("http.status_code", statusCode); | ||
} | ||
if (this.inferredSpan) { | ||
this.inferredSpan.setTag("http.status_code", statusCode); | ||
} | ||
} | ||
}; | ||
TraceListener.prototype.onCompleteInvocation = function () { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var tracerInitialized; | ||
return __generator(this, function (_a) { | ||
var tracerInitialized, finishTime; | ||
return __generator(this, function (_b) { | ||
// Create a new dummy Datadog subsegment for function trigger tags so we | ||
@@ -121,2 +168,7 @@ // can attach them to X-Ray spans when hybrid tracing is used | ||
} | ||
if (this.inferredSpan) { | ||
(0, utils_1.logDebug)("Finishing inferred span"); | ||
finishTime = this.inferredSpan.isAsync() ? (_a = this.wrappedCurrentSpan) === null || _a === void 0 ? void 0 : _a.startTime() : Date.now(); | ||
this.inferredSpan.finish(finishTime); | ||
} | ||
return [2 /*return*/]; | ||
@@ -128,16 +180,2 @@ }); | ||
var _a, _b, _c; | ||
// The aws.lambda span needs to have a parented to the Datadog trace context from the | ||
// incoming event if available or the X-Ray trace context if hybrid tracing is enabled | ||
var parentSpanContext = null; | ||
if (this.contextService.traceSource === constants_2.Source.Event || this.config.mergeDatadogXrayTraces) { | ||
var rootTraceHeaders = this.contextService.rootTraceHeaders; | ||
parentSpanContext = this.tracerWrapper.extract(rootTraceHeaders); | ||
(0, utils_1.logDebug)("Attempting to find parent for the aws.lambda span"); | ||
} | ||
else { | ||
(0, utils_1.logDebug)("Didn't attempt to find parent for aws.lambda span", { | ||
mergeDatadogXrayTraces: this.config.mergeDatadogXrayTraces, | ||
traceSource: this.contextService.traceSource, | ||
}); | ||
} | ||
var options = {}; | ||
@@ -170,4 +208,4 @@ if (this.context) { | ||
} | ||
if (parentSpanContext !== null) { | ||
options.childOf = parentSpanContext; | ||
if (this.lambdaSpanParentContext) { | ||
options.childOf = this.lambdaSpanParentContext; | ||
} | ||
@@ -174,0 +212,0 @@ options.type = "serverless"; |
@@ -8,3 +8,3 @@ import { Context, SNSMessage } from "aws-lambda"; | ||
createInferredSpan(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): any; | ||
createInferredSpanForLambdaUrl(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): SpanWrapper; | ||
isApiGatewayAsync(event: any): boolean; | ||
createInferredSpanForApiGateway(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): SpanWrapper; | ||
@@ -17,3 +17,4 @@ createInferredSpanForDynamoDBStreamEvent(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): SpanWrapper; | ||
createInferredSpanForS3(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): SpanWrapper; | ||
createInferredSpanForEventBridge(event: any, context: Context | undefined, parentSpanContext: SpanContext | undefined): SpanWrapper; | ||
} | ||
//# sourceMappingURL=span-inferrer.d.ts.map |
@@ -18,5 +18,2 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -26,3 +23,2 @@ exports.SpanInferrer = void 0; | ||
var span_wrapper_1 = require("./span-wrapper"); | ||
var util_1 = __importDefault(require("util")); | ||
var SpanInferrer = /** @class */ (function () { | ||
@@ -34,6 +30,2 @@ function SpanInferrer(traceWrapper) { | ||
var eventSource = (0, trigger_1.parseEventSource)(event); | ||
console.log("PARENT CONTEXT: " + util_1.default.inspect(parentSpanContext)); | ||
if (eventSource === trigger_1.eventSources.lambdaUrl) { | ||
return this.createInferredSpanForLambdaUrl(event, context, parentSpanContext); | ||
} | ||
if (eventSource === trigger_1.eventSources.apiGateway) { | ||
@@ -54,44 +46,28 @@ return this.createInferredSpanForApiGateway(event, context, parentSpanContext); | ||
} | ||
if (eventSource === trigger_1.eventSources.s3) { | ||
return this.createInferredSpanForS3(event, context, parentSpanContext); | ||
} | ||
if (eventSource === trigger_1.eventSources.eventBridge) { | ||
return this.createInferredSpanForEventBridge(event, context, parentSpanContext); | ||
} | ||
}; | ||
SpanInferrer.prototype.createInferredSpanForLambdaUrl = function (event, context, parentSpanContext) { | ||
var options = {}; | ||
var domain = event.requestContext.domainName; | ||
var path = event.rawPath; | ||
var method = event.requestContext.http.method; | ||
var resourceName = [method, path].join(" "); | ||
options.tags = { | ||
operation_name: "aws.lambda.url", | ||
"http.url": domain + path, | ||
endpoint: path, | ||
"http.method": method, | ||
resource_names: resourceName, | ||
request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
"span.type": "http", | ||
"resource.name": resourceName, | ||
"service.name": domain, | ||
service: domain, | ||
_inferred_span: { | ||
tag_source: "self", | ||
synchronicity: "sync", | ||
}, | ||
}; | ||
options.childOf = parentSpanContext; | ||
options.startTime = event.requestContext.timeEpoch; | ||
var spanWrapperOptions = { | ||
isAsync: false, | ||
}; | ||
return new span_wrapper_1.SpanWrapper(this.traceWrapper.startSpan("aws.lambda.url", options), spanWrapperOptions); | ||
SpanInferrer.prototype.isApiGatewayAsync = function (event) { | ||
return (event.headers && event.headers["X-Amz-Invocation-Type"] && event.headers["X-Amz-Invocation-Type"] === "Event"); | ||
}; | ||
SpanInferrer.prototype.createInferredSpanForApiGateway = function (event, context, parentSpanContext) { | ||
console.log("INTERNAL EVENT: " + JSON.stringify(event)); | ||
var options = {}; | ||
var domain = event.requestContext.domainName; | ||
var path = event.rawPath; | ||
var method = event.requestContext.httpMethod || event.requestContext.http.method; // httpMethod for v1, http.method for v2 | ||
var resourceName = [method, path].join(" "); | ||
var path = event.rawPath || event.requestContext.routeKey; | ||
var method; | ||
if (event.requestContext.httpMethod) { | ||
method = event.requestContext.httpMethod; | ||
} | ||
else if (event.requestContext.http) { | ||
method = event.requestContext.http.method; | ||
} | ||
var resourceName = [domain, path].join(" "); | ||
options.tags = { | ||
operation_name: "aws.api_gateway", | ||
operation_name: "aws.apigateway", | ||
"http.url": domain + path, | ||
endpoint: path, | ||
"http.method": method, | ||
resource_names: resourceName, | ||
@@ -102,14 +78,29 @@ request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
"service.name": domain, | ||
apiid: event.requestContext.apiId, | ||
service: domain, | ||
_inferred_span: { | ||
tag_source: "self", | ||
synchronicity: "sync", | ||
synchronicity: this.isApiGatewayAsync(event), | ||
}, | ||
}; | ||
options.childOf = parentSpanContext; | ||
// Set APIGW v1 or v1 metadata | ||
if (method) { | ||
options.tags["http.method"] = method; | ||
options.tags.stage = event.requestContext.stage; | ||
options.tags.domain_name = domain; | ||
} | ||
// Set websocket metadata | ||
if (event.requestContext.messageDirection) { | ||
options.tags.message_direction = event.requestContext.messageDirection; | ||
options.tags.connection_id = event.requestContext.connectionId; | ||
options.tags.event_type = event.requestContext.eventType; | ||
} | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = event.requestContext.timeEpoch; | ||
var spanWrapperOptions = { | ||
isAsync: false, | ||
isAsync: this.isApiGatewayAsync(event), | ||
}; | ||
return new span_wrapper_1.SpanWrapper(this.traceWrapper.startSpan("aws.api_gateway", options), spanWrapperOptions); | ||
return new span_wrapper_1.SpanWrapper(this.traceWrapper.startSpan("aws.apigateway", options), spanWrapperOptions); | ||
}; | ||
@@ -121,3 +112,3 @@ SpanInferrer.prototype.createInferredSpanForDynamoDBStreamEvent = function (event, context, parentSpanContext) { | ||
var referenceRecord = Records[0]; | ||
var eventSourceARN = referenceRecord.eventSourceARN, eventName = referenceRecord.eventName; | ||
var eventSourceARN = referenceRecord.eventSourceARN, eventName = referenceRecord.eventName, eventVersion = referenceRecord.eventVersion, eventID = referenceRecord.eventID, dynamodb = referenceRecord.dynamodb; | ||
var _b = __read((eventSourceARN === null || eventSourceARN === void 0 ? void 0 : eventSourceARN.split("/")) || [undefined, undefined], 2), tableArn = _b[0], tableName = _b[1]; | ||
@@ -127,3 +118,3 @@ var resourceName = eventName + " " + tableName; | ||
operation_name: "aws.dynamodb", | ||
"aws.dynamodb.table_name": tableName, | ||
tablename: tableName, | ||
resource_names: resourceName, | ||
@@ -138,4 +129,14 @@ request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
}, | ||
event_name: eventName, | ||
event_version: eventVersion, | ||
event_source_arn: eventSourceARN, | ||
event_id: eventID, | ||
}; | ||
options.childOf = parentSpanContext; | ||
if (dynamodb) { | ||
options.tags.stream_view_type = dynamodb.StreamViewType; | ||
options.tags.size_bytes = dynamodb.SizeBytes; | ||
} | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Number((_a = referenceRecord.dynamodb) === null || _a === void 0 ? void 0 : _a.ApproximateCreationDateTime) * 1000; | ||
@@ -151,3 +152,3 @@ var spanWrapperOptions = { | ||
var referenceRecord = Records[0]; | ||
var _a = referenceRecord.Sns, TopicArn = _a.TopicArn, Timestamp = _a.Timestamp; | ||
var EventSubscriptionArn = referenceRecord.EventSubscriptionArn, _a = referenceRecord.Sns, TopicArn = _a.TopicArn, Timestamp = _a.Timestamp, Type = _a.Type, Subject = _a.Subject, MessageId = _a.MessageId; | ||
var topicName = TopicArn === null || TopicArn === void 0 ? void 0 : TopicArn.split(":").pop(); | ||
@@ -159,3 +160,3 @@ var resourceName = topicName; | ||
request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
"span.type": "web", | ||
"span.type": "sns", | ||
"resource.name": resourceName, | ||
@@ -167,4 +168,12 @@ service: "sns", | ||
}, | ||
type: Type, | ||
subject: Subject, | ||
message_id: MessageId, | ||
topicname: topicName, | ||
topic_arn: TopicArn, | ||
event_subscription_arn: EventSubscriptionArn, | ||
}; | ||
options.childOf = parentSpanContext; | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Date.parse(Timestamp); | ||
@@ -178,3 +187,3 @@ var spanWrapperOptions = { | ||
var options = {}; | ||
var TopicArn = event.TopicArn, Timestamp = event.Timestamp; | ||
var TopicArn = event.TopicArn, Timestamp = event.Timestamp, Type = event.Type, Subject = event.Subject, MessageId = event.MessageId; | ||
var topicName = TopicArn === null || TopicArn === void 0 ? void 0 : TopicArn.split(":").pop(); | ||
@@ -185,4 +194,3 @@ var resourceName = topicName; | ||
resource_names: resourceName, | ||
request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
"span.type": "web", | ||
"span.type": "sns", | ||
"resource.name": resourceName, | ||
@@ -194,4 +202,11 @@ service: "sns", | ||
}, | ||
type: Type, | ||
subject: Subject, | ||
message_id: MessageId, | ||
topicname: topicName, | ||
topic_arn: TopicArn, | ||
}; | ||
options.childOf = parentSpanContext; | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Date.parse(Timestamp); | ||
@@ -207,3 +222,3 @@ var spanWrapperOptions = { | ||
var referenceRecord = Records[0]; | ||
var _a = referenceRecord.attributes, SentTimestamp = _a.SentTimestamp, ApproximateReceiveCount = _a.ApproximateReceiveCount, eventSourceARN = referenceRecord.eventSourceARN, body = referenceRecord.body; | ||
var _a = referenceRecord.attributes, SentTimestamp = _a.SentTimestamp, ApproximateReceiveCount = _a.ApproximateReceiveCount, SenderId = _a.SenderId, eventSourceARN = referenceRecord.eventSourceARN, receiptHandle = referenceRecord.receiptHandle, body = referenceRecord.body; | ||
var queueName = eventSourceARN === null || eventSourceARN === void 0 ? void 0 : eventSourceARN.split(":").pop(); | ||
@@ -223,2 +238,6 @@ var resourceName = queueName; | ||
}, | ||
queuename: queueName, | ||
event_source_arn: eventSourceARN, | ||
receipt_handle: receiptHandle, | ||
sender_id: SenderId, | ||
}; | ||
@@ -254,4 +273,5 @@ if (ApproximateReceiveCount && Number(ApproximateReceiveCount) > 0) { | ||
var referenceRecord = Records[0]; | ||
var approximateArrivalTimestamp = referenceRecord.kinesis.approximateArrivalTimestamp, eventSourceARN = referenceRecord.eventSourceARN; | ||
var _a = referenceRecord.kinesis, approximateArrivalTimestamp = _a.approximateArrivalTimestamp, partitionKey = _a.partitionKey, eventSourceARN = referenceRecord.eventSourceARN, eventName = referenceRecord.eventName, eventVersion = referenceRecord.eventVersion, eventID = referenceRecord.eventID; | ||
var streamName = eventSourceARN === null || eventSourceARN === void 0 ? void 0 : eventSourceARN.split(":").pop(); | ||
var shardId = eventID.split(":").pop(); | ||
options.tags = { | ||
@@ -268,5 +288,14 @@ operation_name: "aws.kinesis", | ||
}, | ||
streamname: streamName, | ||
event_id: eventID, | ||
event_name: eventName, | ||
event_source_arn: eventSourceARN, | ||
event_version: eventVersion, | ||
partition_key: partitionKey, | ||
shardid: shardId, | ||
}; | ||
options.childOf = parentSpanContext; | ||
options.startTime = Number(approximateArrivalTimestamp); | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Number(approximateArrivalTimestamp) * 1000; | ||
var spanWrapperOptions = { | ||
@@ -281,3 +310,3 @@ isAsync: true, | ||
var referenceRecord = Records[0]; | ||
var bucketName = referenceRecord.s3.bucket.name, eventTime = referenceRecord.eventTime; | ||
var _a = referenceRecord.s3, _b = _a.bucket, bucketName = _b.name, arn = _b.arn, _c = _a.object, key = _c.key, size = _c.size, eTag = _c.eTag, eventTime = referenceRecord.eventTime, eventName = referenceRecord.eventName; | ||
options.tags = { | ||
@@ -294,4 +323,12 @@ operation_name: "aws.s3", | ||
}, | ||
bucketname: bucketName, | ||
bucket_arn: arn, | ||
event_name: eventName, | ||
object_key: key, | ||
object_size: size, | ||
object_etag: eTag, | ||
}; | ||
options.childOf = parentSpanContext; | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Date.parse(eventTime); | ||
@@ -303,2 +340,26 @@ var spanWrapperOptions = { | ||
}; | ||
SpanInferrer.prototype.createInferredSpanForEventBridge = function (event, context, parentSpanContext) { | ||
var options = {}; | ||
var _a = event, time = _a.time, source = _a.source; | ||
options.tags = { | ||
operation_name: "aws.eventbridge", | ||
resource_names: source, | ||
request_id: context === null || context === void 0 ? void 0 : context.awsRequestId, | ||
"span.type": "web", | ||
"resource.name": source, | ||
service: "eventbridge", | ||
_inferred_span: { | ||
tag_source: "self", | ||
synchronicity: "async", | ||
}, | ||
}; | ||
if (parentSpanContext) { | ||
options.childOf = parentSpanContext; | ||
} | ||
options.startTime = Date.parse(time); | ||
var spanWrapperOptions = { | ||
isAsync: true, | ||
}; | ||
return new span_wrapper_1.SpanWrapper(this.traceWrapper.startSpan("aws.eventbridge", options), spanWrapperOptions); | ||
}; | ||
return SpanInferrer; | ||
@@ -305,0 +366,0 @@ }()); |
@@ -0,4 +1,6 @@ | ||
import { Context } from "aws-lambda"; | ||
import { parentIDHeader, samplingPriorityHeader, traceIDHeader } from "./constants"; | ||
import { TraceContext } from "./context"; | ||
import { TracerWrapper } from "./tracer-wrapper"; | ||
import { TraceExtractor } from "./listener"; | ||
/** | ||
@@ -17,4 +19,5 @@ * Headers that can be added to a request. | ||
private tracerWrapper; | ||
rootTraceContext?: TraceContext; | ||
private rootTraceContext?; | ||
constructor(tracerWrapper: TracerWrapper); | ||
extractHeadersFromContext(event: any, context: Context, extractor?: TraceExtractor): Partial<TraceHeaders> | undefined; | ||
get currentTraceContext(): TraceContext | undefined; | ||
@@ -21,0 +24,0 @@ get currentTraceHeaders(): Partial<TraceHeaders>; |
@@ -17,2 +17,3 @@ "use strict"; | ||
var constants_1 = require("./constants"); | ||
var context_1 = require("./context"); | ||
/** | ||
@@ -25,2 +26,6 @@ * Service for retrieving the latest version of the request context from xray. | ||
} | ||
TraceContextService.prototype.extractHeadersFromContext = function (event, context, extractor) { | ||
this.rootTraceContext = (0, context_1.extractTraceContext)(event, context, extractor); | ||
return this.currentTraceHeaders; | ||
}; | ||
Object.defineProperty(TraceContextService.prototype, "currentTraceContext", { | ||
@@ -27,0 +32,0 @@ get: function () { |
@@ -7,2 +7,11 @@ import { TraceContext } from "./context"; | ||
} | ||
export interface SpanOptions { | ||
childOf?: SpanContext; | ||
tags?: { | ||
[key: string]: any; | ||
}; | ||
startTime?: number; | ||
service?: string; | ||
type?: string; | ||
} | ||
export interface TraceOptions { | ||
@@ -24,4 +33,5 @@ resource?: string; | ||
wrap<T = (...args: any[]) => any>(name: string, options: TraceOptions, fn: T): any; | ||
startSpan<T = (...args: any[]) => any>(name: string, options: TraceOptions): T | null; | ||
traceContext(): TraceContext | undefined; | ||
} | ||
//# sourceMappingURL=tracer-wrapper.d.ts.map |
@@ -78,2 +78,8 @@ "use strict"; | ||
}; | ||
TracerWrapper.prototype.startSpan = function (name, options) { | ||
if (!this.isTracerAvailable) { | ||
return null; | ||
} | ||
return this.tracer.startSpan(name, options); | ||
}; | ||
TracerWrapper.prototype.traceContext = function () { | ||
@@ -80,0 +86,0 @@ if (!this.isTracerAvailable) { |
import { Context } from "aws-lambda"; | ||
export declare enum eventSources { | ||
apiGateway = "api-gateway", | ||
applicationLoadBalancer = "application-load-balancer", | ||
cloudFront = "cloudfront", | ||
cloudWatchEvents = "cloudwatch-events", | ||
cloudWatchLogs = "cloudwatch-logs", | ||
cloudWatch = "cloudwatch", | ||
dynamoDB = "dynamodb", | ||
eventBridge = "eventbridge", | ||
kinesis = "kinesis", | ||
s3 = "s3", | ||
sns = "sns", | ||
sqs = "sqs" | ||
} | ||
/** | ||
@@ -8,3 +22,3 @@ * parseEventSource parses the triggering event to determine the source | ||
*/ | ||
export declare function parseEventSource(event: any): string | undefined; | ||
export declare function parseEventSource(event: any): eventSources.apiGateway | eventSources.applicationLoadBalancer | eventSources.cloudFront | eventSources.cloudWatchEvents | eventSources.cloudWatchLogs | eventSources.dynamoDB | eventSources.eventBridge | eventSources.kinesis | eventSources.s3 | eventSources.sns | eventSources.sqs | undefined; | ||
/** | ||
@@ -27,3 +41,3 @@ * parseEventSourceARN parses the triggering event to determine the event source's | ||
[key: string]: string; | ||
}, result: any): string | undefined; | ||
} | undefined, result: any): string | undefined; | ||
//# sourceMappingURL=trigger.d.ts.map |
@@ -33,3 +33,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.extractHTTPStatusCodeTag = exports.extractTriggerTags = exports.parseEventSourceARN = exports.parseEventSource = void 0; | ||
exports.extractHTTPStatusCodeTag = exports.extractTriggerTags = exports.parseEventSourceARN = exports.parseEventSource = exports.eventSources = void 0; | ||
var eventType = __importStar(require("../utils/event-type-guards")); | ||
@@ -39,3 +39,5 @@ var utils_1 = require("../utils"); | ||
function isHTTPTriggerEvent(eventSource) { | ||
return eventSource === "api-gateway" || eventSource === "application-load-balancer"; | ||
return (eventSource === "api-gateway" || | ||
eventSource === "application-load-balancer" || | ||
eventSource === "lambda-function-url"); | ||
} | ||
@@ -85,2 +87,20 @@ function getAWSPartitionByRegion(region) { | ||
} | ||
function extractEventBridgeARN(event) { | ||
return event.source; | ||
} | ||
var eventSources; | ||
(function (eventSources) { | ||
eventSources["apiGateway"] = "api-gateway"; | ||
eventSources["applicationLoadBalancer"] = "application-load-balancer"; | ||
eventSources["cloudFront"] = "cloudfront"; | ||
eventSources["cloudWatchEvents"] = "cloudwatch-events"; | ||
eventSources["cloudWatchLogs"] = "cloudwatch-logs"; | ||
eventSources["cloudWatch"] = "cloudwatch"; | ||
eventSources["dynamoDB"] = "dynamodb"; | ||
eventSources["eventBridge"] = "eventbridge"; | ||
eventSources["kinesis"] = "kinesis"; | ||
eventSources["s3"] = "s3"; | ||
eventSources["sns"] = "sns"; | ||
eventSources["sqs"] = "sqs"; | ||
})(eventSources = exports.eventSources || (exports.eventSources = {})); | ||
/** | ||
@@ -93,34 +113,37 @@ * parseEventSource parses the triggering event to determine the source | ||
function parseEventSource(event) { | ||
var eventSource; | ||
if (eventType.isAPIGatewayEvent(event) || eventType.isAPIGatewayEventV2(event)) { | ||
eventSource = "api-gateway"; | ||
if (eventType.isAPIGatewayEvent(event) || | ||
eventType.isAPIGatewayEventV2(event) || | ||
eventType.isAPIGatewayWebsocketEvent(event)) { | ||
return eventSources.apiGateway; | ||
} | ||
if (eventType.isALBEvent(event)) { | ||
eventSource = "application-load-balancer"; | ||
return eventSources.applicationLoadBalancer; | ||
} | ||
if (eventType.isCloudWatchLogsEvent(event)) { | ||
eventSource = "cloudwatch-logs"; | ||
return eventSources.cloudWatchLogs; | ||
} | ||
if (eventType.isCloudWatchEvent(event)) { | ||
eventSource = "cloudwatch-events"; | ||
return eventSources.cloudWatchEvents; | ||
} | ||
if (eventType.isCloudFrontRequestEvent(event)) { | ||
eventSource = "cloudfront"; | ||
return eventSources.cloudFront; | ||
} | ||
if (eventType.isDynamoDBStreamEvent(event)) { | ||
eventSource = "dynamodb"; | ||
return eventSources.dynamoDB; | ||
} | ||
if (eventType.isKinesisStreamEvent(event)) { | ||
eventSource = "kinesis"; | ||
return eventSources.kinesis; | ||
} | ||
if (eventType.isS3Event(event)) { | ||
eventSource = "s3"; | ||
return eventSources.s3; | ||
} | ||
if (eventType.isSNSEvent(event)) { | ||
eventSource = "sns"; | ||
return eventSources.sns; | ||
} | ||
if (eventType.isSQSEvent(event)) { | ||
eventSource = "sqs"; | ||
return eventSources.sqs; | ||
} | ||
return eventSource; | ||
if (eventType.isEventBridgeEvent(event)) { | ||
return eventSources.eventBridge; | ||
} | ||
} | ||
@@ -181,2 +204,5 @@ exports.parseEventSource = parseEventSource; | ||
} | ||
if (source === "eventbridge") { | ||
eventSourceARN = extractEventBridgeARN(event); | ||
} | ||
return eventSourceARN; | ||
@@ -183,0 +209,0 @@ } |
@@ -1,4 +0,5 @@ | ||
import { APIGatewayEvent, APIGatewayProxyEventV2, AppSyncResolverEvent, ALBEvent, CloudWatchLogsEvent, ScheduledEvent, CloudFrontRequestEvent, DynamoDBStreamEvent, KinesisStreamEvent, S3Event, SNSEvent, SQSEvent } from "aws-lambda"; | ||
import { APIGatewayEvent, APIGatewayProxyEventV2, AppSyncResolverEvent, ALBEvent, CloudWatchLogsEvent, ScheduledEvent, CloudFrontRequestEvent, DynamoDBStreamEvent, KinesisStreamEvent, S3Event, SNSEvent, SQSEvent, EventBridgeEvent } from "aws-lambda"; | ||
export declare function isAPIGatewayEvent(event: any): event is APIGatewayEvent; | ||
export declare function isAPIGatewayEventV2(event: any): event is APIGatewayProxyEventV2; | ||
export declare function isAPIGatewayWebsocketEvent(event: any): event is any; | ||
export declare function isALBEvent(event: any): event is ALBEvent; | ||
@@ -13,3 +14,5 @@ export declare function isCloudWatchLogsEvent(event: any): event is CloudWatchLogsEvent; | ||
export declare function isSQSEvent(event: any): event is SQSEvent; | ||
export declare function isSNSSQSEvent(event: any): event is SQSEvent; | ||
export declare function isAppSyncResolverEvent(event: any): event is AppSyncResolverEvent<any>; | ||
export declare function isEventBridgeEvent(event: any): event is EventBridgeEvent<any, any>; | ||
//# sourceMappingURL=event-type-guards.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isAppSyncResolverEvent = exports.isSQSEvent = exports.isSNSEvent = exports.isS3Event = exports.isKinesisStreamEvent = exports.isDynamoDBStreamEvent = exports.isCloudFrontRequestEvent = exports.isCloudWatchEvent = exports.isCloudWatchLogsEvent = exports.isALBEvent = exports.isAPIGatewayEventV2 = exports.isAPIGatewayEvent = void 0; | ||
exports.isEventBridgeEvent = exports.isAppSyncResolverEvent = exports.isSNSSQSEvent = exports.isSQSEvent = exports.isSNSEvent = exports.isS3Event = exports.isKinesisStreamEvent = exports.isDynamoDBStreamEvent = exports.isCloudFrontRequestEvent = exports.isCloudWatchEvent = exports.isCloudWatchLogsEvent = exports.isALBEvent = exports.isAPIGatewayWebsocketEvent = exports.isAPIGatewayEventV2 = exports.isAPIGatewayEvent = void 0; | ||
var constants_1 = require("../trace/constants"); | ||
@@ -11,5 +11,12 @@ function isAPIGatewayEvent(event) { | ||
function isAPIGatewayEventV2(event) { | ||
return (event.requestContext !== undefined && event.version === constants_1.apiGatewayEventV2 && event.rawQueryString !== undefined); | ||
return (event.requestContext !== undefined && | ||
event.version === constants_1.apiGatewayEventV2 && | ||
event.rawQueryString !== undefined && | ||
!event.requestContext.domainName.includes("lambda-url")); | ||
} | ||
exports.isAPIGatewayEventV2 = isAPIGatewayEventV2; | ||
function isAPIGatewayWebsocketEvent(event) { | ||
return event.requestContext !== undefined && event.requestContext.messageDirection !== undefined; | ||
} | ||
exports.isAPIGatewayWebsocketEvent = isAPIGatewayWebsocketEvent; | ||
function isALBEvent(event) { | ||
@@ -51,2 +58,17 @@ return event.requestContext !== undefined && event.requestContext.elb !== undefined; | ||
exports.isSQSEvent = isSQSEvent; | ||
function isSNSSQSEvent(event) { | ||
if (Array.isArray(event.Records) && event.Records.length > 0 && event.Records[0].eventSource === "aws:sqs") { | ||
try { | ||
var body = JSON.parse(event.Records[0].body); | ||
if (body.Type === "Notification" && body.TopicArn) { | ||
return true; | ||
} | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
exports.isSNSSQSEvent = isSNSSQSEvent; | ||
function isAppSyncResolverEvent(event) { | ||
@@ -56,2 +78,6 @@ return event.info !== undefined && event.info.selectionSetGraphQL !== undefined; | ||
exports.isAppSyncResolverEvent = isAppSyncResolverEvent; | ||
function isEventBridgeEvent(event) { | ||
return event["detail-type"] !== undefined; | ||
} | ||
exports.isEventBridgeEvent = isEventBridgeEvent; | ||
//# sourceMappingURL=event-type-guards.js.map |
{ | ||
"name": "datadog-lambda-js", | ||
"version": "4.67.0", | ||
"version": "4.68.0", | ||
"description": "Lambda client library that supports hybrid tracing in node js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# datadog-lambda-js | ||
![build](https://github.com/DataDog/datadog-lambda-js/workflows/build/badge.svg) | ||
@@ -38,11 +39,11 @@ [![Code Coverage](https://img.shields.io/codecov/c/github/DataDog/datadog-lambda-js)](https://codecov.io/gh/DataDog/datadog-lambda-js) | ||
1. Add the following snippet to the function code to manually initialize the tracer with your desired settings. | ||
```js | ||
const tracer = require('dd-trace').init({ | ||
enabled: true, | ||
tags: { | ||
"_dd.origin": "lambda", | ||
}, | ||
sampleRate: 0.1 // e.g., keep 10% of traces | ||
}); | ||
``` | ||
```js | ||
const tracer = require("dd-trace").init({ | ||
enabled: true, | ||
tags: { | ||
"_dd.origin": "lambda", | ||
}, | ||
sampleRate: 0.1, // e.g., keep 10% of traces | ||
}); | ||
``` | ||
@@ -63,3 +64,3 @@ ### Trace & Log Correlation | ||
const { datadog } = require("datadog-lambda-js"); | ||
const tracer = require('dd-trace').init({}); | ||
const tracer = require("dd-trace").init({}); | ||
@@ -117,3 +118,3 @@ module.exports.myHandler = datadog(myHandler, { | ||
If `DD_FLUSH_TO_LOG` is set to `false` (not recommended), you must set `DD_SITE`. Possible values are `datadoghq.com`, `datadoghq.eu`, `us3.datadoghq.com`, `us5.datadoghq.com`, and `ddog-gov.com`. The default is `datadoghq.com`. | ||
If `DD_FLUSH_TO_LOG` is set to `false` (not recommended), you must set `DD_SITE`. Possible values are `datadoghq.com`, `datadoghq.eu`, `us3.datadoghq.com`, `us5.datadoghq.com`, and `ddog-gov.com`. The default is `datadoghq.com`. | ||
@@ -144,2 +145,6 @@ ### DD_LOG_LEVEL | ||
### DD_TRACE_MANAGED_SERVICES | ||
Create inferred spans for managed services. Defaults to `true`. | ||
## Opening Issues | ||
@@ -146,0 +151,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
Sorry, the diff of this file is not supported yet
371062
5224
167