Skip to content

Commit

Permalink
Fix "fetch" in IE (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
t2t2 authored Jun 7, 2022
1 parent 734e4f7 commit 72354b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/SplunkFetchInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { HrTime } from '@opentelemetry/api';
import { hrTime } from '@opentelemetry/core';
import { FetchInstrumentation, FetchInstrumentationConfig } from '@opentelemetry/instrumentation-fetch';
import { captureTraceParent } from './servertiming';

interface SpanData {
entries: PerformanceResourceTiming[];
observer?: PerformanceObserver;
spanUrl: string;
startTime: HrTime;
}

type ExposedSuper = {
_prepareSpanData: (spanUrl: string) => SpanData;
};

export class SplunkFetchInstrumentation extends FetchInstrumentation {
constructor(config: FetchInstrumentationConfig = {}) {
const origCustomAttrs = config.applyCustomAttributesOnSpan;
Expand All @@ -33,6 +46,21 @@ export class SplunkFetchInstrumentation extends FetchInstrumentation {
};

super(config);

const _superPrepareSpanData = (this as unknown as ExposedSuper)._prepareSpanData.bind(this) as ExposedSuper['_prepareSpanData'];
(this as any as ExposedSuper)._prepareSpanData = (spanUrl: string) => {
// Fix: PerformanceObserver feature detection is broken and crashes in IE
// Is fixed in 0.29.0 but contrib isn't updated yet
if (
typeof PerformanceObserver !== 'function'
) {
const startTime = hrTime();
const entries: PerformanceResourceTiming[] = [];
return { entries, startTime, spanUrl };
}

return _superPrepareSpanData(spanUrl);
};
}

enable(): void {
Expand Down
5 changes: 2 additions & 3 deletions src/SplunkXhrPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ export class SplunkXhrPlugin extends XMLHttpRequestInstrumentation {
// Fix: PerformanceObserver feature detection is broken and crashes in IE
// Is fixed in 0.29.0 but contrib isn't updated yet
if (
false ||
typeof window.PerformanceObserver !== 'function' ||
typeof window.PerformanceResourceTiming !== 'function'
typeof PerformanceObserver !== 'function' ||
typeof PerformanceResourceTiming !== 'function'
) {
return;
}
Expand Down

0 comments on commit 72354b3

Please sign in to comment.