#injecting service into decorator

1 messages · Page 1 of 1 (latest)

cosmic sierra
#

I need to create decorator which will be used to generate traces, to do so I will need an external service injected in this decorator - what's the recommended way of doing such thing? can I somehow simply inject service into decorator?

stone cloud
#

I think you are looking for Interceptors : https://docs.nestjs.com/interceptors

cosmic sierra
#

I don't think so - I want to trace just a single method not a whole request

wicked spindle
#

you could set a metadata key for the decorated method and on startup wrap all methods that have this metadata with a trace function

gleaming flume
#

While it's not possible to inject a service into a decorator, you can take a method decorator and modify the descriptor.value to reference the class's injected values. I have an example of this with my ogma package where I provide a @Log() method that printed a log at the beginning and end of the method for tracing purposes

tribal steppe
crystal hemlock
#

@cosmic sierra If you need to generate traces, just go with OTEL. There's no point in inventing a custom solution. https://www.npmjs.com/package/nestjs-otel

cosmic sierra
crystal hemlock
#

@OtelHistorgram might do that