-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathextractor.ts
More file actions
132 lines (111 loc) · 4.91 KB
/
Copy pathextractor.ts
File metadata and controls
132 lines (111 loc) · 4.91 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
import { Context } from "aws-lambda";
import { TraceConfig } from "../listener";
import { logDebug, logError } from "../../utils";
import { XrayService } from "../xray-service";
import {
AppSyncEventTraceExtractor,
CustomTraceExtractor,
DurableExecutionEventTraceExtractor,
EventBridgeEventTraceExtractor,
EventBridgeSQSEventTraceExtractor,
HTTPEventTraceExtractor,
KinesisEventTraceExtractor,
LambdaContextTraceExtractor,
SNSEventTraceExtractor,
SNSSQSEventTraceExtractor,
SQSEventTraceExtractor,
StepFunctionEventTraceExtractor,
} from "./extractors";
import { StepFunctionContextService } from "../step-function-service";
import { EventValidator } from "../../utils/event-validator";
import { TracerWrapper } from "../tracer-wrapper";
import { SpanContextWrapper } from "../span-context-wrapper";
export const DATADOG_TRACE_ID_HEADER = "x-datadog-trace-id";
export const DATADOG_PARENT_ID_HEADER = "x-datadog-parent-id";
export const DATADOG_SAMPLING_PRIORITY_HEADER = "x-datadog-sampling-priority";
export interface EventTraceExtractor {
extract(event: any): SpanContextWrapper | null;
}
export interface DatadogTraceHeaders {
[DATADOG_TRACE_ID_HEADER]: string;
[DATADOG_PARENT_ID_HEADER]: string;
[DATADOG_SAMPLING_PRIORITY_HEADER]: string;
}
export class TraceContextExtractor {
private xrayService: XrayService;
private stepFunctionContextService?: StepFunctionContextService;
constructor(private tracerWrapper: TracerWrapper, private config: TraceConfig) {
this.xrayService = new XrayService();
}
async extract(event: any, context: Context): Promise<SpanContextWrapper | null> {
let spanContext: SpanContextWrapper | null = null;
if (this.config.traceExtractor) {
const customExtractor = new CustomTraceExtractor(this.config.traceExtractor);
spanContext = await customExtractor.extract(event, context);
}
if (spanContext === null) {
const eventExtractor = this.getTraceEventExtractor(event);
if (eventExtractor !== undefined) {
spanContext = eventExtractor.extract(event);
}
}
if (spanContext === null) {
this.stepFunctionContextService = StepFunctionContextService.instance(event);
if (this.stepFunctionContextService?.context) {
const extractor = new StepFunctionEventTraceExtractor();
spanContext = extractor?.extract(event);
}
}
if (spanContext === null) {
const contextExtractor = new LambdaContextTraceExtractor(this.tracerWrapper);
spanContext = contextExtractor.extract(context);
}
if (spanContext !== null) {
this.addTraceContextToXray(spanContext);
return spanContext;
}
return this.xrayService.extract();
}
private getTraceEventExtractor(event: any): EventTraceExtractor | undefined {
if (!event || typeof event !== "object") return;
if (EventValidator.isDurableExecutionEvent(event))
return new DurableExecutionEventTraceExtractor(this.tracerWrapper);
const headers = event.headers ?? event.multiValueHeaders;
if (headers !== null && typeof headers === "object") {
return new HTTPEventTraceExtractor(this.tracerWrapper, this.config.decodeAuthorizerContext);
}
if (EventValidator.isSNSEvent(event)) return new SNSEventTraceExtractor(this.tracerWrapper, this.config);
if (EventValidator.isSNSSQSEvent(event)) return new SNSSQSEventTraceExtractor(this.tracerWrapper, this.config);
if (EventValidator.isEventBridgeSQSEvent(event)) return new EventBridgeSQSEventTraceExtractor(this.tracerWrapper);
if (EventValidator.isAppSyncResolverEvent(event)) return new AppSyncEventTraceExtractor(this.tracerWrapper);
if (EventValidator.isSQSEvent(event)) return new SQSEventTraceExtractor(this.tracerWrapper, this.config);
if (EventValidator.isKinesisStreamEvent(event))
return new KinesisEventTraceExtractor(this.tracerWrapper, this.config);
if (EventValidator.isEventBridgeEvent(event)) return new EventBridgeEventTraceExtractor(this.tracerWrapper);
return;
}
private addTraceContextToXray(spanContext: SpanContextWrapper) {
try {
if (this.stepFunctionContextService?.context !== undefined) {
this.xrayService.addStepFunctionContext(this.stepFunctionContextService.context);
logDebug(`Added Step Function metadata to Xray metadata`, { trace: spanContext });
return;
}
const metadata = {
"trace-id": spanContext.toTraceId(),
"parent-id": spanContext.toSpanId(),
"sampling-priority": spanContext.sampleMode(),
};
this.xrayService.addMetadata(metadata);
logDebug(`Added trace context to Xray metadata`, { trace: spanContext });
} catch (error) {
if (error instanceof Error) {
if (this.stepFunctionContextService?.context !== undefined) {
logError("Couldn't add Step Function metadata to Xray", error);
return;
}
logError("Couldn't add trace context to xray metadata", error);
}
}
}
}