Skip to content

Commit

Permalink
update the options in readme, add option for clearing any logs on err…
Browse files Browse the repository at this point in the history
…or so there's no memory hogging
  • Loading branch information
Jani Anttonen committed Jan 29, 2019
1 parent 788861a commit 0b1f797
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,15 @@ A Grafana Loki transport for the nodejs logging library Winston.
This Winston transport is used similarly to other Winston transports. Require winston and define a new LokiTransport() inside its options when creating it.

### Options
LokiTransport() takes a Javascript object as an input. These are the options that are available:

>`host`
The URL of the Grafana Loki server.
It should contain everything from the protocol to the port.

>`interval`
*optional*

The interval at which the transport sends batched logs to Loki. **In seconds.**

>`json`
*optional*

Switch to JSON transport instead of Protobuf.
LokiTransport() takes a Javascript object as an input. These are the options that are available, __required in bold__:

| **Parameter** | **Description** | **Example** |
| ---------------- | --------------------------------------------------------- | -------------------------------- |
| __`host`__ | URL for Grafana Loki | http://localhost:3100 |
| `interval` | The interval at which batched logs are sent in seconds | 30 |
| `json` | Use JSON instead of Protobuf for transport | true |
| `batching` | If batching is not used, the logs are sent as they come | true |
| `clearOnError` | Discard any logs that result in an error during transport | true |

### Example
```js
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = class LokiTransport extends Transport {
host: options.host,
interval: options.interval,
json: options.json,
batching: options.batching
batching: options.batching,
clearOnError: options.clearOnError
})
options.batching && this.batcher.run()
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-loki",
"version": "2.1.1",
"version": "2.1.2",
"description": "A Winston transport for Grafana Loki",
"keywords": [
"winston",
Expand Down
29 changes: 15 additions & 14 deletions src/batcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ module.exports = class Batcher {
logEntry = protoHelpers.createProtoTimestamps(logEntry)
}
this.sendBatchToLoki(logEntry)
return
}
if (this.options.json) {
this.batch.streams.push(logEntry)
} else {
const { streams } = this.batch
logEntry = protoHelpers.createProtoTimestamps(logEntry)
const match = streams.findIndex(
stream => stream.labels === logEntry.labels
)
if (match > -1) {
logEntry.entries.forEach(entry => {
streams[match].entries.push(entry)
})
if (this.options.json) {
this.batch.streams.push(logEntry)
} else {
streams.push(logEntry)
const { streams } = this.batch
logEntry = protoHelpers.createProtoTimestamps(logEntry)
const match = streams.findIndex(
stream => stream.labels === logEntry.labels
)
if (match > -1) {
logEntry.entries.forEach(entry => {
streams[match].entries.push(entry)
})
} else {
streams.push(logEntry)
}
}
}
}
Expand Down Expand Up @@ -101,6 +101,7 @@ module.exports = class Batcher {
resolve()
})
.catch(err => {
this.options.clearOnError && this.clearBatch()
reject(err)
})
}
Expand Down
20 changes: 20 additions & 0 deletions test/batcher.json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe('Batcher tests with JSON transport', function () {
}
expect(batcher.batch.streams.length).toBe(1)
})
it('Should reject promise and clear batch on unsuccessful send if clearOnError is enabled', async function () {
const options = JSON.parse(JSON.stringify(fixtures.options_json))
options.clearOnError = true
batcher = new Batcher(options)

const errorObject = {
statusCode: 404
}
got.post.rejects(errorObject)
batcher.pushLogEntry(JSON.parse(fixtures.logs_mapped[0]))
expect(batcher.batch.streams.length).toBe(1)

try {
await batcher.sendBatchToLoki()
} catch (error) {
expect(error.statusCode).toBe(404)
}

expect(batcher.batch.streams.length).toBe(0)
})
it('Should fail if the batch is not constructed correctly', async function () {
const responseObject = {
statusCode: 200,
Expand Down

0 comments on commit 0b1f797

Please sign in to comment.