-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): structured logging (#14973)
- Loading branch information
1 parent
f651b47
commit eafb8c6
Showing
82 changed files
with
3,390 additions
and
896 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 gatsbyjs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Structured Logging | ||
|
||
## Problems | ||
|
||
- Concurrent tests will need to generate a unique build folder name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
const { spawn } = require(`child_process`) | ||
const path = require(`path`) | ||
|
||
jest.setTimeout(100000) | ||
|
||
const gatsbyBin = path.join( | ||
`node_modules`, | ||
`gatsby`, | ||
`dist`, | ||
`bin`, | ||
`gatsby.js` | ||
) | ||
|
||
describe(`Activities`, () => { | ||
let gatsbyProcess | ||
let events = [] | ||
|
||
beforeAll(async done => { | ||
gatsbyProcess = spawn(gatsbyBin, [`develop`], { | ||
stdio: [`ignore`, `ignore`, `ignore`, `ipc`], | ||
env: { | ||
...process.env, | ||
NODE_ENV: `development`, | ||
ENABLE_GATSBY_REFRESH_ENDPOINT: true, | ||
FAILING_ACTIVITY: true, | ||
}, | ||
}) | ||
|
||
gatsbyProcess.on(`message`, msg => { | ||
// console.log(msg) | ||
events.push(msg) | ||
// we are ready for tests | ||
if ( | ||
msg.action && | ||
msg.action.type === `SET_STATUS` && | ||
msg.action.payload !== `IN_PROGRESS` | ||
) { | ||
done() | ||
} | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
gatsbyProcess.kill() | ||
}) | ||
|
||
it.todo(`emits actions with a timestamp`) | ||
|
||
/* There are two typical lifecycles for activities. Most activities have | ||
- Start | ||
- Update (Optional) | ||
- End | ||
These tests assert whether events are correctly emitted for the complete lifecycle. | ||
*/ | ||
it(`emit start, update and end events for a successful activity`, async () => { | ||
const activityEvents = events.filter( | ||
event => | ||
event.type === `LOG_ACTION` && | ||
event.action.payload.id === `Successful activity` | ||
) | ||
|
||
// console.log(util.inspect(activityEvents, { depth: null })) | ||
|
||
const activityUuid = activityEvents[0].action.payload.uuid | ||
|
||
expect(activityEvents).toEqual([ | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_START`, | ||
payload: expect.objectContaining({ | ||
id: `Successful activity`, | ||
uuid: activityUuid, | ||
total: 100, | ||
}), | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_UPDATE`, | ||
payload: expect.objectContaining({ | ||
id: `Successful activity`, | ||
uuid: activityUuid, | ||
current: 50, | ||
}), | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_END`, | ||
payload: expect.objectContaining({ | ||
id: `Successful activity`, | ||
uuid: activityUuid, | ||
status: `SUCCESS`, | ||
}), | ||
}), | ||
}), | ||
]) | ||
}) | ||
|
||
it(`emit start, update and end events for a failed activity`, async () => { | ||
const activityEvents = events.filter( | ||
event => | ||
event.type === `LOG_ACTION` && | ||
event.action.payload.id === `Failing activity` | ||
) | ||
|
||
// console.log(util.inspect(activityEvents, { depth: null })) | ||
|
||
const activityUuid = activityEvents[0].action.payload.uuid | ||
|
||
expect(activityEvents).toEqual([ | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_START`, | ||
payload: expect.objectContaining({ | ||
id: `Failing activity`, | ||
uuid: activityUuid, | ||
total: 100, | ||
}), | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_UPDATE`, | ||
payload: expect.objectContaining({ | ||
id: `Failing activity`, | ||
uuid: activityUuid, | ||
current: 75, | ||
}), | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `ACTIVITY_END`, | ||
payload: expect.objectContaining({ | ||
id: `Failing activity`, | ||
uuid: activityUuid, | ||
status: `FAILED`, | ||
}), | ||
}), | ||
}), | ||
]) | ||
}) | ||
;[`success`, `info`, `warn`, `log`, `error`].forEach(level => { | ||
// success, info and log all emit `INFO` | ||
const mapActionToLevel = { | ||
success: `INFO`, | ||
info: `INFO`, | ||
warn: `WARNING`, | ||
log: `INFO`, | ||
error: `ERROR`, | ||
} | ||
|
||
it(`emits LOG_ACTION for ${level}`, async () => { | ||
const event = events.find( | ||
e => | ||
e.type === `LOG_ACTION` && | ||
e.action.type === `LOG` && | ||
mapActionToLevel[level] === e.action.payload.level && | ||
level === e.action.payload.text | ||
) | ||
expect(event).toBeDefined() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { spawn } = require(`child_process`) | ||
const path = require(`path`) | ||
|
||
jest.setTimeout(100000) | ||
|
||
const gatsbyBin = path.join( | ||
`node_modules`, | ||
`gatsby`, | ||
`dist`, | ||
`bin`, | ||
`gatsby.js` | ||
) | ||
|
||
describe(`Panic`, () => { | ||
let gatsbyProcess | ||
let events = [] | ||
|
||
beforeEach(async () => { | ||
gatsbyProcess = spawn(gatsbyBin, [`build`], { | ||
// inherit lets us see logs in console | ||
// stdio: [`inherit`, `inherit`, `inherit`, `ipc`], | ||
stdio: [`ignore`, `ignore`, `ignore`, `ipc`], | ||
env: { | ||
...process.env, | ||
NODE_ENV: `production`, | ||
ENABLE_GATSBY_REFRESH_ENDPOINT: true, | ||
PANIC_ON_BUILD: true, | ||
}, | ||
}) | ||
|
||
await new Promise(resolve => { | ||
gatsbyProcess.on(`message`, msg => { | ||
events.push(msg) | ||
}) | ||
|
||
gatsbyProcess.on(`exit`, exitCode => { | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
|
||
it(`emits SET_STATUS FAILURE`, async () => { | ||
expect(events).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `SET_STATUS`, | ||
payload: `FAILED`, | ||
}), | ||
}), | ||
]) | ||
) | ||
}) | ||
it(`emits LOG`, async () => { | ||
expect(events).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: `LOG_ACTION`, | ||
action: expect.objectContaining({ | ||
type: `LOG`, | ||
payload: expect.objectContaining({ | ||
level: `ERROR`, | ||
text: `Your house is on fire`, | ||
}), | ||
}), | ||
}), | ||
]) | ||
) | ||
}) | ||
}) |
Oops, something went wrong.