-
Notifications
You must be signed in to change notification settings - Fork 737
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
use sendBeacon for exporting when page is hidden #4619
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -29,27 +29,22 @@ export abstract class OTLPExporterBrowserBase< | |
ExportItem, | ||
ServiceRequest, | ||
> extends OTLPExporterBase<OTLPExporterConfigBase, ExportItem, ServiceRequest> { | ||
protected _headers: Record<string, string>; | ||
private _useXHR: boolean = false; | ||
protected _headers: Record<string, string> = {}; | ||
private _alwaysUseXhr: boolean = false; | ||
|
||
/** | ||
* @param config | ||
*/ | ||
constructor(config: OTLPExporterConfigBase = {}) { | ||
super(config); | ||
this._useXHR = | ||
!!config.headers || typeof navigator.sendBeacon !== 'function'; | ||
if (this._useXHR) { | ||
this._headers = Object.assign( | ||
{}, | ||
parseHeaders(config.headers), | ||
baggageUtils.parseKeyPairsIntoRecord( | ||
getEnv().OTEL_EXPORTER_OTLP_HEADERS | ||
) | ||
); | ||
} else { | ||
this._headers = {}; | ||
} | ||
this._alwaysUseXhr = | ||
config.alwaysUseXhr || typeof navigator.sendBeacon !== 'function'; | ||
|
||
this._headers = Object.assign( | ||
{}, | ||
parseHeaders(config.headers), | ||
baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_HEADERS) | ||
); | ||
} | ||
|
||
onInit(): void {} | ||
|
@@ -69,20 +64,22 @@ export abstract class OTLPExporterBrowserBase< | |
const body = JSON.stringify(serviceRequest); | ||
|
||
const promise = new Promise<void>((resolve, reject) => { | ||
if (this._useXHR) { | ||
sendWithXhr( | ||
if (!this._alwaysUseXhr && document.hidden && body.length < 64 * 1024) { | ||
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.
IMHO, this (should) use a combination of the page hide events If your listening to events then we would also need to handle the "show" events to reverse the value. |
||
// sendBeacon must be used when the page is being hidden, as per: https://w3c.github.io/beacon/. | ||
// Note that headers are ignored in this case, if this is a problem, configuration option `alwaysUseXhr` must be set to true. | ||
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. Hmm is this enough to add (Alternatively could provide an option for user to pass their own conditional check so and have this+header len as sane default) * - yeah there's better alternatives but sometimes people make bad decisions 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. Additionally, "if" the page is unloading then you should use (if available) the 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.
|
||
sendWithBeacon( | ||
body, | ||
this.url, | ||
this._headers, | ||
this.timeoutMillis, | ||
{ type: 'application/json' }, | ||
resolve, | ||
reject | ||
); | ||
} else { | ||
sendWithBeacon( | ||
sendWithXhr( | ||
body, | ||
this.url, | ||
{ type: 'application/json' }, | ||
this._headers, | ||
this.timeoutMillis, | ||
resolve, | ||
reject | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,5 @@ export interface OTLPExporterConfigBase { | |
/** Maximum time the OTLP exporter will wait for each batch export. | ||
* The default value is 10000ms. */ | ||
timeoutMillis?: number; | ||
alwaysUseXhr?: boolean; | ||
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. rather than expose a new API property called As you may not be able to use XHR and you might want to use fetch instead (like in a worker). Suggestion: Something like what we do in Application Insights This allows the user to "request" their preferred transport (sendBeacon, XHR, fetch) and the SDK will use the most appropriate transport for the given scenario. The difference between the |
||
} |
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.
can we move the
64 * 1024
to a constant?