Skip to content

Commit 0acd8a6

Browse files
mrstorkpieh
andauthored
fix: swap @opentelemetry packages for @netlify/otel (#3170)
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent cbdd9da commit 0acd8a6

File tree

11 files changed

+773
-203
lines changed

11 files changed

+773
-203
lines changed

package-lock.json

Lines changed: 657 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
"@netlify/build": "^35.1.7",
5858
"@netlify/config": "^24.0.4",
5959
"@netlify/edge-bundler": "^14.5.5",
60-
"@netlify/edge-functions-bootstrap": "^2.14.0",
6160
"@netlify/edge-functions": "^2.17.1",
61+
"@netlify/edge-functions-bootstrap": "^2.14.0",
6262
"@netlify/eslint-config-node": "^7.0.1",
6363
"@netlify/functions": "^4.3.0",
64+
"@netlify/otel": "^4.3.0",
6465
"@netlify/serverless-functions-api": "^2.5.0",
6566
"@netlify/zip-it-and-ship-it": "^14.1.8",
6667
"@opentelemetry/api": "^1.8.0",

src/build/templates/handler-monorepo.tmpl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
createRequestContext,
33
runWithRequestContext,
44
} from '{{cwd}}/.netlify/dist/run/handlers/request-context.cjs'
5-
import { getTracer } from '{{cwd}}/.netlify/dist/run/handlers/tracer.cjs'
5+
import { getTracer, withActiveSpan } from '{{cwd}}/.netlify/dist/run/handlers/tracer.cjs'
66

77
process.chdir('{{cwd}}')
88

