Skip to content

Commit

Permalink
feat: support app.httpclient and agent.httpclient auto set tracer (#1393
Browse files Browse the repository at this point in the history
)
  • Loading branch information
leoner authored and popomore committed Sep 10, 2017
1 parent 3aaee8f commit f7c0d85
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/core/httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ class HttpClient extends urllib.HttpClient {
});
this.app = app;
}

request(url, args, callback) {
if (typeof args === 'function') {
callback = args;
args = null;
}

args = args || {};

if (!args.ctx && !args.tracer) {
args.tracer = this.app.tracer;
}

return super.request(url, args, callback);
}
}

function normalizeConfig(app) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"egg-doctools": "^2.0.1",
"egg-mock": "^3.8.0",
"egg-plugin-puml": "^2.4.0",
"egg-tracer": "^1.1.0",
"egg-view-nunjucks": "^2.1.3",
"eslint": "^4.1.1",
"eslint-config-egg": "^5.0.0",
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/httpclient-tracer/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
const done = app.readyCallback('ready');
setTimeout(done, 5000);
};
8 changes: 8 additions & 0 deletions test/fixtures/apps/httpclient-tracer/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
tracer: {
enable: true,
package: 'egg-tracer',
},
}
3 changes: 3 additions & 0 deletions test/fixtures/apps/httpclient-tracer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "httpclient-tracer"
}
211 changes: 211 additions & 0 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,215 @@ describe('test/lib/core/httpclient.test.js', () => {
});
});
});

describe('httpclient tracer', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-tracer');
return app.ready();
});

after(() => app.close());

it('should app request auto set tracer', function* () {
const httpclient = app.httpclient;

let reqTracer;
let resTracer;

httpclient.on('request', function(options) {
reqTracer = options.args.tracer;
});

httpclient.on('response', function(options) {
resTracer = options.req.args.tracer;
});

let res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
});

assert(res.status === 200);
assert(reqTracer === resTracer);

assert(reqTracer.traceId);
assert(reqTracer.traceId === resTracer.traceId);

reqTracer = null;
resTracer = null;

res = yield httpclient.request('https://www.alipay.com');

assert(res.status === 200);
assert(reqTracer === resTracer);

assert(reqTracer.traceId);
assert(reqTracer.traceId === resTracer.traceId);
});

it('should agent request auto set tracer', function* () {
const httpclient = app.agent.httpclient;

let reqTracer;
let resTracer;

httpclient.on('request', function(options) {
reqTracer = options.args.tracer;
});

httpclient.on('response', function(options) {
resTracer = options.req.args.tracer;
});

const res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
});

assert(res.status === 200);
assert(reqTracer === resTracer);

assert(reqTracer.traceId);
assert(reqTracer.traceId === resTracer.traceId);
});

it('should app request with ctx and tracer', function* () {
const httpclient = app.httpclient;

let reqTracer;
let resTracer;

httpclient.on('request', function(options) {
reqTracer = options.args.tracer || options.ctx.tracer;
});

httpclient.on('response', function(options) {
resTracer = options.req.args.tracer || options.ctx.tracer;
});

let res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
});

assert(res.status === 200);

assert(reqTracer.traceId);
assert(reqTracer.traceId === resTracer.traceId);

reqTracer = null;
resTracer = null;
res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
ctx: {},
tracer: {
id: '1234',
},
});

assert(res.status === 200);
assert(reqTracer.id === resTracer.id);
assert(reqTracer.id === '1234');

reqTracer = null;
resTracer = null;
res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
ctx: {
tracer: {
id: '5678',
},
},
});

assert(res.status === 200);
assert(reqTracer.id === resTracer.id);
assert(reqTracer.id === '5678');
});
});

describe('before app ready multi httpclient request tracer', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-tracer');
});

after(() => app.close());

it('should app request before ready use same tracer', function* () {
const httpclient = app.httpclient;

let reqTracers = [];
let resTracers = [];

httpclient.on('request', function(options) {
reqTracers.push(options.args.tracer);
});

httpclient.on('response', function(options) {
resTracers.push(options.req.args.tracer);
});

let res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
});
assert(res.status === 200);


res = yield httpclient.request('https://github.com', {
method: 'GET',
});

assert(res.status === 200);

res = yield httpclient.request('https://www.npmjs.com', {
method: 'GET',
});
assert(res.status === 200);

assert(reqTracers.length === 3);
assert(resTracers.length === 3);

assert(reqTracers[0] === reqTracers[1]);
assert(reqTracers[1] === reqTracers[2]);

assert(resTracers[0] === reqTracers[2]);
assert(resTracers[1] === resTracers[0]);
assert(resTracers[2] === resTracers[1]);

assert(reqTracers[0].traceId);

reqTracers = [];
resTracers = [];

yield app.ready();

res = yield httpclient.request('https://www.alipay.com', {
method: 'GET',
});
assert(res.status === 200);


res = yield httpclient.request('https://github.com', {
method: 'GET',
});
assert(res.status === 200);

res = yield httpclient.request('https://www.npmjs.com', {
method: 'GET',
});
assert(res.status === 200);

assert(reqTracers.length === 3);
assert(resTracers.length === 3);

assert(reqTracers[0] !== reqTracers[1]);
assert(reqTracers[1] !== reqTracers[2]);

assert(resTracers[0] !== reqTracers[2]);
assert(resTracers[1] !== resTracers[0]);
assert(resTracers[2] !== resTracers[1]);

assert(reqTracers[0].traceId);
});
});

});

0 comments on commit f7c0d85

Please sign in to comment.