|
| 1 | +import { |
| 2 | + trace, |
| 3 | + SpanOptions, |
| 4 | + SpanKind, |
| 5 | + Exception, |
| 6 | + context as api_context, |
| 7 | + SpanStatusCode |
| 8 | +} from '@opentelemetry/api' |
| 9 | +import { SemanticAttributes } from '@opentelemetry/semantic-conventions' |
| 10 | +import { Initialiser, setConfig } from '../config.js' |
| 11 | +import { exportSpans, proxyExecutionContext } from './common.js' |
| 12 | +import { instrumentEnv } from './env.js' |
| 13 | +import { wrap } from '../wrap.js' |
| 14 | + |
| 15 | +type ScheduledHandler = ExportedHandlerScheduledHandler<unknown> |
| 16 | +export type ScheduledHandlerArgs = Parameters<ScheduledHandler> |
| 17 | + |
| 18 | +const traceIdSymbol = Symbol('traceId') |
| 19 | + |
| 20 | +let cold_start = true |
| 21 | +export function executeScheduledHandler(scheduledFn: ScheduledHandler, [controller, env, ctx]: ScheduledHandlerArgs): Promise<void> { |
| 22 | + const tracer = trace.getTracer('scheduledHandler') |
| 23 | + const attributes = { |
| 24 | + [SemanticAttributes.FAAS_TRIGGER]: 'timer', |
| 25 | + [SemanticAttributes.FAAS_COLDSTART]: cold_start, |
| 26 | + [SemanticAttributes.FAAS_CRON]: controller.cron, |
| 27 | + [SemanticAttributes.FAAS_TIME]: new Date(controller.scheduledTime).toISOString() |
| 28 | + } |
| 29 | + cold_start = false |
| 30 | + const options: SpanOptions = { |
| 31 | + attributes, |
| 32 | + kind: SpanKind.SERVER, |
| 33 | + } |
| 34 | + |
| 35 | + const promise = tracer.startActiveSpan('scheduledHandler', options, async (span) => { |
| 36 | + const traceId = span.spanContext().traceId |
| 37 | + api_context.active().setValue(traceIdSymbol, traceId) |
| 38 | + try { |
| 39 | + await scheduledFn(controller, env, ctx) |
| 40 | + } catch (error) { |
| 41 | + span.recordException(error as Exception) |
| 42 | + span.setStatus({ code: SpanStatusCode.ERROR }) |
| 43 | + span.end() |
| 44 | + throw error |
| 45 | + } |
| 46 | + }) |
| 47 | + return promise |
| 48 | +} |
| 49 | + |
| 50 | +export function createScheduledHandler(scheduledFn: ScheduledHandler, initialiser: Initialiser) { |
| 51 | + const scheduledHandler: ProxyHandler<ScheduledHandler> = { |
| 52 | + async apply(target, _thisArg, argArray: Parameters<ScheduledHandler>): Promise<void> { |
| 53 | + const [controller, orig_env, orig_ctx] = argArray |
| 54 | + const config = initialiser(orig_env as Record<string, unknown>, controller) |
| 55 | + const env = instrumentEnv(orig_env as Record<string, unknown>) |
| 56 | + const { ctx, tracker } = proxyExecutionContext(orig_ctx) |
| 57 | + const context = setConfig(config) |
| 58 | + |
| 59 | + try { |
| 60 | + const args: ScheduledHandlerArgs = [controller, env, ctx] |
| 61 | + |
| 62 | + return await api_context.with(context, executeScheduledHandler, undefined, target, args) |
| 63 | + } catch (error) { |
| 64 | + throw error |
| 65 | + } finally { |
| 66 | + orig_ctx.waitUntil(exportSpans(tracker)) |
| 67 | + } |
| 68 | + }, |
| 69 | + } |
| 70 | + return wrap(scheduledFn, scheduledHandler) |
| 71 | +} |
0 commit comments