@fluidframework/telemetry-utils 2.0.0-dev.2.2.0.111723 → 2.0.0-dev.3.1.0.125672

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.
Files changed (103) hide show
  1. package/.eslintrc.js +11 -13
  2. package/.mocharc.js +2 -2
  3. package/api-extractor.json +2 -2
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/config.js +3 -3
  6. package/dist/config.js.map +1 -1
  7. package/dist/debugLogger.d.ts.map +1 -1
  8. package/dist/debugLogger.js.map +1 -1
  9. package/dist/errorLogging.d.ts +8 -7
  10. package/dist/errorLogging.d.ts.map +1 -1
  11. package/dist/errorLogging.js +23 -20
  12. package/dist/errorLogging.js.map +1 -1
  13. package/dist/eventEmitterWithErrorHandling.d.ts.map +1 -1
  14. package/dist/eventEmitterWithErrorHandling.js.map +1 -1
  15. package/dist/events.d.ts.map +1 -1
  16. package/dist/events.js.map +1 -1
  17. package/dist/fluidErrorBase.d.ts.map +1 -1
  18. package/dist/fluidErrorBase.js +4 -4
  19. package/dist/fluidErrorBase.js.map +1 -1
  20. package/dist/index.d.ts +3 -2
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/logger.d.ts +15 -4
  24. package/dist/logger.d.ts.map +1 -1
  25. package/dist/logger.js +69 -11
  26. package/dist/logger.js.map +1 -1
  27. package/dist/mockLogger.d.ts +12 -2
  28. package/dist/mockLogger.d.ts.map +1 -1
  29. package/dist/mockLogger.js +24 -2
  30. package/dist/mockLogger.js.map +1 -1
  31. package/dist/packageVersion.d.ts +1 -1
  32. package/dist/packageVersion.js +1 -1
  33. package/dist/packageVersion.js.map +1 -1
  34. package/dist/sampledTelemetryHelper.d.ts.map +1 -1
  35. package/dist/sampledTelemetryHelper.js.map +1 -1
  36. package/dist/telemetryTypes.d.ts +81 -0
  37. package/dist/telemetryTypes.d.ts.map +1 -0
  38. package/dist/telemetryTypes.js +7 -0
  39. package/dist/telemetryTypes.js.map +1 -0
  40. package/dist/thresholdCounter.d.ts.map +1 -1
  41. package/dist/thresholdCounter.js.map +1 -1
  42. package/dist/utils.d.ts.map +1 -1
  43. package/dist/utils.js.map +1 -1
  44. package/lib/config.d.ts.map +1 -1
  45. package/lib/config.js +3 -3
  46. package/lib/config.js.map +1 -1
  47. package/lib/debugLogger.d.ts.map +1 -1
  48. package/lib/debugLogger.js +1 -1
  49. package/lib/debugLogger.js.map +1 -1
  50. package/lib/errorLogging.d.ts +8 -7
  51. package/lib/errorLogging.d.ts.map +1 -1
  52. package/lib/errorLogging.js +23 -20
  53. package/lib/errorLogging.js.map +1 -1
  54. package/lib/eventEmitterWithErrorHandling.d.ts.map +1 -1
  55. package/lib/eventEmitterWithErrorHandling.js.map +1 -1
  56. package/lib/events.d.ts.map +1 -1
  57. package/lib/events.js.map +1 -1
  58. package/lib/fluidErrorBase.d.ts.map +1 -1
  59. package/lib/fluidErrorBase.js +4 -4
  60. package/lib/fluidErrorBase.js.map +1 -1
  61. package/lib/index.d.ts +3 -2
  62. package/lib/index.d.ts.map +1 -1
  63. package/lib/index.js +2 -2
  64. package/lib/index.js.map +1 -1
  65. package/lib/logger.d.ts +15 -4
  66. package/lib/logger.d.ts.map +1 -1
  67. package/lib/logger.js +69 -12
  68. package/lib/logger.js.map +1 -1
  69. package/lib/mockLogger.d.ts +12 -2
  70. package/lib/mockLogger.d.ts.map +1 -1
  71. package/lib/mockLogger.js +24 -2
  72. package/lib/mockLogger.js.map +1 -1
  73. package/lib/packageVersion.d.ts +1 -1
  74. package/lib/packageVersion.js +1 -1
  75. package/lib/packageVersion.js.map +1 -1
  76. package/lib/sampledTelemetryHelper.d.ts.map +1 -1
  77. package/lib/sampledTelemetryHelper.js.map +1 -1
  78. package/lib/telemetryTypes.d.ts +81 -0
  79. package/lib/telemetryTypes.d.ts.map +1 -0
  80. package/lib/telemetryTypes.js +6 -0
  81. package/lib/telemetryTypes.js.map +1 -0
  82. package/lib/thresholdCounter.d.ts.map +1 -1
  83. package/lib/thresholdCounter.js.map +1 -1
  84. package/lib/utils.d.ts.map +1 -1
  85. package/lib/utils.js.map +1 -1
  86. package/package.json +14 -12
  87. package/prettier.config.cjs +1 -1
  88. package/src/config.ts +173 -172
  89. package/src/debugLogger.ts +118 -111
  90. package/src/errorLogging.ts +306 -300
  91. package/src/eventEmitterWithErrorHandling.ts +16 -12
  92. package/src/events.ts +26 -26
  93. package/src/fluidErrorBase.ts +42 -38
  94. package/src/index.ts +29 -9
  95. package/src/logger.ts +568 -500
  96. package/src/mockLogger.ts +118 -88
  97. package/src/packageVersion.ts +1 -1
  98. package/src/sampledTelemetryHelper.ts +122 -122
  99. package/src/telemetryTypes.ts +96 -0
  100. package/src/thresholdCounter.ts +34 -34
  101. package/src/utils.ts +15 -15
  102. package/tsconfig.esnext.json +6 -6
  103. package/tsconfig.json +9 -13
