Skip to content

Commit

Permalink
feat(gatsby): Allow external systems to setup tracing for builds (#34204
Browse files Browse the repository at this point in the history
)
  • Loading branch information
KyleAMathews committed Dec 8, 2021
1 parent 548a82d commit d3aa933
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/gatsby/src/commands/build-html.ts
Expand Up @@ -412,11 +412,11 @@ export const buildHTML = async ({

export async function buildHTMLPagesAndDeleteStaleArtifacts({
workerPool,
buildSpan,
parentSpan,
program,
}: {
workerPool: GatsbyWorkerPool
buildSpan?: Span
parentSpan?: Span
program: IBuildArgs
}): Promise<{
toRegenerate: Array<string>
Expand All @@ -439,7 +439,7 @@ export async function buildHTMLPagesAndDeleteStaleArtifacts({
toRegenerate.length,
0,
{
parentSpan: buildSpan,
parentSpan,
}
)
buildHTMLActivityProgress.start()
Expand Down
30 changes: 23 additions & 7 deletions packages/gatsby/src/commands/build.ts
Expand Up @@ -62,7 +62,11 @@ import {
} from "../utils/page-mode"
import { validateEngines } from "../utils/validate-engines"

module.exports = async function build(program: IBuildArgs): Promise<void> {
module.exports = async function build(
program: IBuildArgs,
// Let external systems running Gatsby to inject attributes
externalTelemetryAttributes: Record<string, any>
): Promise<void> {
// global gatsby object to use without store
global.__GATSBY = {
buildId: uuid.v4(),
Expand Down Expand Up @@ -90,9 +94,13 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
markWebpackStatusAsPending()

const publicDir = path.join(program.directory, `public`)
await initTracer(
process.env.GATSBY_OPEN_TRACING_CONFIG_FILE || program.openTracingConfigFile
)
if (!externalTelemetryAttributes) {
await initTracer(
process.env.GATSBY_OPEN_TRACING_CONFIG_FILE ||
program.openTracingConfigFile
)
}

const buildActivity = report.phantomActivity(`build`)
buildActivity.start()

Expand All @@ -106,6 +114,13 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
const buildSpan = buildActivity.span
buildSpan.setTag(`directory`, program.directory)

// Add external tags to buildSpan
if (externalTelemetryAttributes) {
Object.entries(externalTelemetryAttributes).forEach(([key, value]) => {
buildActivity.span.setTag(key, value)
})
}

const { gatsbyNodeGraphQLFunction, workerPool } = await bootstrap({
program,
parentSpan: buildSpan,
Expand Down Expand Up @@ -391,7 +406,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
await buildHTMLPagesAndDeleteStaleArtifacts({
program,
workerPool,
buildSpan,
parentSpan: buildSpan,
})

await waitMaterializePageMode
Expand Down Expand Up @@ -453,9 +468,10 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {

report.info(`Done building in ${process.uptime()} sec`)

buildSpan.finish()
await stopTracer()
buildActivity.end()
if (!externalTelemetryAttributes) {
await stopTracer()
}

if (program.logPages) {
if (toRegenerate.length) {
Expand Down
8 changes: 7 additions & 1 deletion packages/gatsby/src/utils/source-nodes.ts
Expand Up @@ -86,6 +86,7 @@ function deleteStaleNodes(state: IGatsbyState, nodes: Array<Node>): void {
}

let isInitialSourcing = true
let sourcingCount = 0
export default async ({
webhookBody,
pluginName,
Expand All @@ -97,8 +98,11 @@ export default async ({
parentSpan?: Span
deferNodeMutation?: boolean
}): Promise<void> => {
const traceId = isInitialSourcing
? `initial-sourceNodes`
: `sourceNodes #${sourcingCount}`
await apiRunner(`sourceNodes`, {
traceId: `initial-sourceNodes`,
traceId,
waitForCascadingActions: true,
deferNodeMutation,
parentSpan,
Expand All @@ -120,4 +124,6 @@ export default async ({
}

store.dispatch(actions.apiFinished({ apiName: `sourceNodes` }))

sourcingCount += 1
}

0 comments on commit d3aa933

Please sign in to comment.