Per-request custom properties on auto-collected request
See original GitHub issueI can’t seem to find any docs or examples about how to add custom properties onto auto-collected requests. For example, a userId or any other data that is discovered or calculated during the processing of a request.
I’ve tried stashing the data into
appInsights.defaultClient.context.tags[myKey] = 'foo';
but the additional properties don’t show up in customDimensions (or anywhere if I try | search ‘foo’).
Am I missing a mechanism that allows custom properties to be added to auto-collected requests?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:20 (9 by maintainers)
Top Results From Across the Web
Adding custom properties for each request in Application ...
It looks like adding new properties to existing request is possible using ITelemetryInitializer as mentioned here. I created sample class as given below...
Read more >Adding custom properties for each request in ... - MSDN
I'm trying to add custom properties to by Application Insights to each request. I'm trying to achieve the same thing as described on...
Read more >How to access TestCase custom property in a request?
In the test step custom properties, I have a property named "Username" with value "XXXXXXXXXX". The test step name is "TC57". Then, in...
Read more >View Custom Properties of a File or Folder in an ObjectStore ...
Use this operation to list specific or all custom properties of a file or folder that is public shared or resides within a...
Read more >Requesting custom properties with private app integration
Hello, I'm moving from an API key to a private app. When importing data from Hubspot, I typically request a set of custom...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

What you tried is actually quite close!
What you’ll want instead is
appInsights.defaultClient.commonProperties[myKey] = 'foo';client.context.tagsis only for well-known Application Insights fields (like user id via the property nameai.user.id) rather than custom properties. You can reference the list of accepted tag names through the ContextTagKeys class (https://github.com/Microsoft/ApplicationInsights-node.js/blob/7b145dcfdbe7afac146e8ef27898395505dcb2df/Declarations/Contracts/Generated/ContextTagKeys.ts )Alternatively, the generic approach for modifying telemetry items (autocollected or otherwise) is a telemetry processor. They’re described in the readme but here’s an example:
If you’re using automatic dependency correlation, you can stash your own data for retrieval via the context variable by setting fields on the object returned by
appInsights.getCorrelationContext(). The readme has more in-depth info about that (and what data is returned in the context variable by default)The telemetry processor + context approach will be the most reliable if you handle requests in parallel (and the values are determined based on the request), as you otherwise won’t be able to ensure that the value you set in
commonPropertieswill be the right one for the request being recorded at the time of telemetry recording.Would you accept a PR that shows an example that uses a custom property on the
getCorrelationContext()object and uses a custom telemetry processor to track per-request data?Similar to something like the following:
I had to dig through a bunch of issues before I was able to figure out what to do, so an example would be nice to have in the docs.