@@ -4,89 +4,89 @@
4
4
  */
5
5
 
6
6
  import {
7
- ILoggingError,
8
- ITaggedTelemetryPropertyType,
9
- ITelemetryLogger,
10
- ITelemetryProperties,
11
- TelemetryEventPropertyType,
7
+ ILoggingError,
8
+ ITaggedTelemetryPropertyType,
9
+ ITelemetryLogger,
10
+ ITelemetryProperties,
11
+ TelemetryEventPropertyType,
12
12
  } from "@fluidframework/common-definitions";
13
13
  import { v4 as uuid } from "uuid";
14
14
  import {
15
- hasErrorInstanceId,
16
- IFluidErrorBase,
17
- isFluidError,
18
- isValidLegacyError,
15
+ hasErrorInstanceId,
16
+ IFluidErrorBase,
17
+ isFluidError,
18
+ isValidLegacyError,
19
19
  } from "./fluidErrorBase";
20
+ import { ITaggedTelemetryPropertyTypeExt, TelemetryEventPropertyTypeExt } from "./telemetryTypes";
20
21
 
21
22
  /** @returns true if value is an object but neither null nor an array */
22
23
  const isRegularObject = (value: any): boolean => {
23
- return value !== null && !Array.isArray(value) && typeof value === "object";
24
+ return value !== null && !Array.isArray(value) && typeof value === "object";
24
25
  };
25
26
 
26
27
  /** Inspect the given error for common "safe" props and return them */
27
28
  export function extractLogSafeErrorProperties(error: any, sanitizeStack: boolean) {
28
- const removeMessageFromStack = (stack: string, errorName?: string) => {
29
- if (!sanitizeStack) {
30
- return stack;
31
- }
32
- const stackFrames = stack.split("\n");
33
- stackFrames.shift(); // Remove "[ErrorName]: [ErrorMessage]"
34
- if (errorName !== undefined) {
35
- stackFrames.unshift(errorName); // Add "[ErrorName]"
36
- }
37
- return stackFrames.join("\n");
38
- };
39
-
40
- const message = (typeof error?.message === "string")
41
- ? error.message as string
42
- : String(error);
43
-
44
- const safeProps: { message: string; errorType?: string; stack?: string; } = {
45
- message,
46
- };
47
-
48
- if (isRegularObject(error)) {
49
- const { errorType, stack, name } = error;
50
-
51
- if (typeof errorType === "string") {
52
- safeProps.errorType = errorType;
53
- }
54
-
55
- if (typeof stack === "string") {
56
- const errorName = (typeof name === "string") ? name : undefined;
57
- safeProps.stack = removeMessageFromStack(stack, errorName);
58
- }
59
- }
60
-
61
- return safeProps;
29
+ const removeMessageFromStack = (stack: string, errorName?: string) => {
30
+ if (!sanitizeStack) {
31
+ return stack;
32
+ }
33
+ const stackFrames = stack.split("\n");
34
+ stackFrames.shift(); // Remove "[ErrorName]: [ErrorMessage]"
35
+ if (errorName !== undefined) {
36
+ stackFrames.unshift(errorName); // Add "[ErrorName]"
37
+ }
38
+ return stackFrames.join("\n");
39
+ };
40
+
41
+ const message = typeof error?.message === "string" ? (error.message as string) : String(error);
42
+
43
+ const safeProps: { message: string; errorType?: string; stack?: string } = {
44
+ message,
45
+ };
46
+
47
+ if (isRegularObject(error)) {
48
+ const { errorType, stack, name } = error;
49
+
50
+ if (typeof errorType === "string") {
51
+ safeProps.errorType = errorType;
52
+ }
53
+
54
+ if (typeof stack === "string") {
55
+ const errorName = typeof name === "string" ? name : undefined;
56
+ safeProps.stack = removeMessageFromStack(stack, errorName);
57
+ }
58
+ }
59
+
60
+ return safeProps;
62
61
  }
