@elastic/opentelemetry-browser 0.1.0
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/CHANGELOG.md +6 -0
- package/LICENSE +202 -0
- package/NOTICE.md +5765 -0
- package/README.md +22 -0
- package/build/elastic-otel-browser.js +11593 -0
- package/build/elastic-otel-browser.js.map +7 -0
- package/build/elastic-otel-browser.min.js +14 -0
- package/build/elastic-otel-browser.min.js.map +7 -0
- package/lib/bundle.js +8 -0
- package/lib/context.js +347 -0
- package/lib/detector.js +226 -0
- package/lib/index.js +6 -0
- package/lib/logging.js +67 -0
- package/lib/sdk.js +232 -0
- package/lib/semconv.js +68 -0
- package/lib/version.js +6 -0
- package/package.json +81 -0
- package/types/bundle.d.ts +1 -0
- package/types/context.d.ts +2 -0
- package/types/detector.d.ts +47 -0
- package/types/index.d.ts +1 -0
- package/types/logging.d.ts +9 -0
- package/types/sdk.d.ts +32 -0
- package/types/semconv.d.ts +58 -0
- package/types/version.d.ts +1 -0
package/lib/bundle.js
ADDED
package/lib/context.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and contributors
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// This manager patches some async APIs to keep track of the context
|
|
7
|
+
// on the most common cases for web applications like when user performs
|
|
8
|
+
// an action and:
|
|
9
|
+
// - a timer is set
|
|
10
|
+
// - a fetch/XHR request is made
|
|
11
|
+
// - a Promise is chained with `then`, `catch` or `finally`
|
|
12
|
+
|
|
13
|
+
import {ROOT_CONTEXT} from '@opentelemetry/api';
|
|
14
|
+
import {createLogger} from './logging.js';
|
|
15
|
+
|
|
16
|
+
// use the diag one?
|
|
17
|
+
const logger = createLogger({logLevel: 'warn'});
|
|
18
|
+
|
|
19
|
+
// Keep the state of the current context here so only
|
|
20
|
+
// the manager has direct access.
|
|
21
|
+
let _currentContext = ROOT_CONTEXT;
|
|
22
|
+
let _managerEnabled = false;
|
|
23
|
+
/** @type {import('@opentelemetry/api').ContextManager} */
|
|
24
|
+
export const AsyncApisContextManager = {
|
|
25
|
+
active: function () {
|
|
26
|
+
return _currentContext;
|
|
27
|
+
},
|
|
28
|
+
with: function (context, fn, thisArg, ...args) {
|
|
29
|
+
const prevContext = _currentContext;
|
|
30
|
+
_currentContext = context || ROOT_CONTEXT;
|
|
31
|
+
try {
|
|
32
|
+
return fn.call(thisArg, ...args);
|
|
33
|
+
} finally {
|
|
34
|
+
_currentContext = prevContext;
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
// @ts-ignore -- upstream types expects a generic type as return
|
|
38
|
+
bind: function (context, target) {
|
|
39
|
+
if (typeof target === 'function') {
|
|
40
|
+
return bindFn(target, this, context);
|
|
41
|
+
}
|
|
42
|
+
return target;
|
|
43
|
+
},
|
|
44
|
+
enable: function () {
|
|
45
|
+
if (_managerEnabled) {
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
const manager = this;
|
|
49
|
+
wrap(window, 'setTimeout', (origSetTimeout) => {
|
|
50
|
+
return function (...args) {
|
|
51
|
+
// Coerce the delay argument to number like the original function does.
|
|
52
|
+
// ref: https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#non-number_delay_values_are_silently_coerced_into_numbers
|
|
53
|
+
const delay = Number(args[1]);
|
|
54
|
+
// Only carry the context if the delay is low enough. `NaN` is the same as `undefined`
|
|
55
|
+
// and schedules the callback immediately. The max value (50) is arbitrary.
|
|
56
|
+
if (isNaN(delay) || delay <= 50) {
|
|
57
|
+
args[0] = bindFn(args[0], manager, manager.active());
|
|
58
|
+
}
|
|
59
|
+
return origSetTimeout.apply(this, args);
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
wrap(window, 'setImmediate', (origSetImmediate) => {
|
|
63
|
+
return function (...args) {
|
|
64
|
+
args[0] = bindFn(args[0], manager, manager.active());
|
|
65
|
+
return origSetImmediate.apply(this, args);
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
wrapXMLHttpRequest(manager);
|
|
69
|
+
if (window.Promise) {
|
|
70
|
+
wrapPromise(manager);
|
|
71
|
+
}
|
|
72
|
+
_currentContext = ROOT_CONTEXT;
|
|
73
|
+
_managerEnabled = true;
|
|
74
|
+
return manager;
|
|
75
|
+
},
|
|
76
|
+
disable: function () {
|
|
77
|
+
if (_managerEnabled) {
|
|
78
|
+
unwrap(window, 'setTimeout');
|
|
79
|
+
unwrap(window, 'setImmediate');
|
|
80
|
+
unwrapXMLHttpRequest();
|
|
81
|
+
if (window.Promise) {
|
|
82
|
+
unwrapPromise();
|
|
83
|
+
}
|
|
84
|
+
_currentContext = ROOT_CONTEXT;
|
|
85
|
+
_managerEnabled = false;
|
|
86
|
+
}
|
|
87
|
+
return this;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// -- helper functions
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @param {Function} fn
|
|
95
|
+
* @param {import('@opentelemetry/api').ContextManager} manager
|
|
96
|
+
* @param {import('@opentelemetry/api').Context} context
|
|
97
|
+
* @returns {Function}
|
|
98
|
+
*/
|
|
99
|
+
function bindFn(fn, manager, context) {
|
|
100
|
+
if (typeof fn !== 'function') {
|
|
101
|
+
return fn;
|
|
102
|
+
}
|
|
103
|
+
const ctx = context || manager.active();
|
|
104
|
+
function wrappedCtxFn(...args) {
|
|
105
|
+
return manager.with(ctx, () => fn.apply(this, args));
|
|
106
|
+
}
|
|
107
|
+
Object.defineProperty(wrappedCtxFn, 'length', {
|
|
108
|
+
enumerable: false,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: false,
|
|
111
|
+
value: fn.length,
|
|
112
|
+
});
|
|
113
|
+
return wrappedCtxFn;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Wraps the promise constructor so any new promises carry
|
|
118
|
+
* the context in their sbsequent calls to `.then` and `.catch`
|
|
119
|
+
* @param {import('@opentelemetry/api').ContextManager} manager
|
|
120
|
+
*/
|
|
121
|
+
function wrapPromise(manager) {
|
|
122
|
+
wrap(Promise.prototype, 'then', (origThen) => {
|
|
123
|
+
return function (onResolved, onRejected) {
|
|
124
|
+
return origThen.call(
|
|
125
|
+
this,
|
|
126
|
+
bindFn(onResolved, manager, manager.active()),
|
|
127
|
+
bindFn(onRejected, manager, manager.active())
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
wrap(Promise.prototype, 'catch', (origCatch) => {
|
|
132
|
+
return function (onRejected) {
|
|
133
|
+
return origCatch.call(
|
|
134
|
+
this,
|
|
135
|
+
bindFn(onRejected, manager, manager.active())
|
|
136
|
+
);
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
wrap(Promise.prototype, 'finally', (origFinally) => {
|
|
140
|
+
return function (onCompleted) {
|
|
141
|
+
return origFinally.call(
|
|
142
|
+
this,
|
|
143
|
+
bindFn(onCompleted, manager, manager.active())
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function unwrapPromise() {
|
|
150
|
+
unwrap(Promise.prototype, 'then');
|
|
151
|
+
unwrap(Promise.prototype, 'catch');
|
|
152
|
+
unwrap(Promise.prototype, 'finally');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const xhrProps = ['onreadystatechange'];
|
|
156
|
+
const xhrProto = globalThis.XMLHttpRequest.prototype;
|
|
157
|
+
const xhrTargetProps = [
|
|
158
|
+
'onabort',
|
|
159
|
+
'onerror',
|
|
160
|
+
'onload',
|
|
161
|
+
'onloadend',
|
|
162
|
+
'onloadstart',
|
|
163
|
+
'onprogress',
|
|
164
|
+
'ontimeout',
|
|
165
|
+
];
|
|
166
|
+
const xhrTargetProto = globalThis.XMLHttpRequestEventTarget.prototype;
|
|
167
|
+
/**
|
|
168
|
+
* We keep track of the event listeners on the same XHR instance that were
|
|
169
|
+
* registered in a property named `__bound` so its easier to be cleared
|
|
170
|
+
* and, therefore, garbage collected.
|
|
171
|
+
* @param {import('@opentelemetry/api').ContextManager} manager
|
|
172
|
+
*/
|
|
173
|
+
function wrapXMLHttpRequest(manager) {
|
|
174
|
+
// Wrap events
|
|
175
|
+
wrap(xhrProto, 'addEventListener', function (origAEL) {
|
|
176
|
+
return function (...args) {
|
|
177
|
+
const xhr = this;
|
|
178
|
+
if (typeof args[1] === 'function') {
|
|
179
|
+
xhr.__bound = xhr.__bound || new Map();
|
|
180
|
+
const handler = args[1];
|
|
181
|
+
const bound = xhr.__bound.get(handler);
|
|
182
|
+
args[1] = bound || bindFn(handler, manager, manager.active());
|
|
183
|
+
xhr.__bound.set(handler, args[1]);
|
|
184
|
+
}
|
|
185
|
+
return origAEL.apply(xhr, args);
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
wrap(xhrProto, 'removeEventListener', function (origREL) {
|
|
189
|
+
return function (...args) {
|
|
190
|
+
const xhr = this;
|
|
191
|
+
if (typeof args[1] === 'function') {
|
|
192
|
+
const handler = args[1];
|
|
193
|
+
const bound = xhr.__bound?.get(handler);
|
|
194
|
+
if (bound) {
|
|
195
|
+
args[1] = bound;
|
|
196
|
+
// NOTE: this handler might be registered more than once
|
|
197
|
+
// or for other event types so better to keep it in the map
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return origREL.apply(xhr, args);
|
|
201
|
+
};
|
|
202
|
+
});
|
|
203
|
+
// Wrap on... properties of XMLHttpRequest.prototype
|
|
204
|
+
for (const prop of xhrProps) {
|
|
205
|
+
const descriptor = Object.getOwnPropertyDescriptor(xhrProto, prop);
|
|
206
|
+
if (descriptor) {
|
|
207
|
+
wrapDescriptor(descriptor, manager);
|
|
208
|
+
Object.defineProperty(xhrProto, prop, descriptor);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Wrap onload, onerror, on... properties of XMLHttpRequestEventTarget.prototype
|
|
213
|
+
for (const prop of xhrTargetProps) {
|
|
214
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
215
|
+
xhrTargetProto,
|
|
216
|
+
prop
|
|
217
|
+
);
|
|
218
|
+
if (descriptor) {
|
|
219
|
+
wrapDescriptor(descriptor, manager);
|
|
220
|
+
Object.defineProperty(xhrTargetProto, prop, descriptor);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function unwrapXMLHttpRequest() {
|
|
226
|
+
unwrap(xhrProto, 'addEventListener');
|
|
227
|
+
unwrap(xhrProto, 'removeEventListener');
|
|
228
|
+
// Unwrap onload, onerror, on... properties from:
|
|
229
|
+
// - XMLHttpRequest.prototype
|
|
230
|
+
// - XMLHttpRequestEventTarget.prototype
|
|
231
|
+
for (const prop of xhrProps) {
|
|
232
|
+
const descriptor = Object.getOwnPropertyDescriptor(xhrProto, prop);
|
|
233
|
+
if (descriptor) {
|
|
234
|
+
unwrap(descriptor, 'set');
|
|
235
|
+
unwrap(descriptor, 'get');
|
|
236
|
+
Object.defineProperty(xhrProto, prop, descriptor);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
for (const prop of xhrTargetProps) {
|
|
240
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
241
|
+
xhrTargetProto,
|
|
242
|
+
prop
|
|
243
|
+
);
|
|
244
|
+
if (descriptor) {
|
|
245
|
+
unwrap(descriptor, 'set');
|
|
246
|
+
unwrap(descriptor, 'get');
|
|
247
|
+
Object.defineProperty(xhrTargetProto, prop, descriptor);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// shimmer functions
|
|
253
|
+
function wrap(nodule, name, wrapper) {
|
|
254
|
+
if (!nodule || !nodule[name]) {
|
|
255
|
+
logger.warn('no original function ' + String(name) + ' to wrap');
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!wrapper) {
|
|
260
|
+
logger.warn('no wrapper function');
|
|
261
|
+
logger.warn(new Error().stack);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const original = nodule[name];
|
|
266
|
+
|
|
267
|
+
if (typeof original !== 'function' || typeof wrapper !== 'function') {
|
|
268
|
+
logger.warn('original object and wrapper must be functions');
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const wrapped = wrapper(original, name);
|
|
273
|
+
// Some frameworks check the `toString` to check if the function is native
|
|
274
|
+
if (typeof original.toString === 'function') {
|
|
275
|
+
wrapped.toString = original.toString.bind(original);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
defineProperty(wrapped, '__original', original);
|
|
279
|
+
defineProperty(wrapped, '__unwrap', () => {
|
|
280
|
+
if (nodule[name] === wrapped) {
|
|
281
|
+
defineProperty(nodule, name, original);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
defineProperty(wrapped, '__wrapped', true);
|
|
285
|
+
defineProperty(nodule, name, wrapped);
|
|
286
|
+
return wrapped;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// shimmer unwrap function
|
|
290
|
+
function unwrap(nodule, name) {
|
|
291
|
+
if (!nodule || !nodule[name]) {
|
|
292
|
+
logger.warn('no function to unwrap.');
|
|
293
|
+
logger.warn(new Error().stack);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const wrapped = nodule[name];
|
|
298
|
+
if (!wrapped.__unwrap) {
|
|
299
|
+
logger.warn(
|
|
300
|
+
`no original to unwrap to -- has ${String(name)} already been unwrapped?`
|
|
301
|
+
);
|
|
302
|
+
} else {
|
|
303
|
+
wrapped.__unwrap();
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function wrapDescriptor(descriptor, manager) {
|
|
309
|
+
wrap(descriptor, 'set', function (origSet) {
|
|
310
|
+
return function (value) {
|
|
311
|
+
if (typeof value === 'function') {
|
|
312
|
+
const origValue = value;
|
|
313
|
+
value = bindFn(origValue, manager, manager.active());
|
|
314
|
+
value.__original = origValue;
|
|
315
|
+
}
|
|
316
|
+
return origSet.call(this, value);
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
wrap(descriptor, 'get', function (origGet) {
|
|
320
|
+
return function () {
|
|
321
|
+
const value = origGet.call(this);
|
|
322
|
+
if (typeof value === 'function' && value.__original) {
|
|
323
|
+
return value.__original;
|
|
324
|
+
}
|
|
325
|
+
return value;
|
|
326
|
+
};
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Sets a property on an object, preserving its enumerability.
|
|
332
|
+
* This function assumes that the property is already writable.
|
|
333
|
+
* @param {any} obj
|
|
334
|
+
* @param {string | number | symbol} name
|
|
335
|
+
* @param {unknown} value
|
|
336
|
+
*/
|
|
337
|
+
function defineProperty(obj, name, value) {
|
|
338
|
+
const enumerable =
|
|
339
|
+
!!obj[name] && Object.prototype.propertyIsEnumerable.call(obj, name);
|
|
340
|
+
|
|
341
|
+
Object.defineProperty(obj, name, {
|
|
342
|
+
configurable: true,
|
|
343
|
+
enumerable,
|
|
344
|
+
writable: true,
|
|
345
|
+
value,
|
|
346
|
+
});
|
|
347
|
+
}
|
package/lib/detector.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and contributors
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {resourceFromAttributes} from '@opentelemetry/resources';
|
|
7
|
+
import {SDK_INFO} from '@opentelemetry/core';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
ATTR_BROWSER_BRANDS,
|
|
11
|
+
ATTR_BROWSER_LANGUAGE,
|
|
12
|
+
ATTR_BROWSER_MOBILE,
|
|
13
|
+
ATTR_BROWSER_PLATFORM,
|
|
14
|
+
ATTR_USER_AGENT_ORIGINAL,
|
|
15
|
+
} from './semconv.js';
|
|
16
|
+
import {EDOT_VERSION} from './version.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} UserAgentData
|
|
20
|
+
* @property {{brand: string; version: string}[]} brands
|
|
21
|
+
* @property {string} platform
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @template T
|
|
26
|
+
* @typedef {T | Array<T | undefined | null>} MaybeArray<T>
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {MaybeArray<number> | MaybeArray<boolean> | MaybeArray<string>} AttributeValue
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {Record<string, AttributeValue>} attribs
|
|
34
|
+
* @param {string | undefined} serviceName
|
|
35
|
+
* @param {string | undefined} serviceVersion
|
|
36
|
+
* @returns {import('@opentelemetry/resources').Resource}
|
|
37
|
+
*/
|
|
38
|
+
export function detectResource(attribs, serviceName, serviceVersion) {
|
|
39
|
+
/** @type {MaybeArray<number>} */
|
|
40
|
+
if (typeof serviceName === 'string' && serviceName) {
|
|
41
|
+
attribs['service.name'] = serviceName;
|
|
42
|
+
}
|
|
43
|
+
if (typeof serviceVersion === 'string' && serviceVersion) {
|
|
44
|
+
attribs['service.version'] = serviceVersion;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @type {Navigator & { userAgentData?: UserAgentData }} */
|
|
48
|
+
const {userAgent, userAgentData} = navigator;
|
|
49
|
+
const browserInfo = getBrowserInfo(userAgent);
|
|
50
|
+
let platform;
|
|
51
|
+
|
|
52
|
+
if (userAgentData) {
|
|
53
|
+
platform = {name: userAgentData.platform, version: ''};
|
|
54
|
+
attribs[ATTR_BROWSER_BRANDS] = userAgentData.brands.map(
|
|
55
|
+
(i) => `${i.brand} ${i.version}`
|
|
56
|
+
);
|
|
57
|
+
} else {
|
|
58
|
+
platform = getPlatformInfo(userAgent);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (browserInfo) {
|
|
62
|
+
attribs['browser.name'] = browserInfo.name;
|
|
63
|
+
if (browserInfo.version) {
|
|
64
|
+
attribs['browser.version'] = browserInfo.version;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (platform) {
|
|
68
|
+
attribs[ATTR_BROWSER_PLATFORM] = platform.name;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
attribs[ATTR_BROWSER_MOBILE] = navigator.userAgent.includes('Mobi');
|
|
72
|
+
attribs[ATTR_BROWSER_LANGUAGE] = navigator.language;
|
|
73
|
+
attribs[ATTR_USER_AGENT_ORIGINAL] = userAgent;
|
|
74
|
+
attribs['telemetry.distro.name'] = 'elastic';
|
|
75
|
+
attribs['telemetry.distro.version'] = EDOT_VERSION;
|
|
76
|
+
|
|
77
|
+
// This might be usefull info
|
|
78
|
+
// ['browser.touch_screen_enabled']: navigator.maxTouchPoints > 0,
|
|
79
|
+
// ['network.effective_type']: navigator['networkInformation']?.effectiveType || 'unknown',
|
|
80
|
+
// ['screen.width']: window.screen.width,
|
|
81
|
+
// ['screen.height']: window.screen.height,
|
|
82
|
+
// ['screen.size']: computeScreenSize(window.screen.width),
|
|
83
|
+
|
|
84
|
+
return resourceFromAttributes({...attribs, ...SDK_INFO});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// -- helper functions
|
|
88
|
+
// ref: https://mdn2.netlify.app/en-us/docs/web/http/browser_detection_using_the_user_agent/#which_part_of_the_user_agent_contains_the_information_you_are_looking_for
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string} userAgent
|
|
92
|
+
* @returns {{name: string; version: string} | undefined}
|
|
93
|
+
*/
|
|
94
|
+
export function getPlatformInfo(userAgent) {
|
|
95
|
+
/** @type {Array<[string,RegExp[]]>} */
|
|
96
|
+
const platforms = [
|
|
97
|
+
// {name: 'Firefox OS', test: /Firefox\/([\w\.]+)?/i},
|
|
98
|
+
['Symbian', [/SymbianOS\/([\w\.]+)/i]],
|
|
99
|
+
['Windows Phone', [/Windows Phone ([\w\.]+)/i]],
|
|
100
|
+
['Xbox', [/Xbox?/i]],
|
|
101
|
+
['Windows', [/Windows (\d+)/i]],
|
|
102
|
+
['Windows RT', [/Windows NT ([\w\.]+).+ARM;/i]],
|
|
103
|
+
['Windows', [/Windows NT (\d+\.\d+)/i]],
|
|
104
|
+
['Windows IoT', [/Windows IoT (\d+\.\d+)/i]],
|
|
105
|
+
['iOS', [/iPhone OS (\d+(_\d+)*)/i, /iPad;.+CPU OS (\d+(_\d+)*)/i]],
|
|
106
|
+
[
|
|
107
|
+
'macOS',
|
|
108
|
+
[/Mac OS (\d+(_\d+)*)/i, /Mac OS X ([\w\.]+)/i, /Intel Mac OS X/],
|
|
109
|
+
],
|
|
110
|
+
['Ubuntu Touch', [/Ubuntu ([\w\.]+).+Mobile/i]],
|
|
111
|
+
['Android', [/Android ([\w\.]+)/i, /Android;/i]],
|
|
112
|
+
['Linux', [/Linux ([\w_]+)/i, /Linux;/]],
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
for (const p of platforms) {
|
|
116
|
+
const [name, tests] = p;
|
|
117
|
+
for (const t of tests) {
|
|
118
|
+
const match = t.exec(userAgent);
|
|
119
|
+
if (match) {
|
|
120
|
+
let version;
|
|
121
|
+
if (match[1]) {
|
|
122
|
+
version = match[1].replaceAll('_', '.');
|
|
123
|
+
}
|
|
124
|
+
return {name, version};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} userAgent
|
|
132
|
+
* @returns {{name: string; version: string | undefined} | undefined}
|
|
133
|
+
*/
|
|
134
|
+
export function getBrowserInfo(userAgent) {
|
|
135
|
+
// note: only get the major version
|
|
136
|
+
/** @type {Array<[string, RegExp[]]>} */
|
|
137
|
+
const browsers = [
|
|
138
|
+
// Branded or inapp browsers
|
|
139
|
+
['AliPay', [/AliPayClient\/([\w\.]+)/i]],
|
|
140
|
+
[
|
|
141
|
+
'Baidu',
|
|
142
|
+
[
|
|
143
|
+
/bd(?:browser|spark)\/([\w\.]+)/i,
|
|
144
|
+
/baidu(?:boxapp|hd)\/([\w\.]+)/i,
|
|
145
|
+
/BIDUBrowser\/([\w\.]+)/i,
|
|
146
|
+
],
|
|
147
|
+
],
|
|
148
|
+
['Bing', [/Bing(?:Web|Sapphire)\/([\w\.]+)/i]],
|
|
149
|
+
['Camino', [/Camino\/([\w\.]+)/i]],
|
|
150
|
+
['Coc Coc', [/coc_coc_browser\/([\w\.]+)/i]],
|
|
151
|
+
['Dragon', [/(?:Dragon|Comodo_Dragon)\/([\w\.]+)/i]],
|
|
152
|
+
['DuckDuckGo', [/(?:Ddg|DuckDuckGo)\/([\w\.]+)/i]],
|
|
153
|
+
['Electron', [/Electron\/([\w\.]+)/i]],
|
|
154
|
+
['Facebook', [/;fbav\/([\w\.]+);/i, /(?:fban\/fbios|fb_iab\/fb4a)/i]],
|
|
155
|
+
['GSA', [/GSA\/([\w\.]+)/i]],
|
|
156
|
+
['Huawei Browser', [/HuaweiBrowser\/([\w\.]+)/i]],
|
|
157
|
+
['Iron', [/(?:Iron|Iron Safari)\/([\w\.]+)/i]],
|
|
158
|
+
['LG Browser', [/LG Browser\/([\w\.]+)/i]],
|
|
159
|
+
['Lunascape', [/Lunascape\/([\w\.]+)/i]],
|
|
160
|
+
['Maxthon', [/Maxthon\/([\w\.]+)/i, /Mx(?:Browser|iOS)\/([\w\.]+)/i]],
|
|
161
|
+
['MIUI Browser', [/MiuiBrowser\/([\w\.]+)/i]],
|
|
162
|
+
['NokiaBrowser', [/NokiaBrowser\/([\w\.]+)/i]],
|
|
163
|
+
['Oculus Browser', [/OculusBrowser\/([\w\.]+)/i]],
|
|
164
|
+
['Pico Browser', [/PicoBrowser\/([\w\.]+)/i]],
|
|
165
|
+
['Qwant', [/Qwant(?:Mobile|Browser|iOS)\/([\w\.]+)/i]],
|
|
166
|
+
['Samsung Internet', [/SamsungBrowser\/([\w\.]+)/i]],
|
|
167
|
+
['Silk', [/Silk\/([\w\.]+)/i]],
|
|
168
|
+
['Smart Lenovo Browser', [/SLBrowser\/([\w\.]+)/i]],
|
|
169
|
+
['Snapchat', [/Snapchat\/([\w\.]+)/i]],
|
|
170
|
+
['Steam', [/Valve Steam/i]],
|
|
171
|
+
['TikTok', [/musical_ly.+app_version\/([\w\.]+)/i]],
|
|
172
|
+
['Twitter', [/Twitter/i]],
|
|
173
|
+
['Vivaldi', [/Vivaldi\/([\w\.]+)/i]],
|
|
174
|
+
['Yandex', [/Ya(?:SearchBrowser|Browser)\/([\w\.]+)/i]],
|
|
175
|
+
[
|
|
176
|
+
'WeChat',
|
|
177
|
+
[
|
|
178
|
+
/microm.+\bqbcore\/([\w\.]+)/i,
|
|
179
|
+
/\bqbcore\/([\w\.]+).+microm/i,
|
|
180
|
+
/micromessenger\/([\w\.]+)/i,
|
|
181
|
+
],
|
|
182
|
+
],
|
|
183
|
+
// Some wechat UAs contain qqbrowser so this should be last
|
|
184
|
+
['QQBrowser', [/M?QQbrowser\/([\w\.]+)/i]],
|
|
185
|
+
|
|
186
|
+
// The usual suspects
|
|
187
|
+
['Brave', [/Brave\/([\w\.]+)/i]],
|
|
188
|
+
['Edge', [/(?:Edg|Edge|EdgA|EdgW|EdgiOS)\/([\w\.]+)/i]],
|
|
189
|
+
['Opera', [/(?:OPR|OPT|OPiOS|Opera)\/([\w\.]+)/i, /Coast\/([\w\.]+)/i]],
|
|
190
|
+
['Chromium', [/Chromium\/([\w\.]+)/i]],
|
|
191
|
+
[
|
|
192
|
+
'Chrome Headless',
|
|
193
|
+
[/HeadlessChrome\/([\w\.]+)/i, /HeadlessChrome Safari\/([\w\.]+)/i],
|
|
194
|
+
],
|
|
195
|
+
['Chrome', [/Chrome\/([\w\.]+)/i, /Cr(?:iOS|Mo)\/([\w\.]+)/i]],
|
|
196
|
+
['Android Browser', [/Android \d.+Safari\/([\w\.]+)/i]],
|
|
197
|
+
['Firefox', [/(?:Firefox|FxiOS)\/([\w\.]+)/i]],
|
|
198
|
+
['Safari', [/Safari\/([\w\.]+)/i]],
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
for (const b of browsers) {
|
|
202
|
+
const [name, tests] = b;
|
|
203
|
+
for (const t of tests) {
|
|
204
|
+
const match = t.exec(userAgent);
|
|
205
|
+
if (match) {
|
|
206
|
+
let version;
|
|
207
|
+
if (match[1]) {
|
|
208
|
+
version = match[1].replaceAll('_', '.');
|
|
209
|
+
}
|
|
210
|
+
return {name, version};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// /**
|
|
217
|
+
// * @param {number} screenWidth
|
|
218
|
+
// * @returns {'small' | 'medium' | 'large' | 'unknown'}
|
|
219
|
+
// */
|
|
220
|
+
// function computeScreenSize (screenWidth) {
|
|
221
|
+
// if (screenWidth <= 768) return 'small';
|
|
222
|
+
// else if (screenWidth > 768 && screenWidth <= 1024) return 'medium';
|
|
223
|
+
// else if (screenWidth > 1024) return 'large';
|
|
224
|
+
|
|
225
|
+
// return 'unknown';
|
|
226
|
+
// };
|
package/lib/index.js
ADDED
package/lib/logging.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and contributors
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {DiagLogLevel} from '@opentelemetry/api';
|
|
7
|
+
|
|
8
|
+
let _logLevel = DiagLogLevel.INFO;
|
|
9
|
+
const logLevelMap = {
|
|
10
|
+
ALL: DiagLogLevel.ALL,
|
|
11
|
+
VERBOSE: DiagLogLevel.VERBOSE,
|
|
12
|
+
DEBUG: DiagLogLevel.DEBUG,
|
|
13
|
+
INFO: DiagLogLevel.INFO,
|
|
14
|
+
WARN: DiagLogLevel.WARN,
|
|
15
|
+
ERROR: DiagLogLevel.ERROR,
|
|
16
|
+
NONE: DiagLogLevel.NONE,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* TODO: luggite like?
|
|
21
|
+
* @param {Object} config
|
|
22
|
+
* @param {string} config.logLevel
|
|
23
|
+
* @returns {import('@opentelemetry/api').DiagLogger}
|
|
24
|
+
*/
|
|
25
|
+
export function createLogger(config) {
|
|
26
|
+
_logLevel = logLevelMap[config.logLevel.toUpperCase()];
|
|
27
|
+
|
|
28
|
+
if (_logLevel == null) {
|
|
29
|
+
console.warn(
|
|
30
|
+
`Unknown log level "${config.logLevel}", expected one of ${Object.keys(logLevelMap)}, using default`
|
|
31
|
+
);
|
|
32
|
+
_logLevel = logLevelMap.INFO;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
error: makeLogFunction(logLevelMap.ERROR, console.error.bind(console)),
|
|
37
|
+
warn: makeLogFunction(logLevelMap.WARN, console.error.bind(console)),
|
|
38
|
+
info: makeLogFunction(logLevelMap.INFO, console.log.bind(console)),
|
|
39
|
+
debug: makeLogFunction(logLevelMap.DEBUG, console.log.bind(console)),
|
|
40
|
+
verbose: makeLogFunction(
|
|
41
|
+
logLevelMap.VERBOSE,
|
|
42
|
+
console.trace.bind(console)
|
|
43
|
+
),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Build a log emitter function for level minLevel. I.e. this is the
|
|
49
|
+
* creator of `log.info`, `log.error`, etc.
|
|
50
|
+
*
|
|
51
|
+
* @param {number} minLevel
|
|
52
|
+
* @param {(...args: any[]) => void} emitFn
|
|
53
|
+
* @returns {function(...any): void}
|
|
54
|
+
*/
|
|
55
|
+
function makeLogFunction(minLevel, emitFn) {
|
|
56
|
+
return function LOG(...args) {
|
|
57
|
+
const shouldLog = _logLevel >= minLevel;
|
|
58
|
+
if (args.length === 0) {
|
|
59
|
+
// `log.<level>()`
|
|
60
|
+
return shouldLog;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (shouldLog) {
|
|
64
|
+
emitFn(...args);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|