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
Changes from 2 commits
f811a48
af266cc
0ec3029
a48ea3d
9eb0382
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likewise, check if datadogRum is initialized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed in a48ea3d |
||
} | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed in a48ea3d