63
62
 
64
63
  /** type guard for ILoggingError interface */
65
- export const isILoggingError = (x: any): x is ILoggingError => typeof x?.getTelemetryProperties === "function";
64
+ export const isILoggingError = (x: any): x is ILoggingError =>
65
+ typeof x?.getTelemetryProperties === "function";
66
66
 
67
67
  /** Copy props from source onto target, but do not overwrite an existing prop that matches */
68
68
  function copyProps(target: ITelemetryProperties | LoggingError, source: ITelemetryProperties) {
69
- for (const key of Object.keys(source)) {
70
- if (target[key] === undefined) {
71
- target[key] = source[key];
72
- }
73
- }
69
+ for (const key of Object.keys(source)) {
70
+ if (target[key] === undefined) {
71
+ target[key] = source[key];
72
+ }
73
+ }
74
74
  }
75
75
 
76
76
  /** Metadata to annotate an error object when annotating or normalizing it */
77
77
  export interface IFluidErrorAnnotations {
78
- /** Telemetry props to log with the error */
79
- props?: ITelemetryProperties;
78
+ /** Telemetry props to log with the error */
79
+ props?: ITelemetryProperties;
80
80
  }
81
81
 
82
82
  /** For backwards compatibility with pre-errorInstanceId valid errors */
83
83
  function patchLegacyError(
84
- legacyError: Omit<IFluidErrorBase, "errorInstanceId">,
84
+ legacyError: Omit<IFluidErrorBase, "errorInstanceId">,
85
85
  ): asserts legacyError is IFluidErrorBase {
86
- const patchMe: { -readonly [P in "errorInstanceId"]?: IFluidErrorBase[P] } = legacyError as any;
87
- if (patchMe.errorInstanceId === undefined) {
88
- patchMe.errorInstanceId = uuid();
89
- }
86
+ const patchMe: { -readonly [P in "errorInstanceId"]?: IFluidErrorBase[P] } = legacyError as any;
87
+ if (patchMe.errorInstanceId === undefined) {
88
+ patchMe.errorInstanceId = uuid();
89
+ }
90
90
  }
91
91
 
92
92
  /**
@@ -96,50 +96,50 @@ function patchLegacyError(
96
96
  * @param annotations - Annotations to apply to the normalized error
97
97
  */
