-
Notifications
You must be signed in to change notification settings - Fork 155
/
aws_p.js
186 lines (151 loc) · 5.34 KB
/
aws_p.js
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
/**
* Capture module.
* @module aws_p
*/
var semver = require('semver');
var Aws = require('../segments/attributes/aws');
var contextUtils = require('../context_utils');
var Utils = require('../utils');
var logger = require('../logger');
var minVersion = '2.7.15';
var throttledErrorDefault = function throttledErrorDefault() {
return false; // If the customer doesn't provide an aws-sdk with a throttled error function, we can't make assumptions.
};
/**
* Configures the AWS SDK to automatically capture information for the segment.
* All created clients will automatically be captured. See 'captureAWSClient'
* for additional details.
* @param {AWS} awssdk - The Javascript AWS SDK.
* @alias module:aws_p.captureAWS
* @returns {AWS}
* @see https://github.com/aws/aws-sdk-js
*/
var captureAWS = function captureAWS(awssdk) {
if (!semver.gte(awssdk.VERSION, minVersion)) {
throw new Error ('AWS SDK version ' + minVersion + ' or greater required.');
}
for (var prop in awssdk) {
if (awssdk[prop].serviceIdentifier) {
var Service = awssdk[prop];
Service.prototype.customizeRequests(captureAWSRequest);
}
}
return awssdk;
};
/**
* Configures any AWS Client instance to automatically capture information for the segment.
* For manual mode, a param with key called 'Segment' is required as a part of the AWS
* call paramaters, and must reference a Segment or Subsegment object.
* @param {AWS.Service} service - An instance of a AWS service to wrap.
* @alias module:aws_p.captureAWSClient
* @returns {AWS.Service}
* @see https://github.com/aws/aws-sdk-js
*/
var captureAWSClient = function captureAWSClient(service) {
service.customizeRequests(captureAWSRequest);
return service;
};
function captureAWSRequest(req) {
var parent = contextUtils.resolveSegment(contextUtils.resolveManualSegmentParams(req.params));
if (!parent) {
var output = this.serviceIdentifier + '.' + req.operation;
if (!contextUtils.isAutomaticMode()) {
logger.getLogger().info('Call ' + output + ' requires a segment object' +
' on the request params as "XRaySegment" for tracing in manual mode. Ignoring.');
} else {
logger.getLogger().info('Call ' + output +
' is missing the sub/segment context for automatic mode. Ignoring.');
}
return req;
}
var throttledError = this.throttledError || throttledErrorDefault;
var stack = (new Error()).stack;
let subsegment;
if (parent.notTraced) {
subsegment = parent.addNewSubsegmentWithoutSampling(this.serviceIdentifier);
} else {
subsegment = parent.addNewSubsegment(this.serviceIdentifier);
}
var traceId = parent.segment ? parent.segment.trace_id : parent.trace_id;
const data = parent.segment ? parent.segment.additionalTraceData : parent.additionalTraceData;
var buildListener = function(req) {
if (parent.noOp) {
return;
}
let traceHeader = 'Root=' + traceId + ';Parent=' + subsegment.id +
';Sampled=' + (subsegment.notTraced ? '0' : '1');
if (data != null) {
for (const [key, value] of Object.entries(data)) {
traceHeader += ';' + key +'=' + value;
}
}
req.httpRequest.headers['X-Amzn-Trace-Id'] = traceHeader;
};
var completeListener = function(res) {
subsegment.addAttribute('namespace', 'aws');
subsegment.addAttribute('aws', new Aws(res, subsegment.name));
var httpRes = res.httpResponse;
if (httpRes) {
subsegment.addAttribute('http', new HttpResponse(httpRes));
if (httpRes.statusCode === 429 || (res.error && throttledError(res.error))) {
subsegment.addThrottleFlag();
}
}
if (res.error) {
var err = { message: res.error.message, name: res.error.code, stack: stack };
if (httpRes && httpRes.statusCode) {
if (Utils.getCauseTypeFromHttpStatus(httpRes.statusCode) == 'error') {
subsegment.addErrorFlag();
}
subsegment.close(err, true);
} else {
subsegment.close(err);
}
} else {
if (httpRes && httpRes.statusCode) {
var cause = Utils.getCauseTypeFromHttpStatus(httpRes.statusCode);
if (cause) {
subsegment[cause] = true;
}
}
subsegment.close();
}
};
req.on('beforePresign', function(req) {
// Only the AWS Presigner triggers this event,
// so we can rely on this event to notify us when
// a request is for a presigned url
parent.removeSubsegment(subsegment);
parent.decrementCounter();
req.removeListener('build', buildListener);
req.removeListener('complete', completeListener);
});
req.on('build', buildListener).on('complete', completeListener);
if (!req.__send) {
req.__send = req.send;
req.send = function(callback) {
if (contextUtils.isAutomaticMode()) {
var session = contextUtils.getNamespace();
session.run(function() {
contextUtils.setSegment(subsegment);
req.__send(callback);
});
} else {
req.__send(callback);
}
};
}
}
function HttpResponse(res) {
this.init(res);
}
HttpResponse.prototype.init = function init(res) {
this.response = {
status: res.statusCode || '',
};
if (res.headers && res.headers['content-length']) {
this.response.content_length = res.headers['content-length'];
}
};
module.exports.captureAWSClient = captureAWSClient;
module.exports.captureAWS = captureAWS;