@dxos/observability 0.3.11-main.604a65a → 0.3.11-main.69484a6
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/dist/lib/browser/index.mjs +553 -28
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +602 -26
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/index.js +0 -1
- package/dist/types/src/index.js.map +1 -1
- package/dist/types/src/observability.d.ts +4 -3
- package/dist/types/src/observability.d.ts.map +1 -1
- package/dist/types/src/observability.js +25 -26
- package/dist/types/src/observability.js.map +1 -1
- package/dist/types/src/{helpers/map-spaces.d.ts → util.d.ts} +3 -2
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/src/{helpers/map-spaces.js → util.js} +2 -1
- package/dist/types/src/util.js.map +1 -0
- package/package.json +14 -15
- package/src/index.ts +0 -1
- package/src/observability.ts +24 -26
- package/src/{helpers/map-spaces.ts → util.ts} +2 -1
- package/dist/lib/browser/chunk-ROSK6U3K.mjs +0 -83
- package/dist/lib/browser/chunk-ROSK6U3K.mjs.map +0 -7
- package/dist/lib/browser/chunk-STWCJK52.mjs +0 -748
- package/dist/lib/browser/chunk-STWCJK52.mjs.map +0 -7
- package/dist/lib/browser/observability-XIDFB6PJ.mjs +0 -10
- package/dist/lib/browser/observability-XIDFB6PJ.mjs.map +0 -7
- package/dist/lib/browser/segment/index.mjs +0 -8
- package/dist/lib/browser/segment/index.mjs.map +0 -7
- package/dist/lib/node/chunk-ARUBPK2W.cjs +0 -170
- package/dist/lib/node/chunk-ARUBPK2W.cjs.map +0 -7
- package/dist/lib/node/chunk-DE7OVD7T.cjs +0 -769
- package/dist/lib/node/chunk-DE7OVD7T.cjs.map +0 -7
- package/dist/lib/node/observability-YMIQHLME.cjs +0 -32
- package/dist/lib/node/observability-YMIQHLME.cjs.map +0 -7
- package/dist/lib/node/segment/index.cjs +0 -30
- package/dist/lib/node/segment/index.cjs.map +0 -7
- package/dist/types/src/helpers/app-observability.d.ts +0 -19
- package/dist/types/src/helpers/app-observability.d.ts.map +0 -1
- package/dist/types/src/helpers/app-observability.js +0 -195
- package/dist/types/src/helpers/app-observability.js.map +0 -1
- package/dist/types/src/helpers/index.d.ts +0 -4
- package/dist/types/src/helpers/index.d.ts.map +0 -1
- package/dist/types/src/helpers/index.js +0 -23
- package/dist/types/src/helpers/index.js.map +0 -1
- package/dist/types/src/helpers/map-spaces.d.ts.map +0 -1
- package/dist/types/src/helpers/map-spaces.js.map +0 -1
- package/dist/types/src/helpers/setup-telemetry-listeners.d.ts +0 -4
- package/dist/types/src/helpers/setup-telemetry-listeners.d.ts.map +0 -1
- package/dist/types/src/helpers/setup-telemetry-listeners.js +0 -96
- package/dist/types/src/helpers/setup-telemetry-listeners.js.map +0 -1
- package/src/helpers/app-observability.ts +0 -205
- package/src/helpers/index.ts +0 -7
- package/src/helpers/setup-telemetry-listeners.ts +0 -115
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// NOTE: localStorage is not available in web workers.
|
|
6
|
-
import * as localForage from 'localforage';
|
|
7
|
-
|
|
8
|
-
// import { type Platform } from '@dxos/client-services';
|
|
9
|
-
import type { Client, Config } from '@dxos/client';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
|
-
// import { type InitOptions as TelemetryInitOptions } from '@dxos/telemetry';
|
|
12
|
-
|
|
13
|
-
import type { Mode, Observability } from '../observability';
|
|
14
|
-
|
|
15
|
-
export const BASE_TELEMETRY_PROPERTIES: any = {};
|
|
16
|
-
// item name is 'telemetry' for backwards compatibility
|
|
17
|
-
export const OBSERVABILITY_DISABLED_KEY = 'telemetry-disabled';
|
|
18
|
-
export const OBSERVABILITY_GROUP_KEY = 'telemetry-group';
|
|
19
|
-
|
|
20
|
-
if (typeof navigator !== 'undefined' && navigator.storage?.estimate) {
|
|
21
|
-
setInterval(async () => {
|
|
22
|
-
try {
|
|
23
|
-
const storageEstimate = await navigator.storage.estimate();
|
|
24
|
-
BASE_TELEMETRY_PROPERTIES.storageUsage = storageEstimate.usage;
|
|
25
|
-
BASE_TELEMETRY_PROPERTIES.storageQuota = storageEstimate.quota;
|
|
26
|
-
} catch (error) {
|
|
27
|
-
log.warn('Failed to run estimate()', error);
|
|
28
|
-
}
|
|
29
|
-
}, 10e3);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// TODO(wittjosiah): Improve privacy of telemetry identifiers.
|
|
33
|
-
// - Identifier should be generated client-side with no attachment to identity.
|
|
34
|
-
// - Identifier can then be reset by user.
|
|
35
|
-
// - Identifier can be synced via HALO to allow for correlation of events bewteen devices.
|
|
36
|
-
// - Identifier should also be stored outside of HALO such that it is available immediately on startup.
|
|
37
|
-
export const getTelemetryIdentifier = (client: Client) => {
|
|
38
|
-
if (!client?.initialized) {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const identity = client.halo.identity.get();
|
|
43
|
-
if (identity) {
|
|
44
|
-
return identity.identityKey.truncate();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return undefined;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const isObservabilityDisabled = async (namespace: string): Promise<boolean> => {
|
|
51
|
-
try {
|
|
52
|
-
return (await localForage.getItem(`${namespace}:${OBSERVABILITY_DISABLED_KEY}`)) === 'true';
|
|
53
|
-
} catch (err) {
|
|
54
|
-
log.catch('Failed to check if observability is disabled, assuming it is', err);
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const storeObservabilityDisabled = async (namespace: string, value: boolean) => {
|
|
60
|
-
try {
|
|
61
|
-
await localForage.setItem(`${namespace}:${OBSERVABILITY_DISABLED_KEY}`, String(value));
|
|
62
|
-
} catch (err) {
|
|
63
|
-
log.catch('Failed to store observability disabled', err);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getObservabilityGroup = async (namespace: string): Promise<string | undefined> => {
|
|
68
|
-
try {
|
|
69
|
-
return (await localForage.getItem(`${namespace}:${OBSERVABILITY_GROUP_KEY}`)) ?? undefined;
|
|
70
|
-
} catch (err) {
|
|
71
|
-
log.catch('Failed to get observability group', err);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export const storeObservabilityGroup = async (namespace: string, value: string) => {
|
|
76
|
-
try {
|
|
77
|
-
await localForage.setItem(`${namespace}:${OBSERVABILITY_GROUP_KEY}`, value);
|
|
78
|
-
} catch (err) {
|
|
79
|
-
log.catch('Failed to store observability group', err);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export type AppObservabilityOptions = {
|
|
84
|
-
namespace: string;
|
|
85
|
-
config: Config;
|
|
86
|
-
mode?: Mode;
|
|
87
|
-
tracingEnable?: boolean;
|
|
88
|
-
replayEnable?: boolean;
|
|
89
|
-
// TODO(nf): options for providers?
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
type IPData = { city: string; region: string; country: string; latitude: number; longitude: number };
|
|
93
|
-
|
|
94
|
-
// TODO(wittjosiah): Store preference for disabling telemetry.
|
|
95
|
-
// At minimum should be stored locally (i.e., localstorage), possibly in halo preference.
|
|
96
|
-
// Needs to be hooked up to settings page for user visibility.
|
|
97
|
-
export const initializeAppObservability = async ({
|
|
98
|
-
namespace,
|
|
99
|
-
config,
|
|
100
|
-
mode = 'basic',
|
|
101
|
-
tracingEnable = true,
|
|
102
|
-
replayEnable = true,
|
|
103
|
-
}: AppObservabilityOptions): Promise<Observability> => {
|
|
104
|
-
log('initializeAppObservability', { config });
|
|
105
|
-
|
|
106
|
-
/*
|
|
107
|
-
const platform = (await client.services.services.SystemService?.getPlatform()) as Platform;
|
|
108
|
-
if (!platform) {
|
|
109
|
-
log.error('failed to get platform, could not initialize observability');
|
|
110
|
-
return undefined;
|
|
111
|
-
}
|
|
112
|
-
*/
|
|
113
|
-
|
|
114
|
-
const group = (await getObservabilityGroup(namespace)) ?? undefined;
|
|
115
|
-
const release = `${namespace}@${config.get('runtime.app.build.version')}`;
|
|
116
|
-
const environment = config.get('runtime.app.env.DX_ENVIRONMENT');
|
|
117
|
-
BASE_TELEMETRY_PROPERTIES.group = group;
|
|
118
|
-
BASE_TELEMETRY_PROPERTIES.release = release;
|
|
119
|
-
BASE_TELEMETRY_PROPERTIES.environment = environment;
|
|
120
|
-
const observabilityDisabled = await isObservabilityDisabled(namespace);
|
|
121
|
-
|
|
122
|
-
const { Observability } = await import('../observability');
|
|
123
|
-
|
|
124
|
-
// TODO(nf): configure mode
|
|
125
|
-
const observability = new Observability({
|
|
126
|
-
namespace,
|
|
127
|
-
group,
|
|
128
|
-
mode,
|
|
129
|
-
config,
|
|
130
|
-
errorLog: {
|
|
131
|
-
sentryInitOptions: {
|
|
132
|
-
environment,
|
|
133
|
-
release,
|
|
134
|
-
tracing: tracingEnable,
|
|
135
|
-
replay: replayEnable,
|
|
136
|
-
// TODO(wittjosiah): Configure these.
|
|
137
|
-
sampleRate: 1.0,
|
|
138
|
-
replaySampleRate: 0.1,
|
|
139
|
-
replaySampleRateOnError: 1.0,
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// global kill switch
|
|
145
|
-
if (observabilityDisabled) {
|
|
146
|
-
observability.setMode('disabled');
|
|
147
|
-
log.info('observability disabled');
|
|
148
|
-
return observability;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
const getIPData = async (config: Config): Promise<IPData | void> => {
|
|
153
|
-
const IP_DATA_CACHE_TIMEOUT = 6 * 60 * 60 * 1000; // 6 hours
|
|
154
|
-
type CachedIPData = {
|
|
155
|
-
data: IPData;
|
|
156
|
-
timestamp: number;
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
// Check cache first.
|
|
160
|
-
const cachedData: null | CachedIPData = await localForage.getItem('dxos:telemetry:ipdata');
|
|
161
|
-
if (cachedData && cachedData.timestamp > Date.now() - IP_DATA_CACHE_TIMEOUT) {
|
|
162
|
-
return cachedData.data;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// TODO(nf): move into observability
|
|
166
|
-
// Fetch data if not cached.
|
|
167
|
-
const IPDATA_API_KEY = config.get('runtime.app.env.DX_IPDATA_API_KEY');
|
|
168
|
-
if (IPDATA_API_KEY) {
|
|
169
|
-
return fetch(`https://api.ipdata.co?api-key=${IPDATA_API_KEY}`)
|
|
170
|
-
.then((res) => res.json())
|
|
171
|
-
.then((data) => {
|
|
172
|
-
// Cache data.
|
|
173
|
-
localForage
|
|
174
|
-
.setItem('dxos:telemetry:ipdata', {
|
|
175
|
-
data,
|
|
176
|
-
timestamp: Date.now(),
|
|
177
|
-
})
|
|
178
|
-
.catch((err) => observability.captureException(err));
|
|
179
|
-
|
|
180
|
-
return data;
|
|
181
|
-
})
|
|
182
|
-
.catch((err) => observability.captureException(err));
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const ipData = await getIPData(config);
|
|
187
|
-
if (ipData && ipData.city) {
|
|
188
|
-
BASE_TELEMETRY_PROPERTIES.city = ipData.city;
|
|
189
|
-
BASE_TELEMETRY_PROPERTIES.region = ipData.region;
|
|
190
|
-
BASE_TELEMETRY_PROPERTIES.country = ipData.country;
|
|
191
|
-
BASE_TELEMETRY_PROPERTIES.latitude = ipData.latitude;
|
|
192
|
-
BASE_TELEMETRY_PROPERTIES.longitude = ipData.longitude;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// TODO(nf): plugin state?
|
|
196
|
-
|
|
197
|
-
// TODO(nf): should provide capability to init Sentry earlier in booting process to capture errors during initialization.
|
|
198
|
-
|
|
199
|
-
observability.initialize();
|
|
200
|
-
} catch (err: any) {
|
|
201
|
-
log.error('Failed to initialize app observability', err);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return observability;
|
|
205
|
-
};
|
package/src/helpers/index.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import type { Client } from '@dxos/client';
|
|
6
|
-
|
|
7
|
-
import { BASE_TELEMETRY_PROPERTIES, getTelemetryIdentifier } from './app-observability';
|
|
8
|
-
import type { Observability } from '../observability';
|
|
9
|
-
|
|
10
|
-
let lastFocusEvent = new Date();
|
|
11
|
-
let totalTime = 0;
|
|
12
|
-
|
|
13
|
-
export const setupTelemetryListeners = (namespace: string, client: Client, observability: Observability) => {
|
|
14
|
-
const clickCallback = (event: any) => {
|
|
15
|
-
if (BASE_TELEMETRY_PROPERTIES.group === 'dxos' && event.target && !event.target.id) {
|
|
16
|
-
// TODO(wittjosiah): Use @dxos/log so these can be filtered.
|
|
17
|
-
console.warn('Click event on element without id:', event.target);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
setTimeout(() =>
|
|
21
|
-
observability.event({
|
|
22
|
-
identityId: getTelemetryIdentifier(client),
|
|
23
|
-
name: `${namespace}.window.click`,
|
|
24
|
-
properties: {
|
|
25
|
-
...BASE_TELEMETRY_PROPERTIES,
|
|
26
|
-
href: window.location.href,
|
|
27
|
-
id: (event.target as HTMLElement)?.id,
|
|
28
|
-
path: (event.path as HTMLElement[])
|
|
29
|
-
?.filter((el) => Boolean(el.tagName))
|
|
30
|
-
.map((el) => `${el.tagName.toLowerCase()}${el.id ? `#${el.id}` : ''}`)
|
|
31
|
-
.reverse()
|
|
32
|
-
.join('>'),
|
|
33
|
-
},
|
|
34
|
-
}),
|
|
35
|
-
);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const focusCallback = () => {
|
|
39
|
-
const now = new Date();
|
|
40
|
-
setTimeout(() =>
|
|
41
|
-
observability.event({
|
|
42
|
-
identityId: getTelemetryIdentifier(client),
|
|
43
|
-
name: `${namespace}.window.focus`,
|
|
44
|
-
properties: {
|
|
45
|
-
...BASE_TELEMETRY_PROPERTIES,
|
|
46
|
-
href: window.location.href,
|
|
47
|
-
timeAway: now.getTime() - lastFocusEvent.getTime(),
|
|
48
|
-
},
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
lastFocusEvent = now;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const blurCallback = () => {
|
|
55
|
-
const now = new Date();
|
|
56
|
-
const timeSpent = now.getTime() - lastFocusEvent.getTime();
|
|
57
|
-
setTimeout(() =>
|
|
58
|
-
observability.event({
|
|
59
|
-
identityId: getTelemetryIdentifier(client),
|
|
60
|
-
name: `${namespace}.window.blur`,
|
|
61
|
-
properties: {
|
|
62
|
-
...BASE_TELEMETRY_PROPERTIES,
|
|
63
|
-
href: window.location.href,
|
|
64
|
-
timeSpent,
|
|
65
|
-
},
|
|
66
|
-
}),
|
|
67
|
-
);
|
|
68
|
-
lastFocusEvent = now;
|
|
69
|
-
totalTime = totalTime + timeSpent;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const unloadCallback = () => {
|
|
73
|
-
setTimeout(() =>
|
|
74
|
-
observability.event({
|
|
75
|
-
identityId: getTelemetryIdentifier(client),
|
|
76
|
-
name: `${namespace}.page.unload`,
|
|
77
|
-
properties: {
|
|
78
|
-
...BASE_TELEMETRY_PROPERTIES,
|
|
79
|
-
href: window.location.href,
|
|
80
|
-
timeSpent: totalTime,
|
|
81
|
-
},
|
|
82
|
-
}),
|
|
83
|
-
);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const errorCallback = (event: ErrorEvent) => {
|
|
87
|
-
setTimeout(() =>
|
|
88
|
-
observability.event({
|
|
89
|
-
identityId: getTelemetryIdentifier(client),
|
|
90
|
-
name: `${namespace}.window.error`,
|
|
91
|
-
properties: {
|
|
92
|
-
...BASE_TELEMETRY_PROPERTIES,
|
|
93
|
-
href: window.location.href,
|
|
94
|
-
message: event.message,
|
|
95
|
-
filename: event.filename,
|
|
96
|
-
stack: (event.error as Error).stack,
|
|
97
|
-
},
|
|
98
|
-
}),
|
|
99
|
-
);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
window.addEventListener('click', clickCallback, true);
|
|
103
|
-
window.addEventListener('focus', focusCallback);
|
|
104
|
-
window.addEventListener('blur', blurCallback);
|
|
105
|
-
window.addEventListener('beforeunload', unloadCallback);
|
|
106
|
-
window.addEventListener('error', errorCallback);
|
|
107
|
-
|
|
108
|
-
return () => {
|
|
109
|
-
window.removeEventListener('click', clickCallback, true);
|
|
110
|
-
window.removeEventListener('focus', focusCallback);
|
|
111
|
-
window.removeEventListener('blur', blurCallback);
|
|
112
|
-
window.removeEventListener('beforeunload', unloadCallback);
|
|
113
|
-
window.removeEventListener('error', errorCallback);
|
|
114
|
-
};
|
|
115
|
-
};
|