Skip to content

Commit

Permalink
npm run format
Browse files Browse the repository at this point in the history
  • Loading branch information
Moocar committed Jul 10, 2018
1 parent 1460e2a commit 40824f7
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 65 deletions.
10 changes: 5 additions & 5 deletions docs/docs/performance-tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gatsby allows a build to be traced, enabling you to find which plugins or parts

## Running Gatsby with tracing turned on

Gatsby code is instrumented with Open Tracing, which is a general tracing API that is implementation agnostic. Therefore, you'll need to include and configure an open tracing compatible library in your application, as well as a backend to collect the trace data.
Gatsby code is instrumented with Open Tracing, which is a general tracing API that is implementation agnostic. Therefore, you'll need to include and configure an open tracing compatible library in your application, as well as a backend to collect the trace data.

The steps required to add tracing are below. Including an [example](/docs/performance-tracing/#local-zipkin-with-docker) of how to get tracing working with zipkin locally using docker

Expand Down Expand Up @@ -37,18 +37,18 @@ There are many open tracing compatible backends available. Below is an example o

[Zipkin](https://zipkin.io/) is an open source Tracing system that can be run locally using docker.

1. Add following dependencies to your site's `package.json`
1. Add following dependencies to your site's `package.json`

- [zipkin](https://www.npmjs.com/package/zipkin)
- [zipkin-javascript-opentracing](https://www.npmjs.com/package/zipkin-javascript-opentracing)
- [zipkin-transport-http](https://www.npmjs.com/package/zipkin-transport-http)

2. Run Zipkin all-in-one docker instance with `docker run -d -p 9411:9411 openzipkin/zipkin`. See [Zipkin Getting Started](https://zipkin.io/pages/quickstart.html) for more information.
2. Run Zipkin all-in-one docker instance with `docker run -d -p 9411:9411 openzipkin/zipkin`. See [Zipkin Getting Started](https://zipkin.io/pages/quickstart.html) for more information.

3. Start Gatsby `build` or `develop` with `--open-tracing-config-file` pointing at the Zipkin configuration file. An example file is provided in the gatsby project under `node_modules/gatsby/dist/utils/tracer/zipkin-local.js` that will send tracing spans to your local docker instance. E.g
3. Start Gatsby `build` or `develop` with `--open-tracing-config-file` pointing at the Zipkin configuration file. An example file is provided in the gatsby project under `node_modules/gatsby/dist/utils/tracer/zipkin-local.js` that will send tracing spans to your local docker instance. E.g

```
gatsby build --open-tracing-config-file node_modules/gatsby/dist/utils/tracer/zipkin-local.js
```
4. Once the build is complete, view your tracing information at [http://localhost:9411](http://localhost:9411)
4. Once the build is complete, view your tracing information at [http://localhost:9411](http://localhost:9411)
27 changes: 15 additions & 12 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ function buildLocalCommands(cli, isLocalSite) {
type: `string`,
default: ``,
describe: `Custom HTTPS key file (relative path; also required: --https, --cert-file). See https://www.gatsbyjs.org/docs/local-https/`,
}).option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
}),
})
.option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
}),
handler: handlerP(
getCommandHandler(`develop`, (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`
Expand All @@ -140,14 +141,16 @@ function buildLocalCommands(cli, isLocalSite) {
type: `boolean`,
default: false,
describe: `Build site with link paths prefixed (set prefix in your config).`,
}).option(`no-uglify`, {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
}).option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
}),
})
.option(`no-uglify`, {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
})
.option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
}),
handler: handlerP(
getCommandHandler(`build`, (args, cmd) => {
process.env.NODE_ENV = `production`
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const reporter = createReporter({ emoji: true, verbose: VERBOSE })
const base = Object.getPrototypeOf(reporter)

type ActivityArgs = {
parentSpan: Object
parentSpan: Object,
}

/* Reporter module.
Expand Down
18 changes: 10 additions & 8 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const report = require(`gatsby-cli/lib/reporter`)
const getConfigFile = require(`./get-config-file`)
const tracer = require(`opentracing`).globalTracer()


// Show stack trace on unhandled promises.
process.on(`unhandledRejection`, (reason, p) => {
report.panic(reason)
Expand Down Expand Up @@ -91,13 +90,14 @@ module.exports = async (args: BootstrapArgs) => {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`onPreBootstrap`, { parentSpan: activity.span },)
await apiRunnerNode(`onPreBootstrap`, { parentSpan: activity.span })
activity.end()

// Delete html and css files from the public directory as we don't want
// deleted pages and styles from previous builds to stick around.
activity = report.activityTimer(
`delete html and css files from previous builds`, {
`delete html and css files from previous builds`,
{
parentSpan: bootstrapSpan,
}
)
Expand Down Expand Up @@ -407,11 +407,13 @@ module.exports = async (args: BootstrapArgs) => {
parentSpan: bootstrapSpan,
})
activity.start()
apiRunnerNode(`onPostBootstrap`, { parentSpan: activity.span }).then(() => {
activity.end()
bootstrapSpan.finish()
resolve({ graphqlRunner })
})
apiRunnerNode(`onPostBootstrap`, { parentSpan: activity.span }).then(
() => {
activity.end()
bootstrapSpan.finish()
resolve({ graphqlRunner })
}
)
}
}, 100)

Expand Down
22 changes: 13 additions & 9 deletions packages/gatsby/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,39 @@ type BuildArgs = {
}

module.exports = async function build(program: BuildArgs) {

initTracer(program.openTracingConfigFile)

const buildSpan = tracer.startSpan(`build`)
buildSpan.setTag(`directory`, program.directory)

const { graphqlRunner } = await bootstrap( { ...program, parentSpan: buildSpan } )
const { graphqlRunner } = await bootstrap({
...program,
parentSpan: buildSpan,
})

await apiRunnerNode(`onPreBuild`, { graphql: graphqlRunner, parentSpan: buildSpan })
await apiRunnerNode(`onPreBuild`, {
graphql: graphqlRunner,
parentSpan: buildSpan,
})

// Copy files from the static directory to
// an equivalent static directory within public.
copyStaticDirectory()

let activity
activity = report.activityTimer(
`Building production JavaScript and CSS bundles`,
{ parentSpan: buildSpan },
{ parentSpan: buildSpan }
)
activity.start()
await buildProductionBundle(program).catch(err => {
reportFailure(`Generating JavaScript bundles failed`, err)
})
activity.end()

activity = report.activityTimer(
`Building static HTML for pages`,
{ parentSpan: buildSpan },
)
activity = report.activityTimer(`Building static HTML for pages`, {
parentSpan: buildSpan,
})
activity.start()
await buildHTML(program).catch(err => {
reportFailure(
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ async function startServer(program) {
}

module.exports = async (program: any) => {

initTracer(program.openTracingConfigFile)

const detect = require(`detect-port`)
Expand Down
12 changes: 10 additions & 2 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ const hasWarnedForPageComponent = new Set()
* },
* })
*/
actions.createPage = (page: PageInput, plugin?: Plugin, actionOptions?: ActionOptions) => {
actions.createPage = (
page: PageInput,
plugin?: Plugin,
actionOptions?: ActionOptions
) => {
let noPageOrComponent = false
let name = `The plugin "${plugin.name}"`
if (plugin.name === `default-site-plugin`) {
Expand Down Expand Up @@ -456,7 +460,11 @@ const typeOwners = {}
* }
* })
*/
actions.createNode = (node: any, plugin?: Plugin, actionOptions?: ActionOptions = {}) => {
actions.createNode = (
node: any,
plugin?: Plugin,
actionOptions?: ActionOptions = {}
) => {
if (!_.isObject(node)) {
return console.log(
chalk.bold.red(
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby/src/redux/plugin-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const apiRunnerNode = require(`../utils/api-runner-node`)
emitter.on(`CREATE_NODE`, action => {
const node = store.getState().nodes[action.payload.id]
const traceTags = { nodeId: node.id, nodeType: node.internal.type }
apiRunnerNode(`onCreateNode`, { node, traceId: action.traceId, parentSpan: action.parentSpan, traceTags })
apiRunnerNode(`onCreateNode`, {
node,
traceId: action.traceId,
parentSpan: action.parentSpan,
traceTags,
})
})

emitter.on(`CREATE_PAGE`, action => {
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/schema/build-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type { ProcessedNodeType } from "./infer-graphql-type"
type TypeMap = { [typeName: string]: ProcessedNodeType }

module.exports = async ({ parentSpan }) => {

const spanArgs = parentSpan ? { childOf: parentSpan } : {}
const span = tracer.startSpan(`build schema`, spanArgs)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const buildNodeConnections = require(`./build-node-connections`)
const { store } = require(`../redux`)
const invariant = require(`invariant`)

module.exports = async ( { parentSpan }) => {
module.exports = async ({ parentSpan }) => {
const typesGQL = await buildNodeTypes({ parentSpan })
const connections = buildNodeConnections(_.values(typesGQL))

Expand Down
13 changes: 5 additions & 8 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ const doubleBind = (boundActionCreators, api, plugin, actionOptions) => {
}

const runAPI = (plugin, api, args) => {


const gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
if (gatsbyNode[api]) {

const parentSpan = args && args.parentSpan
const spanOptions = parentSpan ? { childOf: parentSpan } : {}
const pluginSpan = tracer.startSpan(`run-plugin`, spanOptions)
Expand Down Expand Up @@ -79,7 +76,7 @@ const runAPI = (plugin, api, args) => {
}

const namespacedCreateNodeId = id => createNodeId(id, plugin.name)

const apiCallArgs = [
{
...args,
Expand Down Expand Up @@ -128,7 +125,6 @@ let waitingForCasacadeToFinish = []

module.exports = async (api, args = {}, pluginSource) =>
new Promise(resolve => {

const { parentSpan } = args
const apiSpanArgs = parentSpan ? { childOf: parentSpan } : {}
const apiSpan = tracer.startSpan(`run-api`, apiSpanArgs)
Expand All @@ -137,7 +133,7 @@ module.exports = async (api, args = {}, pluginSource) =>
_.forEach(args.traceTags, (value, key) => {
apiSpan.setTag(key, value)
})

// Check that the API is documented.
if (!apiList[api]) {
reporter.error(`api: "${api}" is not a valid Gatsby api`)
Expand Down Expand Up @@ -189,8 +185,9 @@ module.exports = async (api, args = {}, pluginSource) =>
} else {
pluginName = `Plugin ${plugin.name}`
}
Promise.resolve(runAPI(plugin, api, { ...args, parentSpan: apiSpan }))
.asCallback(callback)
Promise.resolve(
runAPI(plugin, api, { ...args, parentSpan: apiSpan })
).asCallback(callback)
},
(err, results) => {
if (err) {
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby/src/utils/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function discoverPluginsWithoutNodes(storeState) {
return _.difference(nodeCreationPlugins, nodeOwners)
}

module.exports = async ( { parentSpan }) => {

module.exports = async ({ parentSpan }) => {
await apiRunner(`sourceNodes`, {
traceId: `initial-sourceNodes`,
waitForCascadingActions: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/tracer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const opentracing = require(`opentracing`)
let tracerProvider

/**
* tracerFile should be a js file that exports two functions.
* tracerFile should be a js file that exports two functions.
*
* `create` - Create and return an open-tracing compatible tracer. See
* https://github.com/opentracing/opentracing-javascript/blob/master/src/tracer.ts
Expand Down
27 changes: 14 additions & 13 deletions packages/gatsby/src/utils/tracer/zipkin-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let recorder
* https://github.com/opentracing/opentracing-javascript/blob/master/src/tracer.ts
*/
function create() {

logger = new HttpLogger({
// endpoint of local docker zipkin instance
endpoint: `http://localhost:9411/api/v1/spans`,
Expand Down Expand Up @@ -47,7 +46,6 @@ function create() {
* the queue
*/
async function stop() {

// First, write all partial spans to the http logger
recorder.partialSpans.forEach((span, id) => {
if (recorder._timedOut(span)) {
Expand All @@ -72,19 +70,22 @@ async function _processQueue() {
body: postBody,
headers: self.headers,
timeout: self.timeout,
}).then((response) => {
if (response.status !== 202) {
const err = `Unexpected response while sending Zipkin data, status:` +
`${response.status}, body: ${postBody}`
})
.then(response => {
if (response.status !== 202) {
const err =
`Unexpected response while sending Zipkin data, status:` +
`${response.status}, body: ${postBody}`

if (self.errorListenerSet) self.emit(`error`, new Error(err))
if (self.errorListenerSet) self.emit(`error`, new Error(err))
else console.error(err)
}
})
.catch(error => {
const err = `Error sending Zipkin data ${error}`
if (self.errorListenerSet) this.emit(`error`, new Error(err))
else console.error(err)
}
}).catch((error) => {
const err = `Error sending Zipkin data ${error}`
if (self.errorListenerSet) this.emit(`error`, new Error(err))
else console.error(err)
})
})
}
return true
}
Expand Down

0 comments on commit 40824f7

Please sign in to comment.