Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ function instrument(SQSConsumer) {
span.disableAutoEnd();
const res = orig.apply(this, arguments);

res
.then(() => {
span.d = Date.now() - span.ts;
span.transmitManual();
})
.catch(err => {
span.ec = 1;
tracingUtil.setErrorDetails(span, err, 'sqs');
span.d = Date.now() - span.ts;
span.transmitManual();
});
if (res && typeof res.then === 'function') {
res
.then(() => {
span.d = Date.now() - span.ts;
span.transmitManual();
})
.catch(err => {
span.ec = 1;
tracingUtil.setErrorDetails(span, err, 'sqs');
span.d = Date.now() - span.ts;
span.transmitManual();
});
}

return res;
});
Expand All @@ -56,17 +58,19 @@ function instrument(SQSConsumer) {
span.disableAutoEnd();
const res = orig.apply(this, arguments);

res
.then(() => {
span.d = Date.now() - span.ts;
span.transmitManual();
})
.catch(err => {
span.ec = 1;
tracingUtil.setErrorDetails(span, err, 'sqs');
span.d = Date.now() - span.ts;
span.transmitManual();
});
if (res && typeof res.then === 'function') {
res
.then(() => {
span.d = Date.now() - span.ts;
span.transmitManual();
})
.catch(err => {
span.ec = 1;
tracingUtil.setErrorDetails(span, err, 'sqs');
span.d = Date.now() - span.ts;
span.transmitManual();
});
}

return res;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ function instrumentedOperation(operation, extractorPre, extractorPost, ctx, orig
}

const promise = original.apply(ctx, originalArgs);
if (promise) {
if (promise && typeof promise.then === 'function') {
promise.then(
result => finishSpan(null, Array.isArray(result) ? result[0] : result, span, extractorPost),
e => finishSpan(e, null, span, extractorPost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,23 @@ function shimPushValue(originalFunction) {
function shimPullValue(originalFunction) {
return function () {
const pullPromise = originalFunction.apply(this, arguments);
return pullPromise.then(result => {
if (result && result.value && result.value[CLS_CONTEXT_SYMBOL]) {
const clsContext = result.value[CLS_CONTEXT_SYMBOL];
if (isActive && clsContext) {
cls.ns.enter(clsContext);
setImmediate(() => {
cls.ns.exit(clsContext);
});

if (pullPromise && typeof pullPromise.then === 'function') {
return pullPromise.then(result => {
if (result && result.value && result.value[CLS_CONTEXT_SYMBOL]) {
const clsContext = result.value[CLS_CONTEXT_SYMBOL];
if (isActive && clsContext) {
cls.ns.enter(clsContext);
setImmediate(() => {
cls.ns.exit(clsContext);
});
}
}
}
return result;
});
return result;
});
}

return pullPromise;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function instrumentConnect(originalConnect) {

const prom = originalConnect.apply(originalThis, originalArgs);

if (prom && prom.then) {
if (prom && typeof prom.then === 'function') {
prom.then(cluster => {
instrumentCluster(cluster, connectionStr);
return cluster;
Expand Down Expand Up @@ -488,7 +488,7 @@ function instrumentOperation({ connectionStr, bucketName, getBucketTypeFn, sql,
if (callbackIndex < 0) {
const prom = original.apply(originalThis, originalArgs);

if (prom.then && prom.catch) {
if (typeof prom?.then === 'function' && typeof prom?.catch === 'function') {
prom
.then(result => {
if (resultHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,14 @@ function instrumentApi(client, actionPath, clusterInfo) {
} else {
// eslint-disable-next-line no-useless-catch
try {
return originalFunction.apply(ctx, originalArgs).then(onSuccess.bind(null, span), error => {
onError(span, error);
throw error;
});
const promise = originalFunction.apply(ctx, originalArgs);
if (typeof promise?.then === 'function') {
return promise.then(onSuccess.bind(null, span), error => {
onError(span, error);
throw error;
});
}
return promise;
} catch (e) {
// Immediately cleanup on synchronous errors.
throw e;
Expand Down Expand Up @@ -448,10 +452,14 @@ function instrumentedRequest(ctx, origEsReq, originalArgs) {
} else {
// eslint-disable-next-line no-useless-catch
try {
return origEsReq.apply(ctx, originalArgs).then(onSuccess.bind(null, span), error => {
onError(span, error);
throw error;
});
const promise = origEsReq.apply(ctx, originalArgs);
if (typeof promise?.then === 'function') {
return promise.then(onSuccess.bind(null, span), error => {
onError(span, error);
throw error;
});
}
return promise;
} catch (e) {
// Immediately cleanup on synchronous errors.
throw e;
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/tracing/instrumentation/databases/ioredis.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ function instrumentSendCommand(original) {
span.stack = tracingUtil.getStackTrace(wrappedInternalSendCommand);

callback = cls.ns.bind(onResult);
command.promise.then(
// make sure that the first parameter is never truthy
callback.bind(null, null),
callback
);
if (typeof command.promise?.then === 'function') {
command.promise.then(
// make sure that the first parameter is never truthy
callback.bind(null, null),
callback
);
}

return original.apply(client, argsForOriginal);

Expand Down Expand Up @@ -190,7 +192,7 @@ function instrumentMultiOrPipelineExec(clsContextForMultiOrPipeline, commandName
span.ts = Date.now();

const result = original.apply(this, arguments);
if (result.then) {
if (typeof result?.then === 'function') {
result.then(
results => {
endCallback.call(null, clsContextForMultiOrPipeline, span, null, results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ function handleCallbackOrPromise(ctx, originalArgs, originalFunction, span) {

const resultPromise = originalFunction.apply(ctx, originalArgs);

if (resultPromise && resultPromise.then) {
if (resultPromise && typeof resultPromise.then === 'function') {
resultPromise
.then(result => {
span.d = Date.now() - span.ts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function instrumentedMethod(ctx, originalFunction, originalArgs, stackTraceRef,
}

const promise = originalFunction.apply(ctx, originalArgs);
if (typeof promise.then === 'function') {
if (typeof promise?.then === 'function') {
promise
.then(value => {
finishSpan(null, span);
Expand Down
46 changes: 26 additions & 20 deletions packages/core/src/tracing/instrumentation/databases/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,23 @@ function instrumentedAccessFunction(
if (isPromiseImpl) {
const resultPromise = originalFunction.apply(ctx, originalArgs);

resultPromise
.then(result => {
span.d = Date.now() - span.ts;
span.transmit();
return result;
})
.catch(error => {
span.ec = 1;
tracingUtil.setErrorDetails(span, error, exports.spanName);

span.d = Date.now() - span.ts;
span.transmit();
return error;
});
if (typeof resultPromise?.then === 'function') {
resultPromise
.then(result => {
span.d = Date.now() - span.ts;
span.transmit();
return result;
})
.catch(error => {
span.ec = 1;
tracingUtil.setErrorDetails(span, error, exports.spanName);

span.d = Date.now() - span.ts;
span.transmit();
throw error;
});
}

return resultPromise;
}

Expand Down Expand Up @@ -238,12 +241,15 @@ function shimGetConnection(original) {

function shimPromiseConnection(original) {
return function getConnection() {
return original.apply(this, arguments).then(connection => {
shimmer.wrap(connection, 'query', shimPromiseQuery);
shimmer.wrap(connection, 'execute', shimPromiseExecute);

return connection;
});
const promise = original.apply(this, arguments);
if (typeof promise?.then === 'function') {
return promise.then(connection => {
shimmer.wrap(connection, 'query', shimPromiseQuery);
shimmer.wrap(connection, 'execute', shimPromiseExecute);

return connection;
});
}
};
}

Expand Down
21 changes: 11 additions & 10 deletions packages/core/src/tracing/instrumentation/frameworks/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ function instrumentedRoutes(thisContext, originalRoutes, originalArgs) {
const instrumentedDispatch = function (ctx, next) {
if (active && cls.isTracing()) {
const dispatchResult = dispatch.apply(this, arguments);
return dispatchResult.then(resolvedValue => {
if (ctx.matched && ctx.matched.length && ctx.matched.length > 0) {
const matchedRouteLayers = ctx.matched.slice();
matchedRouteLayers.sort(byLeastSpecificLayer);
const mostSpecificPath = normalizeLayerPath(matchedRouteLayers[matchedRouteLayers.length - 1].path);
annotateHttpEntrySpanWithPathTemplate(mostSpecificPath);
}
return resolvedValue;
});
if (typeof dispatchResult?.then === 'function') {
return dispatchResult.then(resolvedValue => {
if (ctx.matched && ctx.matched.length && ctx.matched.length > 0) {
const matchedRouteLayers = ctx.matched.slice();
matchedRouteLayers.sort(byLeastSpecificLayer);
const mostSpecificPath = normalizeLayerPath(matchedRouteLayers[matchedRouteLayers.length - 1].path);
annotateHttpEntrySpanWithPathTemplate(mostSpecificPath);
}
return resolvedValue;
});
}
} else {
return dispatch.apply(this, arguments);
}
};

// The router attaches itself as a property to the dispatch function and other methods in koa-router rely on this, so
// we need to attach this property to our dispatch function, too.
instrumentedDispatch.router = dispatch.router;
Expand Down
Loading