|
| 1 | +import { ReadableSpan } from '@opentelemetry/sdk-trace-base' |
| 2 | +import { Initialiser, setConfig } from '../config' |
| 3 | +import { exportSpans, proxyExecutionContext } from './common' |
| 4 | +// import { instrumentEnv } from "./env" |
| 5 | +import { Exception, SpanKind, SpanOptions, SpanStatusCode, context as api_context, trace } from '@opentelemetry/api' |
| 6 | +import { wrap } from '../wrap' |
| 7 | +import { |
| 8 | + gatherIncomingCfAttributes, |
| 9 | + gatherRequestAttributes, |
| 10 | + gatherResponseAttributes, |
| 11 | + getParentContextFromRequest, |
| 12 | +} from './fetch' |
| 13 | + |
| 14 | +type PageHandlerArgs = Parameters<PagesFunction> |
| 15 | + |
| 16 | +let cold_start = true |
| 17 | +export function executePageHandler(pagesFn: PagesFunction, [request]: PageHandlerArgs): Promise<Response> { |
| 18 | + const spanContext = getParentContextFromRequest(request.request) |
| 19 | + |
| 20 | + const tracer = trace.getTracer('pagesHandler') |
| 21 | + const attributes = { |
| 22 | + ['faas.trigger']: 'http', |
| 23 | + ['faas.coldstart']: cold_start, |
| 24 | + ['faas.invocation_id']: request.request.headers.get('cf-ray') ?? undefined, |
| 25 | + } |
| 26 | + cold_start = false |
| 27 | + Object.assign(attributes, gatherRequestAttributes(request.request)) |
| 28 | + Object.assign(attributes, gatherIncomingCfAttributes(request.request)) |
| 29 | + const options: SpanOptions = { |
| 30 | + attributes, |
| 31 | + kind: SpanKind.SERVER, |
| 32 | + } |
| 33 | + |
| 34 | + console.log(request.data) |
| 35 | + |
| 36 | + const promise = tracer.startActiveSpan( |
| 37 | + `${request.request.method} ${request.functionPath}`, |
| 38 | + options, |
| 39 | + spanContext, |
| 40 | + async (span) => { |
| 41 | + const readable = span as unknown as ReadableSpan |
| 42 | + try { |
| 43 | + const response: Response = await pagesFn(request) |
| 44 | + span.setAttributes(gatherResponseAttributes(response)) |
| 45 | + if (readable.attributes['http.route']) { |
| 46 | + span.updateName(`${request.request.method} ${readable.attributes['http.route']}`) |
| 47 | + } |
| 48 | + span.end() |
| 49 | + |
| 50 | + return response |
| 51 | + } catch (error) { |
| 52 | + if (readable.attributes['http.route']) { |
| 53 | + span.updateName(`${request.request.method} ${readable.attributes['http.route']}`) |
| 54 | + } |
| 55 | + span.recordException(error as Exception) |
| 56 | + span.setStatus({ code: SpanStatusCode.ERROR }) |
| 57 | + span.end() |
| 58 | + throw error |
| 59 | + } |
| 60 | + }, |
| 61 | + ) |
| 62 | + return promise |
| 63 | +} |
| 64 | + |
| 65 | +export function createPageHandler< |
| 66 | + E = unknown, |
| 67 | + P extends string = any, |
| 68 | + D extends Record<string, unknown> = Record<string, unknown>, |
| 69 | +>(pageFn: PagesFunction<E, P, D>, initialiser: Initialiser): PagesFunction<E, P, D> { |
| 70 | + const pagesHandler: ProxyHandler<PagesFunction> = { |
| 71 | + apply: async (target, _thisArg, argArray: Parameters<PagesFunction>): Promise<Response> => { |
| 72 | + const [orig_ctx] = argArray |
| 73 | + const config = initialiser(orig_ctx.env as Record<string, unknown>, orig_ctx.request) |
| 74 | + // const env = instrumentEnv(orig_ctx.env as Record<string, unknown>) |
| 75 | + const { ctx, tracker } = proxyExecutionContext(orig_ctx) |
| 76 | + const context = setConfig(config) |
| 77 | + |
| 78 | + try { |
| 79 | + const args: PageHandlerArgs = [ctx] as PageHandlerArgs |
| 80 | + return await api_context.with(context, executePageHandler, undefined, target, args) |
| 81 | + } catch (error) { |
| 82 | + throw error |
| 83 | + } finally { |
| 84 | + orig_ctx.waitUntil(exportSpans(tracker)) |
| 85 | + } |
| 86 | + }, |
| 87 | + } |
| 88 | + return wrap(pageFn, pagesHandler) |
| 89 | +} |
0 commit comments