Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support app or agent get tracer (#2)
- Loading branch information
Showing
18 changed files
with
306 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./index'); |
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,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./index'); |
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,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('../../lib/index.js'); |
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,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('../../lib/index.js'); |
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,10 @@ | ||
'use strict'; | ||
|
||
const isReady = Symbol.for('egg_tracer_is_ready'); | ||
|
||
module.exports = app => { | ||
app.ready(() => { | ||
app[isReady] = true; | ||
}); | ||
}; | ||
|
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,22 @@ | ||
'use strict'; | ||
|
||
const cacheTracer = Symbol('before_ready_tracer'); | ||
const isReady = Symbol.for('egg_tracer_is_ready'); | ||
|
||
module.exports = { | ||
get tracer() { | ||
if (this[isReady]) { | ||
return new this.config.tracer.Class({ | ||
app: this, | ||
}); | ||
} | ||
|
||
if (!this[cacheTracer]) { | ||
this[cacheTracer] = new this.config.tracer.Class({ | ||
app: this, | ||
}); | ||
} | ||
|
||
return this[cacheTracer]; | ||
}, | ||
}; |
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,34 @@ | ||
'use strict'; | ||
|
||
const mm = require('egg-mock'); | ||
const assert = require('assert'); | ||
|
||
describe('test/error.tracer.test.js', () => { | ||
let app; | ||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/error-tracer-test', | ||
}); | ||
return app.ready(); | ||
}); | ||
|
||
after(() => app.close()); | ||
|
||
afterEach(mm.restore); | ||
|
||
it('should get app, agent tracer is null', function* () { | ||
assert(app.appBeforeReadyTracers.length === 0); | ||
assert(app.agent.agentBeforeReadyTracers.length === 0); | ||
|
||
assert(app.appAfterReadyTracers.length === 0); | ||
assert(app.agent.agentAfterReadyTracers.length === 0); | ||
}); | ||
|
||
it('should GET /', () => { | ||
return app.httpRequest() | ||
.get('/') | ||
.expect('x-trace-id', /\w{13}/) | ||
.expect('hi, egg') | ||
.expect(200); | ||
}); | ||
}); |
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,29 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = agent => { | ||
const agentBeforeReadyTracers = []; | ||
const agentAfterReadyTracers = []; | ||
|
||
agent.agentBeforeReadyTracers = agentBeforeReadyTracers; | ||
agent.agentAfterReadyTracers = agentAfterReadyTracers; | ||
|
||
try { | ||
agentBeforeReadyTracers.push(agent.tracer); | ||
agentBeforeReadyTracers.push(agent.tracer); | ||
agentBeforeReadyTracers.push(agent.tracer); | ||
|
||
} catch (e) { | ||
agent.coreLogger.warn(e); | ||
} | ||
|
||
agent.ready(() => { | ||
try { | ||
agentAfterReadyTracers.push(agent.tracer); | ||
agentAfterReadyTracers.push(agent.tracer); | ||
agentAfterReadyTracers.push(agent.tracer); | ||
} catch (e) { | ||
agent.coreLogger.warn(e); | ||
} | ||
}); | ||
}; |
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,29 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = app => { | ||
const appBeforeReadyTracers = []; | ||
const appAfterReadyTracers = []; | ||
|
||
app.appBeforeReadyTracers = appBeforeReadyTracers; | ||
app.appAfterReadyTracers = appAfterReadyTracers; | ||
|
||
try { | ||
appBeforeReadyTracers.push(app.tracer); | ||
appBeforeReadyTracers.push(app.tracer); | ||
appBeforeReadyTracers.push(app.tracer); | ||
|
||
} catch (e) { | ||
app.coreLogger.warn(e); | ||
} | ||
|
||
app.ready(() => { | ||
try { | ||
appAfterReadyTracers.push(app.tracer); | ||
appAfterReadyTracers.push(app.tracer); | ||
appAfterReadyTracers.push(app.tracer); | ||
} catch (e) { | ||
app.coreLogger.warn(e); | ||
} | ||
}); | ||
}; |
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,11 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = app => { | ||
app.get('/', function* () { | ||
assert(this.traceId === this.traceId); | ||
this.set('x-trace-id', this.traceId); | ||
this.body = 'hi, egg'; | ||
}); | ||
}; |
22 changes: 22 additions & 0 deletions
22
test/fixtures/apps/error-tracer-test/config/config.default.js
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,22 @@ | ||
'use strict'; | ||
|
||
/** | ||
* tracer config | ||
* @member Config#tracer | ||
* @property {Tracer} Class - tracer class name | ||
*/ | ||
exports.tracer = { | ||
Class: class Tracer { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.name = ctx.locals.name; | ||
} | ||
|
||
get traceId() { | ||
if (!this.TRACE_ID) { | ||
this.TRACE_ID = Date.now(); | ||
} | ||
return this.TRACE_ID; | ||
} | ||
}, | ||
}; |
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,3 @@ | ||
{ | ||
"name": "error-tracer-test" | ||
} |
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,20 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = agent => { | ||
const agentBeforeReadyTracers = []; | ||
const agentAfterReadyTracers = []; | ||
|
||
agentBeforeReadyTracers.push(agent.tracer); | ||
agentBeforeReadyTracers.push(agent.tracer); | ||
agentBeforeReadyTracers.push(agent.tracer); | ||
|
||
agent.agentBeforeReadyTracers = agentBeforeReadyTracers; | ||
agent.agentAfterReadyTracers = agentAfterReadyTracers; | ||
|
||
agent.ready(() => { | ||
agentAfterReadyTracers.push(agent.tracer); | ||
agentAfterReadyTracers.push(agent.tracer); | ||
agentAfterReadyTracers.push(agent.tracer); | ||
}); | ||
}; |
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,20 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = app => { | ||
const appBeforeReadyTracers = []; | ||
const appAfterReadyTracers = []; | ||
|
||
appBeforeReadyTracers.push(app.tracer); | ||
appBeforeReadyTracers.push(app.tracer); | ||
appBeforeReadyTracers.push(app.tracer); | ||
|
||
app.appBeforeReadyTracers = appBeforeReadyTracers; | ||
app.appAfterReadyTracers = appAfterReadyTracers; | ||
|
||
app.ready(() => { | ||
appAfterReadyTracers.push(app.tracer); | ||
appAfterReadyTracers.push(app.tracer); | ||
appAfterReadyTracers.push(app.tracer); | ||
}); | ||
}; |
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,85 @@ | ||
'use strict'; | ||
|
||
const mm = require('egg-mock'); | ||
const assert = require('assert'); | ||
|
||
describe('test/tracer.test.js', () => { | ||
let app; | ||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/plugin-test', | ||
}); | ||
return app.ready(); | ||
}); | ||
|
||
after(() => app.close()); | ||
|
||
afterEach(mm.restore); | ||
|
||
it('should get app, agent tracer', function* () { | ||
assert(app[Symbol.for('egg_tracer_is_ready')]); | ||
assert(app.agent[Symbol.for('egg_tracer_is_ready')]); | ||
|
||
let [ appTracer_1, appTracer_2, appTracer_3 ] = app.appBeforeReadyTracers; | ||
let [ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentBeforeReadyTracers; | ||
|
||
assert(appTracer_1 === appTracer_2); | ||
assert(appTracer_1 === appTracer_3); | ||
|
||
assert(appTracer_1.traceId); | ||
assert(appTracer_2.traceId); | ||
assert(appTracer_3.traceId); | ||
|
||
assert(appTracer_1 === appTracer_2); | ||
assert(appTracer_1 === appTracer_3); | ||
|
||
assert(appTracer_1.traceId === appTracer_2.traceId); | ||
assert(appTracer_1.traceId === appTracer_3.traceId); | ||
|
||
assert(agentTracer_1.traceId); | ||
assert(agentTracer_2.traceId); | ||
assert(agentTracer_3.traceId); | ||
|
||
assert(agentTracer_1 === agentTracer_2); | ||
assert(agentTracer_1 === agentTracer_3); | ||
|
||
assert(agentTracer_1.traceId); | ||
assert(agentTracer_2.traceId); | ||
assert(agentTracer_3.traceId); | ||
|
||
assert(agentTracer_1.traceId === agentTracer_2.traceId); | ||
assert(agentTracer_1.traceId === agentTracer_3.traceId); | ||
|
||
// app ready | ||
[ appTracer_1, appTracer_2, appTracer_3 ] = app.appAfterReadyTracers; | ||
|
||
assert(appTracer_1 !== appTracer_2); | ||
assert(appTracer_1 !== appTracer_3); | ||
|
||
assert(appTracer_1.traceId); | ||
assert(appTracer_2.traceId); | ||
assert(appTracer_3.traceId); | ||
|
||
assert(appTracer_1 !== appTracer_2); | ||
assert(appTracer_1 !== appTracer_3); | ||
|
||
assert(appTracer_1.traceId !== appTracer_2.traceId); | ||
assert(appTracer_1.traceId !== appTracer_3.traceId); | ||
|
||
// agent ready | ||
[ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentAfterReadyTracers; | ||
assert(agentTracer_1.traceId); | ||
assert(agentTracer_2.traceId); | ||
assert(agentTracer_3.traceId); | ||
|
||
assert(agentTracer_1 !== agentTracer_2); | ||
assert(agentTracer_1 !== agentTracer_3); | ||
|
||
assert(agentTracer_1.traceId); | ||
assert(agentTracer_2.traceId); | ||
assert(agentTracer_3.traceId); | ||
|
||
assert(agentTracer_1.traceId !== agentTracer_2.traceId); | ||
assert(agentTracer_1.traceId !== agentTracer_3.traceId); | ||
}); | ||
}); |