ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

[BUG] Filtering logs with status code 308 does not work

Open andreeatirgovatu opened this issue 1 year ago • 2 comments

Hi,

I was wondering is there the ability to prevent logs with status code 308 (HEAD method) and static files requests. I tried some things within react app like :

    const filteringFn = (envelope: ITelemetryItem) => {
        if (envelope.baseData?.responseCode ===  308 ||  envelope.baseData?.name?.includes('_next')) {
            return false;
        }
        return true;
    };

    const initAppInsights = () => {
        if (isBrowser() && config.appInsightsKey && !window.appInsights) {
            const appInsights = new ApplicationInsights({
                config: {
                    instrumentationKey: config.appInsightsKey,
                    loggingLevelConsole: config.logLevel || 0,
                    loggingLevelTelemetry: config.logLevel || 0,
                },
            });
            appInsights.loadAppInsights();
            appInsights.addTelemetryInitializer(filteringFn);
            window.appInsights = appInsights;
        }
    };

    useEffect(() => {
        initAppInsights();
    }, []);

but it is not working. In app insights I can still see the logs . Screenshot 2024-01-26 at 23 00 32

  • SDK Version [e.g. 22]: "@microsoft/applicationinsights-web": "^2.6.1"

Thank you in advance, Andreea

andreeatirgovatu avatar Jan 26 '24 21:01 andreeatirgovatu

Is this for requests originating from the browser?

ie. you are sending out HEAD requests?

MSNev avatar Feb 02 '24 23:02 MSNev

If so we added the ability to add a dependency listener / initializer (the initializer is what you would want here), where you can "inspect" the events before they are "tracked" -- https://github.com/Microsoft/ApplicationInsights-JS?tab=readme-ov-file#dependency-listeners

  • These where added in 2.8.7

Failing that you can use a standard telemetryInitializer() to inspect every event and just return false when you detect that is an event you don't want to include. https://github.com/Microsoft/ApplicationInsights-JS?tab=readme-ov-file#dependency-listeners

MSNev avatar Feb 02 '24 23:02 MSNev