Skip to content

Commit 42344e8

Browse files
authored
src: record http status
PR-RUL: https://github.com/hyj1991/xprofiler/pull/44 Reviewed-BY: hyj1991 <yeekwanvong@gmail.com>
1 parent 4f7fc1f commit 42344e8

22 files changed

+406
-19
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"src/logbypass/heap.cc",
1818
"src/logbypass/gc.cc",
1919
"src/logbypass/libuv.cc",
20+
"src/logbypass/http.cc",
2021
"src/commands/listener.cc",
2122
"src/commands/send.cc",
2223
"src/commands/parser.cc",

lib/configure.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const defaultConfig = {
1111
log_format_alinode: false,
1212
log_level: 1,
1313
log_type: 0,
14-
enable_fatal_error_hook: true
14+
enable_fatal_error_hook: true,
15+
patch_http: false
1516
};
1617

1718
function checkLogDirAccessiable(logdir) {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xprofiler",
3-
"version": "1.0.0-prepare",
3+
"version": "1.1.0-prepare",
44
"description": "node.js addon to output runtime logs",
55
"bin": {
66
"xprofctl": "bin/xprofctl"
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"build": "npm run lint && node-gyp rebuild",
1111
"format": "clang-format -i --glob=\"src/**/!(report_win)[.h|.cc]\"",
12-
"lint": "npm run format && eslint --fix xprofiler.js \"test/**/*.js\" lib/*.js bin/xprofctl scripts/**/*.js",
12+
"lint": "npm run format && eslint --fix xprofiler.js \"test/**/*.js\" lib/*.js patch/*.js bin/xprofctl scripts/**/*.js",
1313
"test": "mocha -t 0 -R spec test/*.test.js",
1414
"test-single": "mocha -t 0 -R spec",
1515
"cov": "nyc --reporter=html --reporter=text --reporter=lcov mocha -R spec test/*.test.js --timeout 0",
@@ -26,6 +26,7 @@
2626
"files": [
2727
"bin",
2828
"lib",
29+
"patch",
2930
"src",
3031
"binding.gyp",
3132
"xprofiler.js",

patch/http.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const shimmer = require('./shimmer');
4+
const http = require('http');
5+
const https = require('https');
6+
7+
function requestListenerWrapper(original, addLiveRequest, addCloseRequest, addSentRequest) {
8+
return function (req, res) {
9+
addLiveRequest();
10+
const start = Date.now();
11+
12+
res.on('finish', () => addSentRequest(Date.now() - start));
13+
14+
res.on('close', () => addCloseRequest());
15+
16+
// call origin function
17+
const returned = original.apply(this, arguments);
18+
return returned;
19+
};
20+
}
21+
22+
function serverWrapper(addLiveRequest, addCloseRequest, addSentRequest, original) {
23+
return function (opts, requestListener) {
24+
const args = Array.from(arguments);
25+
let returned;
26+
27+
if (typeof opts === 'function') {
28+
args.splice(0, 1, requestListenerWrapper(opts, addLiveRequest, addCloseRequest, addSentRequest));
29+
returned = original.apply(this, args);
30+
}
31+
if (typeof requestListener === 'function') {
32+
args.splice(1, 1, requestListenerWrapper(requestListener, addLiveRequest, addCloseRequest, addSentRequest));
33+
returned = original.apply(this, args);
34+
}
35+
36+
return returned;
37+
};
38+
}
39+
40+
function patchHttp(addLiveRequest, addCloseRequest, addSentRequest) {
41+
// patch http server
42+
shimmer.wrap(http, 'createServer', serverWrapper.bind(this, addLiveRequest, addCloseRequest, addSentRequest));
43+
// patch https server
44+
shimmer.wrap(https, 'createServer', serverWrapper.bind(this, addLiveRequest, addCloseRequest, addSentRequest));
45+
}
46+
47+
module.exports = { patchHttp };

patch/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const { patchHttp } = require('./http');
4+
5+
function patch(config, methods) {
6+
if (config.patch_http) {
7+
const { addLiveRequest, addCloseRequest, addSentRequest } = methods;
8+
patchHttp(addLiveRequest, addCloseRequest, addSentRequest);
9+
}
10+
}
11+
12+
module.exports = { patch };

patch/shimmer.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
function defineProperty(obj, name, value) {
4+
const enumerable = !!obj[name] && obj.propertyIsEnumerable(name); // eslint-disable-line
5+
Object.defineProperty(obj, name, {
6+
configurable: true,
7+
enumerable,
8+
writable: true,
9+
value: value
10+
});
11+
}
12+
13+
function isFunction(funktion) {
14+
return typeof funktion === 'function';
15+
}
16+
17+
function wrap(nodule, name, wrapper) {
18+
if (
19+
!nodule ||
20+
!nodule[name] ||
21+
!wrapper ||
22+
!isFunction(nodule[name]) ||
23+
!isFunction(wrapper)
24+
) {
25+
return;
26+
}
27+
28+
const original = nodule[name];
29+
const wrapped = wrapper(original, name);
30+
31+
defineProperty(wrapped, '__original', original);
32+
defineProperty(wrapped, '__wrapped', true);
33+
34+
defineProperty(nodule, name, wrapped);
35+
36+
defineProperty(wrapped, '__unwrap', function () {
37+
if (nodule[name] === wrapped) { defineProperty(nodule, name, original); }
38+
});
39+
return wrapped;
40+
}
41+
42+
function unwrap(nodule, name) {
43+
if (
44+
!nodule ||
45+
!nodule[name] ||
46+
!nodule[name].__unwrap) {
47+
return;
48+
}
49+
50+
return nodule[name].__unwrap();
51+
}
52+
53+
module.exports = { wrap, unwrap };

src/commands/parser.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ using nlohmann::json;
1313
using std::exception;
1414
using std::string;
1515

16-
#define HANDLE_COMMANDS(cmd_str, handle) \
17-
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
18-
handle( \
19-
parsed, FmtMessage, \
20-
[traceid](json data) { SuccessValue(traceid, data); }, \
21-
[traceid](string message) { ErrorValue(traceid, message); }); \
22-
handled = true; \
16+
#define HANDLE_COMMANDS(cmd_str, handle) \
17+
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
18+
handle(parsed, FmtMessage, \
19+
[traceid](json data) { SuccessValue(traceid, data); }, \
20+
[traceid](string message) { ErrorValue(traceid, message); }); \
21+
handled = true; \
2322
}
2423

2524
void ParseCmd(char *command) {

src/commands/simple/config.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ COMMAND_CALLBACK(GetXprofilerConfig) {
2929
data["log_level"] = GetLogLevel();
3030
data["log_type"] = GetLogType();
3131
data["enable_fatal_error_hook"] = GetEnableFatalErrorHook();
32+
data["patch_http"] = GetPatchHttp();
3233
success(data);
3334
}
3435

src/configure.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static LOG_TYPE log_type = LOG_TO_FILE;
2323
static bool enable_log_uv_handles = true;
2424
static bool log_format_alinode = false;
2525
static bool enable_fatal_error_hook = true;
26+
static bool patch_http = false;
2627

2728
void Configure(const FunctionCallbackInfo<Value> &info) {
2829
if (!info[0]->IsObject()) {
@@ -38,6 +39,7 @@ void Configure(const FunctionCallbackInfo<Value> &info) {
3839
CONVERT_BOOL(enable_log_uv_handles)
3940
CONVERT_BOOL(log_format_alinode)
4041
CONVERT_BOOL(enable_fatal_error_hook)
42+
CONVERT_BOOL(patch_http)
4143

4244
info.GetReturnValue().Set(New<Boolean>(true));
4345
}
@@ -52,6 +54,7 @@ void GetConfig(const FunctionCallbackInfo<Value> &info) {
5254
CONFIG_NATIVE_NUMBER(enable_log_uv_handles, Boolean)
5355
CONFIG_NATIVE_NUMBER(log_format_alinode, Boolean)
5456
CONFIG_NATIVE_NUMBER(enable_fatal_error_hook, Boolean)
57+
CONFIG_NATIVE_NUMBER(patch_http, Boolean)
5558

5659
info.GetReturnValue().Set(config);
5760
}
@@ -64,4 +67,5 @@ DEFINE_GET_SET_FUNCTION(LogType, LOG_TYPE, log_type)
6467
DEFINE_GET_SET_FUNCTION(FormatAsAlinode, bool, log_format_alinode)
6568
DEFINE_GET_SET_FUNCTION(EnableLogUvHandles, bool, enable_log_uv_handles)
6669
DEFINE_GET_SET_FUNCTION(EnableFatalErrorHook, bool, enable_fatal_error_hook)
70+
DEFINE_GET_SET_FUNCTION(PatchHttp, bool, patch_http)
6771
} // namespace xprofiler

src/configure.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ DECLARE_GET_SET_FUNCTION(LogType, LOG_TYPE)
6262
DECLARE_GET_SET_FUNCTION(FormatAsAlinode, bool)
6363
DECLARE_GET_SET_FUNCTION(EnableLogUvHandles, bool)
6464
DECLARE_GET_SET_FUNCTION(EnableFatalErrorHook, bool)
65+
DECLARE_GET_SET_FUNCTION(PatchHttp, bool)
6566

6667
// javascript accessible
6768
void Configure(const FunctionCallbackInfo<Value> &info);

0 commit comments

Comments
 (0)