dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

Hook not altering resource name to include path

Open jmichel-bc opened this issue 3 years ago • 5 comments

Expected behaviour I am expecting the resource to include the URL path alongside the method, e.g. "POST /login" Actual behaviour Only seeing all POST requests to the server grouped together, and all of the internal requests beng made to external services are being traced with the method + path

Steps to reproduce

I am using a fastify server, and the below tracer config:

tracer.use('http', {
  client: {
    service: "service-name",
    hooks: {
      request: (span, req, res) => {
        (span as Span).setTag('resource.name', `${req?.method} ${req?.path}`)
      }
    }
  }
});

(I have tried using the 'fastify' plugin, and applying a similar hook, but this also results in resources that don't contain the path):

tracer.use('fastify', {
  hooks:{
    request: (span:any, req, res) => {
      span.setTag('resource.name', `${req?.method} ${req?.url}`)
  }},

})

Environment

  • Node.js version: 14
  • Tracer version: ^2.7.0
  • Agent version:
  • Relevant library versions:

jmichel-bc avatar May 12 '22 22:05 jmichel-bc

Hi @jmichel-bc! Usually when this happens, it's because tracer.init() was called after fastify was required. tracer.init() needs to happen before any other requires.

By default, our HTTP server instrumentation does not add the path due to high cardinality and since the path may contain sensitive data. When using an instrumented web framework like fastify, we can get the parameterized path from that. Usually this is added without issue, but it might not work in cases where fastify was loaded too early (meaning before tracer.init() was called).

If you want the entire path every time, you could do something like the first config snippet you provided, but you'd need to switch from client to server on the second line.

bengl avatar May 13 '22 14:05 bengl

Hmmm, I have adjusted the code such that the initialised tracer is the first import; still having the same issue with no paths being appeneded to the resource name using the fastify plguin with the hook above!

jmichel-bc avatar May 16 '22 14:05 jmichel-bc

@jmichel-bc Can you share the import code? It looks from the above snippet that you're using TypeScript, which could be hoisting your imports depending on where you call them. We also provide an example of how to avoid hoisting in our setup docs.

rochdev avatar May 16 '22 15:05 rochdev

Hmmm, I have adjusted the code such that the initialised tracer is the first import; still having the same issue with no paths being appeneded to the resource name using the fastify plguin with the hook above!

I had a similar issue on my side and I've temporary downgraded to [email protected]

arnaud-zg avatar Jul 08 '22 12:07 arnaud-zg

@jmichel-bc Can you share the import code? It looks from the above snippet that you're using TypeScript, which could be hoisting your imports depending on where you call them. We also provide an example of how to avoid hoisting in our setup docs.

Indeed, what you've suggested works, I've followed the documentation and load the tracer with [email protected] before calling the method NestFactory.create and it works on my side.

arnaud-zg avatar Jul 08 '22 13:07 arnaud-zg

Looks like this issue is pretty old and the OP didn't reply to Roch's question. Is it still an issue? It looks like things were solved so I'll close it for now. Feel free to ping us and reopen if I'm misunderstanding!

tlhunter avatar Dec 18 '23 20:12 tlhunter