98
98
  export function normalizeError(
99
- error: unknown,
100
- annotations: IFluidErrorAnnotations = {},
99
+ error: unknown,
100
+ annotations: IFluidErrorAnnotations = {},
101
101
  ): IFluidErrorBase {
102
- // Back-compat, while IFluidErrorBase is rolled out
103
- if (isValidLegacyError(error)) {
104
- patchLegacyError(error);
105
- }
106
-
107
- if (isFluidError(error)) {
108
- // We can simply add the telemetry props to the error and return it
109
- error.addTelemetryProperties(annotations.props ?? {});
110
- return error;
111
- }
112
-
113
- // We have to construct a new Fluid Error, copying safe properties over
114
- const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);
115
- const fluidError: IFluidErrorBase = new NormalizedLoggingError({
116
- message,
117
- stack,
118
- });
119
-
120
- // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)
121
- // Anywhere they are set should be on a valid Fluid Error that would have been returned above,
122
- // but we can't prove it with the types, so adding this defensive measure.
123
- if (typeof error === "object" && error !== null) {
124
- const { canRetry, retryAfterSeconds } = error as any;
125
- Object.assign(normalizeError, { canRetry, retryAfterSeconds });
126
- }
127
-
128
- if (typeof (error) !== "object") {
129
- // This is only interesting for non-objects
130
- fluidError.addTelemetryProperties({ typeofError: typeof (error) });
131
- }
132
-
133
- const errorTelemetryProps = LoggingError.typeCheck(error)
134
- ? error.getTelemetryProperties()
135
- : { untrustedOrigin: 1 }; // This will let us filter errors that did not originate from our own codebase
136
-
137
- fluidError.addTelemetryProperties({
138
- ...errorTelemetryProps,
139
- ...annotations.props,
140
- });
141
-
142
- return fluidError;
102
+ // Back-compat, while IFluidErrorBase is rolled out
103
+ if (isValidLegacyError(error)) {
104
+ patchLegacyError(error);
105
+ }
106
+
107
+ if (isFluidError(error)) {
108
+ // We can simply add the telemetry props to the error and return it
109
+ error.addTelemetryProperties(annotations.props ?? {});
110
+ return error;
111
+ }
112
+
113
+ // We have to construct a new Fluid Error, copying safe properties over
114
+ const { message, stack } = extractLogSafeErrorProperties(error, false /* sanitizeStack */);
115
+ const fluidError: IFluidErrorBase = new NormalizedLoggingError({
116
+ message,
117
+ stack,
118
+ });
119
+
120
+ // We need to preserve these properties which are used in a non-typesafe way throughout driver code (see #8743)
121
+ // Anywhere they are set should be on a valid Fluid Error that would have been returned above,
122
+ // but we can't prove it with the types, so adding this defensive measure.
123
+ if (typeof error === "object" && error !== null) {
124
+ const { canRetry, retryAfterSeconds } = error as any;
125
+ Object.assign(normalizeError, { canRetry, retryAfterSeconds });
126
+ }
127
+
128
+ if (typeof error !== "object") {
129
+ // This is only interesting for non-objects
130
+ fluidError.addTelemetryProperties({ typeofError: typeof error });
131
+ }
132
+
133
+ const errorTelemetryProps = LoggingError.typeCheck(error)
134
+ ? error.getTelemetryProperties()
135
+ : { untrustedOrigin: 1 }; // This will let us filter errors that did not originate from our own codebase
136
+
137
+ fluidError.addTelemetryProperties({
138
+ ...errorTelemetryProps,
139
+ ...annotations.props,
140
+ });
141
+
142
+ return fluidError;
143
143
  }
144
144
 
145
145
  let stackPopulatedOnCreation: boolean | undefined;
@@ -154,25 +154,25 @@ let stackPopulatedOnCreation: boolean | undefined;
154
154
  * @returns Error object that has stack populated.
155
155
  */
156
156
  export function generateErrorWithStack(): Error {
157
- const err = new Error("<<generated stack>>");
157
+ const err = new Error("<<generated stack>>");
158
158
 
159
- if (stackPopulatedOnCreation === undefined) {
160
- stackPopulatedOnCreation = (err.stack !== undefined);
161
- }
159
+ if (stackPopulatedOnCreation === undefined) {
160
+ stackPopulatedOnCreation = err.stack !== undefined;
161
+ }
162
162
 
163
- if (stackPopulatedOnCreation) {
164
- return err;
165
- }
163
+ if (stackPopulatedOnCreation) {
164
+ return err;
165
+ }
166
166
 
167
- try {
168
- throw err;
169
- } catch (e) {
170
- return e as Error;
171
- }
167
+ try {
168
+ throw err;
169
+ } catch (e) {
170
+ return e as Error;
171
+ }
172
172
  }
173
173
 
174
174
  export function generateStack(): string | undefined {
175
- return generateErrorWithStack().stack;
175
+ return generateErrorWithStack().stack;
176
176
  }
177
177
 
178
178
  /**
@@ -183,72 +183,72 @@ export function generateStack(): string | undefined {
183
183
  * @returns A new error object "wrapping" the given error
184
184
  */
