Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
feat: add client.sent property (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Aug 6, 2018
1 parent be90d53 commit b71d686
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +92,7 @@ Client.prototype._write = function (obj, enc, cb) {
this._chopper.chop(cb)
}
} else {
this._received++
this._stream.write(obj, cb)
}
}
Expand Down Expand Up @@ -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()
Expand Down
35 changes: 35 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit b71d686

Please sign in to comment.