Problem Statement
It is a lot more convenient to attach decorators to class methods than to wrap their contents in a callback and bump up nesting by one or two.
classThingDoer{doThing(){Sentry.withScope((scope)=>{Sentry.startSpan({name: 'ThingDoer.doThing',},()=>{scope.setExtra('extra','extra');console.log('doing thing');},);});}}vs.
classThingDoer{
@Sentry.WithScope()
@Sentry.WithSpan()doThing(){constscope=Sentry.getCurrentScope();scope.setExtra('extra','extra');console.log('doing thing');}
Also, should Sentry.startSpan() not be named Sentry.withSpan()?
Solution Brainstorm
Rudimentary span implementation:
exportfunctionWithSpan(spanConfig?: SentryTypes.StartSpanOptions,): MethodDecorator{// @ts-expect-error -- Not sure what the error isreturnfunction<Textends(...args: readonlyunknown[])=>Promise<unknown>>(target: object,propertyKey: string|symbol,descriptor: TypedPropertyDescriptor<T>,){constoriginalMethod=descriptor.value;// istanbul ignore nextif(!originalMethod)return;constconfig: SentryTypes.StartSpanOptions=spanConfig??{name: target.constructor.name+'.'+propertyKey.toString(),op: 'method',};// @ts-expect-error -- Not sure what the error isdescriptor.value=function(...args: unknown[]): Promise<unknown>{returnSentry.startSpan(config,()=>originalMethod.apply(this,args));};};}
Problem Statement
It is a lot more convenient to attach decorators to class methods than to wrap their contents in a callback and bump up nesting by one or two.
vs.
Also, should
Sentry.startSpan()not be namedSentry.withSpan()?Solution Brainstorm
Rudimentary span implementation: