Skip to content

Commit

Permalink
Merge pull request #118 from JaniAnttonen/development
Browse files Browse the repository at this point in the history
Merge changes with fixes to timestamp handling to master
  • Loading branch information
JaniAnttonen authored Aug 22, 2022
2 parents 6c7b3a1 + 7bec0a4 commit ea6e8f6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 46 deletions.
2 changes: 1 addition & 1 deletion examples/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
image: grafana/promtail:latest
volumes:
- /var/log:/var/log
command: -config.file=/etc/promtail/docker-config.yaml
command: -config.file=/etc/promtail/config.yml
networks:
- loki

Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ class LokiTransport extends Transport {

// build custom labels if provided
let lokiLabels = { level: level }
lokiLabels = Object.assign(lokiLabels, labels)

if (this.labels) {
lokiLabels = Object.assign(lokiLabels, this.labels)
} else {
lokiLabels.job = label
}

lokiLabels = Object.assign(lokiLabels, labels)

// follow the format provided
const line = this.useCustomFormat
? info[MESSAGE]
Expand All @@ -75,11 +76,19 @@ class LokiTransport extends Transport {
lokiLabels = Object.fromEntries(Object.entries(lokiLabels).map(([key, value]) => [key, value ? value.toString() : value]))

// Construct the log to fit Grafana Loki's accepted format
let ts
if (timestamp) {
ts = new Date(timestamp)
ts = isNaN(ts) ? Date.now() : ts.valueOf()
} else {
ts = Date.now()
}

const logEntry = {
labels: lokiLabels,
entries: [
{
ts: timestamp || Date.now().valueOf(),
ts,
line
}
]
Expand Down
50 changes: 29 additions & 21 deletions package-lock.json

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

24 changes: 2 additions & 22 deletions test/custom-labels-lines.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
const { createLogger, format } = require('winston')
const LokiTransport = require('winston-loki')

// from: https://jestjs.io/docs/en/expect#expectextendmatchers
expect.extend({
toBeWithinRange (received, floor, ceiling) {
const pass = received >= floor && received <= ceiling
if (pass) {
return {
message: () =>
`expected ${received} not to be within range ${floor} - ${ceiling}`,
pass: true
}
} else {
return {
message: () =>
`expected ${received} to be within range ${floor} - ${ceiling}`,
pass: false
}
}
}
})

describe('Integration tests', function () {
it('Winston should accept LokiTransport', function () {
jest.useFakeTimers()
const lokiTransport = new LokiTransport({
host: 'http://localhost',
level: 'debug',
Expand All @@ -46,7 +27,6 @@ describe('Integration tests', function () {

const testMessage = 'testMessage'
const testLabel = 'testLabel'
const now = Date.now()
logger.debug({ message: testMessage, labels: { customLabel: testLabel } })
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(
Expand All @@ -55,7 +35,7 @@ describe('Integration tests', function () {
labels: { level: 'debug', module: 'name', app: 'appname', customLabel: testLabel },
entries: [{
line: `[name] ${testMessage}`,
ts: expect.toBeWithinRange(now - 5, now + 5)
ts: Date.now()
}]
})
})
Expand Down
43 changes: 43 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,47 @@ describe('Integration tests', function () {
JSON.stringify(lokiTransport.batcher.batch.streams[0]).replace(/\s/g, '')
).toEqual(fixtures.logs_mapped_before[2].replace(/\s/g, ''))
})
describe('custom timestamp', () => {
it('LokiTransport should convert provided timestamp to number and use it for Loki format', function () {
const lokiTransport = new LokiTransport(fixtures.options_json)
const timestampString = new Date(fixtures.logs[0].timestamp).toISOString()
const log = { ...fixtures.logs[0], timestamp: timestampString }
lokiTransport.log(log, () => {})
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
{
entries: [{
line: 'testings ',
ts: 1546977515828
}],
labels: {
job: 'test',
level: 'info'
}
}
)
})
it('LokiTransport should current time for timestamp in Loki format ' +
'when provided timestamp cannot be converted to a valid date',
function () {
jest.useFakeTimers()
const lokiTransport = new LokiTransport(fixtures.options_json)
const invalidTimestamp = '12:00:00'
const log = { ...fixtures.logs[0], timestamp: invalidTimestamp }
lokiTransport.log(log, () => {})
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
{
entries: [{
line: 'testings ',
ts: Date.now()
}],
labels: {
job: 'test',
level: 'info'
}
}
)
})
})
})

0 comments on commit ea6e8f6

Please sign in to comment.