in AutoCollection/HttpDependencies.ts [56:128]
private _initialize() {
this._isInitialized = true;
const originalRequest = http.request;
const originalHttpsRequest = https.request;
const clientRequestPatch = (request: http.ClientRequest, options: string | URL | http.RequestOptions | https.RequestOptions) => {
var shouldCollect = !(<any>options)[AutoCollectHttpDependencies.disableCollectionRequestOption] &&
!(<any>request)[AutoCollectHttpDependencies.alreadyAutoCollectedFlag];
// If someone else patched traceparent headers onto this request
if ((<any>options).headers && (<any>options).headers['user-agent'] && (<any>options).headers['user-agent'].toString().indexOf('azsdk-js') !== -1) {
shouldCollect = false;
}
(<any>request)[AutoCollectHttpDependencies.alreadyAutoCollectedFlag] = true;
if (request && options && shouldCollect) {
CorrelationContextManager.wrapEmitter(request);
// If there is no context create one, this apply when no request is triggering the dependency
if (!CorrelationContextManager.getCurrentContext()) {
// Create correlation context and wrap execution
let operationId = null;
if (CorrelationIdManager.w3cEnabled) {
let traceparent = new Traceparent();
operationId = traceparent.traceId;
}
else {
let requestId = CorrelationIdManager.generateRequestId(null);
operationId = CorrelationIdManager.getRootId(requestId);
}
let correlationContext = CorrelationContextManager.generateContextObject(operationId);
CorrelationContextManager.runWithContext(correlationContext, () => {
AutoCollectHttpDependencies.trackRequest(this._client, { options: options, request: request });
});
}
else {
AutoCollectHttpDependencies.trackRequest(this._client, { options: options, request: request });
}
}
};
// On node >= v0.11.12 and < 9.0 (excluding 8.9.0) https.request just calls http.request (with additional options).
// On node < 0.11.12, 8.9.0, and 9.0 > https.request is handled separately
// Patch both and leave a flag to not double-count on versions that just call through
// We add the flag to both http and https to protect against strange double collection in other scenarios
http.request = (options, ...requestArgs: any[]) => {
const request: http.ClientRequest = originalRequest.call(http, options, ...requestArgs);
clientRequestPatch(request, options);
return request;
};
https.request = (options, ...requestArgs: any[]) => {
const request: http.ClientRequest = originalHttpsRequest.call(https, options, ...requestArgs);
clientRequestPatch(request, options);
return request;
};
// Node 8 calls http.request from http.get using a local reference!
// We have to patch .get manually in this case and can't just assume request is enough
// We have to replace the entire method in this case. We can't call the original.
// This is because calling the original will give us no chance to set headers as it internally does .end().
http.get = (options, ...requestArgs: any[]) => {
const request: http.ClientRequest = http.request.call(http, options, ...requestArgs);
request.end();
return request;
};
https.get = (options, ...requestArgs: any[]) => {
const request: http.ClientRequest = https.request.call(https, options, ...requestArgs);
request.end();
return request;
};
}