Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pino.transport() #1003

Merged
merged 66 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a2840c8
Basic transport implemented on top of ThreadStream
mcollina Apr 7, 2021
b2404c9
Support module transports
mcollina Apr 7, 2021
6b7d7bb
Skip package test on win32
mcollina Apr 7, 2021
b3b537a
Ire-enabled failing test on windows
mcollina Apr 7, 2021
b3e4178
full cov report
mcollina Apr 7, 2021
ec34df7
Revert "full cov report"
mcollina Apr 7, 2021
ed8cfd5
ignore lines unreacheable on Windows
mcollina Apr 7, 2021
e1c72b9
Update lib/transport.js
mcollina Apr 7, 2021
44c150c
Update lib/transport.js
mcollina Apr 7, 2021
246b6f0
Revert "Revert "full cov report""
mcollina Apr 7, 2021
eeca496
try out different ignore strategies
mcollina Apr 7, 2021
661de3e
Revert "Revert "Revert "full cov report"""
mcollina Apr 7, 2021
d78a30b
Works on Windows
mcollina Apr 8, 2021
c54769e
Emit 'error' if thread exits abruptly
mcollina Apr 8, 2021
2f4170e
Merge branch 'next' into transport
mcollina Apr 10, 2021
4c01c89
Implement pass-through of WorkerThread options
mcollina Apr 10, 2021
463af9d
Esm support with transport
mcollina Apr 11, 2021
aa28076
Basic multitransport code
mcollina Apr 11, 2021
2f9649f
multitransport example
mcollina Apr 26, 2021
6dd0627
merged transport and multitransport
mcollina Apr 26, 2021
2ad77ce
Increased timeout
mcollina Apr 29, 2021
465ed17
Added node v16
mcollina Apr 30, 2021
446a581
print the destination
mcollina Apr 30, 2021
8e55539
file://
mcollina Apr 30, 2021
7e8f662
Update example
mcollina May 3, 2021
cdaa18c
Merge branch 'next' into transport
mcollina May 3, 2021
63c4f49
Transports
mcollina May 19, 2021
72a9ee0
rm transport local dep from package.json
davidmarkclements May 31, 2021
b46171d
bump sonic boom
davidmarkclements May 31, 2021
153969c
bump pino-std-serializers
davidmarkclements May 31, 2021
bd28024
transport package test working without local transport dep in package…
davidmarkclements May 31, 2021
6ee9e90
typo
davidmarkclements May 31, 2021
21abdac
api mods, extra test
davidmarkclements May 31, 2021
82578e9
Updated thread-stream
mcollina Jun 2, 2021
8e679ee
fixed node v12 support
mcollina Jun 2, 2021
7e6fb61
Skip failing test on windows
mcollina Jun 2, 2021
47f0df3
print coverage report
mcollina Jun 2, 2021
89fd700
Maybe fix v12
mcollina Jun 2, 2021
ad4ee96
print the platform for debugging sake
mcollina Jun 2, 2021
bec0c08
Pass windows test
mcollina Jun 3, 2021
35596e6
print coverage data
mcollina Jun 3, 2021
ad07d04
Maybe Windows
mcollina Jun 3, 2021
9f1c699
caller refactor
mcollina Jun 3, 2021
97e12a3
Updated API
mcollina Jun 3, 2021
f0f06fc
option validation
mcollina Jun 3, 2021
02a4b68
Support modules within destinations
mcollina Jun 4, 2021
58e2914
Use full names for the options
mcollina Jun 4, 2021
ca5d4c5
windows coverage
mcollina Jun 4, 2021
f879653
coverage report
mcollina Jun 4, 2021
a791067
fix coverage on windows
mcollina Jun 4, 2021
c924b92
docs first pass
davidmarkclements Jun 6, 2021
52d5e87
target and targets
mcollina Jun 6, 2021
2bbe8c5
restore istanbul ignore
mcollina Jun 7, 2021
2e307a8
Do not check the coverage in CI
mcollina Jun 7, 2021
21f0c40
--no-check-coverage
mcollina Jun 7, 2021
7fe66d6
Merge branch 'next' into transport
mcollina Jun 7, 2021
caaec82
removed TODO
mcollina Jun 7, 2021
82ebbbf
built in docs, other doc tweaks
davidmarkclements Jun 11, 2021
2474e29
Update test/transport.test.js
mcollina Jun 12, 2021
6c49f3e
Update test/transport.test.js
mcollina Jun 12, 2021
17dc8a9
Update docs/api.md
mcollina Jul 3, 2021
8ef4409
Update docs/api.md
mcollina Jul 3, 2021
8855a33
Update docs/api.md
mcollina Jul 3, 2021
224474e
Update docs/transports.md
mcollina Jul 3, 2021
55e8b61
Update docs/transports.md
mcollina Jul 3, 2021
45cb21c
PR reviews
mcollina Jul 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/multitransport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved

const transport = require('../transport')
const { join } = require('path')

module.exports = function multitransport (definitions, workerOpts) {
return transport(join(__dirname, 'worker.js'), definitions, workerOpts)
}
35 changes: 35 additions & 0 deletions lib/multitransport/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const pino = require('../../pino.js')
const build = require('pino-abstract-transport')

module.exports = async function ({ transports }) {
transports = await Promise.all(transports.map(async (t) => {
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved
const stream = await (await import(t.module)).default(t.opts)
return {
level: t.level,
stream
}
}))
return build(process, { parse: 'lines', metadata: true })

function process (stream) {
const multi = pino.multistream(transports)
stream.on('data', function (chunk) {
const { lastTime, lastMsg, lastObj, lastLevel } = this
multi.lastLevel = lastLevel
multi.lastTime = lastTime
multi.lastMsg = lastMsg
multi.lastObj = lastObj

// TODO handle backpressure
multi.write(chunk)
})

stream.on('end', function () {
for (const transport of transports) {
transport.stream.end()
}
})
}
}
30 changes: 30 additions & 0 deletions lib/transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { createRequire } = require('module')
mcollina marked this conversation as resolved.
Show resolved Hide resolved
const caller = require('get-caller-file')
jsumners marked this conversation as resolved.
Show resolved Hide resolved
const { isAbsolute } = require('path')

