-
Notifications
You must be signed in to change notification settings - Fork 128
/
ProviderService.ts
129 lines (108 loc) · 3.28 KB
/
ProviderService.ts
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
import { Namespace } from 'cls-hooked';
import type {
ProviderServiceInterface,
ContextMissingStrategy,
} from '../types/ProviderServiceInterface.js';
import type { Segment, Subsegment, Logger } from 'aws-xray-sdk-core';
import xraySdk from 'aws-xray-sdk-core';
const {
captureAWS,
captureAWSClient,
captureAWSv3Client,
captureAsyncFunc,
captureFunc,
captureHTTPsGlobal,
getNamespace,
getSegment,
setSegment,
Segment: XraySegment,
setContextMissingStrategy,
setDaemonAddress,
setLogger,
} = xraySdk;
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
class ProviderService implements ProviderServiceInterface {
public captureAWS<T>(awssdk: T): T {
return captureAWS(awssdk);
}
public captureAWSClient<T>(service: T): T {
return captureAWSClient(service);
}
public captureAWSv3Client<T>(service: T): T {
addUserAgentMiddleware(service, 'tracer');
// Type must be aliased as any because of this https://github.com/aws/aws-xray-sdk-node/issues/439#issuecomment-859715660
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return captureAWSv3Client(service as any);
}
public captureAsyncFunc(
name: string,
fcn: (subsegment?: Subsegment) => unknown,
_parent?: Segment | Subsegment
): unknown {
return captureAsyncFunc(name, fcn);
}
public captureFunc(
name: string,
fcn: (subsegment?: Subsegment) => unknown,
_parent?: Segment | Subsegment
): unknown {
return captureFunc(name, fcn);
}
public captureHTTPsGlobal(): void {
// eslint-disable-next-line @typescript-eslint/no-var-requires
captureHTTPsGlobal(require('http'));
// eslint-disable-next-line @typescript-eslint/no-var-requires
captureHTTPsGlobal(require('https'));
}
public getNamespace(): Namespace {
return getNamespace();
}
public getSegment(): Segment | Subsegment | undefined {
return getSegment();
}
public putAnnotation(key: string, value: string | number | boolean): void {
const segment = this.getSegment();
if (segment === undefined) {
console.warn(
'No active segment or subsegment found, skipping annotation'
);
return;
}
if (segment instanceof XraySegment) {
console.warn(
'You cannot annotate the main segment in a Lambda execution environment'
);
return;
}
segment.addAnnotation(key, value);
}
public putMetadata(key: string, value: unknown, namespace?: string): void {
const segment = this.getSegment();
if (segment === undefined) {
console.warn(
'No active segment or subsegment found, skipping metadata addition'
);
return;
}
if (segment instanceof XraySegment) {
console.warn(
'You cannot add metadata to the main segment in a Lambda execution environment'
);
return;
}
segment.addMetadata(key, value, namespace);
}
public setContextMissingStrategy(strategy: ContextMissingStrategy): void {
setContextMissingStrategy(strategy);
}
public setDaemonAddress(address: string): void {
setDaemonAddress(address);
}
public setLogger(logObj: unknown): void {
setLogger(logObj as Logger);
}
public setSegment(segment: Segment | Subsegment): void {
setSegment(segment);
}
}
export { ProviderService };