-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathextractor-utils.ts
More file actions
62 lines (55 loc) · 2.15 KB
/
extractor-utils.ts
File metadata and controls
62 lines (55 loc) · 2.15 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
import { logDebug } from "../../utils";
import { StepFunctionContextService } from "../step-function-service";
import { SpanContextWrapper } from "../span-context-wrapper";
import { TracerWrapper } from "../tracer-wrapper";
import { XrayService } from "../xray-service";
/**
* Common utility functions for trace context extraction
*/
/**
* Attempts to extract trace context from headers, falling back to Step Function context if needed
* @param headers The headers object to extract from
* @param tracerWrapper The tracer wrapper instance
* @returns SpanContextWrapper or null
*/
export function extractTraceContext(headers: any, tracerWrapper: TracerWrapper): SpanContextWrapper | null {
// First try to extract as regular trace headers
const traceContext = tracerWrapper.extract(headers);
if (traceContext) {
return traceContext;
}
// If that fails, check if this is a Step Function context
const stepFunctionInstance = StepFunctionContextService.instance(headers);
if (stepFunctionInstance.context !== undefined) {
if (stepFunctionInstance.spanContext !== null) {
return stepFunctionInstance.spanContext;
}
}
return null;
}
/**
* Extracts trace context from AWS Trace Header
* @param awsTraceHeader The AWS trace header string
* @param eventType The type of event (for logging)
* @returns SpanContextWrapper or null
*/
export function extractFromAWSTraceHeader(awsTraceHeader: string, eventType: string): SpanContextWrapper | null {
const traceContext = XrayService.extraceDDContextFromAWSTraceHeader(awsTraceHeader);
if (traceContext) {
logDebug(`Extracted trace context from ${eventType} event attributes AWSTraceHeader`);
return traceContext;
} else {
logDebug(`No Datadog trace context found from ${eventType} event attributes AWSTraceHeader`);
return null;
}
}
/**
* Common error handler for extraction operations
* @param error The error that occurred
* @param eventType The type of event (for logging)
*/
export function handleExtractionError(error: unknown, eventType: string): void {
if (error instanceof Error) {
logDebug(`Unable to extract trace context from ${eventType} event`, error);
}
}