|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var am = require('../'); |
| 19 | +var aspect = require('../lib/aspect.js'); |
| 20 | +var Probe = require('../lib/probe.js'); |
| 21 | +var request = require('../lib/request.js'); |
| 22 | + |
| 23 | +var url = require('url'); |
| 24 | +var util = require('util'); |
| 25 | + |
| 26 | +// In https 'get' calls 'request' so we only instrument 'request' |
| 27 | +var methods = ['request']; |
| 28 | + |
| 29 | +// Probe to instrument outbound https requests |
| 30 | + |
| 31 | +function HttpsOutboundProbe() { |
| 32 | + Probe.call(this, 'https'); // match the name of the module we're instrumenting |
| 33 | +} |
| 34 | +util.inherits(HttpsOutboundProbe, Probe); |
| 35 | + |
| 36 | +HttpsOutboundProbe.prototype.attach = function(name, target) { |
| 37 | + var that = this; |
| 38 | + if (name === 'https') { |
| 39 | + if (target.__outboundProbeAttached__) return target; |
| 40 | + target.__outboundProbeAttached__ = true; |
| 41 | + |
| 42 | + aspect.around( |
| 43 | + target, |
| 44 | + methods, |
| 45 | + // Before 'https.request' function |
| 46 | + function(obj, methodName, methodArgs, probeData) { |
| 47 | + |
| 48 | + // Start metrics |
| 49 | + that.metricsProbeStart(probeData); |
| 50 | + that.requestProbeStart(probeData); |
| 51 | + |
| 52 | + // End metrics |
| 53 | + aspect.aroundCallback( |
| 54 | + methodArgs, |
| 55 | + probeData, |
| 56 | + function(target, args, probeData) { |
| 57 | + |
| 58 | + // Get HTTPS request method from options |
| 59 | + var options = methodArgs[0]; |
| 60 | + var requestMethod = 'GET'; |
| 61 | + var urlRequested = ''; |
| 62 | + var headers = ''; |
| 63 | + if (options !== null && typeof options === 'object') { |
| 64 | + urlRequested = formatURL(options); |
| 65 | + if (options.method) { |
| 66 | + requestMethod = options.method; |
| 67 | + } |
| 68 | + if (options.headers) { |
| 69 | + headers = options.headers; |
| 70 | + } |
| 71 | + } else if (typeof options === 'string') { |
| 72 | + urlRequested = options; |
| 73 | + var parsedOptions = url.parse(options); |
| 74 | + if (parsedOptions.method) { |
| 75 | + requestMethod = parsedOptions.method; |
| 76 | + } |
| 77 | + if (parsedOptions.headers) { |
| 78 | + headers = parsedOptions.headers; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + that.metricsProbeEnd(probeData, requestMethod, urlRequested, args[0], headers); |
| 83 | + that.requestProbeEnd(probeData, requestMethod, urlRequested, args[0], headers); |
| 84 | + }, |
| 85 | + function(target, args, probeData, ret) { |
| 86 | + // Don't need to do anything after the callback |
| 87 | + return ret; |
| 88 | + } |
| 89 | + ); |
| 90 | + }, |
| 91 | + // After 'https.request' function returns |
| 92 | + function(target, methodName, methodArgs, probeData, rc) { |
| 93 | + // If no callback has been used then end the metrics after returning from the method instead |
| 94 | + if (aspect.findCallbackArg(methodArgs) === undefined) { |
| 95 | + // Need to get request method and URL again |
| 96 | + var options = methodArgs[0]; |
| 97 | + var requestMethod = 'GET'; |
| 98 | + var urlRequested = ''; |
| 99 | + var headers = ''; |
| 100 | + if (options !== null && typeof options === 'object') { |
| 101 | + urlRequested = formatURL(options); |
| 102 | + if (options.method) { |
| 103 | + requestMethod = options.method; |
| 104 | + } |
| 105 | + if (options.headers) { |
| 106 | + headers = options.headers; |
| 107 | + } |
| 108 | + } else if (typeof options === 'string') { |
| 109 | + urlRequested = options; |
| 110 | + var parsedOptions = url.parse(options); |
| 111 | + if (parsedOptions.method) { |
| 112 | + requestMethod = parsedOptions.method; |
| 113 | + } |
| 114 | + if (parsedOptions.headers) { |
| 115 | + headers = parsedOptions.headers; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // End metrics (no response available so pass empty object) |
| 120 | + that.metricsProbeEnd(probeData, requestMethod, urlRequested, {}, headers); |
| 121 | + that.requestProbeEnd(probeData, requestMethod, urlRequested, {}, headers); |
| 122 | + } |
| 123 | + return rc; |
| 124 | + } |
| 125 | + ); |
| 126 | + } |
| 127 | + return target; |
| 128 | +}; |
| 129 | + |
| 130 | +// Get a URL as a string from the options object passed to https.get or https.request |
| 131 | +// See https://nodejs.org/api/http.html#http_http_request_options_callback |
| 132 | +function formatURL(httpsOptions) { |
| 133 | + var url; |
| 134 | + if (httpsOptions.protocol) { |
| 135 | + url = httpsOptions.protocol; |
| 136 | + } else { |
| 137 | + url = 'https:'; |
| 138 | + } |
| 139 | + url += '//'; |
| 140 | + if (httpsOptions.auth) { |
| 141 | + url += httpsOptions.auth + '@'; |
| 142 | + } |
| 143 | + if (httpsOptions.host) { |
| 144 | + url += httpsOptions.host; |
| 145 | + } else if (httpsOptions.hostname) { |
| 146 | + url += httpsOptions.host; |
| 147 | + } else { |
| 148 | + url += 'localhost'; |
| 149 | + } |
| 150 | + if (httpsOptions.port) { |
| 151 | + url += ':' + httpsOptions.port; |
| 152 | + } |
| 153 | + if (httpsOptions.path) { |
| 154 | + url += httpsOptions.path; |
| 155 | + } else { |
| 156 | + url += '/'; |
| 157 | + } |
| 158 | + return url; |
| 159 | +} |
| 160 | + |
| 161 | +/* |
| 162 | + * Lightweight metrics probe for HTTPS requests |
| 163 | + * |
| 164 | + * These provide: |
| 165 | + * time: time event started |
| 166 | + * method: HTTPS method, eg. GET, POST, etc |
| 167 | + * url: The url requested |
| 168 | + * requestHeaders: The HTTPS headers for the request |
| 169 | + * duration: The time for the request to respond |
| 170 | + * contentType: HTTPS content-type |
| 171 | + * statusCode: HTTPS status code |
| 172 | + */ |
| 173 | +HttpsOutboundProbe.prototype.metricsEnd = function(probeData, method, url, res, headers) { |
| 174 | + if (probeData && probeData.timer) { |
| 175 | + probeData.timer.stop(); |
| 176 | + am.emit('https-outbound', { |
| 177 | + time: probeData.timer.startTimeMillis, |
| 178 | + method: method, |
| 179 | + url: url, |
| 180 | + duration: probeData.timer.timeDelta, |
| 181 | + statusCode: res.statusCode, |
| 182 | + contentType: res.headers ? res.headers['content-type'] : undefined, |
| 183 | + requestHeaders: headers, |
| 184 | + }); |
| 185 | + } |
| 186 | +}; |
| 187 | + |
| 188 | +/* |
| 189 | + * Heavyweight request probes for HTTPS outbound requests |
| 190 | + */ |
| 191 | +HttpsOutboundProbe.prototype.requestStart = function(probeData, method, url) { |
| 192 | + var reqType = 'https-outbound'; |
| 193 | + // Do not mark as a root request |
| 194 | + probeData.req = request.startRequest(reqType, url, false, probeData.timer); |
| 195 | +}; |
| 196 | + |
| 197 | +HttpsOutboundProbe.prototype.requestEnd = function(probeData, method, url, res, headers) { |
| 198 | + if (probeData && probeData.req) |
| 199 | + probeData.req.stop({ |
| 200 | + url: url, |
| 201 | + statusCode: res.statusCode, |
| 202 | + contentType: res.headers ? res.headers['content-type'] : undefined, |
| 203 | + requestHeaders: headers, |
| 204 | + }); |
| 205 | +}; |
| 206 | + |
| 207 | +module.exports = HttpsOutboundProbe; |
0 commit comments