question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Per-request custom properties on auto-collected request

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
OsvaldoRosadocommented, May 8, 2018

What you tried is actually quite close!

What you’ll want instead is appInsights.defaultClient.commonProperties[myKey] = 'foo';

client.context.tags is 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:

appInsights.defaultClient.addTelemetryProcessor(( envelope, context ) => {
    var data = envelope.data.baseData;
    data.properties['mykey'] = 'myvalue';
    return true;
});

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 commonProperties will be the right one for the request being recorded at the time of telemetry recording.

1reaction
goto1commented, Dec 14, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found