185
185
  export function wrapError<T extends LoggingError>(
186
- innerError: unknown,
187
- newErrorFn: (message: string) => T,
186
+ innerError: unknown,
187
+ newErrorFn: (message: string) => T,
188
188
  ): T {
189
- const {
190
- message,
191
- stack,
192
- } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);
189
+ const { message, stack } = extractLogSafeErrorProperties(innerError, false /* sanitizeStack */);
193
190
 
194
- const newError = newErrorFn(message);
191
+ const newError = newErrorFn(message);
195
192
 
196
- if (stack !== undefined) {
197
- overwriteStack(newError, stack);
198
- }
193
+ if (stack !== undefined) {
194
+ overwriteStack(newError, stack);
195
+ }
199
196
 
200
- // Mark external errors with untrustedOrigin flag
201
- if (isExternalError(innerError)) {
202
- newError.addTelemetryProperties({ untrustedOrigin: 1 });
203
- }
197
+ // Mark external errors with untrustedOrigin flag
198
+ if (isExternalError(innerError)) {
199
+ newError.addTelemetryProperties({ untrustedOrigin: 1 });
200
+ }
204
201
 
205
- // Reuse errorInstanceId
206
- if (hasErrorInstanceId(innerError)) {
207
- newError.overwriteErrorInstanceId(innerError.errorInstanceId);
202
+ // Reuse errorInstanceId
203
+ if (hasErrorInstanceId(innerError)) {
204
+ newError.overwriteErrorInstanceId(innerError.errorInstanceId);
208
205
 
209
- // For "back-compat" in the logs
210
- newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });
211
- }
206
+ // For "back-compat" in the logs
207
+ newError.addTelemetryProperties({ innerErrorInstanceId: innerError.errorInstanceId });
208
+ }
212
209
 
213
- // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties
214
- // This will include the untrustedOrigin property if the inner error itself was created from an external error
215
- if (isILoggingError(innerError)) {
216
- newError.addTelemetryProperties(innerError.getTelemetryProperties());
217
- }
210
+ // Lastly, copy over all other telemetry properties. Note these will not overwrite existing properties
211
+ // This will include the untrustedOrigin property if the inner error itself was created from an external error
212
+ if (isILoggingError(innerError)) {
213
+ newError.addTelemetryProperties(innerError.getTelemetryProperties());
214
+ }
218
215
 
219
- return newError;
216
+ return newError;
220
217
  }
221
218
 
222
219
  /** The same as wrapError, but also logs the innerError, including the wrapping error's instance id */
223
220
  export function wrapErrorAndLog<T extends LoggingError>(
224
- innerError: unknown,
225
- newErrorFn: (message: string) => T,
226
- logger: ITelemetryLogger,
221
+ innerError: unknown,
222
+ newErrorFn: (message: string) => T,
223
+ logger: ITelemetryLogger,
227
224
  ) {
228
- const newError = wrapError(innerError, newErrorFn);
225
+ const newError = wrapError(innerError, newErrorFn);
229
226
 
230
- // This will match innerError.errorInstanceId if present (see wrapError)
231
- const errorInstanceId = newError.errorInstanceId;
227
+ // This will match innerError.errorInstanceId if present (see wrapError)
228
+ const errorInstanceId = newError.errorInstanceId;
232
229
 
233
- // For "back-compat" in the logs
234
- const wrappedByErrorInstanceId = errorInstanceId;
230
+ // For "back-compat" in the logs
231
+ const wrappedByErrorInstanceId = errorInstanceId;
235
232
 
236
- logger.sendTelemetryEvent({
237
- eventName: "WrapError",
238
- errorInstanceId,
239
- wrappedByErrorInstanceId,
240
- }, innerError);
233
+ logger.sendTelemetryEvent(
234
+ {
235
+ eventName: "WrapError",
236
+ errorInstanceId,
237
+ wrappedByErrorInstanceId,
238
+ },
239
+ innerError,
240
+ );
241
241
 
242
- return newError;
242
+ return newError;
243
243
  }
244
244
 
245
245
  function overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {
246
- // supposedly setting stack on an Error can throw.
247
- try {
248
- Object.assign(error, { stack });
249
- } catch (errorSettingStack) {
250
- error.addTelemetryProperties({ stack2: stack });
251
- }
246
+ // supposedly setting stack on an Error can throw.
247
+ try {
248
+ Object.assign(error, { stack });
249
+ } catch (errorSettingStack) {
250
+ error.addTelemetryProperties({ stack2: stack });
251
+ }
252
252
  }
