-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Queue and ScheduledHandlers
- Loading branch information
1 parent
9402db8
commit 822c9b3
Showing
3 changed files
with
66 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,24 @@ | ||
import { trace, SpanOptions, SpanKind, Exception, context as api_context, SpanStatusCode } from '@opentelemetry/api' | ||
import { SemanticAttributes } from '@opentelemetry/semantic-conventions' | ||
import { Initialiser, setConfig } from '../config.js' | ||
import { exportSpans, proxyExecutionContext } from './common.js' | ||
import { instrumentEnv } from './env.js' | ||
import { wrap } from '../wrap.js' | ||
import { versionAttributes } from './version.js' | ||
import { SpanKind } from '@opentelemetry/api' | ||
import { HandlerInstrumentation, InitialSpanInfo, OrPromise } from '../types.js' | ||
import { | ||
ATTR_FAAS_CRON, | ||
ATTR_FAAS_TIME, | ||
ATTR_FAAS_TRIGGER, | ||
FAAS_TRIGGER_VALUE_TIMER, | ||
} from '@opentelemetry/semantic-conventions/incubating' | ||
|
||
type ScheduledHandler = ExportedHandlerScheduledHandler<unknown> | ||
export type ScheduledHandlerArgs = Parameters<ScheduledHandler> | ||
|
||
const traceIdSymbol = Symbol('traceId') | ||
|
||
let cold_start = true | ||
export function executeScheduledHandler( | ||
scheduledFn: ScheduledHandler, | ||
[controller, env, ctx]: ScheduledHandlerArgs, | ||
): Promise<void> { | ||
const tracer = trace.getTracer('scheduledHandler') | ||
const attributes = { | ||
[SemanticAttributes.FAAS_TRIGGER]: 'timer', | ||
[SemanticAttributes.FAAS_COLDSTART]: cold_start, | ||
[SemanticAttributes.FAAS_CRON]: controller.cron, | ||
[SemanticAttributes.FAAS_TIME]: new Date(controller.scheduledTime).toISOString(), | ||
} | ||
cold_start = false | ||
Object.assign(attributes, versionAttributes(env)) | ||
const options: SpanOptions = { | ||
attributes, | ||
kind: SpanKind.SERVER, | ||
} | ||
|
||
const promise = tracer.startActiveSpan(`scheduledHandler ${controller.cron}`, options, async (span) => { | ||
const traceId = span.spanContext().traceId | ||
api_context.active().setValue(traceIdSymbol, traceId) | ||
try { | ||
await scheduledFn(controller, env, ctx) | ||
} catch (error) { | ||
span.recordException(error as Exception) | ||
span.setStatus({ code: SpanStatusCode.ERROR }) | ||
throw error | ||
} finally { | ||
span.end() | ||
export const scheduledInstrumentation: HandlerInstrumentation<ScheduledController, OrPromise<void>> = { | ||
getInitialSpanInfo: function (controller: ScheduledController): InitialSpanInfo { | ||
return { | ||
name: `scheduledHandler ${controller.cron}`, | ||
options: { | ||
attributes: { | ||
[ATTR_FAAS_TRIGGER]: FAAS_TRIGGER_VALUE_TIMER, | ||
[ATTR_FAAS_CRON]: controller.cron, | ||
[ATTR_FAAS_TIME]: new Date(controller.scheduledTime).toISOString(), | ||
}, | ||
kind: SpanKind.INTERNAL, | ||
}, | ||
} | ||
}) | ||
return promise | ||
} | ||
|
||
export function createScheduledHandler(scheduledFn: ScheduledHandler, initialiser: Initialiser) { | ||
const scheduledHandler: ProxyHandler<ScheduledHandler> = { | ||
async apply(target, _thisArg, argArray: Parameters<ScheduledHandler>): Promise<void> { | ||
const [controller, orig_env, orig_ctx] = argArray | ||
const config = initialiser(orig_env as Record<string, unknown>, controller) | ||
const env = instrumentEnv(orig_env as Record<string, unknown>) | ||
const { ctx, tracker } = proxyExecutionContext(orig_ctx) | ||
const context = setConfig(config) | ||
|
||
try { | ||
const args: ScheduledHandlerArgs = [controller, env, ctx] | ||
|
||
return await api_context.with(context, executeScheduledHandler, undefined, target, args) | ||
} catch (error) { | ||
throw error | ||
} finally { | ||
orig_ctx.waitUntil(exportSpans(tracker)) | ||
} | ||
}, | ||
} | ||
return wrap(scheduledFn, scheduledHandler) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters