-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APM RUM integration - number of resources loaded from network (#116923)…
… (#117314) * add a label with the number of resources actually loaded from network * code review * @v code review + tests * minor review fixes * update jest Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Liza Katz <[email protected]>
- Loading branch information
1 parent
ba7667e
commit a0e1c46
Showing
3 changed files
with
207 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export class CachedResourceObserver { | ||
private loaded = { | ||
networkOrDisk: 0, | ||
memory: 0, | ||
}; | ||
private observer?: PerformanceObserver; | ||
|
||
constructor() { | ||
if (!window.PerformanceObserver) return; | ||
|
||
const cb = (entries: PerformanceObserverEntryList) => { | ||
const e = entries.getEntries(); | ||
e.forEach((entry: Record<string, any>) => { | ||
if (entry.initiatorType === 'script' || entry.initiatorType === 'link') { | ||
// If the resource is fetched from a local cache, or if it is a cross-origin resource, this property returns zero. | ||
// https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/transferSize | ||
if (entry.name.indexOf(window.location.host) > -1 && entry.transferSize === 0) { | ||
this.loaded.memory++; | ||
} else { | ||
this.loaded.networkOrDisk++; | ||
} | ||
} | ||
}); | ||
}; | ||
this.observer = new PerformanceObserver(cb); | ||
this.observer.observe({ | ||
type: 'resource', | ||
buffered: true, | ||
}); | ||
} | ||
|
||
public getCounts() { | ||
return this.loaded; | ||
} | ||
|
||
public destroy() { | ||
this.observer?.disconnect(); | ||
} | ||
} |
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