Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct URLs in Application Insights #139

Open
tlaundal opened this issue May 12, 2023 · 1 comment
Open

Correct URLs in Application Insights #139

tlaundal opened this issue May 12, 2023 · 1 comment

Comments

@tlaundal
Copy link
Contributor

tlaundal commented May 12, 2023

This has been covered before in #30 and #126.

I wanted to bring it up again, as I've found what I think is a quite "correct" solution, and I want to discuss whether and how to incorporate or document it in this repo. This solution differs from previous solutions in that it works with the setAutoCollectIncomingRequestAzureFunctions(true) option of application insights for node.

The first part of the solution is to provide the correct URL to Application Insights by setting a custom property on the correlation context. This can be done in a handle hook with the following code:

CorrelationContextManager.getCurrentContext()?.customProperties?.setProperty('originalUrl', encodeURIComponent(event.request.url));

To actually set this as the URL in application insights, we need to add a telemetry processor like this sometime during startup:

applicationInsights.defaultClient.addTelemetryProcessor((envelope, context) => {
	const baseData = envelope?.data?.baseData as RequestData;
	if (!baseData || envelope.data.baseType !== "RequestData") return true;

	if (!baseData.properties) baseData.properties = {};

	const originalUrl = context?.correlationContext?.customProperties?.getProperty('originalUrl');
	if (originalUrl) {
		const url = decodeURIComponent(originalUrl);
		baseData.name = (baseData.name || '').replace(baseData.url, url);
		baseData.url = url;
	}

	return true;
})

I think we should consider implementing this in the adapter with an opt-in configuration option. Another option would be to document it in the README.

Another small note on application insights and SWA: If you enable application insights integration for the SWA, SWA will log every request to AI before they reach your function. The two entries for each request will have the same value in the id field, so they can be correlated together. From what I've found, there is no hope of customizing the logging done by SWA. However, the logging from SWA gives GeoIP information about the user which is not available to AI in node it seems. Because of this, I think it makes most sense to use both, and have some queries which automatically correlates them.

@geoffrich
Copy link
Owner

I'm open to having this in the adapter itself, if you're willing to submit a PR. Having accurate URLs in App Insights is important.

If I'm understanding correctly, we'd want to add this code to entry.js?

  • the CorrelationContextManager bit would run on each request
  • the telemetry processor would run during startup

If we do have this be opt-in config, how do we make it so the applicationinsights package is an optional dependency and only included for apps that opted-in?

My main goal for any implementation would be to minimize the need to add additional config/extensibility to any integration - ideally you could just toggle this config on to get the correct URL and do any further App Insights customization in your own app, instead of the adapter needing to expose additional config/hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants