Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
Report HTTP errors when flushing spans (#459)
Browse files Browse the repository at this point in the history
Signed-off-by: Espen Hovlandsdal <[email protected]>

Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
rexxars and yurishkuro authored Apr 28, 2021
1 parent a720c61 commit 1e55e22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/reporters/http_sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ export default class HTTPSender {
const req = requester(this._httpOptions, resp => {
// consume response data to free up memory
resp.resume();
SenderUtils.invokeCallback(callback, numSpans);

let error;
if (resp.statusCode >= 400) {
error = `error sending spans over HTTP: server responded with HTTP ${resp.statusCode}`;
this._logger.error(error);
}

SenderUtils.invokeCallback(callback, numSpans, error);
});

req.on('error', err => {
Expand Down
27 changes: 27 additions & 0 deletions test/http_sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,33 @@ describe('http sender', () => {
});
}).timeout(5000);

it('should log http errors when reporting', function(done) {
let loggedError = null;
const noop = () => {};
const logError = errMsg => {
loggedError = errMsg;
};

reporter = new InMemoryReporter();
tracer = new Tracer('test-service-name', reporter, new ConstSampler(true));
sender = new HTTPSender({
logger: { error: logError, info: noop },
endpoint: `http://localhost:${server.address().port}/404`,
maxSpanBatchSize: batchSize,
});
sender.setProcess(reporter._process);

let tracer = new Tracer('test-service-name', new RemoteReporter(sender), new ConstSampler(true));

tracer.startSpan('testSpan').finish();
sender.flush((numSpans, err) => {
assert.equal(numSpans, 1);
expect(err).to.have.string('error sending spans over HTTP: server responded with HTTP 404');
expect(loggedError).to.have.string('error sending spans over HTTP: server responded with HTTP 404');
tracer.close(done);
});
}).timeout(5000);

it('should handle HTTPS collectors', done => {
// Make it ignore the fact that our cert isn't valid.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
Expand Down

0 comments on commit 1e55e22

Please sign in to comment.