Skip to content

Commit

Permalink
Added 'call' event that emits on every call
Browse files Browse the repository at this point in the history
  • Loading branch information
fotis-image committed Dec 14, 2014
1 parent 76b6a5d commit b1328b7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,63 @@ console.log(data);
// }
// }
```

## Events

### call

Α `'call'` event will be emitted when a request is completed (after the response is sent). It can be used to integrate collected data with other analytics systems like [Elasticsearch](http://www.elasticsearch.org) or [StatsD](https://github.com/etsy/statsd/).

The payload of the event will contain information about this specific request:

```javascript
var express = require('express');
var RestAnalytics = require('rest-analytics');

var app = express();
var analytics = new RestAnalytics();
data.analytics.on('call', function(payload) {
console.log(payload);
});

app.use(analytics.middleware());

```

A payload example json is:

| Tables | Are | Cool |
|---------------|---------------|----------|
|request|a|b|
|response|a|b|


```javascript
{
"request": {
"path": "/user",
"ip": "127.0.0.1",
"method": "GET",
"headers": [
{
"key": "header",
"value": "header value"
}
],
"query": {},
"parameters": []
},
"response": {
"status": 200,
"headers": [
{
"key": "header",
"value": "header value"
}
]
},
"timestamp": 1418557961891, // timestamp of call (in ms)
"duration": 0.40668 // duration of the call (in ms)
}

```
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ describe('GET /user', function() {
assert.equal(data.analytics.analytics('GET', '/user').count, 1);
done();
});


});
});

describe('GET /user', function() {
it('should increase route count to 2', function(done) {
var data = appWithAnalytics();
supertest(data.app)
Expand All @@ -52,9 +48,7 @@ describe('GET /user', function() {


});
});

describe('GET /user', function() {
it('should be able to get data providing method and path in different parameteres', function(done) {
var data = appWithAnalytics();
supertest(data.app)
Expand All @@ -67,7 +61,19 @@ describe('GET /user', function() {
done();
});
});
});

it('should emit a call event after a REST call', function(done) {
var data = appWithAnalytics();
data.analytics.on('call', function(message) {
console.log(JSON.stringify(message, null, 2));
done();
});

supertest(data.app)
.get('/user')
.end(function(err, res){
if (err) throw err;
});
});
});

0 comments on commit b1328b7

Please sign in to comment.