From b71d6863281d41741c34addd6e3b16f5f15b42c8 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 2 Aug 2018 10:42:25 +0200 Subject: [PATCH] feat: add client.sent property (#7) --- README.md | 6 ++++++ index.js | 4 ++++ test/test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/README.md b/README.md index 292540b..c71b407 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,12 @@ The client is not closed when the `error` event is emitted. The `finish` event is emitted after the `client.end()` method has been called, and all data has been flushed to the underlying system. +### `client.sent` + +An integer indicating the number of events (spans, transactions, or errors) +sent by the client. An event is considered sent when the HTTP request +used to transmit it have ended. + ### `client.sendSpan(span[, callback])` Send a span to the APM Server. diff --git a/index.js b/index.js index 1b5e44e..95be2e5 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,8 @@ function Client (opts) { if (this._writableState.ending === false) this.destroy() } + this._received = 0 // number of events given to the client for reporting + this.sent = 0 // number of events written to the socket this._active = false this._destroyed = false this._onflushed = null @@ -90,6 +92,7 @@ Client.prototype._write = function (obj, enc, cb) { this._chopper.chop(cb) } } else { + this._received++ this._stream.write(obj, cb) } } @@ -205,6 +208,7 @@ function onStream (opts, client, onerror) { // would not get it here as the internal error listener would have // been removed and the stream would throw the error instead + client.sent = client._received client._active = false if (client._onflushed) { client._onflushed() diff --git a/test/test.js b/test/test.js index fb743d5..f15ca1d 100644 --- a/test/test.js +++ b/test/test.js @@ -469,6 +469,41 @@ test('client.end(callback)', function (t) { }) }) +test('client.sent', function (t) { + t.plan(8) + let client + let requests = 0 + const server = APMServer(function (req, res) { + requests++ + t.equal(client.sent, 3 * (requests - 1)) + req.resume() + req.on('end', function () { + t.equal(client.sent, 3 * requests) + res.end() + if (requests === 2) { + server.close() + t.end() + } + }) + }).client(function (_client) { + client = _client + client.sendError({foo: 42}) + client.sendSpan({foo: 42}) + client.sendTransaction({foo: 42}) + t.equal(client.sent, 0) + client.flush(function () { + t.equal(client.sent, 3) + client.sendError({foo: 42}) + client.sendSpan({foo: 42}) + client.sendTransaction({foo: 42}) + t.equal(client.sent, 3) + client.flush(function () { + t.equal(client.sent, 6) + }) + }) + }) +}) + /** * Side effects */