Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move datadog rum methods to service (DEV-190) #572

Merged
merged 5 commits into from Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/app/app.component.spec.ts
Expand Up @@ -12,14 +12,33 @@ import { TranslateModule } from '@ngx-translate/core';
import { TestConfig } from 'test.config';
import { AppInitService } from './app-init.service';
import { AppComponent } from './app.component';
import { DspApiConfigToken, DspApiConnectionToken } from './main/declarations/dsp-api-tokens';
import { DspApiConfigToken, DspApiConnectionToken, DspInstrumentationToken } from './main/declarations/dsp-api-tokens';
import { DspDataDogConfig, DspInstrumentationConfig } from './main/declarations/dsp-instrumentation-config';
import { HeaderComponent } from './main/header/header.component';
import { SelectLanguageComponent } from './main/select-language/select-language.component';
import { UserMenuComponent } from './user/user-menu/user-menu.component';

describe('AppComponent', () => {

beforeEach(waitForAsync(() => {

const dspDatadogSpy = new DspDataDogConfig(false, '', '', '', '');

const instrumentationConfig: DspInstrumentationConfig = {
environment: 'dev',
dataDog: {
enabled: false,
applicationId: 'app_id',
clientToken: 'client_token',
site: 'site',
service: 'dsp-app'
},
rollbar: {
enabled: false,
accessToken: 'rollbar_token'
}
};

TestBed.configureTestingModule({
declarations: [
AppComponent,
Expand Down Expand Up @@ -47,7 +66,11 @@ describe('AppComponent', () => {
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
}
},
{
provide: DspInstrumentationToken,
useValue: instrumentationConfig
},
]
}).compileComponents();
}));
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.component.ts
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { DatadogRumService } from './main/services/datadog-rum.service';

@Component({
selector: 'app-root',
Expand All @@ -14,12 +15,15 @@ export class AppComponent implements OnInit {

constructor(
private _router: Router,
private _titleService: Title
private _titleService: Title,
private _datadogRumService: DatadogRumService
) {

// set the page title
this._titleService.setTitle('DaSCH Service Platform');

// init datadog RUM
this._datadogRumService.initializeRum();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the datadogRumService initialized here and not at time of construction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in a48ea3d

}

ngOnInit() {
Expand Down
26 changes: 6 additions & 20 deletions src/app/main/action/login-form/login-form.component.spec.ts
Expand Up @@ -17,6 +17,7 @@ import { AppInitService } from 'src/app/app-init.service';
import { TestConfig } from 'test.config';
import { DspApiConfigToken, DspApiConnectionToken, DspInstrumentationToken } from '../../declarations/dsp-api-tokens';
import { DspDataDogConfig, DspInstrumentationConfig } from '../../declarations/dsp-instrumentation-config';
import { DatadogRumService } from '../../services/datadog-rum.service';
import { Session, SessionService } from '../../services/session.service';
import { LoginFormComponent } from './login-form.component';

Expand Down Expand Up @@ -57,23 +58,6 @@ describe('LoginFormComponent', () => {

beforeEach(waitForAsync(() => {

const dspDatadogSpy = new DspDataDogConfig(false, '', '', '', '');

const instrumentationConfig: DspInstrumentationConfig = {
environment: 'dev',
dataDog: {
enabled: false,
applicationId: 'app_id',
clientToken: 'client_token',
site: 'site',
service: 'dsp-app'
},
rollbar: {
enabled: false,
accessToken: 'rollbar_token'
}
};

const authEndpointSpyObj = {
admin: {
usersEndpoint: jasmine.createSpyObj('usersEndpoint', ['getUser'])
Expand All @@ -83,6 +67,8 @@ describe('LoginFormComponent', () => {
}
};

const datadogRumServiceSpy = jasmine.createSpyObj('datadogRumService', ['initializeRum', 'setActiveUser', 'removeActiveUser']);

TestBed.configureTestingModule({
declarations: [
LoginFormComponent,
Expand All @@ -106,9 +92,9 @@ describe('LoginFormComponent', () => {
useValue: authEndpointSpyObj
},
{
provide: DspInstrumentationToken,
useValue: instrumentationConfig
},
provide: DatadogRumService,
useValue: datadogRumServiceSpy
}
]
})
.compileComponents();
Expand Down
28 changes: 4 additions & 24 deletions src/app/main/action/login-form/login-form.component.ts
Expand Up @@ -4,6 +4,7 @@ import { ApiResponseData, ApiResponseError, KnoraApiConnection, LoginResponse, L
import { datadogRum, RumFetchResourceEventDomainContext } from '@datadog/browser-rum';
import { DspApiConnectionToken, DspInstrumentationToken } from '../../declarations/dsp-api-tokens';
import { DspInstrumentationConfig } from '../../declarations/dsp-instrumentation-config';
import { DatadogRumService } from '../../services/datadog-rum.service';
import { NotificationService } from '../../services/notification.service';
import { Session, SessionService } from '../../services/session.service';

Expand Down Expand Up @@ -105,7 +106,7 @@ export class LoginFormComponent implements OnInit {

constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
@Inject(DspInstrumentationToken) private _dspInstrumentationConfig: DspInstrumentationConfig,
private _datadogRumService: DatadogRumService,
private _notification: NotificationService,
private _sessionService: SessionService,
private _fb: FormBuilder
Expand Down Expand Up @@ -156,29 +157,7 @@ export class LoginFormComponent implements OnInit {
this.session = this._sessionService.getSession();
this.loginSuccess.emit(true);
this.loading = false;
if (this._dspInstrumentationConfig.dataDog.enabled) {
datadogRum.init({
applicationId: this._dspInstrumentationConfig.dataDog.applicationId,
clientToken: this._dspInstrumentationConfig.dataDog.clientToken,
site: this._dspInstrumentationConfig.dataDog.site,
service: this._dspInstrumentationConfig.dataDog.service,
env: this._dspInstrumentationConfig.environment,
version: appVersion,
sampleRate: 100,
trackInteractions: true,
beforeSend: (event, context) => {
// collect a RUM resource's response headers
if (event.type === 'resource' && event.resource.type === 'xhr') {
event.context = { ...event.context, responseHeaders: (context as RumFetchResourceEventDomainContext).response.body };
}
},
});

datadogRum.setUser({
id: identifier,
identifierType: identifierType
});
}
this._datadogRumService.setActiveUser(identifier, identifierType);
}
);
},
Expand Down Expand Up @@ -215,6 +194,7 @@ export class LoginFormComponent implements OnInit {
(response: ApiResponseData<LogoutResponse>) => {
this.logoutSuccess.emit(true);
this._sessionService.destroySession();
this._datadogRumService.removeActiveUser();
this.loading = false;
this.buildLoginForm();
this.session = undefined;
Expand Down
48 changes: 48 additions & 0 deletions src/app/main/services/datadog-rum.service.spec.ts
@@ -0,0 +1,48 @@
import { TestBed } from '@angular/core/testing';

import { DatadogRumService } from './datadog-rum.service';

describe('DatadogRumService', () => {
let service: DatadogRumService;
const mockdatadogRumService = jasmine.createSpyObj('datadogRumService', ['initializeRum', 'setActiveUser', 'removeActiveUser']);

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: DatadogRumService, useValue: mockdatadogRumService }
]
});
service = TestBed.inject(DatadogRumService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

describe('initializeRum', () => {
it('should initialize the RUM service', () => {
const arg = 'test';
mockdatadogRumService.initializeRum.and.callThrough();
service.initializeRum();
expect(service.initializeRum).toHaveBeenCalled();
});
});

describe('setActiveUser', () => {
it('should set the active user', () => {
const identifier = 'test';
const identifierType = 'email';
mockdatadogRumService.setActiveUser.and.callThrough();
service.setActiveUser(identifier, identifierType);
expect(service.setActiveUser).toHaveBeenCalledOnceWith(identifier, identifierType);
});
});

describe('removeActiveUser', () => {
it('should remove the active user', () => {
mockdatadogRumService.removeActiveUser.and.callThrough();
service.removeActiveUser();
expect(service.removeActiveUser).toHaveBeenCalled();
});
});
});
48 changes: 48 additions & 0 deletions src/app/main/services/datadog-rum.service.ts
@@ -0,0 +1,48 @@
import { Inject, Injectable } from '@angular/core';
import { datadogRum, RumFetchResourceEventDomainContext } from '@datadog/browser-rum';
import { DspInstrumentationToken } from '../declarations/dsp-api-tokens';
import { DspInstrumentationConfig } from '../declarations/dsp-instrumentation-config';

const { version: appVersion } = require('../../../../package.json');

@Injectable({
providedIn: 'root'
})
export class DatadogRumService {

constructor(
@Inject(DspInstrumentationToken) private _dspInstrumentationConfig: DspInstrumentationConfig
) { }

initializeRum(): void {
if (this._dspInstrumentationConfig.dataDog.enabled) {
datadogRum.init({
applicationId: this._dspInstrumentationConfig.dataDog.applicationId,
clientToken: this._dspInstrumentationConfig.dataDog.clientToken,
site: this._dspInstrumentationConfig.dataDog.site,
service: this._dspInstrumentationConfig.dataDog.service,
env: this._dspInstrumentationConfig.environment,
version: appVersion,
sampleRate: 100,
trackInteractions: true,
beforeSend: (event, context) => {
// collect a RUM resource's response headers
if (event.type === 'resource' && event.resource.type === 'xhr') {
event.context = { ...event.context, responseHeaders: (context as RumFetchResourceEventDomainContext).response.body };
}
},
});
}
}

setActiveUser(identifier: any, identifierType: 'iri' | 'email' | 'username'): void {
Copy link
Contributor

@subotic subotic Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to check if datadogRum is initialized, and only then set the user. Otherwise you can probably ignore it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in a48ea3d

datadogRum.setUser({
id: identifier,
identifierType: identifierType
});
}

removeActiveUser(): void {
datadogRum.removeUser();
Copy link
Contributor

@subotic subotic Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise, check if datadogRum is initialized.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in a48ea3d

}
}