microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
324 stars 141 forks source link

How can I add custom dimensions to my traces? #1148

Closed justinmchase closed 1 year ago

justinmchase commented 1 year ago

I am attempting to add some info per request to all spans. But whatever values we set when calling span.setAttribute(...) seem to not ever show up in our logs in app insights.

We are using koa, here is an example of what we're trying to do:

const config = new ApplicationInsightsConfig()
const appInsights = new ApplicationInsightsClient(config)
const traceHandler = appInsights.getTraceHandler()
traceHandler.addInstrumentation(new KoaInstrumentation({
  requestHook: (span, info) => {
    span.setAttribute('example', 'abc123')
  }
})

Using a debugger I can see that the request hook is called and I can see that the attribute is set onto the span yet when I look into application insights later, the attribute is not present on the log. A lot of standard data about the request appeared, so I can see that exact route was logged but the example attribute is nowhere in the data.

I am expecting the data to appear in the customDimensions field in the Log Analytics Workspace data. I am able to easily do this in the dotnet version of this library but can't seem to figure out how to use this library to track the same data.

justinmchase commented 1 year ago

Looking at the docs more I see this message:

Any attributes you add to spans are exported as custom properties. They populate the customDimensions field in the requests or the dependencies tables in Application Insights.

Which is what I want and expect, but it really seems like when setting the attribute in the instrumentation in this way it does not actually propagate into the customDimensions and I'm not sure why.

justinmchase commented 1 year ago

Derp... reading it closely I see this:

...or the dependencies tables...

I queried the dependency tables and boom, I can see them in there instead of the requests table.

So given that, can I ask what determines whether a metric goes into the requests table vs the dependency table?

hectorhdzg commented 1 year ago

@justinmchase client Spans will end as dependency telemetry and servers Spans will end as request telemetry.