Skip to content

Commit

Permalink
Merge pull request openzipkin#233 from Zocdoc/issue_210
Browse files Browse the repository at this point in the history
record an error if there was one in the express middleware
  • Loading branch information
jcchavezs committed Jul 8, 2018
2 parents fdabdf1 + b706671 commit 5ec1c8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ module.exports = function expressMiddleware({tracer, serviceName, port = 0}) {

res.on('finish', () => {
tracer.scoped(() => {
instrumentation.recordResponse(id, res.statusCode);
const error = res.statusCode >= 400 ? res.statusCode : null;

instrumentation.recordResponse(id, res.statusCode, error);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,48 @@ describe('express middleware - integration test', () => {
});
});
});

it('should mark 500 respones as errors', done => {
const record = sinon.spy();
const recorder = {record};
const ctxImpl = new ExplicitContext();
const tracer = new Tracer({recorder, ctxImpl});

ctxImpl.scoped(() => {
const app = express();
app.use(middleware({
tracer,
serviceName: 'service-a'
}));
app.get('/error', (req, res) => {
tracer.recordBinary('message', 'hello from within app');
res.status(500).send({status: 'An Error Occurred'});
});
const server = app.listen(0, () => {
const port = server.address().port;
const urlPath = `http://127.0.0.1:${port}/error`;

fetch(urlPath, {
method: 'get'
}).then(res => {
server.close();

expect(res.status).to.equal(500);

const annotations = record.args.map(args => args[0]);

expect(annotations[6].annotation.key).to.equal('http.status_code');
expect(annotations[6].annotation.value).to.equal('500');
expect(annotations[7].annotation.key).to.equal('error');
expect(annotations[7].annotation.value).to.equal('500');

done();
})
.catch(err => {
server.close();
done(err);
});
});
});
});
});

0 comments on commit 5ec1c8d

Please sign in to comment.