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
Changes from 4 commits
e62100e
1008e03
2386651
978ea0e
a92a5f8
6f22408
ada0658
9b17a3c
3f6d327
4093ec9
c7ba6bf
23038bd
8e04a73
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 { BatchSpanProcessorBrowserConfig } from '../../../types'; | ||
|
||
export class BatchSpanProcessor extends BatchSpanProcessorBase<BatchSpanProcessorBrowserConfig> { | ||
private _visibilityChangeListener?: () => void | ||
|
||
onInit(config?: BatchSpanProcessorBrowserConfig): void { | ||
if (config?.flushOnDocumentBecomesHidden !== false && document != null) { | ||
this._visibilityChangeListener = () => { | ||
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. This should also trigger flush on a 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. Good catch. I added support for 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. I don't think there's any need for that level of complexity - 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. smart enough to be called twice concurrently you mean? 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. There is also 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 commentThe reason will be displayed to describe this comment to others. Learn more. I described my concerns about using Also I was testing JS auto-instrumentation on different browsers and on IE11 I got a 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 commentThe reason will be displayed to describe this comment to others. Learn more. -1 for If the current 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. 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 |
||
if (document.visibilityState === 'hidden') { | ||
void this.forceFlush(); | ||
} | ||
} | ||
document.addEventListener('visibilitychange', this._visibilityChangeListener); | ||
} | ||
} | ||
|
||
onShutdown(): void { | ||
if (this._visibilityChangeListener) { | ||
document.removeEventListener('visibilitychange', this._visibilityChangeListener); | ||
} | ||
} | ||
} |
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'; |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* 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> { | ||
onInit(): void {} | ||
|
||
onShutdown(): void {} | ||
} |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 { BatchSpanProcessor, SpanExporter } from '../../../src'; | ||
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; | ||
|
||
beforeEach(() => { | ||
sinon.replaceGetter(document, 'visibilityState', () => visibilityState); | ||
visibilityState = 'visible'; | ||
exporter = new TestTracingSpanExporter(); | ||
processor = new BatchSpanProcessor(exporter, {}); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
visibilityChangeEvent = new Event('visibilitychange'); | ||
}); | ||
|
||
afterEach(() => { | ||
processor.onShutdown(); | ||
sinon.restore(); | ||
}); | ||
|
||
describe('when document becomes hidden', () => { | ||
it('should force flush spans', () => { | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
visibilityState = 'hidden'; | ||
document.dispatchEvent(visibilityChangeEvent); | ||
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(); | ||
visibilityState = 'hidden'; | ||
document.dispatchEvent(visibilityChangeEvent); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}); | ||
}) | ||
|
||
describe('AND flushOnDocumentBecomesHidden configuration option', () => { | ||
it('set to true should force flush spans', () => { | ||
processor = new BatchSpanProcessor(exporter, { flushOnDocumentBecomesHidden: true }); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
visibilityState = 'hidden'; | ||
document.dispatchEvent(visibilityChangeEvent); | ||
assert.strictEqual(forceFlushSpy.callCount, 1); | ||
}) | ||
|
||
it('set to false should NOT force flush spans', () => { | ||
processor = new BatchSpanProcessor(exporter, { flushOnDocumentBecomesHidden: false }); | ||
forceFlushSpy = sinon.stub(processor, 'forceFlush'); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
visibilityState = 'hidden'; | ||
document.dispatchEvent(visibilityChangeEvent); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when document becomes visible', () => { | ||
it('should NOT force flush spans', () => { | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
document.dispatchEvent(visibilityChangeEvent); | ||
assert.strictEqual(forceFlushSpy.callCount, 0); | ||
}); | ||
}) | ||
}); |
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.
Why make these abstract if they're only used by browser? Wouldn't it make more sense to just override the constructor in the browser impl then call
onInit
aftersuper
?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.
Done