253
253
 
254
254
  /**
@@ -257,23 +257,25 @@ function overwriteStack(error: IFluidErrorBase | LoggingError, stack: string) {
257
257
  * or wrapped in a well-known error type
258
258
  */
259
259
  export function isExternalError(e: any): boolean {
260
- // LoggingErrors are an internal FF error type. However, an external error can be converted
261
- // into a LoggingError if it is normalized. In this case we must use the untrustedOrigin flag to
262
- // determine whether the original error was infact external.
263
- if (LoggingError.typeCheck(e)) {
264
- if ((e as NormalizedLoggingError).errorType === NORMALIZED_ERROR_TYPE) {
265
- return e.getTelemetryProperties().untrustedOrigin === 1;
266
- }
267
- return false;
268
- }
269
- return !isValidLegacyError(e);
260
+ // LoggingErrors are an internal FF error type. However, an external error can be converted
261
+ // into a LoggingError if it is normalized. In this case we must use the untrustedOrigin flag to
262
+ // determine whether the original error was infact external.
263
+ if (LoggingError.typeCheck(e)) {
264
+ if ((e as NormalizedLoggingError).errorType === NORMALIZED_ERROR_TYPE) {
265
+ return e.getTelemetryProperties().untrustedOrigin === 1;
266
+ }
267
+ return false;
268
+ }
269
+ return !isValidLegacyError(e);
270
270
  }
271
271
 
272
272
  /**
273
- * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property
273
+ * Type guard to identify if a particular telemetry property appears to be a tagged telemetry property
274
274
  */
