@itwin/core-bentley 3.4.0-dev.47 → 3.4.0-dev.48
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/lib/cjs/AccessToken.d.ts +8 -8
- package/lib/cjs/AccessToken.js +9 -9
- package/lib/cjs/AccessToken.js.map +1 -1
- package/lib/cjs/Assert.d.ts +25 -25
- package/lib/cjs/Assert.js +45 -45
- package/lib/cjs/Assert.js.map +1 -1
- package/lib/cjs/AsyncMutex.d.ts +26 -26
- package/lib/cjs/AsyncMutex.js +43 -43
- package/lib/cjs/AsyncMutex.js.map +1 -1
- package/lib/cjs/BeEvent.d.ts +81 -81
- package/lib/cjs/BeEvent.js +156 -156
- package/lib/cjs/BeEvent.js.map +1 -1
- package/lib/cjs/BeSQLite.d.ts +170 -170
- package/lib/cjs/BeSQLite.js +183 -183
- package/lib/cjs/BeSQLite.js.map +1 -1
- package/lib/cjs/BentleyError.d.ts +376 -376
- package/lib/cjs/BentleyError.js +699 -699
- package/lib/cjs/BentleyError.js.map +1 -1
- package/lib/cjs/BentleyLoggerCategory.d.ts +11 -11
- package/lib/cjs/BentleyLoggerCategory.js +19 -19
- package/lib/cjs/BentleyLoggerCategory.js.map +1 -1
- package/lib/cjs/ByteStream.d.ts +92 -92
- package/lib/cjs/ByteStream.js +123 -123
- package/lib/cjs/ByteStream.js.map +1 -1
- package/lib/cjs/ClassUtils.d.ts +14 -14
- package/lib/cjs/ClassUtils.js +27 -27
- package/lib/cjs/ClassUtils.js.map +1 -1
- package/lib/cjs/Compare.d.ts +47 -47
- package/lib/cjs/Compare.js +63 -63
- package/lib/cjs/Compare.js.map +1 -1
- package/lib/cjs/CompressedId64Set.d.ts +134 -134
- package/lib/cjs/CompressedId64Set.js +428 -428
- package/lib/cjs/CompressedId64Set.js.map +1 -1
- package/lib/cjs/Dictionary.d.ts +125 -125
- package/lib/cjs/Dictionary.js +203 -203
- package/lib/cjs/Dictionary.js.map +1 -1
- package/lib/cjs/Disposable.d.ts +80 -80
- package/lib/cjs/Disposable.js +120 -120
- package/lib/cjs/Disposable.js.map +1 -1
- package/lib/cjs/Id.d.ts +277 -277
- package/lib/cjs/Id.js +619 -619
- package/lib/cjs/Id.js.map +1 -1
- package/lib/cjs/IndexMap.d.ts +65 -65
- package/lib/cjs/IndexMap.js +91 -91
- package/lib/cjs/IndexMap.js.map +1 -1
- package/lib/cjs/JsonSchema.d.ts +77 -77
- package/lib/cjs/JsonSchema.js +9 -9
- package/lib/cjs/JsonSchema.js.map +1 -1
- package/lib/cjs/JsonUtils.d.ts +78 -78
- package/lib/cjs/JsonUtils.js +133 -133
- package/lib/cjs/JsonUtils.js.map +1 -1
- package/lib/cjs/LRUMap.d.ts +129 -129
- package/lib/cjs/LRUMap.js +333 -333
- package/lib/cjs/LRUMap.js.map +1 -1
- package/lib/cjs/Logger.d.ts +141 -141
- package/lib/cjs/Logger.js +254 -254
- package/lib/cjs/Logger.js.map +1 -1
- package/lib/cjs/ObservableSet.d.ts +23 -23
- package/lib/cjs/ObservableSet.js +51 -51
- package/lib/cjs/ObservableSet.js.map +1 -1
- package/lib/cjs/OneAtATimeAction.d.ts +31 -31
- package/lib/cjs/OneAtATimeAction.js +91 -91
- package/lib/cjs/OneAtATimeAction.js.map +1 -1
- package/lib/cjs/OrderedId64Iterable.d.ts +74 -74
- package/lib/cjs/OrderedId64Iterable.js +235 -235
- package/lib/cjs/OrderedId64Iterable.js.map +1 -1
- package/lib/cjs/OrderedSet.d.ts +40 -40
- package/lib/cjs/OrderedSet.js +64 -64
- package/lib/cjs/OrderedSet.js.map +1 -1
- package/lib/cjs/PriorityQueue.d.ts +70 -70
- package/lib/cjs/PriorityQueue.js +140 -140
- package/lib/cjs/PriorityQueue.js.map +1 -1
- package/lib/cjs/ProcessDetector.d.ts +59 -59
- package/lib/cjs/ProcessDetector.js +71 -71
- package/lib/cjs/ProcessDetector.js.map +1 -1
- package/lib/cjs/SortedArray.d.ts +232 -232
- package/lib/cjs/SortedArray.js +301 -301
- package/lib/cjs/SortedArray.js.map +1 -1
- package/lib/cjs/StatusCategory.d.ts +30 -30
- package/lib/cjs/StatusCategory.js +460 -460
- package/lib/cjs/StatusCategory.js.map +1 -1
- package/lib/cjs/StringUtils.d.ts +22 -22
- package/lib/cjs/StringUtils.js +146 -146
- package/lib/cjs/StringUtils.js.map +1 -1
- package/lib/cjs/Time.d.ts +122 -122
- package/lib/cjs/Time.js +146 -146
- package/lib/cjs/Time.js.map +1 -1
- package/lib/cjs/Tracing.d.ts +40 -40
- package/lib/cjs/Tracing.js +130 -130
- package/lib/cjs/Tracing.js.map +1 -1
- package/lib/cjs/UnexpectedErrors.d.ts +43 -43
- package/lib/cjs/UnexpectedErrors.js +65 -65
- package/lib/cjs/UnexpectedErrors.js.map +1 -1
- package/lib/cjs/UtilityTypes.d.ts +86 -86
- package/lib/cjs/UtilityTypes.js +26 -26
- package/lib/cjs/UtilityTypes.js.map +1 -1
- package/lib/cjs/YieldManager.d.ts +18 -18
- package/lib/cjs/YieldManager.js +34 -34
- package/lib/cjs/YieldManager.js.map +1 -1
- package/lib/cjs/core-bentley.d.ts +73 -73
- package/lib/cjs/core-bentley.js +89 -89
- package/lib/cjs/core-bentley.js.map +1 -1
- package/lib/cjs/partitionArray.d.ts +21 -21
- package/lib/cjs/partitionArray.js +43 -43
- package/lib/cjs/partitionArray.js.map +1 -1
- package/lib/esm/AccessToken.d.ts +8 -8
- package/lib/esm/AccessToken.js +8 -8
- package/lib/esm/AccessToken.js.map +1 -1
- package/lib/esm/Assert.d.ts +25 -25
- package/lib/esm/Assert.js +41 -41
- package/lib/esm/Assert.js.map +1 -1
- package/lib/esm/AsyncMutex.d.ts +26 -26
- package/lib/esm/AsyncMutex.js +39 -39
- package/lib/esm/AsyncMutex.js.map +1 -1
- package/lib/esm/BeEvent.d.ts +81 -81
- package/lib/esm/BeEvent.js +150 -150
- package/lib/esm/BeEvent.js.map +1 -1
- package/lib/esm/BeSQLite.d.ts +170 -170
- package/lib/esm/BeSQLite.js +180 -180
- package/lib/esm/BeSQLite.js.map +1 -1
- package/lib/esm/BentleyError.d.ts +376 -376
- package/lib/esm/BentleyError.js +695 -695
- package/lib/esm/BentleyError.js.map +1 -1
- package/lib/esm/BentleyLoggerCategory.d.ts +11 -11
- package/lib/esm/BentleyLoggerCategory.js +16 -16
- package/lib/esm/BentleyLoggerCategory.js.map +1 -1
- package/lib/esm/ByteStream.d.ts +92 -92
- package/lib/esm/ByteStream.js +119 -119
- package/lib/esm/ByteStream.js.map +1 -1
- package/lib/esm/ClassUtils.d.ts +14 -14
- package/lib/esm/ClassUtils.js +22 -22
- package/lib/esm/ClassUtils.js.map +1 -1
- package/lib/esm/Compare.d.ts +47 -47
- package/lib/esm/Compare.js +51 -51
- package/lib/esm/Compare.js.map +1 -1
- package/lib/esm/CompressedId64Set.d.ts +134 -134
- package/lib/esm/CompressedId64Set.js +423 -423
- package/lib/esm/CompressedId64Set.js.map +1 -1
- package/lib/esm/Dictionary.d.ts +125 -125
- package/lib/esm/Dictionary.js +199 -199
- package/lib/esm/Dictionary.js.map +1 -1
- package/lib/esm/Disposable.d.ts +80 -80
- package/lib/esm/Disposable.js +112 -112
- package/lib/esm/Disposable.js.map +1 -1
- package/lib/esm/Id.d.ts +277 -277
- package/lib/esm/Id.js +615 -615
- package/lib/esm/Id.js.map +1 -1
- package/lib/esm/IndexMap.d.ts +65 -65
- package/lib/esm/IndexMap.js +86 -86
- package/lib/esm/IndexMap.js.map +1 -1
- package/lib/esm/JsonSchema.d.ts +77 -77
- package/lib/esm/JsonSchema.js +8 -8
- package/lib/esm/JsonSchema.js.map +1 -1
- package/lib/esm/JsonUtils.d.ts +78 -78
- package/lib/esm/JsonUtils.js +130 -130
- package/lib/esm/JsonUtils.js.map +1 -1
- package/lib/esm/LRUMap.d.ts +129 -129
- package/lib/esm/LRUMap.js +326 -326
- package/lib/esm/LRUMap.js.map +1 -1
- package/lib/esm/Logger.d.ts +141 -141
- package/lib/esm/Logger.js +249 -249
- package/lib/esm/Logger.js.map +1 -1
- package/lib/esm/ObservableSet.d.ts +23 -23
- package/lib/esm/ObservableSet.js +47 -47
- package/lib/esm/ObservableSet.js.map +1 -1
- package/lib/esm/OneAtATimeAction.d.ts +31 -31
- package/lib/esm/OneAtATimeAction.js +86 -86
- package/lib/esm/OneAtATimeAction.js.map +1 -1
- package/lib/esm/OrderedId64Iterable.d.ts +74 -74
- package/lib/esm/OrderedId64Iterable.js +232 -232
- package/lib/esm/OrderedId64Iterable.js.map +1 -1
- package/lib/esm/OrderedSet.d.ts +40 -40
- package/lib/esm/OrderedSet.js +59 -59
- package/lib/esm/OrderedSet.js.map +1 -1
- package/lib/esm/PriorityQueue.d.ts +70 -70
- package/lib/esm/PriorityQueue.js +136 -136
- package/lib/esm/PriorityQueue.js.map +1 -1
- package/lib/esm/ProcessDetector.d.ts +59 -59
- package/lib/esm/ProcessDetector.js +67 -67
- package/lib/esm/ProcessDetector.js.map +1 -1
- package/lib/esm/SortedArray.d.ts +232 -232
- package/lib/esm/SortedArray.js +294 -294
- package/lib/esm/SortedArray.js.map +1 -1
- package/lib/esm/StatusCategory.d.ts +30 -30
- package/lib/esm/StatusCategory.js +454 -454
- package/lib/esm/StatusCategory.js.map +1 -1
- package/lib/esm/StringUtils.d.ts +22 -22
- package/lib/esm/StringUtils.js +140 -140
- package/lib/esm/StringUtils.js.map +1 -1
- package/lib/esm/Time.d.ts +122 -122
- package/lib/esm/Time.js +140 -140
- package/lib/esm/Time.js.map +1 -1
- package/lib/esm/Tracing.d.ts +40 -40
- package/lib/esm/Tracing.js +126 -126
- package/lib/esm/Tracing.js.map +1 -1
- package/lib/esm/UnexpectedErrors.d.ts +43 -43
- package/lib/esm/UnexpectedErrors.js +61 -61
- package/lib/esm/UnexpectedErrors.js.map +1 -1
- package/lib/esm/UtilityTypes.d.ts +86 -86
- package/lib/esm/UtilityTypes.js +21 -21
- package/lib/esm/UtilityTypes.js.map +1 -1
- package/lib/esm/YieldManager.d.ts +18 -18
- package/lib/esm/YieldManager.js +30 -30
- package/lib/esm/YieldManager.js.map +1 -1
- package/lib/esm/core-bentley.d.ts +73 -73
- package/lib/esm/core-bentley.js +77 -77
- package/lib/esm/core-bentley.js.map +1 -1
- package/lib/esm/partitionArray.d.ts +21 -21
- package/lib/esm/partitionArray.js +39 -39
- package/lib/esm/partitionArray.js.map +1 -1
- package/package.json +3 -3
package/lib/cjs/Tracing.d.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import type { Attributes, SpanContext, SpanOptions, Tracer } from "@opentelemetry/api";
|
|
2
|
-
/**
|
|
3
|
-
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
4
|
-
* @alpha
|
|
5
|
-
*/
|
|
6
|
-
export declare enum SpanKind {
|
|
7
|
-
INTERNAL = 0,
|
|
8
|
-
SERVER = 1,
|
|
9
|
-
CLIENT = 2,
|
|
10
|
-
PRODUCER = 3,
|
|
11
|
-
CONSUMER = 4
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
15
|
-
* @alpha
|
|
16
|
-
*/
|
|
17
|
-
export declare class Tracing {
|
|
18
|
-
private static _tracer?;
|
|
19
|
-
private static _openTelemetry?;
|
|
20
|
-
/**
|
|
21
|
-
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
22
|
-
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
23
|
-
* @param name name of the new span
|
|
24
|
-
* @param fn function to run inside the new span
|
|
25
|
-
* @param options span options
|
|
26
|
-
* @param parentContext optional context used to retrieve parent span id
|
|
27
|
-
*/
|
|
28
|
-
static withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
31
|
-
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
32
|
-
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
33
|
-
*/
|
|
34
|
-
static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry): void;
|
|
35
|
-
private static withOpenTelemetry;
|
|
36
|
-
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
37
|
-
* @param attributes The attributes to set
|
|
38
|
-
*/
|
|
39
|
-
static setAttributes(attributes: Attributes): void;
|
|
40
|
-
}
|
|
1
|
+
import type { Attributes, SpanContext, SpanOptions, Tracer } from "@opentelemetry/api";
|
|
2
|
+
/**
|
|
3
|
+
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
4
|
+
* @alpha
|
|
5
|
+
*/
|
|
6
|
+
export declare enum SpanKind {
|
|
7
|
+
INTERNAL = 0,
|
|
8
|
+
SERVER = 1,
|
|
9
|
+
CLIENT = 2,
|
|
10
|
+
PRODUCER = 3,
|
|
11
|
+
CONSUMER = 4
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
15
|
+
* @alpha
|
|
16
|
+
*/
|
|
17
|
+
export declare class Tracing {
|
|
18
|
+
private static _tracer?;
|
|
19
|
+
private static _openTelemetry?;
|
|
20
|
+
/**
|
|
21
|
+
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
22
|
+
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
23
|
+
* @param name name of the new span
|
|
24
|
+
* @param fn function to run inside the new span
|
|
25
|
+
* @param options span options
|
|
26
|
+
* @param parentContext optional context used to retrieve parent span id
|
|
27
|
+
*/
|
|
28
|
+
static withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
31
|
+
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
32
|
+
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
33
|
+
*/
|
|
34
|
+
static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry): void;
|
|
35
|
+
private static withOpenTelemetry;
|
|
36
|
+
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
37
|
+
* @param attributes The attributes to set
|
|
38
|
+
*/
|
|
39
|
+
static setAttributes(attributes: Attributes): void;
|
|
40
|
+
}
|
|
41
41
|
//# sourceMappingURL=Tracing.d.ts.map
|
package/lib/cjs/Tracing.js
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Tracing = exports.SpanKind = void 0;
|
|
4
|
-
const Logger_1 = require("./Logger");
|
|
5
|
-
// re-export so that consumers can construct full SpanOptions object without external dependencies
|
|
6
|
-
/**
|
|
7
|
-
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
8
|
-
* @alpha
|
|
9
|
-
*/
|
|
10
|
-
var SpanKind;
|
|
11
|
-
(function (SpanKind) {
|
|
12
|
-
SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
|
|
13
|
-
SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
|
|
14
|
-
SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
|
|
15
|
-
SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
|
|
16
|
-
SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
|
|
17
|
-
})(SpanKind = exports.SpanKind || (exports.SpanKind = {}));
|
|
18
|
-
function isValidPrimitive(val) {
|
|
19
|
-
return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
|
|
20
|
-
}
|
|
21
|
-
// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.
|
|
22
|
-
// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.
|
|
23
|
-
function isValidPrimitiveArray(val) {
|
|
24
|
-
if (!Array.isArray(val))
|
|
25
|
-
return false;
|
|
26
|
-
let itemType;
|
|
27
|
-
for (const x of val) {
|
|
28
|
-
if (x === undefined || x === null)
|
|
29
|
-
continue;
|
|
30
|
-
if (!itemType) {
|
|
31
|
-
itemType = typeof x;
|
|
32
|
-
if (!isValidPrimitive(x))
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
if (typeof x !== itemType)
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
function isPlainObject(obj) {
|
|
41
|
-
return typeof obj === "object" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
|
42
|
-
}
|
|
43
|
-
function* getFlatEntries(obj, path = "") {
|
|
44
|
-
if (isValidPrimitiveArray(obj)) {
|
|
45
|
-
yield [path, obj];
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// Prefer JSON serialization over flattening for any non-POJO types.
|
|
49
|
-
// There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)
|
|
50
|
-
if (!isPlainObject(obj) && !Array.isArray(obj)) {
|
|
51
|
-
yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
// Always serialize empty objects/arrays as empty array values
|
|
55
|
-
const entries = Object.entries(obj);
|
|
56
|
-
if (entries.length === 0)
|
|
57
|
-
yield [path, []];
|
|
58
|
-
for (const [key, val] of entries)
|
|
59
|
-
yield* getFlatEntries(val, (path === "") ? key : `${path}.${key}`);
|
|
60
|
-
}
|
|
61
|
-
function flattenObject(obj) {
|
|
62
|
-
return Object.fromEntries(getFlatEntries(obj));
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
66
|
-
* @alpha
|
|
67
|
-
*/
|
|
68
|
-
class Tracing {
|
|
69
|
-
/**
|
|
70
|
-
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
71
|
-
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
72
|
-
* @param name name of the new span
|
|
73
|
-
* @param fn function to run inside the new span
|
|
74
|
-
* @param options span options
|
|
75
|
-
* @param parentContext optional context used to retrieve parent span id
|
|
76
|
-
*/
|
|
77
|
-
static async withSpan(name, fn, options, parentContext) {
|
|
78
|
-
if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)
|
|
79
|
-
return fn();
|
|
80
|
-
// this case is for context propagation - parentContext is typically constructed from HTTP headers
|
|
81
|
-
const parent = parentContext === undefined
|
|
82
|
-
? Tracing._openTelemetry.context.active()
|
|
83
|
-
: Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);
|
|
84
|
-
return Tracing._openTelemetry.context.with(Tracing._openTelemetry.trace.setSpan(parent, Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())), async () => {
|
|
85
|
-
var _a, _b, _c, _d;
|
|
86
|
-
try {
|
|
87
|
-
return await fn();
|
|
88
|
-
}
|
|
89
|
-
catch (err) {
|
|
90
|
-
if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse
|
|
91
|
-
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttribute("error", true);
|
|
92
|
-
throw err;
|
|
93
|
-
}
|
|
94
|
-
finally {
|
|
95
|
-
(_d = (_c = Tracing._openTelemetry) === null || _c === void 0 ? void 0 : _c.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _d === void 0 ? void 0 : _d.end();
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
101
|
-
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
102
|
-
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
103
|
-
*/
|
|
104
|
-
static enableOpenTelemetry(tracer, api) {
|
|
105
|
-
Tracing._tracer = tracer;
|
|
106
|
-
Tracing._openTelemetry = api;
|
|
107
|
-
Logger_1.Logger.logTrace = Tracing.withOpenTelemetry(Logger_1.Logger.logTrace);
|
|
108
|
-
Logger_1.Logger.logInfo = Tracing.withOpenTelemetry(Logger_1.Logger.logInfo);
|
|
109
|
-
Logger_1.Logger.logWarning = Tracing.withOpenTelemetry(Logger_1.Logger.logWarning);
|
|
110
|
-
Logger_1.Logger.logError = Tracing.withOpenTelemetry(Logger_1.Logger.logError);
|
|
111
|
-
}
|
|
112
|
-
static withOpenTelemetry(base, isError = false) {
|
|
113
|
-
return (category, message, metaData) => {
|
|
114
|
-
var _a, _b;
|
|
115
|
-
try {
|
|
116
|
-
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.addEvent(message, { ...flattenObject(Logger_1.Logger.getMetaData(metaData)), error: isError });
|
|
117
|
-
}
|
|
118
|
-
catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work
|
|
119
|
-
base(category, message, metaData);
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
123
|
-
* @param attributes The attributes to set
|
|
124
|
-
*/
|
|
125
|
-
static setAttributes(attributes) {
|
|
126
|
-
var _a, _b;
|
|
127
|
-
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttributes(attributes);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
exports.Tracing = Tracing;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tracing = exports.SpanKind = void 0;
|
|
4
|
+
const Logger_1 = require("./Logger");
|
|
5
|
+
// re-export so that consumers can construct full SpanOptions object without external dependencies
|
|
6
|
+
/**
|
|
7
|
+
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
8
|
+
* @alpha
|
|
9
|
+
*/
|
|
10
|
+
var SpanKind;
|
|
11
|
+
(function (SpanKind) {
|
|
12
|
+
SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
|
|
13
|
+
SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
|
|
14
|
+
SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
|
|
15
|
+
SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
|
|
16
|
+
SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
|
|
17
|
+
})(SpanKind = exports.SpanKind || (exports.SpanKind = {}));
|
|
18
|
+
function isValidPrimitive(val) {
|
|
19
|
+
return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
|
|
20
|
+
}
|
|
21
|
+
// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.
|
|
22
|
+
// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.
|
|
23
|
+
function isValidPrimitiveArray(val) {
|
|
24
|
+
if (!Array.isArray(val))
|
|
25
|
+
return false;
|
|
26
|
+
let itemType;
|
|
27
|
+
for (const x of val) {
|
|
28
|
+
if (x === undefined || x === null)
|
|
29
|
+
continue;
|
|
30
|
+
if (!itemType) {
|
|
31
|
+
itemType = typeof x;
|
|
32
|
+
if (!isValidPrimitive(x))
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (typeof x !== itemType)
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
function isPlainObject(obj) {
|
|
41
|
+
return typeof obj === "object" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
|
42
|
+
}
|
|
43
|
+
function* getFlatEntries(obj, path = "") {
|
|
44
|
+
if (isValidPrimitiveArray(obj)) {
|
|
45
|
+
yield [path, obj];
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// Prefer JSON serialization over flattening for any non-POJO types.
|
|
49
|
+
// There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)
|
|
50
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) {
|
|
51
|
+
yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Always serialize empty objects/arrays as empty array values
|
|
55
|
+
const entries = Object.entries(obj);
|
|
56
|
+
if (entries.length === 0)
|
|
57
|
+
yield [path, []];
|
|
58
|
+
for (const [key, val] of entries)
|
|
59
|
+
yield* getFlatEntries(val, (path === "") ? key : `${path}.${key}`);
|
|
60
|
+
}
|
|
61
|
+
function flattenObject(obj) {
|
|
62
|
+
return Object.fromEntries(getFlatEntries(obj));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
66
|
+
* @alpha
|
|
67
|
+
*/
|
|
68
|
+
class Tracing {
|
|
69
|
+
/**
|
|
70
|
+
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
71
|
+
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
72
|
+
* @param name name of the new span
|
|
73
|
+
* @param fn function to run inside the new span
|
|
74
|
+
* @param options span options
|
|
75
|
+
* @param parentContext optional context used to retrieve parent span id
|
|
76
|
+
*/
|
|
77
|
+
static async withSpan(name, fn, options, parentContext) {
|
|
78
|
+
if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)
|
|
79
|
+
return fn();
|
|
80
|
+
// this case is for context propagation - parentContext is typically constructed from HTTP headers
|
|
81
|
+
const parent = parentContext === undefined
|
|
82
|
+
? Tracing._openTelemetry.context.active()
|
|
83
|
+
: Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);
|
|
84
|
+
return Tracing._openTelemetry.context.with(Tracing._openTelemetry.trace.setSpan(parent, Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())), async () => {
|
|
85
|
+
var _a, _b, _c, _d;
|
|
86
|
+
try {
|
|
87
|
+
return await fn();
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse
|
|
91
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttribute("error", true);
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
(_d = (_c = Tracing._openTelemetry) === null || _c === void 0 ? void 0 : _c.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _d === void 0 ? void 0 : _d.end();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
101
|
+
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
102
|
+
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
103
|
+
*/
|
|
104
|
+
static enableOpenTelemetry(tracer, api) {
|
|
105
|
+
Tracing._tracer = tracer;
|
|
106
|
+
Tracing._openTelemetry = api;
|
|
107
|
+
Logger_1.Logger.logTrace = Tracing.withOpenTelemetry(Logger_1.Logger.logTrace);
|
|
108
|
+
Logger_1.Logger.logInfo = Tracing.withOpenTelemetry(Logger_1.Logger.logInfo);
|
|
109
|
+
Logger_1.Logger.logWarning = Tracing.withOpenTelemetry(Logger_1.Logger.logWarning);
|
|
110
|
+
Logger_1.Logger.logError = Tracing.withOpenTelemetry(Logger_1.Logger.logError);
|
|
111
|
+
}
|
|
112
|
+
static withOpenTelemetry(base, isError = false) {
|
|
113
|
+
return (category, message, metaData) => {
|
|
114
|
+
var _a, _b;
|
|
115
|
+
try {
|
|
116
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.addEvent(message, { ...flattenObject(Logger_1.Logger.getMetaData(metaData)), error: isError });
|
|
117
|
+
}
|
|
118
|
+
catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work
|
|
119
|
+
base(category, message, metaData);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
123
|
+
* @param attributes The attributes to set
|
|
124
|
+
*/
|
|
125
|
+
static setAttributes(attributes) {
|
|
126
|
+
var _a, _b;
|
|
127
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttributes(attributes);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.Tracing = Tracing;
|
|
131
131
|
//# sourceMappingURL=Tracing.js.map
|
package/lib/cjs/Tracing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tracing.js","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":";;;AAKA,qCAA+C;AAE/C,k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AAC,iBAAiB,CAAC,eAAM,CAAC,UAAU,CAAC,CAAC;QACjE,eAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,iBAAiB,CAAC,eAAM,CAAC,QAAQ,CAAC,CAAC;IAC/D,CAAC;IAEO,MAAM,CAAC,iBAAiB,CAAC,IAAiB,EAAE,UAAmB,KAAK;QAC1E,OAAO,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;;YACrC,IAAI;gBACF,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,QAAQ,CAAC,OAAO,EAAE,EAAE,GAAG,aAAa,CAAC,eAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;aACvK;YAAC,OAAO,EAAE,EAAE,GAAG,CAAC,mHAAmH;YACpI,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,aAAa,CAAC,UAAsB;;QAChD,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAC5G,CAAC;CACF;AArED,0BAqEC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport type { Attributes, AttributeValue, ContextAPI, SpanContext, SpanOptions, TraceAPI, Tracer } from \"@opentelemetry/api\";\r\nimport { LogFunction, Logger } from \"./Logger\";\r\n\r\n// re-export so that consumers can construct full SpanOptions object without external dependencies\r\n/**\r\n * Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)\r\n * @alpha\r\n */\r\nexport enum SpanKind {\r\n INTERNAL = 0,\r\n SERVER = 1,\r\n CLIENT = 2,\r\n PRODUCER = 3,\r\n CONSUMER = 4\r\n}\r\n\r\nfunction isValidPrimitive(val: unknown): val is AttributeValue {\r\n return typeof val === \"string\" || typeof val === \"number\" || typeof val === \"boolean\";\r\n}\r\n\r\n// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.\r\n// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.\r\nfunction isValidPrimitiveArray(val: unknown): val is AttributeValue {\r\n if (!Array.isArray(val))\r\n return false;\r\n\r\n let itemType;\r\n for (const x of val) {\r\n if (x === undefined || x === null)\r\n continue;\r\n\r\n if (!itemType) {\r\n itemType = typeof x;\r\n if (!isValidPrimitive(x))\r\n return false;\r\n }\r\n\r\n if (typeof x !== itemType)\r\n return false;\r\n }\r\n return true;\r\n}\r\n\r\nfunction isPlainObject(obj: unknown): obj is object {\r\n return typeof obj === \"object\" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;\r\n}\r\n\r\nfunction* getFlatEntries(obj: unknown, path = \"\"): Iterable<[string, AttributeValue]> {\r\n if (isValidPrimitiveArray(obj)) {\r\n yield [path, obj];\r\n return;\r\n }\r\n\r\n // Prefer JSON serialization over flattening for any non-POJO types.\r\n // There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)\r\n if (!isPlainObject(obj) && !Array.isArray(obj)) {\r\n yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];\r\n return;\r\n }\r\n\r\n // Always serialize empty objects/arrays as empty array values\r\n const entries = Object.entries(obj);\r\n if (entries.length === 0)\r\n yield [path, []];\r\n\r\n for (const [key, val] of entries)\r\n yield* getFlatEntries(val, (path === \"\") ? key : `${path}.${key}`);\r\n}\r\n\r\nfunction flattenObject(obj: object): Attributes {\r\n return Object.fromEntries(getFlatEntries(obj));\r\n}\r\n\r\n/**\r\n * Enables OpenTelemetry tracing in addition to traditional logging.\r\n * @alpha\r\n */\r\nexport class Tracing {\r\n private static _tracer?: Tracer;\r\n private static _openTelemetry?: { trace: Pick<TraceAPI, \"setSpan\" | \"setSpanContext\" | \"getSpan\">, context: Pick<ContextAPI, \"active\" | \"with\"> };\r\n\r\n /**\r\n * If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.\r\n * If OpenTelemetry tracing is _not_ enabled, runs the provided function.\r\n * @param name name of the new span\r\n * @param fn function to run inside the new span\r\n * @param options span options\r\n * @param parentContext optional context used to retrieve parent span id\r\n */\r\n public static async withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T> {\r\n if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)\r\n return fn();\r\n\r\n // this case is for context propagation - parentContext is typically constructed from HTTP headers\r\n const parent = parentContext === undefined\r\n ? Tracing._openTelemetry.context.active()\r\n : Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);\r\n\r\n return Tracing._openTelemetry.context.with(\r\n Tracing._openTelemetry.trace.setSpan(\r\n parent,\r\n Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())\r\n ),\r\n async () => {\r\n try {\r\n return await fn();\r\n } catch (err) {\r\n if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttribute(\"error\", true);\r\n throw err;\r\n } finally {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.end();\r\n }\r\n },\r\n );\r\n }\r\n\r\n /**\r\n * Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.\r\n * [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.\r\n * @note Node.js OpenTelemetry SDK should be initialized by the user.\r\n */\r\n public static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry) {\r\n Tracing._tracer = tracer;\r\n Tracing._openTelemetry = api;\r\n Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);\r\n Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);\r\n Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);\r\n Logger.logError = Tracing.withOpenTelemetry(Logger.logError);\r\n }\r\n\r\n private static withOpenTelemetry(base: LogFunction, isError: boolean = false): LogFunction {\r\n return (category, message, metaData) => {\r\n try {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.addEvent(message, { ...flattenObject(Logger.getMetaData(metaData)), error: isError });\r\n } catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work\r\n base(category, message, metaData);\r\n };\r\n }\r\n\r\n /** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.\r\n * @param attributes The attributes to set\r\n */\r\n public static setAttributes(attributes: Attributes) {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttributes(attributes);\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"Tracing.js","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":";;;AAKA,qCAA+C;AAE/C,kGAAkG;AAClG;;;GAGG;AACH,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,+CAAY,CAAA;IACZ,2CAAU,CAAA;IACV,2CAAU,CAAA;IACV,+CAAY,CAAA;IACZ,+CAAY,CAAA;AACd,CAAC,EANW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAMnB;AAED,SAAS,gBAAgB,CAAC,GAAY;IACpC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,SAAS,CAAC;AACxF,CAAC;AAED,6GAA6G;AAC7G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C,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,QAAQ,CAAC,OAAO,EAAE,EAAE,GAAG,aAAa,CAAC,eAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;aACvK;YAAC,OAAO,EAAE,EAAE,GAAG,CAAC,mHAAmH;YACpI,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,aAAa,CAAC,UAAsB;;QAChD,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAC5G,CAAC;CACF;AArED,0BAqEC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport type { Attributes, AttributeValue, ContextAPI, SpanContext, SpanOptions, TraceAPI, Tracer } from \"@opentelemetry/api\";\nimport { LogFunction, Logger } from \"./Logger\";\n\n// re-export so that consumers can construct full SpanOptions object without external dependencies\n/**\n * Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)\n * @alpha\n */\nexport enum SpanKind {\n INTERNAL = 0,\n SERVER = 1,\n CLIENT = 2,\n PRODUCER = 3,\n CONSUMER = 4\n}\n\nfunction isValidPrimitive(val: unknown): val is AttributeValue {\n return typeof val === \"string\" || typeof val === \"number\" || typeof val === \"boolean\";\n}\n\n// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.\n// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.\nfunction isValidPrimitiveArray(val: unknown): val is AttributeValue {\n if (!Array.isArray(val))\n return false;\n\n let itemType;\n for (const x of val) {\n if (x === undefined || x === null)\n continue;\n\n if (!itemType) {\n itemType = typeof x;\n if (!isValidPrimitive(x))\n return false;\n }\n\n if (typeof x !== itemType)\n return false;\n }\n return true;\n}\n\nfunction isPlainObject(obj: unknown): obj is object {\n return typeof obj === \"object\" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;\n}\n\nfunction* getFlatEntries(obj: unknown, path = \"\"): Iterable<[string, AttributeValue]> {\n if (isValidPrimitiveArray(obj)) {\n yield [path, obj];\n return;\n }\n\n // Prefer JSON serialization over flattening for any non-POJO types.\n // There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)\n if (!isPlainObject(obj) && !Array.isArray(obj)) {\n yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];\n return;\n }\n\n // Always serialize empty objects/arrays as empty array values\n const entries = Object.entries(obj);\n if (entries.length === 0)\n yield [path, []];\n\n for (const [key, val] of entries)\n yield* getFlatEntries(val, (path === \"\") ? key : `${path}.${key}`);\n}\n\nfunction flattenObject(obj: object): Attributes {\n return Object.fromEntries(getFlatEntries(obj));\n}\n\n/**\n * Enables OpenTelemetry tracing in addition to traditional logging.\n * @alpha\n */\nexport class Tracing {\n private static _tracer?: Tracer;\n private static _openTelemetry?: { trace: Pick<TraceAPI, \"setSpan\" | \"setSpanContext\" | \"getSpan\">, context: Pick<ContextAPI, \"active\" | \"with\"> };\n\n /**\n * If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.\n * If OpenTelemetry tracing is _not_ enabled, runs the provided function.\n * @param name name of the new span\n * @param fn function to run inside the new span\n * @param options span options\n * @param parentContext optional context used to retrieve parent span id\n */\n public static async withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T> {\n if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)\n return fn();\n\n // this case is for context propagation - parentContext is typically constructed from HTTP headers\n const parent = parentContext === undefined\n ? Tracing._openTelemetry.context.active()\n : Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);\n\n return Tracing._openTelemetry.context.with(\n Tracing._openTelemetry.trace.setSpan(\n parent,\n Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())\n ),\n async () => {\n try {\n return await fn();\n } catch (err) {\n if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttribute(\"error\", true);\n throw err;\n } finally {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.end();\n }\n },\n );\n }\n\n /**\n * Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.\n * [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.\n * @note Node.js OpenTelemetry SDK should be initialized by the user.\n */\n public static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry) {\n Tracing._tracer = tracer;\n Tracing._openTelemetry = api;\n Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);\n Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);\n Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);\n Logger.logError = Tracing.withOpenTelemetry(Logger.logError);\n }\n\n private static withOpenTelemetry(base: LogFunction, isError: boolean = false): LogFunction {\n return (category, message, metaData) => {\n try {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.addEvent(message, { ...flattenObject(Logger.getMetaData(metaData)), error: isError });\n } catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work\n base(category, message, metaData);\n };\n }\n\n /** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.\n * @param attributes The attributes to set\n */\n public static setAttributes(attributes: Attributes) {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttributes(attributes);\n }\n}\n"]}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/** @packageDocumentation
|
|
2
|
-
* @module Errors
|
|
3
|
-
*/
|
|
4
|
-
/// <reference types="node" />
|
|
5
|
-
/** A function to be notified when an unexpected error happens
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export declare type OnUnexpectedError = (error: any) => void;
|
|
9
|
-
/**
|
|
10
|
-
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
11
|
-
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
12
|
-
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
13
|
-
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export declare class UnexpectedErrors {
|
|
17
|
-
/** handler for re-throwing exceptions directly */
|
|
18
|
-
static readonly reThrowImmediate: (e: any) => never;
|
|
19
|
-
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
20
|
-
static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
|
|
21
|
-
/** handler for logging exception to console */
|
|
22
|
-
static readonly consoleLog: (e: any) => void;
|
|
23
|
-
/** handler for logging exception with [[Logger]] */
|
|
24
|
-
static readonly errorLog: (e: any) => void;
|
|
25
|
-
private static _telemetry;
|
|
26
|
-
private static _handler;
|
|
27
|
-
private constructor();
|
|
28
|
-
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
29
|
-
* @returns a method to remove the tracker
|
|
30
|
-
*/
|
|
31
|
-
static addTelemetry(tracker: OnUnexpectedError): () => void;
|
|
32
|
-
/** call this method when an unexpected error happens so the global handler can process it.
|
|
33
|
-
* @param error the unexpected error
|
|
34
|
-
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
35
|
-
*/
|
|
36
|
-
static handle(error: any, notifyTelemetry?: boolean): void;
|
|
37
|
-
/** establish a new global *unexpected error* handler.
|
|
38
|
-
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
39
|
-
* The default is [[errorLog]].
|
|
40
|
-
* @returns the previous handler. Useful to temporarily change the handler.
|
|
41
|
-
*/
|
|
42
|
-
static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
|
|
43
|
-
}
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Errors
|
|
3
|
+
*/
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
/** A function to be notified when an unexpected error happens
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export declare type OnUnexpectedError = (error: any) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
11
|
+
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
12
|
+
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
13
|
+
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export declare class UnexpectedErrors {
|
|
17
|
+
/** handler for re-throwing exceptions directly */
|
|
18
|
+
static readonly reThrowImmediate: (e: any) => never;
|
|
19
|
+
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
20
|
+
static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
|
|
21
|
+
/** handler for logging exception to console */
|
|
22
|
+
static readonly consoleLog: (e: any) => void;
|
|
23
|
+
/** handler for logging exception with [[Logger]] */
|
|
24
|
+
static readonly errorLog: (e: any) => void;
|
|
25
|
+
private static _telemetry;
|
|
26
|
+
private static _handler;
|
|
27
|
+
private constructor();
|
|
28
|
+
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
29
|
+
* @returns a method to remove the tracker
|
|
30
|
+
*/
|
|
31
|
+
static addTelemetry(tracker: OnUnexpectedError): () => void;
|
|
32
|
+
/** call this method when an unexpected error happens so the global handler can process it.
|
|
33
|
+
* @param error the unexpected error
|
|
34
|
+
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
35
|
+
*/
|
|
36
|
+
static handle(error: any, notifyTelemetry?: boolean): void;
|
|
37
|
+
/** establish a new global *unexpected error* handler.
|
|
38
|
+
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
39
|
+
* The default is [[errorLog]].
|
|
40
|
+
* @returns the previous handler. Useful to temporarily change the handler.
|
|
41
|
+
*/
|
|
42
|
+
static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
|
|
43
|
+
}
|
|
44
44
|
//# sourceMappingURL=UnexpectedErrors.d.ts.map
|
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*---------------------------------------------------------------------------------------------
|
|
3
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
4
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
5
|
-
*--------------------------------------------------------------------------------------------*/
|
|
6
|
-
/** @packageDocumentation
|
|
7
|
-
* @module Errors
|
|
8
|
-
*/
|
|
9
|
-
var _a;
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.UnexpectedErrors = void 0;
|
|
12
|
-
const Logger_1 = require("./Logger");
|
|
13
|
-
/**
|
|
14
|
-
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
15
|
-
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
16
|
-
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
17
|
-
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
class UnexpectedErrors {
|
|
21
|
-
constructor() { } // this is a singleton
|
|
22
|
-
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
23
|
-
* @returns a method to remove the tracker
|
|
24
|
-
*/
|
|
25
|
-
static addTelemetry(tracker) {
|
|
26
|
-
this._telemetry.push(tracker);
|
|
27
|
-
return () => this._telemetry.splice(this._telemetry.indexOf(tracker), 1);
|
|
28
|
-
}
|
|
29
|
-
/** call this method when an unexpected error happens so the global handler can process it.
|
|
30
|
-
* @param error the unexpected error
|
|
31
|
-
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
32
|
-
*/
|
|
33
|
-
static handle(error, notifyTelemetry = true) {
|
|
34
|
-
this._handler(error);
|
|
35
|
-
if (notifyTelemetry)
|
|
36
|
-
this._telemetry.forEach((telemetry) => {
|
|
37
|
-
try {
|
|
38
|
-
telemetry(error);
|
|
39
|
-
}
|
|
40
|
-
catch (_) { } // ignore errors from telemetry trackers
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
/** establish a new global *unexpected error* handler.
|
|
44
|
-
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
45
|
-
* The default is [[errorLog]].
|
|
46
|
-
* @returns the previous handler. Useful to temporarily change the handler.
|
|
47
|
-
*/
|
|
48
|
-
static setHandler(handler) {
|
|
49
|
-
const oldHandler = this._handler;
|
|
50
|
-
this._handler = handler;
|
|
51
|
-
return oldHandler;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.UnexpectedErrors = UnexpectedErrors;
|
|
55
|
-
_a = UnexpectedErrors;
|
|
56
|
-
/** handler for re-throwing exceptions directly */
|
|
57
|
-
UnexpectedErrors.reThrowImmediate = (e) => { throw e; };
|
|
58
|
-
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
59
|
-
UnexpectedErrors.reThrowDeferred = (e) => setTimeout(() => { throw e; }, 0);
|
|
60
|
-
/** handler for logging exception to console */
|
|
61
|
-
UnexpectedErrors.consoleLog = (e) => console.error(e); // eslint-disable-line no-console
|
|
62
|
-
/** handler for logging exception with [[Logger]] */
|
|
63
|
-
UnexpectedErrors.errorLog = (e) => Logger_1.Logger.logException("unhandled", e);
|
|
64
|
-
UnexpectedErrors._telemetry = [];
|
|
65
|
-
UnexpectedErrors._handler = _a.errorLog; // default to error logging
|
|
1
|
+
"use strict";
|
|
2
|
+
/*---------------------------------------------------------------------------------------------
|
|
3
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
4
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
5
|
+
*--------------------------------------------------------------------------------------------*/
|
|
6
|
+
/** @packageDocumentation
|
|
7
|
+
* @module Errors
|
|
8
|
+
*/
|
|
9
|
+
var _a;
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.UnexpectedErrors = void 0;
|
|
12
|
+
const Logger_1 = require("./Logger");
|
|
13
|
+
/**
|
|
14
|
+
* Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
|
|
15
|
+
* unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
|
|
16
|
+
* errors should not cause program termination, and should instead be logged and swallowed. However, for
|
|
17
|
+
* development/debugging, it can be helpful to re-throw exceptions so they are not missed.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
class UnexpectedErrors {
|
|
21
|
+
constructor() { } // this is a singleton
|
|
22
|
+
/** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
|
|
23
|
+
* @returns a method to remove the tracker
|
|
24
|
+
*/
|
|
25
|
+
static addTelemetry(tracker) {
|
|
26
|
+
this._telemetry.push(tracker);
|
|
27
|
+
return () => this._telemetry.splice(this._telemetry.indexOf(tracker), 1);
|
|
28
|
+
}
|
|
29
|
+
/** call this method when an unexpected error happens so the global handler can process it.
|
|
30
|
+
* @param error the unexpected error
|
|
31
|
+
* @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
|
|
32
|
+
*/
|
|
33
|
+
static handle(error, notifyTelemetry = true) {
|
|
34
|
+
this._handler(error);
|
|
35
|
+
if (notifyTelemetry)
|
|
36
|
+
this._telemetry.forEach((telemetry) => {
|
|
37
|
+
try {
|
|
38
|
+
telemetry(error);
|
|
39
|
+
}
|
|
40
|
+
catch (_) { } // ignore errors from telemetry trackers
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** establish a new global *unexpected error* handler.
|
|
44
|
+
* @param handler the new global handler. You may provide your own function or use one of the static members of this class.
|
|
45
|
+
* The default is [[errorLog]].
|
|
46
|
+
* @returns the previous handler. Useful to temporarily change the handler.
|
|
47
|
+
*/
|
|
48
|
+
static setHandler(handler) {
|
|
49
|
+
const oldHandler = this._handler;
|
|
50
|
+
this._handler = handler;
|
|
51
|
+
return oldHandler;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.UnexpectedErrors = UnexpectedErrors;
|
|
55
|
+
_a = UnexpectedErrors;
|
|
56
|
+
/** handler for re-throwing exceptions directly */
|
|
57
|
+
UnexpectedErrors.reThrowImmediate = (e) => { throw e; };
|
|
58
|
+
/** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
|
|
59
|
+
UnexpectedErrors.reThrowDeferred = (e) => setTimeout(() => { throw e; }, 0);
|
|
60
|
+
/** handler for logging exception to console */
|
|
61
|
+
UnexpectedErrors.consoleLog = (e) => console.error(e); // eslint-disable-line no-console
|
|
62
|
+
/** handler for logging exception with [[Logger]] */
|
|
63
|
+
UnexpectedErrors.errorLog = (e) => Logger_1.Logger.logException("unhandled", e);
|
|
64
|
+
UnexpectedErrors._telemetry = [];
|
|
65
|
+
UnexpectedErrors._handler = _a.errorLog; // default to error logging
|
|
66
66
|
//# sourceMappingURL=UnexpectedErrors.js.map
|