Unverified Commit d0177fa0 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Tracing: Fix memory leak in span map (#85529)

## What?

Fixes a small retainer object that is inserted on each request and not
cleaned up.

While at it also swapped array -> Set for the type checks to leverage
`has`​ instead of `includes`​.
parent 2c56a693
...@@ -125,7 +125,7 @@ type SpanTypes = ...@@ -125,7 +125,7 @@ type SpanTypes =
| `${MiddlewareSpan}` | `${MiddlewareSpan}`
// This list is used to filter out spans that are not relevant to the user // This list is used to filter out spans that are not relevant to the user
export const NextVanillaSpanAllowlist = [ export const NextVanillaSpanAllowlist = new Set([
MiddlewareSpan.execute, MiddlewareSpan.execute,
BaseServerSpan.handleRequest, BaseServerSpan.handleRequest,
RenderSpan.getServerSideProps, RenderSpan.getServerSideProps,
...@@ -142,15 +142,15 @@ export const NextVanillaSpanAllowlist = [ ...@@ -142,15 +142,15 @@ export const NextVanillaSpanAllowlist = [
NextNodeServerSpan.getLayoutOrPageModule, NextNodeServerSpan.getLayoutOrPageModule,
NextNodeServerSpan.startResponse, NextNodeServerSpan.startResponse,
NextNodeServerSpan.clientComponentLoading, NextNodeServerSpan.clientComponentLoading,
] ])
// These Spans are allowed to be always logged // These Spans are allowed to be always logged
// when the otel log prefix env is set // when the otel log prefix env is set
export const LogSpanAllowList = [ export const LogSpanAllowList = new Set([
NextNodeServerSpan.findPageComponents, NextNodeServerSpan.findPageComponents,
NextNodeServerSpan.createComponentTree, NextNodeServerSpan.createComponentTree,
NextNodeServerSpan.clientComponentLoading, NextNodeServerSpan.clientComponentLoading,
] ])
export { export {
BaseServerSpan, BaseServerSpan,
......
...@@ -13,6 +13,8 @@ import type { ...@@ -13,6 +13,8 @@ import type {
} from 'next/dist/compiled/@opentelemetry/api' } from 'next/dist/compiled/@opentelemetry/api'
import { isThenable } from '../../../shared/lib/is-thenable' import { isThenable } from '../../../shared/lib/is-thenable'
const NEXT_OTEL_PERFORMANCE_PREFIX = process.env.NEXT_OTEL_PERFORMANCE_PREFIX
let api: typeof import('next/dist/compiled/@opentelemetry/api') let api: typeof import('next/dist/compiled/@opentelemetry/api')
// we want to allow users to use their own version of @opentelemetry/api if they // we want to allow users to use their own version of @opentelemetry/api if they
...@@ -274,7 +276,7 @@ class NextTracerImpl implements NextTracer { ...@@ -274,7 +276,7 @@ class NextTracerImpl implements NextTracer {
const spanName = options.spanName ?? type const spanName = options.spanName ?? type
if ( if (
(!NextVanillaSpanAllowlist.includes(type) && (!NextVanillaSpanAllowlist.has(type) &&
process.env.NEXT_OTEL_VERBOSE !== '1') || process.env.NEXT_OTEL_VERBOSE !== '1') ||
options.hideSpan options.hideSpan
) { ) {
...@@ -307,20 +309,26 @@ class NextTracerImpl implements NextTracer { ...@@ -307,20 +309,26 @@ class NextTracerImpl implements NextTracer {
spanName, spanName,
options, options,
(span: Span) => { (span: Span) => {
const startTime = let startTime: number | undefined
'performance' in globalThis && 'measure' in performance if (
? globalThis.performance.now() NEXT_OTEL_PERFORMANCE_PREFIX &&
: undefined type &&
LogSpanAllowList.has(type)
) {
startTime =
'performance' in globalThis && 'measure' in performance
? globalThis.performance.now()
: undefined
}
let cleanedUp = false
const onCleanup = () => { const onCleanup = () => {
if (cleanedUp) return
cleanedUp = true
rootSpanAttributesStore.delete(spanId) rootSpanAttributesStore.delete(spanId)
if ( if (startTime) {
startTime &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX &&
LogSpanAllowList.includes(type || ('' as any))
) {
performance.measure( performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${( `${NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(
type.split('.').pop() || '' type.split('.').pop() || ''
).replace( ).replace(
/[A-Z]/g, /[A-Z]/g,
...@@ -345,11 +353,18 @@ class NextTracerImpl implements NextTracer { ...@@ -345,11 +353,18 @@ class NextTracerImpl implements NextTracer {
) )
) )
} }
try { if (fn.length > 1) {
if (fn.length > 1) { try {
return fn(span, (err) => closeSpanWithError(span, err)) return fn(span, (err) => closeSpanWithError(span, err))
} catch (err: any) {
closeSpanWithError(span, err)
throw err
} finally {
onCleanup()
} }
}
try {
const result = fn(span) const result = fn(span)
if (isThenable(result)) { if (isThenable(result)) {
// If there's error make sure it throws // If there's error make sure it throws
...@@ -398,7 +413,7 @@ class NextTracerImpl implements NextTracer { ...@@ -398,7 +413,7 @@ class NextTracerImpl implements NextTracer {
args.length === 3 ? args : [args[0], {}, args[1]] args.length === 3 ? args : [args[0], {}, args[1]]
if ( if (
!NextVanillaSpanAllowlist.includes(name) && !NextVanillaSpanAllowlist.has(name) &&
process.env.NEXT_OTEL_VERBOSE !== '1' process.env.NEXT_OTEL_VERBOSE !== '1'
) { ) {
return fn return fn
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment