@lwrjs/o11y 0.6.0-alpha.10

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.loader.module.define")));t.length>0&&this.instrumentation.incrementCounter("lwr.loader.module.define.count",t.length)}setupObserver(){let t=0;new PerformanceObserver((r=>{const e=[];r.getEntries().forEach((r=>{const{name:n,entryType:s}=r;"mark"===s&&n.startsWith("lwr.")&&e.push(r),"lwr.bootstrap.end"===n&&(this.marks[n]=r),"lwr.bootstrap.error"===n&&(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 n=e.reduce(((t,r)=>r.name.startsWith("lwr.loader.module.define")?t+1:t),0);n>0&&this.instrumentation.incrementCounter("lwr.loader.module.define.count",n);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 o=e.reduce(((t,r)=>r.name.startsWith("lwr.loader.mappings.fetch")?t+1:t),0);o>0&&this.instrumentation.incrementCounter("lwr.loader.mappings.fetch.count",o),e.forEach((t=>performance.clearMarks(t.name)))})).observe({entryTypes:["mark"]})}}r(e,{fields:["marks"]});const n=void 0!==globalThis.performance&&"function"==typeof globalThis.performance.mark,s={track:()=>{}};function o(r){const o=t("lwrjs.instrumentation");(n?new e(o):s).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,117 @@
1
+ // TODO: import constants from shared util module
2
+ const BOOTSTRAP_END_MARK = 'lwr.bootstrap.end';
3
+ const BOOTSTRAP_ERROR_MARK = 'lwr.bootstrap.error';
4
+ const BOOTSTRAP_DURATION_OP = 'lwr.bootstrap.duration';
5
+ const BOOTSTRAP_AVAILABILITY_OP = 'lwr.bootstrap.availability';
6
+ const LOADER_MODULE_DEFINE = 'lwr.loader.module.define';
7
+ const LOADER_MODULE_DEFINE_COUNT = 'lwr.loader.module.define.count';
8
+ const LOADER_MODULE_FETCH = 'lwr.loader.module.fetch';
9
+ const LOADER_MODULE_FETCH_COUNT = 'lwr.loader.module.fetch.count';
10
+ const LOADER_MAPPING_FETCH = 'lwr.loader.mappings.fetch';
11
+ const LOADER_MAPPING_FETCH_COUNT = 'lwr.loader.mappings.fetch.count';
12
+ export class PerformanceApiSink {
13
+ marks = {};
14
+
15
+ constructor(instrumentation) {
16
+ this.instrumentation = instrumentation;
17
+ }
18
+
19
+ track() {
20
+ this.trackExistingMarks();
21
+ this.setupObserver();
22
+ } // Retrieve existing metrics to this point
23
+
24
+
25
+ trackExistingMarks() {
26
+ /**
27
+ * Loader marks prior to service bootstrap:
28
+ * - lwr.loader.module.define
29
+ */
30
+ const marks = performance.getEntriesByType('mark').filter(e => e.name.startsWith(LOADER_MODULE_DEFINE)); // initialize the module define count
31
+ // at this point in time, the count will include modules NOT fetched by the loader:
32
+ // - required modules
33
+ // - preload modules
34
+ // - the application bootstrap module
35
+ // - the loader module itself
36
+
37
+ if (marks.length > 0) {
38
+ this.instrumentation.incrementCounter(LOADER_MODULE_DEFINE_COUNT, marks.length);
39
+ }
40
+ } // Add observer to log future metrics
41
+ // observe lwr client runtime performance metrics
42
+
43
+
44
+ setupObserver() {
45
+ let bootstrapErrorCount = 0;
46
+ const observer = new PerformanceObserver(list => {
47
+ // grab the lwr performance marks
48
+ const marks = [];
49
+ /**
50
+ * Bootstrap marks after to service bootstrap:
51
+ * - lwr.bootstrap.end
52
+ * - lwr.bootstrap.error
53
+ * Loader marks
54
+ * - lwr.loader.*
55
+ */
56
+
57
+ list.getEntries().forEach(entry => {
58
+ const {
59
+ name,
60
+ entryType
61
+ } = entry;
62
+
63
+ if (entryType === 'mark' && name.startsWith('lwr.')) {
64
+ marks.push(entry);
65
+ }
66
+
67
+ if (name === BOOTSTRAP_END_MARK) {
68
+ this.marks[name] = entry;
69
+ }
70
+
71
+ if (name === BOOTSTRAP_ERROR_MARK) {
72
+ bootstrapErrorCount += 1;
73
+ }
74
+ });
75
+
76
+ if (this.marks[BOOTSTRAP_END_MARK] !== undefined) {
77
+ this.instrumentation.trackValue(BOOTSTRAP_DURATION_OP, this.marks[BOOTSTRAP_END_MARK].startTime);
78
+ this.instrumentation.trackValue(BOOTSTRAP_AVAILABILITY_OP, 1 / (1 + bootstrapErrorCount) * 100); // reset to prevent multiple reports
79
+
80
+ delete this.marks[BOOTSTRAP_END_MARK];
81
+ } // count how many modules were defined and report the count
82
+
83
+
84
+ const moduleDefineCount = marks.reduce((count, mark) => {
85
+ return mark.name.startsWith(LOADER_MODULE_DEFINE) ? count + 1 : count;
86
+ }, 0);
87
+
88
+ if (moduleDefineCount > 0) {
89
+ this.instrumentation.incrementCounter(LOADER_MODULE_DEFINE_COUNT, moduleDefineCount);
90
+ } // count how many modules were loaded and report the count
91
+
92
+
93
+ const moduleLoadCount = marks.reduce((count, mark) => {
94
+ return mark.name.startsWith(LOADER_MODULE_FETCH) ? count + 1 : count;
95
+ }, 0);
96
+
97
+ if (moduleLoadCount > 0) {
98
+ this.instrumentation.incrementCounter(LOADER_MODULE_FETCH_COUNT, moduleLoadCount);
99
+ } // count how many mapping resources were loaded and report the count
100
+
101
+
102
+ const mappingLoadCount = marks.reduce((count, mark) => {
103
+ return mark.name.startsWith(LOADER_MAPPING_FETCH) ? count + 1 : count;
104
+ }, 0);
105
+
106
+ if (mappingLoadCount > 0) {
107
+ this.instrumentation.incrementCounter(LOADER_MAPPING_FETCH_COUNT, mappingLoadCount);
108
+ }
109
+
110
+ marks.forEach(mark => performance.clearMarks(mark.name));
111
+ });
112
+ observer.observe({
113
+ entryTypes: ['mark']
114
+ });
115
+ }
116
+
117
+ }
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.10",
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.10",
28
+ "o11y": "238.1.4"
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": "85914e17d6214748489426252dd8fa41c8938b8f"
50
+ }