microsoft / ApplicationInsights-JS

Microsoft Application Insights SDK for JavaScript
MIT License
650 stars 240 forks source link

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

Open andreeatirgovatu opened 9 months ago

andreeatirgovatu commented 9 months ago

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

Thank you in advance, Andreea

MSNev commented 8 months ago

Is this for requests originating from the browser?

ie. you are sending out HEAD requests?

MSNev commented 8 months ago

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

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