Skip to content

Commit 4ddbdd6

Browse files
committed
refactor: telemetry feature flag
1 parent a15f8a6 commit 4ddbdd6

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

src/index.js

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,52 +44,49 @@ import { NoopSpanProcessor } from '@opentelemetry/sdk-trace-base'
4444
* @import { Environment } from './bindings.js'
4545
*/
4646

47-
const handler = {
48-
/** @type {Handler<Context, Environment>} */
49-
fetch (request, env, ctx) {
50-
console.log(request.method, request.url)
51-
const middleware = composeMiddleware(
52-
// Prepare the Context
53-
withCdnCache,
54-
withContext,
55-
withCorsHeaders,
56-
withVersionHeader,
57-
withErrorHandler,
58-
withParsedIpfsUrl,
59-
createWithHttpMethod('GET', 'HEAD'),
60-
withAuthToken,
61-
withLocator,
62-
withGatewayIdentity,
63-
// TODO: replace this with a handler to fetch the real delegations
64-
withDelegationStubs,
47+
/**
48+
* The middleware stack
49+
*/
50+
const middleware = composeMiddleware(
51+
// Prepare the Context
52+
withCdnCache,
53+
withContext,
54+
withCorsHeaders,
55+
withVersionHeader,
56+
withErrorHandler,
57+
withParsedIpfsUrl,
58+
createWithHttpMethod('GET', 'HEAD'),
59+
withAuthToken,
60+
withLocator,
61+
withGatewayIdentity,
62+
// TODO: replace this with a handler to fetch the real delegations
63+
withDelegationStubs,
6564

66-
// Rate-limit requests
67-
withRateLimit,
65+
// Rate-limit requests
66+
withRateLimit,
6867

69-
// Fetch CAR data - Double-check why this can't be placed after the authorized space middleware
70-
withCarBlockHandler,
68+
// Fetch CAR data - Double-check why this can't be placed after the authorized space middleware
69+
withCarBlockHandler,
7170

72-
// Authorize requests
73-
withAuthorizedSpace,
71+
// Authorize requests
72+
withAuthorizedSpace,
7473

75-
// Track Egress
76-
withEgressClient,
77-
withEgressTracker,
74+
// Track Egress
75+
withEgressClient,
76+
withEgressTracker,
7877

79-
// Fetch data
80-
withContentClaimsDagula,
81-
withFormatRawHandler,
82-
withFormatCarHandler,
78+
// Fetch data
79+
withContentClaimsDagula,
80+
withFormatRawHandler,
81+
withFormatCarHandler,
8382

84-
// Prepare the Response
85-
withContentDispositionHeader,
86-
withFixedLengthStream
87-
)
88-
return middleware(handleUnixfs)(request, env, ctx)
89-
}
90-
}
83+
// Prepare the Response
84+
withContentDispositionHeader,
85+
withFixedLengthStream
86+
)
9187

9288
/**
89+
* Configure the OpenTelemetry exporter based on the environment
9390
*
9491
* @param {Environment} env
9592
* @param {*} _trigger
@@ -109,9 +106,42 @@ function config (env, _trigger) {
109106
service: { name: 'freeway' }
110107
}
111108
}
112-
export default process.env.FF_TELEMETRY_ENABLED === 'true'
113-
? instrument(handler, config)
114-
: handler
109+
110+
/**
111+
* The promise to the pre-configured handler
112+
*
113+
* @type {Promise<Handler<Context, Environment>> | null}
114+
*/
115+
let handlerPromise = null
116+
117+
/**
118+
* Pre-configure the handler based on the environment.
119+
*
120+
* @param {Environment} env
121+
* @returns {Promise<Handler<Context, Environment>>}
122+
*/
123+
async function initializeHandler (env) {
124+
const baseHandler = middleware(handleUnixfs)
125+
const finalHandler = env.FF_TELEMETRY_ENABLED === 'true'
126+
? instrument(baseHandler, config)
127+
: baseHandler
128+
return finalHandler
129+
}
130+
131+
const handler = {
132+
/** @type {Handler<Context, Environment>} */
133+
async fetch (request, env, ctx) {
134+
console.log(request.method, request.url)
135+
// Initialize the handler only once and reuse the promise
136+
if (!handlerPromise) {
137+
handlerPromise = initializeHandler(env)
138+
}
139+
const handler = await handlerPromise
140+
return handler(request, env, ctx)
141+
}
142+
}
143+
144+
export default handler
115145

116146
/**
117147
* @type {Middleware<BlockContext & UnixfsContext & IpfsUrlContext, BlockContext & UnixfsContext & IpfsUrlContext, Environment>}

0 commit comments

Comments
 (0)