Skip to content

Commit ea6e8f6

Browse files
authored
Merge pull request #118 from JaniAnttonen/development
Merge changes with fixes to timestamp handling to master
2 parents 6c7b3a1 + 7bec0a4 commit ea6e8f6

File tree

5 files changed

+86
-46
lines changed

5 files changed

+86
-46
lines changed

examples/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
image: grafana/promtail:latest
1717
volumes:
1818
- /var/log:/var/log
19-
command: -config.file=/etc/promtail/docker-config.yaml
19+
command: -config.file=/etc/promtail/config.yml
2020
networks:
2121
- loki
2222

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ class LokiTransport extends Transport {
5656

5757
// build custom labels if provided
5858
let lokiLabels = { level: level }
59-
lokiLabels = Object.assign(lokiLabels, labels)
6059

6160
if (this.labels) {
6261
lokiLabels = Object.assign(lokiLabels, this.labels)
6362
} else {
6463
lokiLabels.job = label
6564
}
6665

66+
lokiLabels = Object.assign(lokiLabels, labels)
67+
6768
// follow the format provided
6869
const line = this.useCustomFormat
6970
? info[MESSAGE]
@@ -75,11 +76,19 @@ class LokiTransport extends Transport {
7576
lokiLabels = Object.fromEntries(Object.entries(lokiLabels).map(([key, value]) => [key, value ? value.toString() : value]))
7677

7778
// Construct the log to fit Grafana Loki's accepted format
79+
let ts
80+
if (timestamp) {
81+
ts = new Date(timestamp)
82+
ts = isNaN(ts) ? Date.now() : ts.valueOf()
83+
} else {
84+
ts = Date.now()
85+
}
86+
7887
const logEntry = {
7988
labels: lokiLabels,
8089
entries: [
8190
{
82-
ts: timestamp || Date.now().valueOf(),
91+
ts,
8392
line
8493
}
8594
]

package-lock.json

Lines changed: 29 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/custom-labels-lines.test.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
const { createLogger, format } = require('winston')
22
const LokiTransport = require('winston-loki')
33

4-
// from: https://jestjs.io/docs/en/expect#expectextendmatchers
5-
expect.extend({
6-
toBeWithinRange (received, floor, ceiling) {
7-
const pass = received >= floor && received <= ceiling
8-
if (pass) {
9-
return {
10-
message: () =>
11-
`expected ${received} not to be within range ${floor} - ${ceiling}`,
12-
pass: true
13-
}
14-
} else {
15-
return {
16-
message: () =>
17-
`expected ${received} to be within range ${floor} - ${ceiling}`,
18-
pass: false
19-
}
20-
}
21-
}
22-
})
23-
244
describe('Integration tests', function () {
255
it('Winston should accept LokiTransport', function () {
6+
jest.useFakeTimers()
267
const lokiTransport = new LokiTransport({
278
host: 'http://localhost',
289
level: 'debug',
@@ -46,7 +27,6 @@ describe('Integration tests', function () {
4627

4728
const testMessage = 'testMessage'
4829
const testLabel = 'testLabel'
49-
const now = Date.now()
5030
logger.debug({ message: testMessage, labels: { customLabel: testLabel } })
5131
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
5232
expect(
@@ -55,7 +35,7 @@ describe('Integration tests', function () {
5535
labels: { level: 'debug', module: 'name', app: 'appname', customLabel: testLabel },
5636
entries: [{
5737
line: `[name] ${testMessage}`,
58-
ts: expect.toBeWithinRange(now - 5, now + 5)
38+
ts: Date.now()
5939
}]
6040
})
6141
})

test/transport.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,47 @@ describe('Integration tests', function () {
9090
JSON.stringify(lokiTransport.batcher.batch.streams[0]).replace(/\s/g, '')
9191
).toEqual(fixtures.logs_mapped_before[2].replace(/\s/g, ''))
9292
})
93+
describe('custom timestamp', () => {
94+
it('LokiTransport should convert provided timestamp to number and use it for Loki format', function () {
95+
const lokiTransport = new LokiTransport(fixtures.options_json)
96+
const timestampString = new Date(fixtures.logs[0].timestamp).toISOString()
97+
const log = { ...fixtures.logs[0], timestamp: timestampString }
98+
lokiTransport.log(log, () => {})
99+
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
100+
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
101+
{
102+
entries: [{
103+
line: 'testings ',
104+
ts: 1546977515828
105+
}],
106+
labels: {
107+
job: 'test',
108+
level: 'info'
109+
}
110+
}
111+
)
112+
})
113+
it('LokiTransport should current time for timestamp in Loki format ' +
114+
'when provided timestamp cannot be converted to a valid date',
115+
function () {
116+
jest.useFakeTimers()
117+
const lokiTransport = new LokiTransport(fixtures.options_json)
118+
const invalidTimestamp = '12:00:00'
119+
const log = { ...fixtures.logs[0], timestamp: invalidTimestamp }
120+
lokiTransport.log(log, () => {})
121+
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
122+
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
123+
{
124+
entries: [{
125+
line: 'testings ',
126+
ts: Date.now()
127+
}],
128+
labels: {
129+
job: 'test',
130+
level: 'info'
131+
}
132+
}
133+
)
134+
})
135+
})
93136
})

0 commit comments

Comments
 (0)