Skip to content

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

@cnolascobim

Description

@cnolascobim

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions