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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f811a48
refactor: moves datadog rum methods to a service
mdelez af266cc
cleanup: remove console warning
mdelez 0ec3029
Merge branch 'main' into wip/dev-190-datadog-rum-init-refactor
mdelez a48ea3d
refactor(datadog-rum): moves service init to app-init service and che…
mdelez 9eb0382
refactor: further refactoring of datadog rum init
mdelez File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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('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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 | ||
) { | ||
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 { | ||
if(datadogRum.getInternalContext().application_id) { | ||
datadogRum.setUser({ | ||
id: identifier, | ||
identifierType: identifierType | ||
}); | ||
} | ||
} | ||
|
||
removeActiveUser(): void { | ||
if(datadogRum.getInternalContext().application_id) { | ||
datadogRum.removeUser(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
changed in a48ea3d