Skip to content

Commit

Permalink
Merge pull request #79 from JaniAnttonen/development
Browse files Browse the repository at this point in the history
Merge two bug fixes to master
  • Loading branch information
JaniAnttonen authored Oct 7, 2021
2 parents 80bb0f6 + ae37b3f commit 0eb49ec
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class LokiTransport extends Transport {
rest && Object.keys(rest).length > 0 ? JSON.stringify(rest) : ''
}`

// Make sure all label values are strings
lokiLabels = JSON.parse(JSON.stringify(lokiLabels, (key, value) => value ? value.toString() : value))

// Construct the log to fit Grafana Loki's accepted format
const logEntry = {
labels: lokiLabels,
Expand All @@ -81,7 +84,10 @@ class LokiTransport extends Transport {
}

// Pushes the log to the batcher
this.batcher.pushLogEntry(logEntry)
this.batcher.pushLogEntry(logEntry).catch(err => {
// eslint-disable-next-line no-console
console.error(err)
})

// Trigger the optional callback
callback()
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"options_json": { "host": "http://localhost", "interval": 0.1, "json": true },
"options_no_batching": { "host": "http://localhost", "batching": false },
"options_protobuf": {
"host": "http://localhost",
"interval": 0.1,
Expand Down
16 changes: 16 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('Integration tests', function () {
})
expect(callback).toBe(true)
})
it('LokiTransport shouldn\'t cause unhandledPromiseRejection when batching is turned off', async function () {
let consoleError = false
// eslint-disable-next-line no-console
console.error = jest
.spyOn(console, 'error')
.mockImplementation(() => {
consoleError = true
})

const lokiTransport = new LokiTransport(fixtures.options_no_batching)
lokiTransport.log(fixtures.logs[0], () => {})

// Wait for Promise (this.batcher.pushLogEntry) be rejected
await new Promise(resolve => setTimeout(resolve, 100))
expect(consoleError).toBe(true)
})
it('LokiTransport should transfer logs to the Batcher', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
lokiTransport.log(fixtures.logs[0], () => {})
Expand Down

0 comments on commit 0eb49ec

Please sign in to comment.