-
Notifications
You must be signed in to change notification settings - Fork 804
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
feat(tracing): auto flush BatchSpanProcessor on browser #2243
Merged
dyladan
merged 13 commits into
open-telemetry:main
from
SumoLogic:batch-span-processor-auto-flush
Jun 16, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e62100e
feat(tracing): auto flush BatchSpanProcessor on browser
kkruk-sumo 1008e03
feat: added flushOnDocumentBecomesHidden config to BatchSpanProcessor
kkruk-sumo 2386651
chore(tracing): don't run browser tests for node
kkruk-sumo 978ea0e
chore: added test for flushOnDocumentBecomesHidden option being true
kkruk-sumo a92a5f8
Merge remote-tracking branch 'upstream/main' into batch-span-processo…
kkruk-sumo 6f22408
feat: rename flushOnDocumentBecomesHidden to disableAutoFlushOnDocume…
kkruk-sumo ada0658
chore: added trailing newline
kkruk-sumo 9b17a3c
chore: moved onInit out of BatchSpanProcessorBase
kkruk-sumo 3f6d327
feat: added support for pagehide event
kkruk-sumo 4093ec9
Merge branch 'main' into batch-span-processor-auto-flush
kkruk-sumo c7ba6bf
Merge branch 'main' into batch-span-processor-auto-flush
kkruk-sumo 23038bd
Merge branch 'main' into batch-span-processor-auto-flush
kkruk-sumo 8e04a73
Merge branch 'main' into batch-span-processor-auto-flush
kkruk-sumo 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 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
55 changes: 55 additions & 0 deletions
55
packages/opentelemetry-tracing/src/platform/browser/export/BatchSpanProcessor.ts
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,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BatchSpanProcessorBase } from '../../../export/BatchSpanProcessorBase'; | ||
import { SpanExporter } from '../../../export/SpanExporter'; | ||
import { BatchSpanProcessorBrowserConfig } from '../../../types'; | ||
|
||
export class BatchSpanProcessor extends BatchSpanProcessorBase<BatchSpanProcessorBrowserConfig> { | ||
private _visibilityChangeListener?: () => void | ||
private _pageHideListener?: () => void | ||
|
||
constructor(_exporter: SpanExporter, config?: BatchSpanProcessorBrowserConfig) { | ||
super(_exporter, config) | ||
this.onInit(config) | ||
} | ||
|
||
private onInit(config?: BatchSpanProcessorBrowserConfig): void { | ||
if (config?.disableAutoFlushOnDocumentHide !== true && document != null) { | ||
this._visibilityChangeListener = () => { | ||
if (document.visibilityState === 'hidden') { | ||
void this.forceFlush(); | ||
} | ||
} | ||
this._pageHideListener = () => { | ||
void this.forceFlush() | ||
} | ||
document.addEventListener('visibilitychange', this._visibilityChangeListener); | ||
|
||
// use 'pagehide' event as a fallback for Safari; see https://bugs.webkit.org/show_bug.cgi?id=116769 | ||
document.addEventListener('pagehide', this._pageHideListener); | ||
} | ||
} | ||
|
||
protected onShutdown(): void { | ||
if (this._visibilityChangeListener) { | ||
document.removeEventListener('visibilitychange', this._visibilityChangeListener); | ||
} | ||
if (this._pageHideListener) { | ||
document.removeEventListener('pagehide', this._pageHideListener); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/opentelemetry-tracing/src/platform/browser/index.ts
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,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './export/BatchSpanProcessor'; |
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,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './node'; |
22 changes: 22 additions & 0 deletions
22
packages/opentelemetry-tracing/src/platform/node/export/BatchSpanProcessor.ts
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,22 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BatchSpanProcessorBase } from '../../../export/BatchSpanProcessorBase'; | ||
import { BufferConfig } from '../../../types'; | ||
|
||
export class BatchSpanProcessor extends BatchSpanProcessorBase<BufferConfig> { | ||
protected onShutdown(): void {} | ||
} |
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,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './export/BatchSpanProcessor'; |
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
102 changes: 102 additions & 0 deletions
102
packages/opentelemetry-tracing/test/browser/export/BatchSpanProcessor.test.ts
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,102 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
import { SpanExporter } from '../../../src'; | ||
import { BatchSpanProcessor } from '../../../src/platform/browser/export/BatchSpanProcessor'; | ||
import { TestTracingSpanExporter } from '../../common/export/TestTracingSpanExporter'; | ||
|
||
describe('BatchSpanProcessor - web', () => { | ||
let visibilityState: VisibilityState = 'visible'; | ||
let exporter: SpanExporter | ||
let processor: BatchSpanProcessor; | ||
let forceFlushSpy: sinon.SinonStub; | ||
let visibilityChangeEvent: Event; | ||
let pageHideEvent: Event | ||
|
||
beforeEach(() => { | ||
sinon.replaceGetter(document, 'visibilityState', () => visibilityState); | ||
visibilityState = 'visible'; | ||
exporter = new TestTracingSpanExporter(); | ||
processor = new BatchSpanProcessor(exporter, {}); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
visibilityChangeEvent = new Event('visibilitychange'); | ||
pageHideEvent = new Event('pagehide'); | ||
}); | ||
|
||
afterEach(async () => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('when document becomes hidden', () => { | ||
const testDocumentHide = (hideDocument: () => void) => { | ||
it('should force flush spans', () => { | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
hideDocument() | ||
assert.strictEqual(forceFlushSpy.callCount, 1); | ||
}); | ||
|
||
describe('AND shutdown has been called', () => { | ||
it('should NOT force flush spans', async () => { | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
await processor.shutdown(); | ||
hideDocument() | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}); | ||
}) | ||
|
||
describe('AND disableAutoFlushOnDocumentHide configuration option', () => { | ||
it('set to false should force flush spans', () => { | ||
processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: false }); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
hideDocument() | ||
assert.strictEqual(forceFlushSpy.callCount, 1); | ||
}) | ||
|
||
it('set to true should NOT force flush spans', () => { | ||
processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: true }); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
hideDocument() | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}) | ||
}) | ||
} | ||
|
||
describe('by the visibilitychange event', () => { | ||
testDocumentHide(() => { | ||
visibilityState = 'hidden'; | ||
document.dispatchEvent(visibilityChangeEvent); | ||
}) | ||
}) | ||
|
||
describe('by the pagehide event', () => { | ||
testDocumentHide(() => { | ||
document.dispatchEvent(pageHideEvent); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when document becomes visible', () => { | ||
it('should NOT force flush spans', () => { | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
document.dispatchEvent(visibilityChangeEvent); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}); | ||
}) | ||
}); |
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
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.
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.
This should also trigger flush on a
pagehide
event - Safari doesn't fully supportvisibilitychange
: https://caniuse.com/?search=visibilitychangeThere 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.
Good catch. I added support for the
pagehide
event.I didn't add any check to prevent double call of
forceFlush
when bothpagehide
andvisibilitychange
events are dispatched to make the code simpler. Let me know if you want such mechanism.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.
I don't think there's any need for that level of complexity -
flush
implementations should be smart enough.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.
smart enough to be called twice concurrently you mean?
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.
There is also
beforeunload
andunload
(depending on the browser support matrix that we want to support -- specifically older IE instances (which are probably already excluded by other usages)) that you may want to also consider adding.Additionally, there is actually a set of corner cases that we should support around pages hooking theses same events after the SDK has initialized but still creating / sending telemetry. The simplest solution for this is to set a flag during these flush events (or more specifically the unload events) and then during the onEnd() as well as batching you also trigger the same auto flush.
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.
I described my concerns about using
unload
event here #2205 .Also I was testing JS auto-instrumentation on different browsers and on IE11 I got a
Script error
so I guess we don't support it.This PR is about auto flushing created spans when page become hidden, so I don't think there is something wrong with sending telemetry when page is not active, or I didn't get your point right?
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.
-1 for
unload
: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon has a full writeup of the recommended approach. What's in the PR now looks good.If the current
BatchSpanExporter.forceFlush
isn't smart enough to deal with two calls in succession, I think it should be improved as a separate PR rather than complicating state/logic here. A surface skim looks like it might have some issues...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.
My comment is not a blocking comment for this PR -- as @johnbley says What's in the PR now looks good as it's making this better