const ThreadStream = require('thread-stream')

function transport (filename, workerData, workerOpts = {}) {
if (!(isAbsolute(filename) || filename.indexOf('file://') === 0)) {
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved
const callerFile = caller()
const callerRequire = createRequire(callerFile)
filename = callerRequire.resolve(filename)
}

const stream = new ThreadStream({
filename,
workerData,
workerOpts,
sync: true // TODO should this be configurable?
mcollina marked this conversation as resolved.
Show resolved Hide resolved
})

stream.on('ready', function () {
stream.unref()
})

return stream
mcollina marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = transport
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@
"tap": "^15.0.1",
"tape": "^5.0.0",
"through2": "^4.0.0",
"transport": "./test/fixtures/transport",
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved
"winston": "^3.3.3"
},
"dependencies": {
"fast-redact": "^3.0.0",
"fast-safe-stringify": "^2.0.7",
"flatstr": "^1.0.12",
"get-caller-file": "^2.0.5",
"pino-abstract-transport": "^0.1.0",
jsumners marked this conversation as resolved.
Show resolved Hide resolved
"pino-std-serializers": "^3.1.0",
"quick-format-unescaped": "^4.0.3",
"sonic-boom": "^1.0.2"
"sonic-boom": "^1.0.2",
"thread-stream": "^0.8.0"
}
}
2 changes: 2 additions & 0 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ module.exports.destination = (dest = process.stdout.fd) => {
}
}

module.exports.transport = require('./lib/transport')
module.exports.multistream = require('./lib/multistream')
module.exports.multitransport = require('./lib/multitransport')

module.exports.final = final
module.exports.levels = mappings()
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/to-file-transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const fs = require('fs')
const { once } = require('events')

async function run (opts) {
const stream = fs.createWriteStream(opts.dest)
await once(stream, 'open')
return stream
}

module.exports = run
8 changes: 8 additions & 0 deletions test/fixtures/to-file-transport.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createWriteStream } from 'fs'
import { once } from 'events'

export default async function run (opts) {
const stream = createWriteStream(opts.dest)
await once(stream, 'open')
return stream
}
12 changes: 12 additions & 0 deletions test/fixtures/transport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

mcollina marked this conversation as resolved.
Show resolved Hide resolved
const fs = require('fs')
const { once } = require('events')

async function run (opts) {
const stream = fs.createWriteStream(opts.dest)
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved
await once(stream, 'open')
return stream
}

module.exports = run
5 changes: 5 additions & 0 deletions test/fixtures/transport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "transport",
"version": "0.0.1",
"main": "./index.js"
}
52 changes: 52 additions & 0 deletions test/multitransport.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

const os = require('os')
const { join } = require('path')
const { readFile } = require('fs').promises
const { test } = require('tap')
const { watchFileCreated } = require('./helper')
const pino = require('../')

const { pid } = process
const hostname = os.hostname()

test('pino.multitransport with two files', async ({ same }) => {
const dest1 = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const dest2 = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const transport = pino.multitransport({
transports: [{
level: 'info',
module: join(__dirname, 'fixtures', 'to-file-transport.js'),
opts: { dest: dest1 }
}, {
level: 'info',
module: join(__dirname, 'fixtures', 'to-file-transport.js'),
opts: { dest: dest2 }
}]
})
const instance = pino(transport)
instance.info('hello')
await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
const result1 = JSON.parse(await readFile(dest1))
delete result1.time
same(result1, {
pid,
hostname,
level: 30,
msg: 'hello'
})
const result2 = JSON.parse(await readFile(dest2))
delete result2.time
same(result2, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})
96 changes: 96 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

const os = require('os')
const { join } = require('path')
const { readFile } = require('fs').promises
const { test } = require('tap')
const { watchFileCreated } = require('./helper')
const pino = require('../')
const url = require('url')

const { pid } = process
const hostname = os.hostname()

test('pino.transport with file', async ({ same }) => {
const dest = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino(pino.transport(join(__dirname, 'fixtures', 'to-file-transport.js'), { dest }))
instance.info('hello')
await watchFileCreated(dest)
const result = JSON.parse(await readFile(dest))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})

test('pino.transport with package', async ({ same }) => {
const dest = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino(pino.transport('transport', { dest }))
instance.info('hello')
await watchFileCreated(dest)
const result = JSON.parse(await readFile(dest))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})

test('pino.transport with file URL', async ({ same }) => {
const dest = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino(pino.transport(url.pathToFileURL(join(__dirname, 'fixtures', 'to-file-transport.js')).href, { dest }))
instance.info('hello')
await watchFileCreated(dest)
const result = JSON.parse(await readFile(dest))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})

test('pino.transport errors if file does not exists', ({ plan, pass }) => {
plan(1)
const instance = pino.transport(join(__dirname, 'fixtures', 'non-existent-file'), {}, {
stdin: true,
stdout: true,
stderr: true
})
instance.on('error', function () {
pass('error received')
})
})

test('pino.transport with esm', async ({ same }) => {
const dest = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const instance = pino(pino.transport(join(__dirname, 'fixtures', 'to-file-transport.mjs'), { dest }))
instance.info('hello')
await watchFileCreated(dest)
const result = JSON.parse(await readFile(dest))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})