diff --git a/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.test.ts b/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.test.ts index 0127f44ae3d..d2da4ba6b8a 100644 --- a/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.test.ts +++ b/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.test.ts @@ -1,21 +1,13 @@ import { BuildInfo } from '@grafana/data'; import { GrafanaEdition } from '@grafana/data/src/types/config'; import { BaseTransport, Instrumentation, InternalLoggerLevel } from '@grafana/faro-core'; +import * as faroWebSdkModule from '@grafana/faro-web-sdk'; import { FetchTransport, initializeFaro } from '@grafana/faro-web-sdk'; import { EchoEventType, EchoMeta } from '@grafana/runtime'; import { GrafanaJavascriptAgentBackend, GrafanaJavascriptAgentBackendOptions } from './GrafanaJavascriptAgentBackend'; import { GrafanaJavascriptAgentEchoEvent } from './types'; -jest.mock('@grafana/faro-web-sdk', () => { - const originalModule = jest.requireActual('@grafana/faro-web-sdk'); - return { - __esModule: true, - ...originalModule, - initializeFaro: jest.fn(), - }; -}); - describe('GrafanaJavascriptAgentEchoBackend', () => { beforeEach(() => { jest.resetAllMocks(); @@ -50,10 +42,15 @@ describe('GrafanaJavascriptAgentEchoBackend', () => { }, }; - it('will set up FetchTransport if customEndpoint is provided', async () => { + it('will set up FetchTransport if customEndpoint is provided', () => { // arrange - const originalModule = jest.requireActual('@grafana/faro-web-sdk'); - jest.mocked(initializeFaro).mockImplementation(originalModule.initializeFaro); + jest.spyOn(faroWebSdkModule, 'initializeFaro').mockReturnValueOnce({ + ...faroWebSdkModule.faro, + api: { + ...faroWebSdkModule.faro.api, + setUser: jest.fn(), + }, + }); //act const backend = new GrafanaJavascriptAgentBackend(options); @@ -63,7 +60,7 @@ describe('GrafanaJavascriptAgentEchoBackend', () => { expect(backend.transports[0]).toBeInstanceOf(FetchTransport); }); - it('will initialize GrafanaJavascriptAgent and set user', async () => { + it('will initialize GrafanaJavascriptAgent and set user', () => { // arrange const mockedSetUser = jest.fn(); const mockedInstrumentationsForConfig: Instrumentation[] = []; @@ -159,7 +156,7 @@ describe('GrafanaJavascriptAgentEchoBackend', () => { }); }); - it('will forward events to transports', async () => { + it('will forward events to transports', () => { //arrange const mockedSetUser = jest.fn(); const mockedInstrumentationsForConfig: Instrumentation[] = []; diff --git a/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.ts b/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.ts index b93448c2599..1ac479de07b 100644 --- a/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.ts +++ b/public/app/core/services/echo/backends/grafana-javascript-agent/GrafanaJavascriptAgentBackend.ts @@ -2,12 +2,13 @@ import { BuildInfo } from '@grafana/data'; import { BaseTransport } from '@grafana/faro-core'; import { initializeFaro, - defaultMetas, BrowserConfig, ErrorsInstrumentation, ConsoleInstrumentation, WebVitalsInstrumentation, + SessionInstrumentation, FetchTransport, + type Instrumentation, } from '@grafana/faro-web-sdk'; import { EchoBackend, EchoEvent, EchoEventType } from '@grafana/runtime'; @@ -31,8 +32,9 @@ export class GrafanaJavascriptAgentBackend transports: BaseTransport[]; constructor(public options: GrafanaJavascriptAgentBackendOptions) { - // configure instrumentalizations - const instrumentations = []; + // configure instrumentations. + const instrumentations: Instrumentation[] = []; + this.transports = []; if (options.customEndpoint) { @@ -49,6 +51,9 @@ export class GrafanaJavascriptAgentBackend instrumentations.push(new WebVitalsInstrumentation()); } + // session instrumentation must be added! + instrumentations.push(new SessionInstrumentation()); + // initialize GrafanaJavascriptAgent so it can set up its hooks and start collecting errors const grafanaJavaScriptAgentOptions: BrowserConfig = { globalObjectKey: options.globalObjectKey || 'faro', @@ -64,7 +69,6 @@ export class GrafanaJavascriptAgentBackend 'ResizeObserver loop completed', 'Non-Error exception captured with keys', ], - metas: [...defaultMetas], sessionTracking: { persistent: true, generateSessionId() {