UNPKG

@hotmeshio/hotmesh

Version:

Serverless Workflow

34 lines (33 loc) 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trace = void 0; const common_1 = require("./common"); const context_1 = require("./context"); const isSideEffectAllowed_1 = require("./isSideEffectAllowed"); /** * Executes a distributed trace, outputting the provided attributes * to the telemetry sink (e.g. OpenTelemetry). * * This trace will only run once per workflow execution by default. * * @param {StringScalarType} attributes - Key-value attributes to attach to the trace. * @param {{ once: boolean }} [config={ once: true }] - If `once` is true, trace only runs once. * @returns {Promise<boolean>} True if tracing succeeded, otherwise false. */ async function trace(attributes, config = { once: true }) { const store = common_1.asyncLocalStorage.getStore(); const workflowTopic = store.get('workflowTopic'); const connection = store.get('connection'); const namespace = store.get('namespace'); const hotMeshClient = await common_1.WorkerService.getHotMesh(workflowTopic, { connection, namespace, }); const { raw, COUNTER } = (0, context_1.getContext)(); const { trc: traceId, spn: spanId, aid: activityId } = raw.metadata; if (!config.once || await (0, isSideEffectAllowed_1.isSideEffectAllowed)(hotMeshClient, 'trace')) { return await common_1.TelemetryService.traceActivity(namespace, attributes, activityId, traceId, spanId, COUNTER.counter); } return true; } exports.trace = trace;