svelte-adapter-azure-swa icon indicating copy to clipboard operation
svelte-adapter-azure-swa copied to clipboard

Correct URLs in Application Insights

Open tlaundal opened this issue 1 year ago • 1 comments

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.

tlaundal avatar May 12 '23 18:05 tlaundal

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.

geoffrich avatar May 24 '23 00:05 geoffrich