-
Notifications
You must be signed in to change notification settings - Fork 156
[ci-visibility] Implement driver-agnostic integration with CI Visibility #2639
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
Merged
amortemousque
merged 8 commits into
main
from
nikita-tkachenko/rum-ci-visibility-integration
Apr 3, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb85d18
Add ci visibility context form cookie
amortemousque ccfdb00
Merge branch 'main' into nikita-tkachenko/rum-ci-visibility-integration
amortemousque 365b8a5
Fix types
amortemousque df908cd
Fix test
amortemousque 257a042
Fix review
amortemousque 6141054
Add comment
amortemousque aa713b0
Fix tests
amortemousque cbe079f
Optimization
amortemousque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
import type { Subscription } from '@datadog/browser-core' | ||
import { ONE_MINUTE, deleteCookie, setCookie } from '@datadog/browser-core' | ||
import type { Clock } from '@datadog/browser-core/test' | ||
import { mockClock } from '@datadog/browser-core/test' | ||
import type { RumConfiguration } from '../domain/configuration' | ||
import { WATCH_COOKIE_INTERVAL_DELAY, createCookieObservable } from './cookieObservable' | ||
|
||
const COOKIE_NAME = 'cookie_name' | ||
const COOKIE_DURATION = ONE_MINUTE | ||
|
||
describe('cookieObservable', () => { | ||
let subscription: Subscription | ||
let originalSupportedEntryTypes: PropertyDescriptor | undefined | ||
let clock: Clock | ||
beforeEach(() => { | ||
clock = mockClock() | ||
originalSupportedEntryTypes = Object.getOwnPropertyDescriptor(window, 'cookieStore') | ||
}) | ||
|
||
afterEach(() => { | ||
subscription?.unsubscribe() | ||
if (originalSupportedEntryTypes) { | ||
Object.defineProperty(window, 'cookieStore', originalSupportedEntryTypes) | ||
} | ||
clock.cleanup() | ||
deleteCookie(COOKIE_NAME) | ||
}) | ||
|
||
it('should notify observers on cookie change', (done) => { | ||
const observable = createCookieObservable({} as RumConfiguration, COOKIE_NAME) | ||
|
||
subscription = observable.subscribe((cookieChange) => { | ||
expect(cookieChange).toEqual('foo') | ||
|
||
done() | ||
}) | ||
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION) | ||
clock.tick(WATCH_COOKIE_INTERVAL_DELAY) | ||
}) | ||
|
||
it('should notify observers on cookie change when cookieStore is not supported', () => { | ||
Object.defineProperty(window, 'cookieStore', { get: () => undefined, configurable: true }) | ||
const observable = createCookieObservable({} as RumConfiguration, COOKIE_NAME) | ||
|
||
let cookieChange: string | undefined | ||
subscription = observable.subscribe((change) => (cookieChange = change)) | ||
|
||
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION) | ||
clock.tick(WATCH_COOKIE_INTERVAL_DELAY) | ||
|
||
expect(cookieChange).toEqual('foo') | ||
}) | ||
|
||
it('should not notify observers on cookie change when the cookie value as not changed when cookieStore is not supported', () => { | ||
Object.defineProperty(window, 'cookieStore', { get: () => undefined, configurable: true }) | ||
const observable = createCookieObservable({} as RumConfiguration, COOKIE_NAME) | ||
|
||
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION) | ||
|
||
let cookieChange: string | undefined | ||
subscription = observable.subscribe((change) => (cookieChange = change)) | ||
|
||
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION) | ||
clock.tick(WATCH_COOKIE_INTERVAL_DELAY) | ||
|
||
expect(cookieChange).toBeUndefined() | ||
}) | ||
}) |
This file contains hidden or 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,64 @@ | ||
import type { Configuration, CookieStore } from '@datadog/browser-core' | ||
import { | ||
setInterval, | ||
clearInterval, | ||
Observable, | ||
addEventListener, | ||
ONE_SECOND, | ||
findCommaSeparatedValue, | ||
DOM_EVENT, | ||
find, | ||
} from '@datadog/browser-core' | ||
|
||
export interface CookieStoreWindow extends Window { | ||
cookieStore?: CookieStore | ||
} | ||
|
||
export type CookieObservable = ReturnType<typeof createCookieObservable> | ||
|
||
export function createCookieObservable(configuration: Configuration, cookieName: string) { | ||
const detectCookieChangeStrategy = (window as CookieStoreWindow).cookieStore | ||
? listenToCookieStoreChange(configuration) | ||
: watchCookieFallback | ||
|
||
return new Observable<string | undefined>((observable) => | ||
detectCookieChangeStrategy(cookieName, (event) => observable.notify(event)) | ||
) | ||
} | ||
|
||
function listenToCookieStoreChange(configuration: Configuration) { | ||
return (cookieName: string, callback: (event: string | undefined) => void) => { | ||
const listener = addEventListener( | ||
configuration, | ||
(window as CookieStoreWindow).cookieStore!, | ||
DOM_EVENT.CHANGE, | ||
(event) => { | ||
// Based on our experimentation, we're assuming that entries for the same cookie cannot be in both the 'changed' and 'deleted' arrays. | ||
// However, due to ambiguity in the specification, we asked for clarification: https://github.com/WICG/cookie-store/issues/226 | ||
const changeEvent = | ||
find(event.changed, (event) => event.name === cookieName) || | ||
find(event.deleted, (event) => event.name === cookieName) | ||
if (changeEvent) { | ||
callback(changeEvent.value) | ||
} | ||
} | ||
) | ||
return listener.stop | ||
} | ||
} | ||
|
||
export const WATCH_COOKIE_INTERVAL_DELAY = ONE_SECOND | ||
|
||
function watchCookieFallback(cookieName: string, callback: (event: string | undefined) => void) { | ||
const previousCookieValue = findCommaSeparatedValue(document.cookie, cookieName) | ||
const watchCookieIntervalId = setInterval(() => { | ||
const cookieValue = findCommaSeparatedValue(document.cookie, cookieName) | ||
if (cookieValue !== previousCookieValue) { | ||
callback(cookieValue) | ||
} | ||
}, WATCH_COOKIE_INTERVAL_DELAY) | ||
|
||
return () => { | ||
clearInterval(watchCookieIntervalId) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
28 changes: 0 additions & 28 deletions
28
packages/rum-core/src/domain/contexts/ciTestContext.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The goal is to use this observable to also watch the session cookie. (in a following PR)