-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-RUL: https://github.com/hyj1991/xprofiler/pull/44 Reviewed-BY: hyj1991 <yeekwanvong@gmail.com>
- Loading branch information
Showing
22 changed files
with
406 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const shimmer = require('./shimmer'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
function requestListenerWrapper(original, addLiveRequest, addCloseRequest, addSentRequest) { | ||
return function (req, res) { | ||
addLiveRequest(); | ||
const start = Date.now(); | ||
|
||
res.on('finish', () => addSentRequest(Date.now() - start)); | ||
|
||
res.on('close', () => addCloseRequest()); | ||
|
||
// call origin function | ||
const returned = original.apply(this, arguments); | ||
return returned; | ||
}; | ||
} | ||
|
||
function serverWrapper(addLiveRequest, addCloseRequest, addSentRequest, original) { | ||
return function (opts, requestListener) { | ||
const args = Array.from(arguments); | ||
let returned; | ||
|
||
if (typeof opts === 'function') { | ||
args.splice(0, 1, requestListenerWrapper(opts, addLiveRequest, addCloseRequest, addSentRequest)); | ||
returned = original.apply(this, args); | ||
} | ||
if (typeof requestListener === 'function') { | ||
args.splice(1, 1, requestListenerWrapper(requestListener, addLiveRequest, addCloseRequest, addSentRequest)); | ||
returned = original.apply(this, args); | ||
} | ||
|
||
return returned; | ||
}; | ||
} | ||
|
||
function patchHttp(addLiveRequest, addCloseRequest, addSentRequest) { | ||
// patch http server | ||
shimmer.wrap(http, 'createServer', serverWrapper.bind(this, addLiveRequest, addCloseRequest, addSentRequest)); | ||
// patch https server | ||
shimmer.wrap(https, 'createServer', serverWrapper.bind(this, addLiveRequest, addCloseRequest, addSentRequest)); | ||
} | ||
|
||
module.exports = { patchHttp }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const { patchHttp } = require('./http'); | ||
|
||
function patch(config, methods) { | ||
if (config.patch_http) { | ||
const { addLiveRequest, addCloseRequest, addSentRequest } = methods; | ||
patchHttp(addLiveRequest, addCloseRequest, addSentRequest); | ||
} | ||
} | ||
|
||
module.exports = { patch }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
function defineProperty(obj, name, value) { | ||
const enumerable = !!obj[name] && obj.propertyIsEnumerable(name); // eslint-disable-line | ||
Object.defineProperty(obj, name, { | ||
configurable: true, | ||
enumerable, | ||
writable: true, | ||
value: value | ||
}); | ||
} | ||
|
||
function isFunction(funktion) { | ||
return typeof funktion === 'function'; | ||
} | ||
|
||
function wrap(nodule, name, wrapper) { | ||
if ( | ||
!nodule || | ||
!nodule[name] || | ||
!wrapper || | ||
!isFunction(nodule[name]) || | ||
!isFunction(wrapper) | ||
) { | ||
return; | ||
} | ||
|
||
const original = nodule[name]; | ||
const wrapped = wrapper(original, name); | ||
|
||
defineProperty(wrapped, '__original', original); | ||
defineProperty(wrapped, '__wrapped', true); | ||
|
||
defineProperty(nodule, name, wrapped); | ||
|
||
defineProperty(wrapped, '__unwrap', function () { | ||
if (nodule[name] === wrapped) { defineProperty(nodule, name, original); } | ||
}); | ||
return wrapped; | ||
} | ||
|
||
function unwrap(nodule, name) { | ||
if ( | ||
!nodule || | ||
!nodule[name] || | ||
!nodule[name].__unwrap) { | ||
return; | ||
} | ||
|
||
return nodule[name].__unwrap(); | ||
} | ||
|
||
module.exports = { wrap, unwrap }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "uv.h" | ||
|
||
#include "../logger.h" | ||
#include "http.h" | ||
|
||
namespace xprofiler { | ||
using Nan::To; | ||
|
||
static uv_mutex_t http_mutex; | ||
|
||
static const char module_type[] = "http"; | ||
|
||
// http server | ||
static unsigned int live_http_request = 0; | ||
static unsigned int http_response_close = 0; | ||
static unsigned int http_response_sent = 0; | ||
static unsigned int http_rt = 0; // ms | ||
|
||
int InitHttpStatus() { | ||
int rc = uv_mutex_init(&http_mutex); | ||
return rc; | ||
} | ||
|
||
void AddLiveRequest(const FunctionCallbackInfo<Value> &info) { | ||
uv_mutex_lock(&http_mutex); | ||
live_http_request++; | ||
uv_mutex_unlock(&http_mutex); | ||
} | ||
|
||
void AddCloseRequest(const FunctionCallbackInfo<Value> &info) { | ||
uv_mutex_lock(&http_mutex); | ||
http_response_close++; | ||
uv_mutex_unlock(&http_mutex); | ||
} | ||
|
||
void AddSentRequest(const FunctionCallbackInfo<Value> &info) { | ||
if (!info[0]->IsNumber()) { | ||
Error(module_type, "request cost must be number!"); | ||
return; | ||
} | ||
|
||
unsigned int cost = To<uint32_t>(info[0]).ToChecked(); | ||
|
||
uv_mutex_lock(&http_mutex); | ||
http_response_sent++; | ||
http_rt += cost; | ||
uv_mutex_unlock(&http_mutex); | ||
} | ||
|
||
void WriteHttpStatus(bool log_format_alinode) { | ||
uv_mutex_lock(&http_mutex); | ||
|
||
double rt = 0.00; | ||
if (http_response_sent != 0) { | ||
rt = http_rt * 1.00 / http_response_sent; | ||
} | ||
|
||
if (log_format_alinode) | ||
Info("http", | ||
"live_http_request: %d, " | ||
"http_request_handled: %d, " | ||
"http_response_sent: %d, " | ||
"http_rt: %.2lf", | ||
live_http_request, http_response_sent, http_response_sent, rt); | ||
else | ||
Info("http", | ||
"live_http_request: %d, " | ||
"http_response_close: %d, " | ||
"http_response_sent: %d, " | ||
"http_rt: %.2lf", | ||
live_http_request, http_response_close, http_response_sent, rt); | ||
|
||
// reset | ||
live_http_request = 0; | ||
http_response_sent = 0; | ||
http_response_close = 0; | ||
http_rt = 0; | ||
|
||
uv_mutex_unlock(&http_mutex); | ||
} | ||
} // namespace xprofiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef _SRC_LOGBYPASS_HTTP_H | ||
#define _SRC_LOGBYPASS_HTTP_H | ||
|
||
#include "nan.h" | ||
|
||
namespace xprofiler { | ||
using Nan::FunctionCallbackInfo; | ||
using v8::Value; | ||
|
||
int InitHttpStatus(); | ||
void WriteHttpStatus(bool log_format_alinode); | ||
|
||
// javascript-accessible | ||
void AddLiveRequest(const FunctionCallbackInfo<Value> &info); | ||
void AddCloseRequest(const FunctionCallbackInfo<Value> &info); | ||
void AddSentRequest(const FunctionCallbackInfo<Value> &info); | ||
} // namespace xprofiler | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.