Faro: Add missing SessionInstrumentation for Faro config (#79826)

This commit is contained in:
Marco Schaefer 2024-01-03 13:00:27 +01:00 committed by GitHub
parent b1c7aa269b
commit 7d5432e10a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View File

@ -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[] = [];

View File

@ -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() {