@temporalio/interceptors-opentelemetry
Version:
Temporal.io SDK interceptors bundle for tracing with opentelemetry
65 lines (56 loc) • 2.26 kB
text/typescript
/**
* Utilities for working with a possibly missing `@temporalio/workflow` peer dependency
* @module
*/
import type * as WorkflowModule from '@temporalio/workflow';
import type { SdkFlags as SdkFlagsT } from '@temporalio/workflow/lib/flags';
type SdkFlags = typeof SdkFlagsT;
// @temporalio/workflow is an optional peer dependency.
// It can be missing as long as the user isn't attempting to construct a workflow interceptor.
// If we start shipping ES modules alongside CJS, we will have to reconsider
// this dynamic import as `import` is async for ES modules.
let workflowModule: typeof WorkflowModule | undefined;
let workflowModuleLoadError: any | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
workflowModule = require('@temporalio/workflow');
} catch (err) {
// Capture the module not found error to rethrow if the module is required
workflowModuleLoadError = err;
}
/**
* Returns `@temporalio/workflow` module if present.
* Throws if the module failed to load
*/
export function getWorkflowModule(): typeof WorkflowModule {
if (workflowModuleLoadError) {
throw workflowModuleLoadError;
}
return workflowModule!;
}
/**
* Returns if an SDK flag was set
*
* Expects to be called only after `ensureWorkflowModuleLoaded`.
* Will throw if `@temporalio/workflow` is not available
*/
export function hasSdkFlag(flag: keyof SdkFlags): boolean {
const { SdkFlags } = require('@temporalio/workflow/lib/flags') as typeof import('@temporalio/workflow/lib/flags'); // eslint-disable-line @typescript-eslint/no-require-imports
const { getActivator } =
require('@temporalio/workflow/lib/global-attributes') as typeof import('@temporalio/workflow/lib/global-attributes'); // eslint-disable-line @typescript-eslint/no-require-imports
return getActivator().hasFlag(SdkFlags[flag]);
}
/**
* Checks if the workflow module loaded successfully and throws if not.
*/
export function ensureWorkflowModuleLoaded(): void {
if (workflowModuleLoadError) {
throw workflowModuleLoadError;
}
}
/**
* Returns the workflow module if available, or undefined if it failed to load.
*/
export function getWorkflowModuleIfAvailable(): typeof WorkflowModule | undefined {
return workflowModule;
}