Skip to content

Commit

Permalink
update setAppState in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSvietlova committed Dec 19, 2024
1 parent 1ad8146 commit f3444ec
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/core/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function createClient<S extends CoreSchema, C extends Configuration, T> (
}

for (const plugin of plugins) {
plugin.configure(configuration, spanFactory, setAppState)
plugin.configure(configuration, spanFactory, setAppState, appState)
}
},
startSpan: (name, spanOptions?: SpanOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { AppState } from './core'
import type { SpanFactory } from './span-factory'

export interface Plugin<C extends Configuration> {
configure: (configuration: InternalConfiguration<C>, spanFactory: SpanFactory<C>, setAppState: (state: AppState) => void) => void
configure: (configuration: InternalConfiguration<C>, spanFactory: SpanFactory<C>, setAppState: (state: AppState) => void, state: AppState) => void
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class FullPageLoadPlugin implements Plugin<BrowserConfiguration> {
private readonly webVitals: WebVitals
private readonly performance: PerformanceWithTiming
private readonly setAppState: (appState: AppState) => void
private readonly appState: AppState

// if the page was backgrounded at any point in the loading process a page
// load span is invalidated as the browser will deprioritise the page
Expand All @@ -29,7 +30,8 @@ export class FullPageLoadPlugin implements Plugin<BrowserConfiguration> {
onSettle: OnSettle,
backgroundingListener: BackgroundingListener,
performance: PerformanceWithTiming,
setAppState: (appState: AppState) => void
setAppState: (appState: AppState) => void,
appState: AppState
) {
this.document = document
this.location = location
Expand All @@ -38,6 +40,7 @@ export class FullPageLoadPlugin implements Plugin<BrowserConfiguration> {
this.onSettle = onSettle
this.performance = performance
this.setAppState = setAppState
this.appState = appState

backgroundingListener.onStateChange(state => {
if (!this.wasBackgrounded && state === 'in-background') {
Expand Down Expand Up @@ -91,7 +94,9 @@ export class FullPageLoadPlugin implements Plugin<BrowserConfiguration> {

this.webVitals.attachTo(span)
this.spanFactory.endSpan(span, endTime)
this.setAppState('ready')
if(this.appState === 'starting') {

Check failure on line 97 in packages/platforms/browser/lib/auto-instrumentation/full-page-load-plugin.ts

View workflow job for this annotation

GitHub Actions / linting

Expected space(s) after "if"
this.setAppState('ready')
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class RouteChangePlugin implements Plugin<BrowserConfiguration> {
} satisfies Span
}
}

this.setAppState('navigating')
// create internal options for validation
const routeChangeSpanOptions = {
...options,
Expand All @@ -81,7 +81,6 @@ export class RouteChangePlugin implements Plugin<BrowserConfiguration> {
// update the span name using the validated route
cleanOptions.name += route
const span = this.spanFactory.startSpan(cleanOptions.name, cleanOptions.options)

span.setAttribute('bugsnag.span.category', 'route_change')
span.setAttribute('bugsnag.browser.page.route', route)
span.setAttribute('bugsnag.browser.page.previous_route', previousRoute)
Expand Down Expand Up @@ -119,7 +118,6 @@ export class RouteChangePlugin implements Plugin<BrowserConfiguration> {
span.name = `[RouteChange]${route}`
span.setAttribute('bugsnag.browser.page.route', route)
previousRoute = route
this.setAppState('navigating')

// update the URL attribute as well
if (permittedAttributes.url) {
Expand Down
41 changes: 19 additions & 22 deletions packages/platforms/browser/lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { AppState } from '../../../core/lib/core'
export let onSettle: OnSettlePlugin
export let DefaultRoutingProvider: ReturnType<typeof createDefaultRoutingProvider>
let BugsnagPerformance: Client<BrowserConfiguration>
let setAppState: (appState: AppState) => void

if (typeof window === 'undefined' || typeof document === 'undefined') {
onSettle = createNoopOnSettle()
Expand All @@ -42,7 +41,7 @@ if (typeof window === 'undefined' || typeof document === 'undefined') {
xhrRequestTracker,
performance
)
DefaultRoutingProvider = createDefaultRoutingProvider(onSettle, window.location, setAppState)
DefaultRoutingProvider = createDefaultRoutingProvider(onSettle, window.location)

BugsnagPerformance = createClient({
backgroundingListener,
Expand All @@ -52,27 +51,25 @@ if (typeof window === 'undefined' || typeof document === 'undefined') {
deliveryFactory: createFetchDeliveryFactory(window.fetch, clock, backgroundingListener),
idGenerator,
schema: createSchema(window.location.hostname, new DefaultRoutingProvider()),
plugins: (spanFactory, spanContextStorage, setAppState) => {
DefaultRoutingProvider.setAppState = setAppState('starting')
return [
plugins: (spanFactory, spanContextStorage, setAppState, appState) => [
onSettle,
new FullPageLoadPlugin(
document,
window.location,
spanFactory,
webVitals,
onSettle,
new FullPageLoadPlugin(
document,
window.location,
spanFactory,
webVitals,
onSettle,
backgroundingListener,
performance,
setAppState
),
// ResourceLoadPlugin should always come after FullPageLoad plugin, as it should use that
// span context as the parent of it's spans
new ResourceLoadPlugin(spanFactory, spanContextStorage, window.PerformanceObserver),
new NetworkRequestPlugin(spanFactory, spanContextStorage, fetchRequestTracker, xhrRequestTracker),
new RouteChangePlugin(spanFactory, window.location, document, setAppState)
]
},
backgroundingListener,
performance,
setAppState,
appState
),
// ResourceLoadPlugin should always come after FullPageLoad plugin, as it should use that
// span context as the parent of it's spans
new ResourceLoadPlugin(spanFactory, spanContextStorage, window.PerformanceObserver),
new NetworkRequestPlugin(spanFactory, spanContextStorage, fetchRequestTracker, xhrRequestTracker),
new RouteChangePlugin(spanFactory, window.location, document, setAppState)
],
persistence,
retryQueueFactory: (delivery, retryQueueMaxSize) => new InMemoryQueue(delivery, retryQueueMaxSize)
})
Expand Down
9 changes: 1 addition & 8 deletions packages/platforms/browser/lib/default-routing-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { OnSettle } from './on-settle'
import { getAbsoluteUrl } from '@bugsnag/request-tracker-performance'
import type { RouteResolver, RoutingProvider, StartRouteChangeCallback } from './routing-provider'
import { AppState } from '@bugsnag/core-performance/dist/types/core'

export const defaultRouteResolver: RouteResolver = (url: URL) => url.pathname || '/'

Expand All @@ -17,25 +16,21 @@ export const createNoopRoutingProvider = () => {
}
}

export const createDefaultRoutingProvider = (onSettle: OnSettle, location: Location, setAppState?: (appState: AppState) => void) => {
export const createDefaultRoutingProvider = (onSettle: OnSettle, location: Location) => {
return class DefaultRoutingProvider implements RoutingProvider {
resolveRoute: RouteResolver
setAppState?: (appState: AppState) => void

Check failure on line 22 in packages/platforms/browser/lib/default-routing-provider.ts

View workflow job for this annotation

GitHub Actions / linting

Trailing spaces not allowed
constructor (resolveRoute = defaultRouteResolver) {
this.resolveRoute = resolveRoute
this.setAppState = setAppState
}

listenForRouteChanges (startRouteChangeSpan: StartRouteChangeCallback) {
addEventListener('popstate', (ev) => {
const url = new URL(location.href)
const span = startRouteChangeSpan(url, 'popstate')
this.setAppState?.('navigating')

onSettle((endTime) => {
span.end(endTime)
this.setAppState?.('ready')
})
})

Expand All @@ -46,11 +41,9 @@ export const createDefaultRoutingProvider = (onSettle: OnSettle, location: Locat
if (url) {
const absoluteURL = new URL(getAbsoluteUrl(url.toString(), document.baseURI))
const span = startRouteChangeSpan(absoluteURL, 'pushState')
// this.setAppState?.('navigating')

onSettle((endTime) => {
span.end(endTime)
// this.setAppState?.('ready')
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ export class AppStartPlugin implements Plugin<ReactNativeConfiguration> {
private readonly clock: Clock
private readonly appRegistry: typeof AppRegistry
private readonly setAppState: (appState: AppState) => void
private readonly appState: AppState

constructor (
appStartTime: number,
spanFactory: SpanFactory<ReactNativeConfiguration>,
clock: Clock,
appRegistry: typeof AppRegistry,
setAppState: (appState: AppState) => void
setAppState: (appState: AppState) => void,
appState: AppState
) {
this.appStartTime = appStartTime
this.spanFactory = spanFactory
this.clock = clock
this.appRegistry = appRegistry
this.setAppState = setAppState
this.appState = appState
}

configure (configuration: InternalConfiguration<ReactNativeConfiguration>) {
Expand All @@ -48,7 +51,9 @@ export class AppStartPlugin implements Plugin<ReactNativeConfiguration> {
React.useEffect(() => {
if (appStartSpan.isValid()) {
this.spanFactory.endSpan(appStartSpan, this.clock.now())
this.setAppState('ready')
if (this.appState === 'starting') {
this.setAppState('ready')
}
}
}, [])

Expand Down
4 changes: 2 additions & 2 deletions packages/platforms/react-native/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const BugsnagPerformance = createClient({
deliveryFactory,
idGenerator,
persistence,
plugins: (spanFactory, spanContextStorage, setAppState) => [
new AppStartPlugin(appStartTime, spanFactory, clock, AppRegistry, setAppState),
plugins: (spanFactory, spanContextStorage, setAppState, appState) => [
new AppStartPlugin(appStartTime, spanFactory, clock, AppRegistry, setAppState, appState),
new NetworkRequestPlugin(spanFactory, spanContextStorage, xhrRequestTracker)
],
resourceAttributesSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ class BugsnagPluginReactNativeNavigationPerformance implements Plugin<ReactNativ
this.Navigation.events().registerComponentWillAppearListener(event => {
if (typeof this.startTime === 'number') {
clearTimeout(this.startTimeout)

this.setAppState('navigating')

Check failure on line 89 in packages/plugin-react-native-navigation/lib/react-native-navigation-plugin.ts

View workflow job for this annotation

GitHub Actions / linting

Expected indentation of 8 spaces but found 10
const routeName = event.componentName
this.currentNavigationSpan = createNavigationSpan(spanFactory, routeName, { startTime: this.startTime })
this.currentNavigationSpan.setAttribute('bugsnag.navigation.triggered_by', '@bugsnag/plugin-react-native-navigation-performance')

if (this.previousRoute) {
this.currentNavigationSpan.setAttribute('bugsnag.navigation.previous_route', this.previousRoute)

Check failure on line 95 in packages/plugin-react-native-navigation/lib/react-native-navigation-plugin.ts

View workflow job for this annotation

GitHub Actions / linting

Block must not be padded by blank lines
this.setAppState('navigating')

}

this.previousRoute = routeName
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-react-navigation/lib/navigation-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ export class NavigationContextProvider extends React.Component<Props> {
// invalid time to cause it to be discarded from the context stack.
if (this.currentSpan) {
spanFactory.endSpan(this.currentSpan, DISCARDED)
this.props.setAppState('ready')
}

const span = createNavigationSpan(spanFactory, currentRoute, { startTime: updateTime })
this.props.setAppState('navigating')
span.setAttribute('bugsnag.navigation.triggered_by', '@bugsnag/plugin-react-navigation-performance')

if (this.previousRoute) {
span.setAttribute('bugsnag.navigation.previous_route', this.previousRoute)
this.props.setAppState('navigating')
}

this.currentSpan = span
Expand All @@ -90,7 +89,6 @@ export class NavigationContextProvider extends React.Component<Props> {

setTimeout(() => {
this.triggerNavigationEnd()
this.props.setAppState('ready')
})
}
}
Expand Down

0 comments on commit f3444ec

Please sign in to comment.