Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f7dee1

Browse filesBrowse files
committed
refactor: create reusable helper to record warnings
1 parent 2e7a951 commit 2f7dee1
Copy full SHA for 2f7dee1

File tree

Expand file treeCollapse file tree

3 files changed

+24
-23
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-23
lines changed

‎src/run/handlers/cache.cts

Copy file name to clipboardExpand all lines: src/run/handlers/cache.cts
+5-13Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from '../regional-blob-store.cjs'
2929

3030
import { getLogger, getRequestContext } from './request-context.cjs'
31-
import { getTracer } from './tracer.cjs'
31+
import { getTracer, recordWarning } from './tracer.cjs'
3232

3333
type TagManifestBlobCache = Record<string, Promise<TagManifest | null>>
3434

@@ -85,13 +85,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
8585
if (!requestContext) {
8686
// we will not be able to use request context for date header calculation
8787
// we will fallback to using blobs
88-
getCacheKeySpan.recordException(
89-
new Error('CacheHandler was called without a request context'),
90-
)
91-
getCacheKeySpan.setAttributes({
92-
severity: 'alert',
93-
warning: true,
94-
})
88+
recordWarning(new Error('CacheHandler was called without a request context'), getCacheKeySpan)
9589
return
9690
}
9791

@@ -100,15 +94,13 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
10094
// so as a safety measure we will not use any of them and let blobs be used
10195
// to calculate the date header
10296
requestContext.responseCacheGetLastModified = undefined
103-
getCacheKeySpan.recordException(
97+
recordWarning(
10498
new Error(
10599
`Multiple response cache keys used in single request: ["${requestContext.responseCacheKey}, "${key}"]`,
106100
),
101+
getCacheKeySpan,
107102
)
108-
getCacheKeySpan.setAttributes({
109-
severity: 'alert',
110-
warning: true,
111-
})
103+
112104
return
113105
}
114106

‎src/run/handlers/tracer.cts

Copy file name to clipboardExpand all lines: src/run/handlers/tracer.cts
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,16 @@ export function getTracer(): RuntimeTracer {
8989

9090
return tracer
9191
}
92+
93+
export function recordWarning(warning: Error, span?: Span) {
94+
const spanToRecordWarningOn = span ?? trace.getActiveSpan()
95+
if (!spanToRecordWarningOn) {
96+
return
97+
}
98+
99+
spanToRecordWarningOn.recordException(warning)
100+
spanToRecordWarningOn.setAttributes({
101+
severity: 'alert',
102+
warning: true,
103+
})
104+
}

‎src/run/headers.ts

Copy file name to clipboardExpand all lines: src/run/headers.ts
+6-10Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
44
import type { NetlifyCachedRouteValue, NetlifyCacheHandlerValue } from '../shared/cache-types.cjs'
55

66
import { getLogger, RequestContext } from './handlers/request-context.cjs'
7+
import { recordWarning } from './handlers/tracer.cjs'
78
import { getMemoizedKeyValueStoreBackedByRegionalBlobStore } from './regional-blob-store.cjs'
89

910
const ALL_VARIATIONS = Symbol.for('ALL_VARIATIONS')
@@ -145,13 +146,10 @@ export const adjustDateHeader = async ({
145146
// request context would contain lastModified value
146147
// this is not fatal as we have fallback,
147148
// but we want to know about it happening
148-
span.recordException(
149+
recordWarning(
149150
new Error('lastModified not found in requestContext, falling back to trying blobs'),
151+
span,
150152
)
151-
span.setAttributes({
152-
severity: 'alert',
153-
warning: true,
154-
})
155153

156154
const cacheStore = getMemoizedKeyValueStoreBackedByRegionalBlobStore({ consistency: 'strong' })
157155
const cacheEntry = await cacheStore.get<NetlifyCacheHandlerValue>(
@@ -164,15 +162,13 @@ export const adjustDateHeader = async ({
164162
if (!lastModified) {
165163
// this should never happen as we only execute this code path for cached responses
166164
// and those should always have lastModified value
167-
span.recordException(
165+
recordWarning(
168166
new Error(
169167
'lastModified not found in either requestContext or blobs, date header for cached response is not set',
170168
),
169+
span,
171170
)
172-
span.setAttributes({
173-
severity: 'alert',
174-
warning: true,
175-
})
171+
176172
return
177173
}
178174

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.