Skip to content

Commit

Permalink
feat: first implement (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and fengmk2 committed Jan 20, 2017
1 parent 256c26b commit 5aeb382
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .autod.conf.js
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
test: [
'test',
'benchmark',
],
devdep: [
'egg',
'egg-ci',
'egg-bin',
'autod',
'eslint',
'eslint-config-egg',
'supertest',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
'./docs',
'./coverage',
],
registry: 'https://r.cnpmjs.org',
};
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
test/fixtures
coverage
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
.DS_Store
*.swp

11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Alibaba Group Holding Limited and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 66 additions & 1 deletion README.md
@@ -1,2 +1,67 @@
# egg-security
# egg-instrument

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-instrument.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-instrument
[travis-image]: https://img.shields.io/travis/eggjs/egg-instrument.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-instrument
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-instrument.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-instrument?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-instrument.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-instrument
[snyk-image]: https://snyk.io/test/npm/egg-instrument/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-instrument
[download-image]: https://img.shields.io/npm/dm/egg-instrument.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-instrument

Compute the duration of an operation in local environment.

## Install

```bash
$ npm i egg-instrument --save
```

## Usage

```js
// {app_root}/config/plugin.js
exports.instrument = {
enable: true,
package: 'egg-instrument',
};
```

Instrument can calculate the duration of an operation, for example

```js
// app/controller/home.js
exports.index = function* (ctx) {
const ins = ctx.instrument('service', 'home.getData');
const data = ctx.service.home.getData();
ins.end();
ctx.body = data;
};
```

Then you can see the infomation in console

```
2017-01-20 15:32:58,567 INFO 30445 [-/127.0.0.1/-/1024ms GET /] [service] home.getData 1006ms
```

If you are not in context level, you can use `app.instrument` or `agent.instrument`.

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](LICENSE)
8 changes: 8 additions & 0 deletions app/extend/agent.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
instrument(event, action) {
const ctx = this.createAnonymousContext();
return ctx.instrument(event, action);
},
};
8 changes: 8 additions & 0 deletions app/extend/application.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
instrument(event, action) {
const ctx = this.createAnonymousContext();
return ctx.instrument(event, action);
},
};
11 changes: 11 additions & 0 deletions app/extend/context.js
@@ -0,0 +1,11 @@
'use strict';

const Instrument = require('../../lib/instrument');

module.exports = {
instrument(event, action) {
const ins = new Instrument(this);
ins.start(event, action);
return ins;
},
};
15 changes: 15 additions & 0 deletions appveyor.yml
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run ci

build: off
23 changes: 23 additions & 0 deletions lib/instrument.js
@@ -0,0 +1,23 @@
'use strict';

const ms = require('ms');

class Instrument {
constructor(ctx) {
this.ctx = ctx;
this.app = ctx.app;
}

start(event, action) {
this.start = Date.now();
this.event = event;
this.action = action;
}

end() {
const duration = ms(Date.now() - this.start);
this.ctx.logger.info(`[${this.event}] ${this.action} ${duration}`);
}
}

module.exports = Instrument;
56 changes: 56 additions & 0 deletions package.json
@@ -0,0 +1,56 @@
{
"name": "egg-instrument",
"version": "1.0.0",
"description": "Compute the duration of an operation in local environment",
"eggPlugin": {
"name": "instrument",
"env": [ "local" ]
},
"keywords": [
"egg",
"eggPlugin",
"egg-plugin"
],
"dependencies": {
"ms": "^0.7.2"
},
"devDependencies": {
"autod": "^2.7.1",
"egg": "^0.8.0",
"egg-bin": "^2.0.1",
"egg-ci": "^1.1.0",
"egg-mock": "^2.1.0",
"eslint": "^3.13.1",
"eslint-config-egg": "^3.2.0",
"supertest": "^2.0.1",
"webstorm-disable-index": "^1.1.2"
},
"engines": {
"node": ">=6.0.0"
},
"scripts": {
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint .",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"files": [
"app",
"lib"
],
"ci": {
"version": "6, 7"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-instrument.git"
},
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-instrument#readme",
"author": "popomore <sakura9515@gmail.com>",
"license": "MIT"
}
10 changes: 10 additions & 0 deletions test/fixtures/apps/instrument/agent.js
@@ -0,0 +1,10 @@
'use strict';

module.exports = agent => {
agent.messenger.on('instrument', () => {
const ins = agent.instrument('agent', 'action');
setTimeout(() => {
ins.end();
}, 1000);
});
};
26 changes: 26 additions & 0 deletions test/fixtures/apps/instrument/app/router.js
@@ -0,0 +1,26 @@
'use strict';

const sleep = require('ko-sleep');


module.exports = app => {
app.get('/context-instrument', function* () {
const ins = this.instrument('context', 'action');
yield sleep(1000);
ins.end();
this.body = 'done';
});

app.get('/app-instrument', function* () {
const ins = this.app.instrument('app', 'action');
yield sleep(1000);
ins.end();
this.body = 'done';
});

app.get('/agent-instrument', function* () {
this.app.messenger.sendToAgent('instrument');
yield sleep(2000);
this.body = 'done';
});
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/instrument/package.json
@@ -0,0 +1,3 @@
{
"name": "instrument"
}
45 changes: 45 additions & 0 deletions test/instrument.test.js
@@ -0,0 +1,45 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');

describe('test/instrument.test.js', () => {
let app;
before(() => {
mm.env('local');
app = mm.cluster({
baseDir: 'apps/instrument',
});
app.debug();
return app.ready();
});
after(() => app.close());
afterEach(mm.restore);

it('should call ctx.instrument', function* () {
yield request(app.callback())
.get('/context-instrument')
.expect('done')
.expect(200);

app.expect('stdout', /\[context] action \d+ms/);
});

it('should call app.instrument', function* () {
yield request(app.callback())
.get('/app-instrument')
.expect('done')
.expect(200);

app.expect('stdout', /\[app] action \d+ms/);
});

it('should call agent.instrument', function* () {
yield request(app.callback())
.get('/agent-instrument')
.expect('done')
.expect(200);

app.expect('stdout', /\[agent] action \d+ms/);
});
});

0 comments on commit 5aeb382

Please sign in to comment.