@@ -15,8 +15,8 @@ export default async function (req, context) {
1515
const tracer = getTracer()
1616

1717
const handlerResponse = await runWithRequestContext(requestContext, () => {
18-
return tracer.withActiveSpan('Next.js Server Handler', async (span) => {
19-
span.setAttributes({
18+
return withActiveSpan(tracer, 'Next.js Server Handler', async (span) => {
19+
span?.setAttributes({
2020
'account.id': context.account.id,
2121
'deploy.id': context.deploy.id,
2222
'request.id': context.requestId,
@@ -32,7 +32,7 @@ export default async function (req, context) {
3232
cachedHandler = handler
3333
}
3434
const response = await cachedHandler(req, context, span, requestContext)
35-
span.setAttributes({
35+
span?.setAttributes({
3636
'http.status_code': response.status,
3737
})
3838
return response

src/build/templates/handler.tmpl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
runWithRequestContext,
44
} from './.netlify/dist/run/handlers/request-context.cjs'
55
import serverHandler from './.netlify/dist/run/handlers/server.js'
6-
import { getTracer } from './.netlify/dist/run/handlers/tracer.cjs'
6+
import { getTracer, withActiveSpan } from './.netlify/dist/run/handlers/tracer.cjs'
77

88
// Set feature flag for regional blobs
99
process.env.USE_REGIONAL_BLOBS = '{{useRegionalBlobs}}'
@@ -13,8 +13,8 @@ export default async function handler(req, context) {
1313
const tracer = getTracer()
1414

1515
const handlerResponse = await runWithRequestContext(requestContext, () => {
16-
return tracer.withActiveSpan('Next.js Server Handler', async (span) => {
17-
span.setAttributes({
16+
return withActiveSpan(tracer, 'Next.js Server Handler', async (span) => {
17+
span?.setAttributes({
1818
'account.id': context.account.id,
1919
'deploy.id': context.deploy.id,
2020
'request.id': context.requestId,
@@ -26,7 +26,7 @@ export default async function handler(req, context) {
2626
cwd: process.cwd(),
2727
})
2828
const response = await serverHandler(req, context, span, requestContext)
29-
span.setAttributes({
29+
span?.setAttributes({
3030
'http.status_code': response.status,
3131
})
3232
return response

src/run/handlers/cache.cts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'
55
import { join } from 'node:path'
66
import { join as posixJoin } from 'node:path/posix'
77

8-
import { type Span } from '@opentelemetry/api'
8+
import type { Span } from '@netlify/otel/opentelemetry'
99
import type { PrerenderManifest } from 'next/dist/build/index.js'
1010
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1111

@@ -32,7 +32,7 @@ import {
3232
type RevalidateTagDurations,
3333
type TagStaleOrExpiredStatus,
3434
} from './tags-handler.cjs'
35-
import { getTracer, recordWarning } from './tracer.cjs'
35+
import { getTracer, recordWarning, withActiveSpan } from './tracer.cjs'
3636

3737
let memoizedPrerenderManifest: PrerenderManifest
3838

@@ -74,7 +74,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
7474
private captureResponseCacheLastModified(
7575
cacheValue: NetlifyCacheHandlerValue,
7676
key: string,
77-
getCacheKeySpan: Span,
77+
getCacheKeySpan?: Span,
7878
) {
7979
if (cacheValue.value?.kind === 'FETCH') {
8080
return
@@ -262,17 +262,17 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
262262
async get(
263263
...args: Parameters<CacheHandlerForMultipleVersions['get']>
264264
): ReturnType<CacheHandlerForMultipleVersions['get']> {
265-
return this.tracer.withActiveSpan('get cache key', async (span) => {
265+
return withActiveSpan(this.tracer, 'get cache key', async (span) => {
266266
const [key, context = {}] = args
267267
getLogger().debug(`[NetlifyCacheHandler.get]: ${key}`)
268268

269-
span.setAttributes({ key })
269+
span?.setAttributes({ key })
270270

271271
const blob = await this.cacheStore.get<NetlifyCacheHandlerValue>(key, 'blobStore.get')
272272

273273
// if blob is null then we don't have a cache entry
274274
if (!blob) {
275-
span.addEvent('Cache miss', { key })
275+
span?.addEvent('Cache miss', { key })
276276
return null
277277
}
278278

@@ -281,7 +281,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
281281
if (getRequestContext()?.isBackgroundRevalidation && typeof ttl === 'number' && ttl < 0) {
282282
// background revalidation request should allow data that is not yet stale,
283283
// but opt to discard STALE data, so that Next.js generate fresh response
284-
span.addEvent('Discarding stale entry due to SWR background revalidation request', {
284+
span?.addEvent('Discarding stale entry due to SWR background revalidation request', {
285285
key,
286286
ttl,
287287
})
@@ -303,14 +303,14 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
303303
)
304304

305305
if (expiredByTags) {
306-
span.addEvent('Expired', { expiredByTags, key, ttl })
306+
span?.addEvent('Expired', { expiredByTags, key, ttl })
307307
return null
308308
}
309309

310310
this.captureResponseCacheLastModified(blob, key, span)
311311

312312
if (staleByTags) {
313-
span.addEvent('Stale', { staleByTags, key, ttl })
313+
span?.addEvent('Stale', { staleByTags, key, ttl })
314314
// note that we modify this after we capture last modified to ensure that Age is correct
315315
// but we still let Next.js know that entry is stale
316316
blob.lastModified = -1 // indicate that the entry is stale
@@ -324,7 +324,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
324324

325325
switch (blob.value?.kind) {
326326
case 'FETCH':
327-
span.addEvent('FETCH', {
327+
span?.addEvent('FETCH', {
328328
lastModified: blob.lastModified,
329329
revalidate: context.revalidate,
330330
ttl,
@@ -336,7 +336,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
336336

337337
case 'ROUTE':
338338
case 'APP_ROUTE': {
339-
span.addEvent(blob.value?.kind, {
339+
span?.addEvent(blob.value?.kind, {
340340
lastModified: blob.lastModified,
341341
status: blob.value.status,
342342
revalidate: blob.value.revalidate,
@@ -362,7 +362,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
362362
requestContext.pageHandlerRevalidate = revalidate
363363
}
364364

365-
span.addEvent(blob.value?.kind, { lastModified: blob.lastModified, revalidate, ttl })
365+
span?.addEvent(blob.value?.kind, { lastModified: blob.lastModified, revalidate, ttl })
366366

367367
await this.injectEntryToPrerenderManifest(key, blob.value)
368368

@@ -379,7 +379,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
379379

380380
const { revalidate, rscData, segmentData, ...restOfPageValue } = blob.value
381381

382-
span.addEvent(blob.value?.kind, { lastModified: blob.lastModified, revalidate, ttl })
382+
span?.addEvent(blob.value?.kind, { lastModified: blob.lastModified, revalidate, ttl })
383383

384384
await this.injectEntryToPrerenderManifest(key, blob.value)
385385

@@ -400,7 +400,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
400400
}
401401
}
402402
default:
403-
span.recordException(new Error(`Unknown cache entry kind: ${blob.value?.kind}`))
403+
span?.recordException(new Error(`Unknown cache entry kind: ${blob.value?.kind}`))
404404
}
405405
return null
406406
})
@@ -452,10 +452,10 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
452452
}
453453

454454
async set(...args: Parameters<CacheHandlerForMultipleVersions['set']>) {
455-
return this.tracer.withActiveSpan('set cache key', async (span) => {
455+
return withActiveSpan(this.tracer, 'set cache key', async (span?: Span) => {
456456
const [key, data, context] = args
457457
const lastModified = Date.now()
458-
span.setAttributes({ key, lastModified })
458+
span?.setAttributes({ key, lastModified })
459459

460460
getLogger().debug(`[NetlifyCacheHandler.set]: ${key}`)
461461

src/run/handlers/server.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { OutgoingHttpHeaders } from 'http'
22

33
import { ComputeJsOutgoingMessage, toComputeResponse, toReqRes } from '@fastly/http-compute-js'
44
import type { Context } from '@netlify/functions'
5-
import { Span } from '@opentelemetry/api'
5+
import type { Span } from '@netlify/otel/opentelemetry'
66
import type { WorkerRequestHandler } from 'next/dist/server/lib/types.js'
77

88
import { getRunConfig, setRunConfig } from '../config.js'
@@ -17,7 +17,7 @@ import { nextResponseProxy } from '../revalidate.js'
1717
import { setFetchBeforeNextPatchedIt } from '../storage/storage.cjs'
1818

1919
import { getLogger, type RequestContext } from './request-context.cjs'
20-
import { getTracer, recordWarning } from './tracer.cjs'
20+
import { getTracer, recordWarning, withActiveSpan } from './tracer.cjs'
2121
import { configureUseCacheHandlers } from './use-cache-handler.js'
2222
import { setupWaitUntil } from './wait-until.cjs'
2323
// make use of global fetch before Next.js applies any patching
@@ -61,13 +61,13 @@ const disableFaultyTransferEncodingHandling = (res: ComputeJsOutgoingMessage) =>
6161
export default async (
6262
request: Request,
6363
_context: Context,
64-
topLevelSpan: Span,
64+
topLevelSpan: Span | undefined,
6565
requestContext: RequestContext,
6666
) => {
6767
const tracer = getTracer()
6868

6969
if (!nextHandler) {
70-
await tracer.withActiveSpan('initialize next server', async () => {
70+
await withActiveSpan(tracer, 'initialize next server', async () => {
7171
const { getMockedRequestHandler } = await nextImportPromise
7272
const url = new URL(request.url)
7373

@@ -80,7 +80,7 @@ export default async (
8080
})
8181
}
8282

83-
return await tracer.withActiveSpan('generate response', async (span) => {
83+
return await withActiveSpan(tracer, 'generate response', async (span) => {
8484
const { req, res } = toReqRes(request)
8585

8686
// Work around a bug in http-proxy in next@<14.0.2
@@ -104,7 +104,7 @@ export default async (
104104
getLogger().withError(error).error('next handler error')
105105
console.error(error)
106106
resProxy.statusCode = 500
107-
span.setAttribute('http.status_code', 500)
107+
span?.setAttribute('http.status_code', 500)
108108
resProxy.end('Internal Server Error')
109109
})
110110

@@ -113,7 +113,7 @@ export default async (
113113
const response = await toComputeResponse(resProxy)
114114

115115
if (requestContext.responseCacheKey) {
116-
topLevelSpan.setAttribute('responseCacheKey', requestContext.responseCacheKey)
116+
topLevelSpan?.setAttribute('responseCacheKey', requestContext.responseCacheKey)
117117
}
118118

119119
const nextCache = response.headers.get('x-nextjs-cache')
@@ -135,7 +135,7 @@ export default async (
135135

136136
const netlifyVary = response.headers.get('netlify-vary') ?? undefined
137137
const netlifyCdnCacheControl = response.headers.get('netlify-cdn-cache-control') ?? undefined
138-
topLevelSpan.setAttributes({
138+
topLevelSpan?.setAttributes({
139139
'x-nextjs-cache': nextCache ?? undefined,
140140
isServedFromNextCache,
141141
netlifyVary,
@@ -151,7 +151,7 @@ export default async (
151151
(!isRSCRequest && contentType?.includes('text/html'))) ??
152152
false
153153

154-
topLevelSpan.setAttributes({
154+
topLevelSpan?.setAttributes({
155155
isRSCRequest,
156156
isCacheableAppPage: true,
157157
contentType,

0 commit comments

Comments
 (0)