Skip to content

Commit

Permalink
fix(gatsby): Assign parentSpan to activities that were missing them (#…
Browse files Browse the repository at this point in the history
…33122)

* Pass parentSpan to page onCreateNode

* Add parentSpan to writing page-data.json files activity

* more fixes

* Make typescript happy
  • Loading branch information
KyleAMathews committed Sep 13, 2021
1 parent 91187da commit fc66250
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/index.d.ts
Expand Up @@ -3,6 +3,7 @@ import { Renderer } from "react-dom"
import { EventEmitter } from "events"
import { WindowLocation, NavigateFn, NavigateOptions } from "@reach/router"
import { Reporter } from "gatsby-cli/lib/reporter/reporter"
import { Span } from "opentracing"
export { Reporter }
import {
EnumTypeComposerAsObjectDefinition as ComposeEnumTypeConfig,
Expand Down Expand Up @@ -916,7 +917,7 @@ export interface WrapRootElementNodeArgs extends NodePluginArgs {
}

export interface ParentSpanPluginArgs extends NodePluginArgs {
parentSpan: object
parentSpan: Span
}

export interface NodePluginArgs {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/index.ts
Expand Up @@ -68,7 +68,7 @@ export async function bootstrap(

await createPages(context)

await handleStalePageData()
await handleStalePageData(parentSpan)

await rebuildSchemaWithSitePage(context)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/commands/build.ts
Expand Up @@ -205,7 +205,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
rewriteActivityTimer.end()
}

await flushPendingPageDataWrites()
await flushPendingPageDataWrites(buildSpan)
markWebpackStatusAsDone()

if (telemetry.isTrackingEnabled()) {
Expand Down
Expand Up @@ -327,8 +327,11 @@ let isFirstBuild = true
export async function onPreBootstrap({
reporter,
store,
parentSpan,
}: ParentSpanPluginArgs): Promise<void> {
const activity = reporter.activityTimer(`Compiling Gatsby Functions`)
const activity = reporter.activityTimer(`Compiling Gatsby Functions`, {
parentSpan,
})
activity.start()

const {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/plugin-runner.ts
Expand Up @@ -68,6 +68,7 @@ export const startPluginRunner = (): void => {
if (node.internal.type === `SitePage`) {
apiRunnerNode(`onCreateNode`, {
node,
parentSpan: action.parentSpan,
traceTags: { nodeId: node.id, nodeType: node.internal.type },
})
}
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/redux/types.ts
Expand Up @@ -6,6 +6,7 @@ import { IGatsbyCLIState } from "gatsby-cli/src/reporter/redux/types"
import { ThunkAction } from "redux-thunk"
import { InternalJob, JobResultInterface } from "../utils/jobs/manager"
import { ITypeMetadata } from "../schema/infer/inference-metadata"
import { Span } from "opentracing"

type SystemPath = string
type Identifier = string
Expand Down Expand Up @@ -790,6 +791,9 @@ export interface ICreateNodeAction {
type: `CREATE_NODE`
payload: IGatsbyNode
oldNode?: IGatsbyNode
traceId: string
parentSpan: Span
followsSpan: Span
}

export interface IAddFieldToNodeAction {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/src/services/create-pages.ts
Expand Up @@ -107,7 +107,9 @@ export async function createPages({
`Deleted ${deletedPages.length} page${deletedPages.length === 1 ? `` : `s`}`
)

const tim = reporter.activityTimer(`Checking for changed pages`)
const tim = reporter.activityTimer(`Checking for changed pages`, {
parentSpan,
})
tim.start()

const { changedPages } = findChangedPages(
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby/src/services/initialize.ts
Expand Up @@ -427,9 +427,13 @@ export async function initialize({

// Init plugins once cache is initialized
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
await apiRunnerNode(`onPluginInit`)
await apiRunnerNode(`onPluginInit`, {
parentSpan: activity.span,
})
} else {
await apiRunnerNode(`unstable_onPluginInit`)
await apiRunnerNode(`unstable_onPluginInit`, {
parentSpan: activity.span,
})
}

activity.end()
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/src/services/source-nodes.ts
Expand Up @@ -31,7 +31,9 @@ export async function sourceNodes({

reporter.verbose(`Checking for deleted pages`)

const tim = reporter.activityTimer(`Checking for changed pages`)
const tim = reporter.activityTimer(`Checking for changed pages`, {
parentSpan,
})
tim.start()

const { changedPages, deletedPages } = findChangedPages(
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/state-machines/query-running/actions.ts
Expand Up @@ -7,8 +7,8 @@ import {
} from "xstate"
import { enqueueFlush } from "../../utils/page-data"

export const flushPageData = (): void => {
enqueueFlush()
export const flushPageData = (context: IQueryRunningContext): void => {
enqueueFlush(context.parentSpan)
}

export const assignDirtyQueries = assign<
Expand Down
16 changes: 10 additions & 6 deletions packages/gatsby/src/utils/page-data.ts
Expand Up @@ -15,6 +15,7 @@ import {
reverseFixedPagePath,
IPageData,
} from "./page-data-helpers"
import { Span } from "opentracing"

export { reverseFixedPagePath }

Expand Down Expand Up @@ -146,7 +147,7 @@ export function isFlushEnqueued(): boolean {
return isFlushPending
}

export async function flush(): Promise<void> {
export async function flush(parentSpan?: Span): Promise<void> {
if (isFlushing) {
// We're already in the middle of a flush
return
Expand All @@ -167,7 +168,8 @@ export async function flush(): Promise<void> {
const writePageDataActivity = reporter.createProgress(
`Writing page-data.json files to public directory`,
pagePaths.size,
0
0,
{ id: `write-page-data-public-directory`, parentSpan }
)
writePageDataActivity.start()

Expand Down Expand Up @@ -260,15 +262,15 @@ export async function flush(): Promise<void> {
return
}

export function enqueueFlush(): void {
export function enqueueFlush(parentSpan?: Span): void {
if (isWebpackStatusPending()) {
isFlushPending = true
} else {
flush()
flush(parentSpan)
}
}

export async function handleStalePageData(): Promise<void> {
export async function handleStalePageData(parentSpan: Span): Promise<void> {
if (!(await fs.pathExists(`public/page-data`))) {
return
}
Expand All @@ -277,7 +279,9 @@ export async function handleStalePageData(): Promise<void> {
// we get the list of those and compare against expected page-data files
// and remove ones that shouldn't be there anymore

const activity = reporter.activityTimer(`Cleaning up stale page-data`)
const activity = reporter.activityTimer(`Cleaning up stale page-data`, {
parentSpan,
})
activity.start()

const pageDataFilesFromPreviousBuilds = await new Promise<Set<string>>(
Expand Down

0 comments on commit fc66250

Please sign in to comment.