275
- export function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {
276
- return typeof (x?.tag) === "string";
275
+ export function isTaggedTelemetryPropertyValue(
276
+ x: ITaggedTelemetryPropertyTypeExt | TelemetryEventPropertyTypeExt,
277
+ ): x is ITaggedTelemetryPropertyType | ITaggedTelemetryPropertyTypeExt {
278
+ return typeof (x as any)?.tag === "string";
277
279
  }
278
280
 
279
281
  /**
@@ -283,51 +285,51 @@ export function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPro
283
285
  * otherwise returns null since this is what we support at the moment.
284
286
  */
285
287
  function filterValidTelemetryProps(x: any, key: string): TelemetryEventPropertyType {
286
- if (Array.isArray(x) && x.every((val) => isTelemetryEventPropertyValue(val))) {
287
- return JSON.stringify(x);
288
- }
289
- if (isTelemetryEventPropertyValue(x)) {
290
- return x;
291
- }
292
- // We don't support logging arbitrary objects
293
- console.error(`UnSupported Format of Logging Error Property for key ${key}:`, x);
294
- return "REDACTED (arbitrary object)";
288
+ if (Array.isArray(x) && x.every((val) => isTelemetryEventPropertyValue(val))) {
289
+ return JSON.stringify(x);
290
+ }
291
+ if (isTelemetryEventPropertyValue(x)) {
292
+ return x;
293
+ }
294
+ // We don't support logging arbitrary objects
295
+ console.error(`UnSupported Format of Logging Error Property for key ${key}:`, x);
296
+ return "REDACTED (arbitrary object)";
295
297
  }
296
298
 
297
299
  // checking type of x, returns false if x is null
298
300
  function isTelemetryEventPropertyValue(x: any): x is TelemetryEventPropertyType {
299
- switch (typeof x) {
300
- case "string":
301
- case "number":
302
- case "boolean":
303
- case "undefined":
304
- return true;
305
- default:
306
- return false;
307
- }
301
+ switch (typeof x) {
302
+ case "string":
303
+ case "number":
304
+ case "boolean":
305
+ case "undefined":
306
+ return true;
307
+ default:
308
+ return false;
309
+ }
308
310
  }
309
311
  /**
310
312
  * Walk an object's enumerable properties to find those fit for telemetry.
311
313
  */
312
314
  function getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryProperties {
313
- const props: ITelemetryProperties = {};
314
- for (const key of Object.keys(obj)) {
315
- if (keysToOmit.has(key)) {
316
- continue;
317
- }
318
- const val = obj[key];
319
-
320
- // ensure only valid props get logged, since props of logging error could be in any shape
321
- if (isTaggedTelemetryPropertyValue(val)) {
322
- props[key] = {
323
- value: filterValidTelemetryProps(val.value, key),
324
- tag: val.tag,
325
- };
326
- } else {
327
- props[key] = filterValidTelemetryProps(val, key);
328
- }
329
- }
330
- return props;
315
+ const props: ITelemetryProperties = {};
316
+ for (const key of Object.keys(obj)) {
317
+ if (keysToOmit.has(key)) {
318
+ continue;
319
+ }
320
+ const val = obj[key];
321
+
322
+ // ensure only valid props get logged, since props of logging error could be in any shape
323
+ if (isTaggedTelemetryPropertyValue(val)) {
324
+ props[key] = {
325
+ value: filterValidTelemetryProps(val.value, key),
326
+ tag: val.tag,
327
+ };
328
+ } else {
329
+ props[key] = filterValidTelemetryProps(val, key);
330
+ }
331
+ }
332
+ return props;
331
333
  }
332
334
 
333
335
  /**
@@ -336,18 +338,18 @@ function getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryPr
336
338
  * Avoids runtime errors with circular references.
337
339
  * Not ideal, as will cut values that are not necessarily circular references.
338
340
  * Could be improved by implementing Node's util.inspect() for browser (minus all the coloring code)
339
- */
341
+ */
340
342
  export const getCircularReplacer = () => {
341
- const seen = new WeakSet();
342
- return (key: string, value: any): any => {
343
- if (typeof value === "object" && value !== null) {
344
- if (seen.has(value)) {
345
- return "<removed/circular>";
346
- }
347
- seen.add(value);
348
- }
349
- return value;
350
- };
343
+ const seen = new WeakSet();
344
+ return (key: string, value: any): any => {
345
+ if (typeof value === "object" && value !== null) {
346
+ if (seen.has(value)) {
347
+ return "<removed/circular>";
348
+ }
349
+ seen.add(value);
350
+ }
351
+ return value;
352
+ };
351
353
  };
352
354
 
353
355
  /**
@@ -357,90 +359,94 @@ export const getCircularReplacer = () => {
357
359
  *
358
360
  * PLEASE take care to avoid setting sensitive data on this object without proper tagging!
359
361
  */
360
- export class LoggingError extends Error implements ILoggingError, Omit<IFluidErrorBase, "errorType"> {
361
- private _errorInstanceId = uuid();
362
- get errorInstanceId() { return this._errorInstanceId; }
363
- overwriteErrorInstanceId(id: string) { this._errorInstanceId = id; }
364
-
365
- /** Back-compat to appease isFluidError typeguard in old code that may handle this error */
366
- // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.
367
- private readonly fluidErrorCode: "-" = "-";
368
-
369
- /**
370
- * Create a new LoggingError
371
- * @param message - Error message to use for Error base class
372
- * @param props - telemetry props to include on the error for when it's logged
373
- * @param omitPropsFromLogging - properties by name to omit from telemetry props
374
- */
375
- constructor(
376
- message: string,
377
- props?: ITelemetryProperties,
378
- private readonly omitPropsFromLogging: Set<string> = new Set(),
379
- ) {
380
- super(message);
381
-
382
- // Don't log this list itself, or the private _errorInstanceId
383
- omitPropsFromLogging.add("omitPropsFromLogging");
384
- omitPropsFromLogging.add("_errorInstanceId");
385
-
386
- if (props) {
387
- this.addTelemetryProperties(props);
388
- }
389
- }
390
-
391
- /**
392
- * Determines if a given object is an instance of a LoggingError
393
- * @param object - any object
394
- * @returns - true if the object is an instance of a LoggingError, false if not.
395
- */
396
- public static typeCheck(object: unknown): object is LoggingError {
397
- if (typeof object === "object" && object !== null) {
398
- return typeof (object as LoggingError).addTelemetryProperties === "function"
399
- && typeof (object as LoggingError).getTelemetryProperties === "function"
400
- && typeof (object as LoggingError).errorInstanceId === "string";
401
- }
402
- return false;
403
- }
404
-
405
- /**
406
- * Add additional properties to be logged
407
- */
408
- public addTelemetryProperties(props: ITelemetryProperties) {
409
- copyProps(this, props);
410
- }
411
-
412
- /**
413
- * Get all properties fit to be logged to telemetry for this error
414
- */
415
- public getTelemetryProperties(): ITelemetryProperties {
416
- const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);
417
- // Include non-enumerable props that are not returned by getValidTelemetryProps
418
- return {
419
- ...taggableProps,
420
- stack: this.stack,
421
- message: this.message,
422
- errorInstanceId: this._errorInstanceId,
423
- };
424
- }
362
+ export class LoggingError
363
+ extends Error
364
+ implements ILoggingError, Omit<IFluidErrorBase, "errorType">
365
+ {
366
+ private _errorInstanceId = uuid();
367
+ get errorInstanceId() {
368
+ return this._errorInstanceId;
369
+ }
370
+ overwriteErrorInstanceId(id: string) {
371
+ this._errorInstanceId = id;
372
+ }
373
+
374
+ /** Back-compat to appease isFluidError typeguard in old code that may handle this error */
375
+ // @ts-expect-error - This field shouldn't be referenced in the current version, but needs to exist at runtime.
376
+ private readonly fluidErrorCode: "-" = "-";
377
+
378
+ /**
379
+ * Create a new LoggingError
380
+ * @param message - Error message to use for Error base class
381
+ * @param props - telemetry props to include on the error for when it's logged
382
+ * @param omitPropsFromLogging - properties by name to omit from telemetry props
383
+ */
384
+ constructor(
385
+ message: string,
386
+ props?: ITelemetryProperties,
387
+ private readonly omitPropsFromLogging: Set<string> = new Set(),
388
+ ) {
389
+ super(message);
390
+
391
+ // Don't log this list itself, or the private _errorInstanceId
392
+ omitPropsFromLogging.add("omitPropsFromLogging");
393
+ omitPropsFromLogging.add("_errorInstanceId");
394
+
395
+ if (props) {
396
+ this.addTelemetryProperties(props);
397
+ }
398
+ }
399
+
400
+ /**
401
+ * Determines if a given object is an instance of a LoggingError
402
+ * @param object - any object
403
+ * @returns - true if the object is an instance of a LoggingError, false if not.
404
+ */
405
+ public static typeCheck(object: unknown): object is LoggingError {
406
+ if (typeof object === "object" && object !== null) {
407
+ return (
408
+ typeof (object as LoggingError).addTelemetryProperties === "function" &&
409
+ typeof (object as LoggingError).getTelemetryProperties === "function" &&
410
+ typeof (object as LoggingError).errorInstanceId === "string"
411
+ );
412
+ }
413
+ return false;
414
+ }
415
+
416
+ /**
417
+ * Add additional properties to be logged
418
+ */
419
+ public addTelemetryProperties(props: ITelemetryProperties) {
420
+ copyProps(this, props);
421
+ }
422
+
423
+ /**
424
+ * Get all properties fit to be logged to telemetry for this error
425
+ */
426
+ public getTelemetryProperties(): ITelemetryProperties {
427
+ const taggableProps = getValidTelemetryProps(this, this.omitPropsFromLogging);
428
+ // Include non-enumerable props that are not returned by getValidTelemetryProps
429
+ return {
430
+ ...taggableProps,
431
+ stack: this.stack,
432
+ message: this.message,
433
+ errorInstanceId: this._errorInstanceId,
434
+ };
435
+ }
425
436
  }
426
437
 
427
438
  /** The Error class used when normalizing an external error */
428
439
  export const NORMALIZED_ERROR_TYPE = "genericError";
429
440
  class NormalizedLoggingError extends LoggingError {
430
- // errorType "genericError" is used as a default value throughout the code.
431
- // Note that this matches ContainerErrorType/DriverErrorType's genericError
432
- errorType = NORMALIZED_ERROR_TYPE;
433
-
434
- constructor(
435
- errorProps: Pick<IFluidErrorBase,
436
- | "message"
437
- | "stack"
438
- >,
439
- ) {
440
- super(errorProps.message);
441
-
442
- if (errorProps.stack !== undefined) {
443
- overwriteStack(this, errorProps.stack);
444
- }
445
- }
441
+ // errorType "genericError" is used as a default value throughout the code.
442
+ // Note that this matches ContainerErrorType/DriverErrorType's genericError
443
+ errorType = NORMALIZED_ERROR_TYPE;
444
+
445
+ constructor(errorProps: Pick<IFluidErrorBase, "message" | "stack">) {
446
+ super(errorProps.message);
447
+
448
+ if (errorProps.stack !== undefined) {
449
+ overwriteStack(this, errorProps.stack);
450
+ }
451
+ }
446
452
  }