Skip to content
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

fix(plugin-fetch): check if PerformanceObserver exists #1662

Merged
merged 12 commits into from
Jan 20, 2021
Merged
11 changes: 10 additions & 1 deletion packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ export class FetchInstrumentation extends InstrumentationBase<
): void {
let resources: PerformanceResourceTiming[] = resourcesObserver.entries;
if (!resources.length) {
if (!performance.getEntriesByType) {
return;
}
// fallback - either Observer is not available or it took longer
// then OBSERVER_WAIT_TIME_MS and observer didn't collect enough
// information
Expand Down Expand Up @@ -249,7 +252,8 @@ export class FetchInstrumentation extends InstrumentationBase<
response: FetchResponse
) {
const endTime = core.hrTime();
spanData.observer.disconnect();
spanData.observer?.disconnect();

this._addFinalSpanAttributes(span, response);

setTimeout(() => {
Expand Down Expand Up @@ -348,6 +352,11 @@ export class FetchInstrumentation extends InstrumentationBase<
private _prepareSpanData(spanUrl: string): SpanData {
const startTime = core.hrTime();
const entries: PerformanceResourceTiming[] = [];

if (typeof window.PerformanceObserver === 'undefined') {
return { entries, startTime, spanUrl };
}

const observer: PerformanceObserver = new PerformanceObserver(list => {
const entries = list.getEntries() as PerformanceResourceTiming[];
entries.forEach(entry => {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-instrumentation-fetch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface FetchError {
*/
export interface SpanData {
entries: PerformanceResourceTiming[];
observer: PerformanceObserver;
observer?: PerformanceObserver;
spanUrl: string;
startTime: api.HrTime;
}
55 changes: 52 additions & 3 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ describe('fetch', () => {
done: any,
fileUrl: string,
config: FetchInstrumentationConfig,
method?: string
method?: string,
disablePerfObserver?: boolean,
disableGetEntries?: boolean
) => {
sandbox = sinon.createSandbox();
sandbox.useFakeTimers();
Expand Down Expand Up @@ -165,8 +167,16 @@ describe('fetch', () => {
})
);

const spyEntries = sandbox.stub(performance, 'getEntriesByType');
spyEntries.withArgs('resource').returns(resources);
if (disablePerfObserver) {
sandbox.stub(window, 'PerformanceObserver').value(undefined);
}
if (disableGetEntries) {
sandbox.stub(performance, 'getEntriesByType').value(undefined);
} else {
const spyEntries = sandbox.stub(performance, 'getEntriesByType');
spyEntries.withArgs('resource').returns(resources);
}

fetchInstrumentation = new FetchInstrumentation(config);
webTracerProviderWithZone = new WebTracerProvider({
logLevel: core.LogLevel.ERROR,
Expand Down Expand Up @@ -586,4 +596,43 @@ describe('fetch', () => {
);
});
});

describe('when PerformanceObserver is undefined', () => {
beforeEach(done => {
prepareData(done, url, {}, undefined, true, false);
});

afterEach(() => {
clearData();
});

it('should still create spans', () => {
assert.strictEqual(
exportSpy.args.length,
2,
`Wrong number of spans: ${exportSpy.args.length}`
);
});
});

describe('when PerformanceObserver and performance.getEntriesByType are undefined', () => {
beforeEach(done => {
prepareData(done, url, {}, undefined, true, true);
});
afterEach(() => {
clearData();
});
it('should capture fetch without preflight', () => {
assert.strictEqual(
exportSpy.args.length,
1,
`Wrong number of spans: ${exportSpy.args.length}`
);
assert.strictEqual(
exportSpy.args[0][0][0].name,
'HTTP GET',
'wrong span captured'
);
});
});
});