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

(version 3.x) Cloud role name and manual trackRequest being overwritten by Azure #1393

Open
cnolascobim opened this issue Oct 1, 2024 · 1 comment
Assignees

Comments

@cnolascobim
Copy link

cnolascobim commented Oct 1, 2024

I have a Svelte project deployed to Azure as a Web App. I have application insights configured in the project to track the server-side. "applicationinsights": "^3.2.2"

When I tested everything locally on my macbook, everything worked perfectly. I could see the requests (using the built in trackRequest function) with my custom operation names on my Azure dashboard (dev environment) under the correct cloud role name in the Performance tab.

Since everything was good, I merged it to main which deployed to the same app insights dev instance. However, as soon as the App Service was up and running the cloud role name and tracking request function I've done have been overwritten by Azure. It also somehow doesn't work locally anymore (as in I can no longer see my manual trackRequests displayed in the Performance tab) . Strangely though I can still see the requests I manually track come in the LiveMetrics tab.

I've done everything from forcing the auto collection to be false. I've debugged it on the log stream and it is being set false but somehow autotracking under the wrong role name (it replaces it with my web app's name instead) is still working.

Is this a 3.x issue? Should I downgrade? Or am I doing something wrong here? I just wanna see the requests I manually track on the Performance tab again..

import { env } from '$env/dynamic/public';
import appInsights from 'applicationinsights';
import type { ApplicationInsightsServerOperationPayload } from './application-insights.model';

export class AppInsightsServerClient {
	private static instance: AppInsightsServerClient;
	private isInitialized = false;

	constructor() {}

	public static getInstance(): AppInsightsServerClient {
		if (!AppInsightsServerClient.instance) {
			AppInsightsServerClient.instance = new AppInsightsServerClient();
			AppInsightsServerClient.instance.initialize();
		}
		return AppInsightsServerClient.instance;
	}

	private initialize() {
		if (this.isInitialized) return;

		const initializer = appInsights
			.setup(env.PUBLIC_APPLICATIONINSIGHTS_CONNECTION_STRING)
			.setAutoCollectRequests(false) // Disable request auto-collection
			.setAutoCollectPerformance(false, false) // Disable performance auto-collection
			.setAutoCollectExceptions(false) // Disable exception auto-collection
			.setAutoCollectDependencies(false) // Disable dependency auto-collection
			.setAutoCollectConsole(false) // Disable console logging
			.setInternalLogging(true, true); // Disable internal logging;

		// appInsights.defaultClient.config.enableAutoCollectRequests = false;
		// appInsights.defaultClient.config.enableAutoCollectDependencies = false;

		console.log('Auto-collect requests:', appInsights.defaultClient.config.enableAutoCollectRequests);
		console.log('Auto-collect dependencies:', appInsights.defaultClient.config.enableAutoCollectDependencies);

		appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.cloudRole] =
			env.PUBLIC_APPLICATIONINSIGHTS_SERVER_ROLE_NAME;

		console.log('Cloud role name in environment:', env.PUBLIC_APPLICATIONINSIGHTS_SERVER_ROLE_NAME);
		console.log(
			'Cloud role name in Azure context:',
			appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.cloudRole]
		);

		initializer.start();
		console.log('AppInsightsServerClient initialized');
		console.log(appInsights.defaultClient.commonProperties);

		this.isInitialized = true;
	}

	public trackException(message: string) {
		appInsights.defaultClient.trackException({ exception: new Error(message) });
		appInsights.defaultClient.flush();
	}

	public trackOperation(data: ApplicationInsightsServerOperationPayload) {
		appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.operationName] = 'Node: ' + data.name;

		console.log(
			'Operation name in Azure context:',
			appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.operationName]
		);

		appInsights.defaultClient.trackRequest(data);

		console.log('Flushing telemetry...');
		appInsights.defaultClient.flush();
	}
}

export interface ApplicationInsightsServerOperationPayload {
	name: string;
	url: string;
	duration: number;
	resultCode: string;
	success: boolean;
}

It is initialised inside the top +layout.server.ts

export const load: LayoutServerLoad = async ({ fetch, request, params, cookies }) => {
	AppInsightsServerClient.getInstance();
}

Help would be much appreciated... very confused.

@cnolascobim cnolascobim changed the title Cloud role name and manual trackRequest being overwritten by Azure (3.x) Cloud role name and manual trackRequest being overwritten by Azure Oct 1, 2024
@cnolascobim cnolascobim changed the title (3.x) Cloud role name and manual trackRequest being overwritten by Azure (version 3.x) Cloud role name and manual trackRequest being overwritten by Azure Oct 1, 2024
@JacksonWeber JacksonWeber self-assigned this Oct 1, 2024
@JacksonWeber
Copy link
Contributor

@cnolascobim

  1. I don't know how you were seeing custom operation names using the v3.2.2 SDK as overriding operation name is not supported in v3 of the SDK. Is it possible you were seeing something like this with the operation name reported as a custom property?
    image

  2. As for setting the cloudRoleName, this issue addresses that.

  3. Regarding no longer being able to see your manually tracked requests under the performance tab. You shouldn't need to modify the initialization of the SDK at all in order to see manually tracked requests. You should be able to find something like this in your performance tab:
    image
    It may be an issue with your call to appInsights.defaultClient.flush(); as that method is async, so just make sure to await its completion to ensure that your manually tracked requests are exported.

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

No branches or pull requests

2 participants