@bugsnag/browser-performance
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -5,4 +5,5 @@ import { defaultNetworkRequestCallback } from '@bugsnag/request-tracker-performance'; | ||
class NetworkRequestPlugin { | ||
constructor(spanFactory, fetchTracker, xhrTracker) { | ||
constructor(spanFactory, spanContextStorage, fetchTracker, xhrTracker) { | ||
this.spanFactory = spanFactory; | ||
this.spanContextStorage = spanContextStorage; | ||
this.fetchTracker = fetchTracker; | ||
@@ -16,5 +17,28 @@ this.xhrTracker = xhrTracker; | ||
return; | ||
const networkRequestInfo = this.networkRequestCallback({ url: startContext.url, type: startContext.type }); | ||
if (!networkRequestInfo) | ||
return; | ||
const shouldPropagateTraceContextByDefault = false; | ||
const defaultRequestInfo = { | ||
url: startContext.url, | ||
type: startContext.type, | ||
propagateTraceContext: shouldPropagateTraceContextByDefault | ||
}; | ||
const networkRequestInfo = this.networkRequestCallback(defaultRequestInfo); | ||
// returning null neither creates a span nor propagates trace context | ||
if (!networkRequestInfo) { | ||
return { | ||
onRequestEnd: undefined, | ||
extraRequestHeaders: undefined | ||
}; | ||
} | ||
if (networkRequestInfo.propagateTraceContext === undefined) { | ||
networkRequestInfo.propagateTraceContext = shouldPropagateTraceContextByDefault; | ||
} | ||
// a span is not created if url is null | ||
if (!networkRequestInfo.url) { | ||
return { | ||
onRequestEnd: undefined, | ||
// propagate trace context if requested using span context | ||
extraRequestHeaders: networkRequestInfo.propagateTraceContext ? this.getExtraRequestHeaders() : undefined | ||
}; | ||
} | ||
// otherwise, create a span and propagate trace context if requested | ||
if (typeof networkRequestInfo.url !== 'string') { | ||
@@ -28,7 +52,13 @@ this.logger.warn(`expected url to be a string following network request callback, got ${typeof networkRequestInfo.url}`); | ||
span.setAttribute('http.url', networkRequestInfo.url); | ||
return (endContext) => { | ||
if (endContext.state === 'success') { | ||
span.setAttribute('http.status_code', endContext.status); | ||
this.spanFactory.endSpan(span, endContext.endTime); | ||
} | ||
return { | ||
onRequestEnd: (endContext) => { | ||
if (endContext.state === 'success') { | ||
span.setAttribute('http.status_code', endContext.status); | ||
this.spanFactory.endSpan(span, endContext.endTime); | ||
} | ||
}, | ||
// propagate trace context using network span | ||
extraRequestHeaders: networkRequestInfo.propagateTraceContext | ||
? this.getExtraRequestHeaders(span) | ||
: undefined | ||
}; | ||
@@ -49,4 +79,24 @@ }; | ||
} | ||
getExtraRequestHeaders(span) { | ||
const extraRequestHeaders = {}; | ||
if (span) { | ||
const traceId = span.traceId; | ||
const parentSpanId = span.id; | ||
const sampled = this.spanFactory.sampler.shouldSample(span.samplingRate); | ||
extraRequestHeaders.traceparent = buildTraceparentHeader(traceId, parentSpanId, sampled); | ||
} | ||
else if (this.spanContextStorage.current) { | ||
const currentSpanContext = this.spanContextStorage.current; | ||
const traceId = currentSpanContext.traceId; | ||
const parentSpanId = currentSpanContext.id; | ||
const sampled = this.spanFactory.sampler.shouldSample(currentSpanContext.samplingRate); | ||
extraRequestHeaders.traceparent = buildTraceparentHeader(traceId, parentSpanId, sampled); | ||
} | ||
return extraRequestHeaders; | ||
} | ||
} | ||
function buildTraceparentHeader(traceId, parentSpanId, sampled) { | ||
return `00-${traceId}-${parentSpanId}-${sampled ? '01' : '00'}`; | ||
} | ||
export { NetworkRequestPlugin }; |
@@ -44,2 +44,3 @@ import { isObject, isString, coreSpanOptionSchema } from '@bugsnag/core-performance'; | ||
traceId: '', | ||
samplingRate: 0, | ||
isValid: () => false, | ||
@@ -68,2 +69,3 @@ end: () => { } | ||
isValid: span.isValid, | ||
samplingRate: span.samplingRate, | ||
end: (endTimeOrOptions) => { | ||
@@ -70,0 +72,0 @@ const options = isObject(endTimeOrOptions) ? endTimeOrOptions : { endTime: endTimeOrOptions }; |
@@ -52,3 +52,3 @@ import { createNoopClient, createClient, InMemoryQueue } from '@bugsnag/core-performance'; | ||
new ResourceLoadPlugin(spanFactory, spanContextStorage, window.PerformanceObserver), | ||
new NetworkRequestPlugin(spanFactory, fetchRequestTracker, xhrRequestTracker), | ||
new NetworkRequestPlugin(spanFactory, spanContextStorage, fetchRequestTracker, xhrRequestTracker), | ||
new RouteChangePlugin(spanFactory, window.location, document) | ||
@@ -55,0 +55,0 @@ ], |
@@ -26,9 +26,11 @@ import { Settler } from './settler.js'; | ||
++this.outstandingRequests; | ||
return (endContext) => { | ||
if (--this.outstandingRequests === 0) { | ||
// we wait 100ms to ensure that requests have actually stopped but don't | ||
// want the settled time to reflect that wait, so we record the time | ||
// here and use that when settling | ||
const settledTime = this.clock.now(); | ||
this.timeout = setTimeout(() => { this.settle(settledTime); }, 100); | ||
return { | ||
onRequestEnd: (endContext) => { | ||
if (--this.outstandingRequests === 0) { | ||
// we wait 100ms to ensure that requests have actually stopped but don't | ||
// want the settled time to reflect that wait, so we record the time | ||
// here and use that when settling | ||
const settledTime = this.clock.now(); | ||
this.timeout = setTimeout(() => { this.settle(settledTime); }, 100); | ||
} | ||
} | ||
@@ -35,0 +37,0 @@ }; |
@@ -8,3 +8,3 @@ import cuid from '@bugsnag/cuid'; | ||
return function resourceAttributesSource(config) { | ||
const attributes = new ResourceAttributes(config.releaseStage, config.appVersion, 'bugsnag.performance.browser', '2.3.0'); | ||
const attributes = new ResourceAttributes(config.releaseStage, config.appVersion, 'bugsnag.performance.browser', '2.4.0'); | ||
attributes.set('browser.user_agent', navigator.userAgent); | ||
@@ -11,0 +11,0 @@ // chromium only |
@@ -1,2 +0,2 @@ | ||
import { type InternalConfiguration, type Plugin, type SpanFactory } from '@bugsnag/core-performance'; | ||
import type { InternalConfiguration, Plugin, SpanFactory, SpanContextStorage } from '@bugsnag/core-performance'; | ||
import { type NetworkRequestInfo, type RequestTracker } from '@bugsnag/request-tracker-performance'; | ||
@@ -9,2 +9,3 @@ import { type BrowserConfiguration } from '../config'; | ||
private spanFactory; | ||
private readonly spanContextStorage; | ||
private fetchTracker; | ||
@@ -15,7 +16,8 @@ private xhrTracker; | ||
private logger; | ||
constructor(spanFactory: SpanFactory<BrowserConfiguration>, fetchTracker: RequestTracker, xhrTracker: RequestTracker); | ||
constructor(spanFactory: SpanFactory<BrowserConfiguration>, spanContextStorage: SpanContextStorage, fetchTracker: RequestTracker, xhrTracker: RequestTracker); | ||
configure(configuration: InternalConfiguration<BrowserConfiguration>): void; | ||
private trackRequest; | ||
private shouldTrackRequest; | ||
private getExtraRequestHeaders; | ||
} | ||
//# sourceMappingURL=network-request-plugin.d.ts.map |
{ | ||
"name": "@bugsnag/browser-performance", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "BugSnag performance monitoring for browsers", | ||
@@ -24,6 +24,6 @@ "homepage": "https://www.bugsnag.com/", | ||
"dependencies": { | ||
"@bugsnag/core-performance": "^2.2.0", | ||
"@bugsnag/core-performance": "^2.4.0", | ||
"@bugsnag/cuid": "^3.0.2", | ||
"@bugsnag/delivery-fetch-performance": "^2.2.0", | ||
"@bugsnag/request-tracker-performance": "^2.2.0" | ||
"@bugsnag/delivery-fetch-performance": "^2.4.0", | ||
"@bugsnag/request-tracker-performance": "^2.4.0" | ||
}, | ||
@@ -36,3 +36,3 @@ "type": "module", | ||
], | ||
"gitHead": "fca46e481fc3d859bf2b99bef9ab680222150c47" | ||
"gitHead": "145aaab33c80c3efb898cf657cf3369557bcb468" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82978
1386