Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use withSpan() inside Span decorator #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
136 changes: 45 additions & 91 deletions src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,56 @@ export interface ISpanConfig {
type?: string;
}

export function withSpan<T>(fn: T, config?: ISpanConfig): T {
const spanName = config && config.name ? config.name : (fn as any).name;
const spanType = config && config.type ? config.type : 'function';

return ((...args: any) => {
const span = activeApm.startSpan(spanName, spanType);

try {
const ret = (fn as any)(...args);

if (span) {
if (ret && ret.then && ret.catch) {
ret.then(() => span.end()).catch((err: Error) => {
activeApm.captureError(err);
function executeSpan<T>(fn: T, spanName: string, spanType: string, ...args: any) {
const span = activeApm.startSpan(spanName, spanType);

try {
const ret = (fn as any)(...args);

if (span) {
if (ret && ret.then && ret.catch) {
return ret
.then((res: any) => {
span.end()
return res;
})
.catch((err: Error) => {
activeApm.captureError(err);

if (activeApm.currentTransaction) {
activeApm.currentTransaction.result = 'error';
}
if (activeApm.currentTransaction) {
activeApm.currentTransaction.result = 'error';
}

span.end();
});
} else {
span.end();
}
throw err;
});
} else {
span.end();
}
}

return ret;
} catch (err) {
activeApm.captureError(err);

if (activeApm.currentTransaction) {
activeApm.currentTransaction.result = 'error';
}
return ret;
} catch (err) {
activeApm.captureError(err);

if (span) {
span.end();
}
if (activeApm.currentTransaction) {
activeApm.currentTransaction.result = 'error';
}

throw err;
if (span) {
span.end();
}

throw err;
}
}

export function withSpan<T>(fn: T, config?: ISpanConfig): T {
const spanName = config && config.name ? config.name : (fn as any).name;
const spanType = config && config.type ? config.type : 'function';

return ((...args: any) => {
return executeSpan(fn, spanName, spanType, ...args);
}) as any;
}

Expand All @@ -67,64 +77,8 @@ export function Span(config?: ISpanConfig) {
const spanName = config.name || `${className}.${fnName}`;
const spanType = config.type || className;

if (descriptor.value.constructor.name === 'AsyncFunction') {
descriptor.value = async function(...args: any[]) {
const span = activeApm.startSpan(spanName, spanType);

try {
const ret = await original.apply(this, args);

if (span) {
span.end();
}

return ret;
} catch (err) {
activeApm.captureError(err);
const curTransaction = activeApm.currentTransaction;

if (curTransaction) {
curTransaction.result = 'error';
}

if (span) {
span.end();
}

throw err;
}
};
} else {
descriptor.value = function(...args: any[]) {
const span = activeApm.startSpan(spanName, spanType);

try {
const ret = original.apply(this, args);

if (span) {
if (ret && ret.then && ret.catch) {
ret.then(() => span.end()).catch(() => span.end());
} else {
span.end();
}
}

return ret;
} catch (err) {
activeApm.captureError(err);
const curTransaction = activeApm.currentTransaction;

if (curTransaction) {
curTransaction.result = 'error';
}

if (span) {
span.end();
}

throw err;
}
};
}
descriptor.value = function(...args: any[]) {
return executeSpan(original.bind(this), spanName, spanType, ...args);
};
};
}