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

Koa integration: resource tagging by route only works for routes defined by koa-router

Open leoyu037 opened this issue 8 years ago • 4 comments

A lot of our node-koa apps serve single page webapps that do client-side routing, so they don't use koa-router. When this is the case, it's hard to distinguish requests for the main payload with requests for other static assets in the APM UI.

For example in our "contacts" app, both main payload requests and static asset requests get bucketed into the same GET resource that doesn't have a route associated with it:

screen shot 2018-11-05 at 10 29 20 am

Both types of traces are commingled in this resource:

Static asset request: screen shot 2018-11-05 at 10 29 36 am

Main payload request: screen shot 2018-11-05 at 10 44 51 am

leoyu037 avatar Nov 05 '18 15:11 leoyu037

Specifically, we use koa-heartbeat and koa-static-server

leoyu037 avatar Nov 06 '18 16:11 leoyu037

This is a major pain point for my team as well. I would love some documentation using the official libraries which explains how to implement custom resource endpoints.

KempfCreative avatar Mar 11 '20 00:03 KempfCreative

There are a few options depending on the specific case:

koa-heartbeat

In this case, since it's only a heartbeat I assume what is wanted is to ignore these routes. This can be done with the blacklist option of the tracer which will stop tracing these routes:

tracer.use('koa', {
  blacklist: ['/healthz']
})
Custom or unsupported routers

In this case the route can be added manually in a span hook:

tracer.use('koa', {
  hooks: {
    request: (span, req, res) => {
      span.setTag('http.route', req.myCustomRoute)
    }
  }
})
koa-static-server

In this case, it's not really possible to provide a relevant value since there is no route. Using the URL is not really a good option since the cardinality would be really high and the produced data would not really be valuable. If you know that there is a finite number of known static assets that do not vary a lot, then it would be possible to use the hook from above as if it was a route.

We could definitely document all of this better, but otherwise it should be possible to achieve the end goal with the existing constructs.

Please let me know if this helps!

rochdev avatar Mar 11 '20 01:03 rochdev

Closing as there is no way to do this out of the box without a router that we can patch, and solutions for individual use cases were provided above.

rochdev avatar May 18 '21 20:05 rochdev