Skip to content

Commit 0534291

Browse files
mattcolegatetobespc
authored andcommitted
Improve CodeBeat ratings for Appmetrics (F rated) (#457)
* Improve CodeBeat rating for extract_all_binaries.js * Add .codebeatsettings file so CodeBeat is less harsh * Improve CodeBeat score of http-outbound-probe.js * Mirror outbound-probe improvements from http to https * Improve CodeBeat score for getRequestItems() * Further improve CodeBeat score for getRequestItems()
1 parent 20372ac commit 0534291

File tree

4 files changed

+84
-175
lines changed

4 files changed

+84
-175
lines changed

.codebeatsettings

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"JAVASCRIPT": {
3+
"BLOCK_NESTING": [4, 5, 6, 7],
4+
"ARITY": [5, 6, 7, 8],
5+
"LOC": [30, 45, 70, 100],
6+
}
7+
}

extract_all_binaries.js

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,15 @@ var ensureSupportedPlatformOrExit = function() {
9494
};
9595

9696
var getSupportedNodeVersionOrExit = function() {
97-
if (process.version.indexOf('v0.10') === 0) {
98-
return '0.10';
99-
}
100-
if (process.version.indexOf('v0.12') === 0) {
101-
return '0.12';
102-
}
103-
if (process.version.indexOf('v2') === 0) {
104-
return '2';
105-
}
106-
if (process.version.indexOf('v4') === 0) {
107-
return '4';
108-
}
109-
if (process.version.indexOf('v5') === 0) {
110-
return '5';
111-
}
112-
if (process.version.indexOf('v6') === 0) {
113-
return '6';
114-
}
115-
if (process.version.indexOf('v7') === 0) {
116-
return '7';
117-
}
118-
if (process.version.indexOf('v8') === 0) {
119-
return '8';
97+
var supportedMajorVersions = "v4, v5, v6, v7, v8"
98+
// version strings are of the format 'vN.N.N' where N is a positive integer.
99+
// we want the first N.
100+
var majorVersion = process.version.substring(1, process.version.indexOf('.'));
101+
if (supportedMajorVersions.indexOf('v' + majorVersion) === -1) {
102+
console.log('Unsupported version ' + process.version + '. Trying rebuild.');
103+
fail();
120104
}
121-
console.log('Unsupported version ' + process.version + '. Trying rebuild.');
122-
fail();
105+
return majorVersion;
123106
};
124107

125108
var getAgentCorePlatformVersionDownloadURL = function() {
@@ -135,48 +118,15 @@ var getWindowsRedisFiles = function() {
135118
};
136119

137120
var downloadAndExtractTGZ = function(filepath, destDir, agentCoreFlag) {
121+
var readStreamTargetDir = 'binaries/appmetrics/tgz/';
138122
if (agentCoreFlag) {
139-
if (fs.existsSync('binaries/agentcore/tgz/' + filepath)) {
140-
fs
141-
.createReadStream('binaries/agentcore/tgz/' + filepath)
142-
.pipe(zlib.createGunzip())
143-
.on('error', function(err) {
144-
console.log('ERROR: Failed to gunzip ' + filepath + ': ' + err.message);
145-
fail();
146-
})
147-
.pipe(tar.Extract({ path: destDir }))
148-
.on('error', function(err) {
149-
console.log('ERROR: Failed to untar ' + filepath + ': ' + err.message);
150-
fail();
151-
})
152-
.on('close', function() {
153-
console.log('Download and extract of ' + filepath + ' finished.');
154-
});
155-
} else {
156-
console.log(filepath + ' does not exist.');
157-
fail();
158-
}
123+
readStreamTargetDir = 'binaries/agentcore/tgz/';
124+
}
125+
if (fs.existsSync(readStreamTargetDir + filepath)) {
126+
zipAndExtract(readStreamTargetDir, filepath, destDir);
159127
} else {
160-
if (fs.existsSync('binaries/appmetrics/tgz/' + filepath)) {
161-
fs
162-
.createReadStream('binaries/appmetrics/tgz/' + filepath)
163-
.pipe(zlib.createGunzip())
164-
.on('error', function(err) {
165-
console.log('ERROR: Failed to gunzip ' + filepath + ': ' + err.message);
166-
fail();
167-
})
168-
.pipe(tar.Extract({ path: destDir }))
169-
.on('error', function(err) {
170-
console.log('ERROR: Failed to untar ' + filepath + ': ' + err.message);
171-
fail();
172-
})
173-
.on('close', function() {
174-
console.log('Download and extract of ' + filepath + ' finished.');
175-
});
176-
} else {
177-
console.log(filepath + ' does not exist.');
178-
fail();
179-
}
128+
console.log(filepath + ' does not exist.');
129+
fail();
180130
}
181131
};
182132

@@ -185,24 +135,28 @@ function fail() {
185135
process.exit(1);
186136
}
187137

188-
var installWinRedis = function(filepath, destDir) {
189-
fs
190-
.createReadStream('binaries/winredis/' + filepath)
138+
function zipAndExtract(targetDir, relativeFilepath, destDir) {
139+
fs
140+
.createReadStream(targetDir + relativeFilepath)
191141
.pipe(zlib.createGunzip())
192142
.on('error', function(err) {
193-
console.log('ERROR: Failed to gunzip ' + filepath + ': ' + err.message);
143+
console.log('ERROR: Failed to gunzip ' + relativeFilepath + ': ' + err.message);
194144
fail();
195145
})
196146
.pipe(tar.Extract({ path: destDir }))
197147
.on('error', function(err) {
198-
console.log('ERROR: Failed to untar ' + filepath + ': ' + err.message);
148+
console.log('ERROR: Failed to untar ' + relativeFilepath + ': ' + err.message);
199149
fail();
200150
})
201151
.on('close', function() {
202-
console.log('Download and extract of ' + filepath + ' finished.');
152+
console.log('Download and extract of ' + relativeFilepath + ' finished.');
203153
});
204154
};
205155

156+
var installWinRedis = function(filepath, destDir) {
157+
zipAndExtract('binaries/winredis/', filepath, destDir);
158+
};
159+
206160
/*
207161
* Start the download
208162
*/

probes/http-outbound-probe.js

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,48 @@ function HttpOutboundProbe() {
3737
}
3838
util.inherits(HttpOutboundProbe, Probe);
3939

40+
function getRequestItems(options) {
41+
var returnObject = { requestMethod: 'GET', urlRequested: '', headers: '' };
42+
if (options !== null) {
43+
var parsedOptions;
44+
switch(typeof options) {
45+
case 'object':
46+
returnObject.urlRequested = formatURL(options);
47+
parsedOptions = options;
48+
break;
49+
case 'string':
50+
returnObject.urlRequested = options;
51+
parsedOptions = url.parse(options);
52+
break;
53+
}
54+
if (parsedOptions.method) { returnObject.requestMethod = parsedOptions.method; }
55+
if (parsedOptions.headers) { returnObject.headers = parsedOptions.headers; }
56+
}
57+
return returnObject;
58+
}
59+
4060
HttpOutboundProbe.prototype.attach = function(name, target) {
4161
var that = this;
4262
if (name === 'http') {
4363
if (target.__outboundProbeAttached__) return target;
4464
target.__outboundProbeAttached__ = true;
45-
4665
aspect.around(
4766
target,
4867
methods,
4968
// Before 'http.request' function
5069
function(obj, methodName, methodArgs, probeData) {
51-
5270
// Start metrics
5371
that.metricsProbeStart(probeData);
5472
that.requestProbeStart(probeData);
55-
5673
// End metrics
5774
aspect.aroundCallback(
5875
methodArgs,
5976
probeData,
6077
function(target, args, probeData) {
61-
6278
// Get HTTP request method from options
63-
var options = methodArgs[0];
64-
var requestMethod = 'GET';
65-
var urlRequested = '';
66-
var headers = '';
67-
if (options !== null && typeof options === 'object') {
68-
urlRequested = formatURL(options);
69-
if (options.method) {
70-
requestMethod = options.method;
71-
}
72-
if (options.headers) {
73-
headers = options.headers;
74-
}
75-
} else if (typeof options === 'string') {
76-
urlRequested = options;
77-
var parsedOptions = url.parse(options);
78-
if (parsedOptions.method) {
79-
requestMethod = parsedOptions.method;
80-
}
81-
if (parsedOptions.headers) {
82-
headers = parsedOptions.headers;
83-
}
84-
}
85-
86-
that.metricsProbeEnd(probeData, requestMethod, urlRequested, args[0], headers);
87-
that.requestProbeEnd(probeData, requestMethod, urlRequested, args[0], headers);
79+
var ri = getRequestItems(methodArgs[0]);
80+
that.metricsProbeEnd(probeData, ri.requestMethod, ri.urlRequested, args[0], ri.headers);
81+
that.requestProbeEnd(probeData, ri.requestMethod, ri.urlRequested, args[0], ri.headers);
8882
},
8983
function(target, args, probeData, ret) {
9084
// Don't need to do anything after the callback
@@ -97,32 +91,10 @@ HttpOutboundProbe.prototype.attach = function(name, target) {
9791
// If no callback has been used then end the metrics after returning from the method instead
9892
if (aspect.findCallbackArg(methodArgs) === undefined) {
9993
// Need to get request method and URL again
100-
var options = methodArgs[0];
101-
var requestMethod = 'GET';
102-
var urlRequested = '';
103-
var headers = '';
104-
if (options !== null && typeof options === 'object') {
105-
urlRequested = formatURL(options);
106-
if (options.method) {
107-
requestMethod = options.method;
108-
}
109-
if (options.headers) {
110-
headers = options.headers;
111-
}
112-
} else if (typeof options === 'string') {
113-
urlRequested = options;
114-
var parsedOptions = url.parse(options);
115-
if (parsedOptions.method) {
116-
requestMethod = parsedOptions.method;
117-
}
118-
if (parsedOptions.headers) {
119-
headers = parsedOptions.headers;
120-
}
121-
}
122-
94+
var ri = getRequestItems(methodArgs[0]);
12395
// End metrics (no response available so pass empty object)
124-
that.metricsProbeEnd(probeData, requestMethod, urlRequested, {}, headers);
125-
that.requestProbeEnd(probeData, requestMethod, urlRequested, {}, headers);
96+
that.metricsProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
97+
that.requestProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
12698
}
12799
return rc;
128100
}

probes/https-outbound-probe.js

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ function HttpsOutboundProbe() {
3333
}
3434
util.inherits(HttpsOutboundProbe, Probe);
3535

36+
function getRequestItems(options) {
37+
var returnObject = { requestMethod: 'GET', urlRequested: '', headers: '' };
38+
if (options !== null) {
39+
var parsedOptions;
40+
switch(typeof options) {
41+
case 'object':
42+
returnObject.urlRequested = formatURL(options);
43+
parsedOptions = options;
44+
break;
45+
case 'string':
46+
returnObject.urlRequested = options;
47+
parsedOptions = url.parse(options);
48+
break;
49+
}
50+
if (parsedOptions.method) { returnObject.requestMethod = parsedOptions.method; }
51+
if (parsedOptions.headers) { returnObject.headers = parsedOptions.headers; }
52+
}
53+
return returnObject;
54+
}
55+
3656
HttpsOutboundProbe.prototype.attach = function(name, target) {
3757
var that = this;
3858
if (name === 'https') {
@@ -56,31 +76,9 @@ HttpsOutboundProbe.prototype.attach = function(name, target) {
5676
function(target, args, probeData) {
5777

5878
// 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);
79+
var ri = getRequestItems(methodArgs[0]);
80+
that.metricsProbeEnd(probeData, ri.requestMethod, ri.urlRequested, args[0], ri.headers);
81+
that.requestProbeEnd(probeData, ri.requestMethod, ri.urlRequested, args[0], ri.headers);
8482
},
8583
function(target, args, probeData, ret) {
8684
// Don't need to do anything after the callback
@@ -93,32 +91,10 @@ HttpsOutboundProbe.prototype.attach = function(name, target) {
9391
// If no callback has been used then end the metrics after returning from the method instead
9492
if (aspect.findCallbackArg(methodArgs) === undefined) {
9593
// 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-
94+
var ri = getRequestItems(methodArgs[0]);
11995
// End metrics (no response available so pass empty object)
120-
that.metricsProbeEnd(probeData, requestMethod, urlRequested, {}, headers);
121-
that.requestProbeEnd(probeData, requestMethod, urlRequested, {}, headers);
96+
that.metricsProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
97+
that.requestProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
12298
}
12399
return rc;
124100
}

0 commit comments

Comments
 (0)