@lwrjs/o11y 0.6.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
MIT LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Salesforce.com, Inc.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getInstrumentation as t}from"o11y/client";import{registerDecorators as r}from"lwc";class e{constructor(t){this.marks={},this.instrumentation=t}track(){this.trackExistingMarks(),this.setupObserver()}trackExistingMarks(){const t=performance.getEntriesByType("mark").filter((t=>t.name.startsWith("lwr.bootstrap.")));for(const r of t){const{name:t}=r;this.marks[t]=r,performance.clearMarks(t)}}setupObserver(){let t=0;new PerformanceObserver((r=>{const e=[];r.getEntries().forEach((r=>{const{name:s,entryType:a}=r;"mark"===a&&s.startsWith("lwr.")&&e.push(r),"lwr.bootstrap.end"===s&&(this.marks[s]=r),"lwr.bootstrap.error"===s&&(t+=1)})),void 0!==this.marks["lwr.bootstrap.end"]&&(this.instrumentation.trackValue("lwr.bootstrap.duration",this.marks["lwr.bootstrap.end"].startTime),this.instrumentation.trackValue("lwr.bootstrap.availability",1/(1+t)*100),delete this.marks["lwr.bootstrap.end"]);const s=e.reduce(((t,r)=>r.name.startsWith("lwr.loader.module.fetch")?t+1:t),0);s>0&&this.instrumentation.incrementCounter("lwr.loader.module.fetch.count",s);const a=e.reduce(((t,r)=>r.name.startsWith("lwr.loader.mappings.fetch")?t+1:t),0);a>0&&this.instrumentation.incrementCounter("lwr.loader.mappings.fetch.count",a),e.forEach((t=>performance.clearMarks(t.name)))})).observe({entryTypes:["mark"]})}}r(e,{fields:["marks"]});const s=void 0!==globalThis.performance&&"function"==typeof globalThis.performance.mark,a={track:()=>{}};function o(r){const o=t("lwrjs.instrumentation");(s?new e(o):a).track()}export default o;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getInstrumentation } from 'o11y/client';
|
|
2
|
+
import { PerformanceApiSink } from './performanceApiSink'; // Check if the Performance API is available
|
|
3
|
+
// e.g. JSDom (used in Jest) doesn't implement these
|
|
4
|
+
|
|
5
|
+
const isPerfSupported = typeof globalThis.performance !== 'undefined' && typeof globalThis.performance.mark === 'function';
|
|
6
|
+
|
|
7
|
+
const noop = () => {// noop
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const noopSink = {
|
|
11
|
+
track: noop
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Sets up o11y instrumentation instance.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export default function hookO11yClient(loaderServices) {
|
|
18
|
+
const instrumentation = getInstrumentation('lwrjs.instrumentation');
|
|
19
|
+
const sink = isPerfSupported ? new PerformanceApiSink(instrumentation) : noopSink;
|
|
20
|
+
sink.track();
|
|
21
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// TODO: import constants from shared util module
|
|
2
|
+
const BOOTSTRAP_MARK = 'lwr.bootstrap.';
|
|
3
|
+
const BOOTSTRAP_END_MARK = 'lwr.bootstrap.end';
|
|
4
|
+
const BOOTSTRAP_ERROR_MARK = 'lwr.bootstrap.error';
|
|
5
|
+
const BOOTSTRAP_DURATION_OP = 'lwr.bootstrap.duration';
|
|
6
|
+
const BOOTSTRAP_AVAILABILITY_OP = 'lwr.bootstrap.availability';
|
|
7
|
+
const LOADER_MODULE_FETCH = 'lwr.loader.module.fetch';
|
|
8
|
+
const LOADER_MODULE_FETCH_COUNT = 'lwr.loader.module.fetch.count';
|
|
9
|
+
const LOADER_MAPPING_FETCH = 'lwr.loader.mappings.fetch';
|
|
10
|
+
const LOADER_MAPPING_FETCH_COUNT = 'lwr.loader.mappings.fetch.count';
|
|
11
|
+
export class PerformanceApiSink {
|
|
12
|
+
marks = {};
|
|
13
|
+
|
|
14
|
+
constructor(instrumentation) {
|
|
15
|
+
this.instrumentation = instrumentation;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
track() {
|
|
19
|
+
this.trackExistingMarks();
|
|
20
|
+
this.setupObserver();
|
|
21
|
+
} // Retrieve existing metrics to this point
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
trackExistingMarks() {
|
|
25
|
+
/**
|
|
26
|
+
* Bootstrap marks prior to service bootstrap:
|
|
27
|
+
* - lwr.bootstrap.init
|
|
28
|
+
* - lwr.bootstrap.application
|
|
29
|
+
*/
|
|
30
|
+
const marks = performance.getEntriesByType('mark').filter(e => e.name.startsWith(BOOTSTRAP_MARK)); // TODO: saved these marks for now, but do we have real usages for them?
|
|
31
|
+
|
|
32
|
+
for (const mark of marks) {
|
|
33
|
+
const {
|
|
34
|
+
name
|
|
35
|
+
} = mark;
|
|
36
|
+
this.marks[name] = mark;
|
|
37
|
+
performance.clearMarks(name);
|
|
38
|
+
}
|
|
39
|
+
} // Add observer to log future metrics
|
|
40
|
+
// observe lwr client runtime performance metrics
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
setupObserver() {
|
|
44
|
+
let bootstrapErrorCount = 0;
|
|
45
|
+
const observer = new PerformanceObserver(list => {
|
|
46
|
+
// grab the lwr performance marks
|
|
47
|
+
const marks = [];
|
|
48
|
+
/**
|
|
49
|
+
* Bootstrap marks after to service bootstrap:
|
|
50
|
+
* - lwr.bootstrap.end
|
|
51
|
+
* - lwr.bootstrap.error
|
|
52
|
+
* Loader marks
|
|
53
|
+
* - lwr.loader.*
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
list.getEntries().forEach(entry => {
|
|
57
|
+
const {
|
|
58
|
+
name,
|
|
59
|
+
entryType
|
|
60
|
+
} = entry;
|
|
61
|
+
|
|
62
|
+
if (entryType === 'mark' && name.startsWith('lwr.')) {
|
|
63
|
+
marks.push(entry);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (name === BOOTSTRAP_END_MARK) {
|
|
67
|
+
this.marks[name] = entry;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (name === BOOTSTRAP_ERROR_MARK) {
|
|
71
|
+
bootstrapErrorCount += 1;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (this.marks[BOOTSTRAP_END_MARK] !== undefined) {
|
|
76
|
+
this.instrumentation.trackValue(BOOTSTRAP_DURATION_OP, this.marks[BOOTSTRAP_END_MARK].startTime);
|
|
77
|
+
this.instrumentation.trackValue(BOOTSTRAP_AVAILABILITY_OP, 1 / (1 + bootstrapErrorCount) * 100); // reset to prevent multiple reports
|
|
78
|
+
|
|
79
|
+
delete this.marks[BOOTSTRAP_END_MARK];
|
|
80
|
+
} // count how many modules were loaded and report the count
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
const moduleLoadCount = marks.reduce((count, mark) => {
|
|
84
|
+
return mark.name.startsWith(LOADER_MODULE_FETCH) ? count + 1 : count;
|
|
85
|
+
}, 0);
|
|
86
|
+
|
|
87
|
+
if (moduleLoadCount > 0) {
|
|
88
|
+
this.instrumentation.incrementCounter(LOADER_MODULE_FETCH_COUNT, moduleLoadCount);
|
|
89
|
+
} // count how many mapping resources were loaded and report the count
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
const mappingLoadCount = marks.reduce((count, mark) => {
|
|
93
|
+
return mark.name.startsWith(LOADER_MAPPING_FETCH) ? count + 1 : count;
|
|
94
|
+
}, 0);
|
|
95
|
+
|
|
96
|
+
if (mappingLoadCount > 0) {
|
|
97
|
+
this.instrumentation.incrementCounter(LOADER_MAPPING_FETCH_COUNT, mappingLoadCount);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
marks.forEach(mark => performance.clearMarks(mark.name));
|
|
101
|
+
});
|
|
102
|
+
observer.observe({
|
|
103
|
+
entryTypes: ['mark']
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lwrjs/o11y",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.6.0-alpha.8",
|
|
8
|
+
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/salesforce/lwr.git",
|
|
12
|
+
"directory": "packages/@lwrjs/o11y"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/salesforce/lwr/issues"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"files": [
|
|
19
|
+
"build/**/*.js",
|
|
20
|
+
"build/**/*.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "node ../../../bin/pack-lwc --dir modules build/modules && yarn build:bundle",
|
|
24
|
+
"build:bundle": "rollup --config scripts/rollup.moduleBundle.config.cjs"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@lwrjs/shared-utils": "0.6.0-alpha.8",
|
|
28
|
+
"o11y": "238.1.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"rollup-plugin-terser": "^7.0.2"
|
|
32
|
+
},
|
|
33
|
+
"lwc": {
|
|
34
|
+
"modules": [
|
|
35
|
+
{
|
|
36
|
+
"dir": "build/modules"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"npm": "o11y"
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"expose": [
|
|
43
|
+
"lwr/o11yService"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=14.15.4 <17"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "f57b843b079ef42fdd1e727521e5a88e70bf850a"
|
|
50
|
+
}
|