Repository URL to install this package:
|
Version:
0.7.0 ▾
|
// (c) Copyright 2023 Supertenant Ltd. - all rights reserved.
// See LICENSE file in project root for license terms.
"use strict";const deepMerge=require("../../util/deepMerge"),tracingUtil=require("../tracingUtil"),constants=require("../constants");let logger,isActive=(logger=require("../../logger").getLogger("tracing/sdk",newLogger=>{logger=newLogger}),!1);module.exports=function(isCallbackApi){let cls=null,runInContext=null;function startSdkSpan(name,kind,sdkKind,forceRootContext,stackTraceRef,tags,traceId,parentSpanId,callback){forceRootContext=forceRootContext?cls.ns.createRootContext():null;return runInContext(()=>{var span=cls.startSpan("sdk",kind,traceId,parentSpanId);return span.stack=tracingUtil.getStackTrace(stackTraceRef),span.data.sdk={name:name,type:sdkKind},tags&&(span.data.sdk.custom={tags:Object.assign({},tags)}),callNext(callback)},forceRootContext)}function completeSpan(error,span,tags){span.data.sdk?(error&&(span.ec=1,span.data.sdk.custom||(span.data.sdk.custom={tags:{}}),span.data.sdk.custom.tags||(span.data.sdk.custom.tags={}),null==span.data.sdk.custom.tags.message)&&(span.data.sdk.custom.tags.message=tracingUtil.getErrorDetails(error)),span.data.sdk.custom&&tags?span.data.sdk.custom.tags=deepMerge(span.data.sdk.custom.tags,tags):tags&&(span.data.sdk.custom={tags:tags}),span.d=Date.now()-span.ts,span.transmit()):logger.warn("Cannot complete an SDK span. The currently active span is not an SDK span, so there seems to be a mismatch in the trace context. This is the currently active span: "+JSON.stringify(span))}function callNext(callback){var span=cls?cls.getCurrentSpan():null;return isCallbackApi?callback(span):Promise.resolve(span)}return{startEntrySpan:function startEntrySpan(name,tags,traceId,parentSpanId,callback){var parentSpan;return isCallbackApi&&2===arguments.length&&"function"==typeof arguments[1]?(callback=tags,tags=traceId=parentSpanId=null):isCallbackApi&&3===arguments.length&&"function"==typeof arguments[2]&&(callback=traceId,traceId=parentSpanId=null),isActive?(parentSpan=cls.getCurrentSpan())?(logger.warn(`Cannot start an entry span (${name}) when another span is already active. Currently, the following span is active: `+JSON.stringify(parentSpan)),callNext(callback)):startSdkSpan(name,constants.ENTRY,constants.SDK.ENTRY,!0,startEntrySpan,tags,traceId,parentSpanId,callback):callNext(callback)},completeEntrySpan:function(error,tags){var span;isActive&&((span=cls.getCurrentSpan())?constants.isEntrySpan(span)?completeSpan(error,span,tags):logger.warn("Cannot complete an entry span as this requires an entry span to be currently active. But the currently active span is not an entry span: "+JSON.stringify(span)):logger.warn("Cannot complete an entry span as this requires an entry span to be currently active. Currently there is no span active at all."))},startIntermediateSpan:function startIntermediateSpan(name,tags,callback){var parentSpan;return isCallbackApi&&2===arguments.length&&"function"==typeof arguments[1]&&(callback=tags,tags=null),isActive?(parentSpan=cls.getCurrentSpan())?constants.isExitSpan(parentSpan)?(logger.warn(`Cannot start an intermediate span (${name}) as this requires an active entry (or intermediate) span as parent. But the currently active span is an exit span: `+JSON.stringify(parentSpan)),callNext(callback)):startSdkSpan(name,constants.INTERMEDIATE,constants.SDK.INTERMEDIATE,!1,startIntermediateSpan,tags,null,null,callback):(logger.warn(`Cannot start an intermediate span (${name}) as this requires an active entry (or intermediate) span as parent. Currently there is no span active at all.`),callNext(callback)):callNext(callback)},completeIntermediateSpan:function(error,tags,spanToBeCompleted){isActive&&((spanToBeCompleted=spanToBeCompleted||cls.getCurrentSpan())?constants.isIntermediateSpan(spanToBeCompleted)?completeSpan(error,spanToBeCompleted,tags):logger.warn("Cannot complete an intermediate span as this requires an intermediate span to be currently active. But the currently active span is not an intermediate span: "+JSON.stringify(spanToBeCompleted)):logger.warn("Cannot complete an intermediate span as this requires an intermediate span to be currently active. Currently there is no span active at all."))},startExitSpan:function startExitSpan(name,tags,callback){return isCallbackApi&&2===arguments.length&&"function"==typeof arguments[1]&&(callback=tags,tags=null),null===cls||cls.skipExitTracing({isActive:isActive})?callNext(callback):startSdkSpan(name,constants.EXIT,constants.SDK.EXIT,!1,startExitSpan,tags,null,null,callback)},completeExitSpan:function(error,tags){var span;isActive&&((span=cls.getCurrentSpan())?constants.isExitSpan(span)?completeSpan(error,span,tags):logger.warn("Cannot complete an exit span as this requires an exit span to be currently active. But the currently active span is not an exit span: "+JSON.stringify(span)):logger.warn("Cannot complete an exit span as this requires an exit span to be currently active. Currently there is no span active at all."))},bindEmitter:function(emitter){isActive&&cls.ns.bindEmitter(emitter)},init:function(_cls){cls=_cls,runInContext=(isCallbackApi?cls.ns.runAndReturn:cls.ns.runPromise).bind(cls.ns)},activate:function(){isActive=!0},deactivate:function(){isActive=!0}}};