Skip to content

Commit 2f70a82

Browse files
fix(gatsby): fix nesting of tracing spans + add docs for OpenTelemetry (#33098)
* nest preprocess source under extract queries activity * Add spans for ast parsing activities * Nest onPostBuild api call under activity * Nest all steps to build html renderer under the activity * Trace persisiting the cache to disk * Revert anything that's not a bug fix * Add docs for using OpenTelemetry with Honeycomb * clarify instructions * type * Update docs/docs/performance-tracing.md Co-authored-by: Ward Peeters <ward@coding-tech.com> Co-authored-by: Ward Peeters <ward@coding-tech.com>
1 parent 3d44652 commit 2f70a82

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

docs/docs/performance-tracing.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,79 @@ The above configuration file can be passed to Gatsby with the `--open-tracing-co
3939

4040
There are many OpenTracing compatible backends available. Below are examples of how to hook [Jaeger](/docs/performance-tracing/#local-jaeger-with-docker) or [Zipkin](/docs/performance-tracing/#local-zipkin-with-docker) into Gatsby.
4141

42-
### local Jaeger with Docker
42+
### OpenTelemetry
43+
44+
[OpenTelemetry](https://opentelemetry.io/) is the new industry standard for tracing. Most vendors now have
45+
built-in support for OpenTelemetry. The following is an example of configuring Gatsby to send build traces
46+
to [Honeycomb](https://www.honeycomb.io/).
47+
48+
1. Install necesary dependencies
49+
50+
```shell
51+
npm install @grpc/grpc-js @opentelemetry/api @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-collector-grpc @opentelemetry/sdk-node @opentelemetry/shim-opentracing opentracing
52+
```
53+
54+
2. Create a file named `tracing.js` in the root of your site with the following code (adding your honeycomb API key and database).
55+
56+
```js
57+
const process = require(`process`)
58+
const { Metadata, credentials } = require(`@grpc/grpc-js`)
59+
const api = require(`@opentelemetry/api`)
60+
const { TracerShim } = require(`@opentelemetry/shim-opentracing`)
61+
const opentracing = require(`opentracing`)
62+
const { NodeSDK } = require(`@opentelemetry/sdk-node`)
63+
const {
64+
getNodeAutoInstrumentations,
65+
} = require(`@opentelemetry/auto-instrumentations-node`)
66+
const { Resource } = require(`@opentelemetry/resources`)
67+
const {
68+
SemanticResourceAttributes,
69+
} = require(`@opentelemetry/semantic-conventions`)
70+
const {
71+
CollectorTraceExporter,
72+
} = require(`@opentelemetry/exporter-collector-grpc`)
73+
74+
const metadata = new Metadata()
75+
metadata.set(`x-honeycomb-team`, `ADD YOUR API KEY`)
76+
metadata.set(`x-honeycomb-dataset`, `ADD YOUR DATASET NAME`)
77+
const traceExporter = new CollectorTraceExporter({
78+
url: `grpc://api.honeycomb.io:443/`,
79+
credentials: credentials.createSsl(),
80+
metadata,
81+
})
82+
83+
const sdk = new NodeSDK({
84+
resource: new Resource({
85+
[SemanticResourceAttributes.SERVICE_NAME]: `gatsby`,
86+
}),
87+
traceExporter,
88+
instrumentations: [getNodeAutoInstrumentations()],
89+
})
90+
91+
sdk
92+
.start()
93+
.then(() => console.log(`Tracing initialized`))
94+
.catch(error => console.log(`Error initializing tracing`, error))
95+
96+
exports.create = () => {
97+
// We shim Gatsby's use of OpenTracing for OpenTelemetry
98+
const tracer = api.trace.getTracer(`my-tracer`)
99+
return new TracerShim(tracer)
100+
}
101+
102+
exports.stop = async () => {
103+
await sdk.shutdown()
104+
}
105+
```
106+
107+
3. OpenTelemetry includes a built-in collector which needs to be started first. So
108+
we run Gatsby in a special way telling Node to require our tracing file immediately.
109+
110+
```shell
111+
node -r ./tracing.js node_modules/gatsby/cli.js build --open-tracing-config-file tracing.js
112+
```
113+
114+
### Local Jaeger with Docker
43115

44116
[Jaeger](https://www.jaegertracing.io/) is an open source tracing system that can be run locally using Docker.
45117

packages/gatsby/src/commands/build.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
238238
let pageRenderer = ``
239239
let waitForCompilerCloseBuildHtml
240240
try {
241-
const result = await buildRenderer(program, Stage.BuildHTML, buildSpan)
241+
const result = await buildRenderer(
242+
program,
243+
Stage.BuildHTML,
244+
buildSSRBundleActivityProgress.span
245+
)
242246
pageRenderer = result.rendererPath
243247
if (_CFLAGS_.GATSBY_MAJOR === `4` && shouldGenerateEngines()) {
244248
// for now copy page-render to `.cache` so page-ssr module can require it as a sibling module
@@ -331,7 +335,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
331335
postBuildActivityTimer.start()
332336
await apiRunnerNode(`onPostBuild`, {
333337
graphql: gatsbyNodeGraphQLFunction,
334-
parentSpan: buildSpan,
338+
parentSpan: postBuildActivityTimer.span,
335339
})
336340
postBuildActivityTimer.end()
337341

packages/gatsby/src/query/file-parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ async function parseToAst(filePath, fileStr, { parentSpan, addError } = {}) {
113113
const transpiled = await apiRunnerNode(`preprocessSource`, {
114114
filename: filePath,
115115
contents: fileStr,
116-
parentSpan: parentSpan,
116+
parentSpan,
117117
})
118+
118119
if (transpiled && transpiled.length) {
119120
for (const item of transpiled) {
120121
try {

packages/gatsby/src/query/query-compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ export default async function compile({ parentSpan } = {}): Promise<
7272
})
7373
),
7474
addError,
75-
parentSpan,
75+
parentSpan: activity.span,
7676
})
7777

7878
const queries = processQueries({
7979
schema,
8080
parsedQueries,
8181
addError,
82-
parentSpan,
82+
parentSpan: activity.span,
8383
})
8484

8585
if (errors.length !== 0) {

0 commit comments

Comments
 (0)