@fluidframework/telemetry-utils 2.0.0-dev-rc.5.0.0.271717 → 2.0.0-dev-rc.5.0.0.272889

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 (108) hide show
  1. package/.eslintrc.cjs +1 -1
  2. package/api-extractor/api-extractor-lint-bundle.json +5 -0
  3. package/api-extractor/api-extractor-lint-legacy.cjs.json +5 -0
  4. package/api-extractor/api-extractor-lint-legacy.esm.json +5 -0
  5. package/api-extractor/api-extractor-lint-public.cjs.json +5 -0
  6. package/api-extractor/api-extractor-lint-public.esm.json +5 -0
  7. package/api-extractor.json +1 -1
  8. package/api-report/telemetry-utils.alpha.api.md +13 -28
  9. package/api-report/telemetry-utils.beta.api.md +7 -7
  10. package/api-report/telemetry-utils.public.api.md +7 -7
  11. package/dist/config.d.ts +2 -2
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js.map +1 -1
  14. package/dist/error.d.ts +5 -5
  15. package/dist/error.d.ts.map +1 -1
  16. package/dist/error.js.map +1 -1
  17. package/dist/errorLogging.d.ts +1 -1
  18. package/dist/errorLogging.d.ts.map +1 -1
  19. package/dist/errorLogging.js +21 -8
  20. package/dist/errorLogging.js.map +1 -1
  21. package/dist/eventEmitterWithErrorHandling.d.ts +2 -2
  22. package/dist/eventEmitterWithErrorHandling.d.ts.map +1 -1
  23. package/dist/eventEmitterWithErrorHandling.js.map +1 -1
  24. package/dist/index.d.ts +8 -8
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +3 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/logger.d.ts +3 -5
  29. package/dist/logger.d.ts.map +1 -1
  30. package/dist/logger.js.map +1 -1
  31. package/dist/mockLogger.d.ts +34 -7
  32. package/dist/mockLogger.d.ts.map +1 -1
  33. package/dist/mockLogger.js +31 -10
  34. package/dist/mockLogger.js.map +1 -1
  35. package/dist/sampledTelemetryHelper.d.ts +6 -2
  36. package/dist/sampledTelemetryHelper.d.ts.map +1 -1
  37. package/dist/sampledTelemetryHelper.js +10 -2
  38. package/dist/sampledTelemetryHelper.js.map +1 -1
  39. package/dist/telemetryEventBatcher.d.ts +4 -27
  40. package/dist/telemetryEventBatcher.d.ts.map +1 -1
  41. package/dist/telemetryEventBatcher.js +5 -28
  42. package/dist/telemetryEventBatcher.js.map +1 -1
  43. package/dist/telemetryTypes.d.ts +32 -18
  44. package/dist/telemetryTypes.d.ts.map +1 -1
  45. package/dist/telemetryTypes.js.map +1 -1
  46. package/dist/thresholdCounter.d.ts +1 -1
  47. package/dist/thresholdCounter.d.ts.map +1 -1
  48. package/dist/thresholdCounter.js.map +1 -1
  49. package/dist/utils.d.ts +13 -1
  50. package/dist/utils.d.ts.map +1 -1
  51. package/dist/utils.js +16 -1
  52. package/dist/utils.js.map +1 -1
  53. package/lib/config.d.ts +2 -2
  54. package/lib/config.d.ts.map +1 -1
  55. package/lib/config.js.map +1 -1
  56. package/lib/error.d.ts +5 -5
  57. package/lib/error.d.ts.map +1 -1
  58. package/lib/error.js.map +1 -1
  59. package/lib/errorLogging.d.ts +1 -1
  60. package/lib/errorLogging.d.ts.map +1 -1
  61. package/lib/errorLogging.js +21 -8
  62. package/lib/errorLogging.js.map +1 -1
  63. package/lib/eventEmitterWithErrorHandling.d.ts +2 -2
  64. package/lib/eventEmitterWithErrorHandling.d.ts.map +1 -1
  65. package/lib/eventEmitterWithErrorHandling.js.map +1 -1
  66. package/lib/index.d.ts +8 -8
  67. package/lib/index.d.ts.map +1 -1
  68. package/lib/index.js +2 -2
  69. package/lib/index.js.map +1 -1
  70. package/lib/logger.d.ts +3 -5
  71. package/lib/logger.d.ts.map +1 -1
  72. package/lib/logger.js.map +1 -1
  73. package/lib/mockLogger.d.ts +34 -7
  74. package/lib/mockLogger.d.ts.map +1 -1
  75. package/lib/mockLogger.js +29 -9
  76. package/lib/mockLogger.js.map +1 -1
  77. package/lib/sampledTelemetryHelper.d.ts +6 -2
  78. package/lib/sampledTelemetryHelper.d.ts.map +1 -1
  79. package/lib/sampledTelemetryHelper.js +10 -2
  80. package/lib/sampledTelemetryHelper.js.map +1 -1
  81. package/lib/telemetryEventBatcher.d.ts +4 -27
  82. package/lib/telemetryEventBatcher.d.ts.map +1 -1
  83. package/lib/telemetryEventBatcher.js +5 -28
  84. package/lib/telemetryEventBatcher.js.map +1 -1
  85. package/lib/telemetryTypes.d.ts +32 -18
  86. package/lib/telemetryTypes.d.ts.map +1 -1
  87. package/lib/telemetryTypes.js.map +1 -1
  88. package/lib/thresholdCounter.d.ts +1 -1
  89. package/lib/thresholdCounter.d.ts.map +1 -1
  90. package/lib/thresholdCounter.js.map +1 -1
  91. package/lib/utils.d.ts +13 -1
  92. package/lib/utils.d.ts.map +1 -1
  93. package/lib/utils.js +14 -0
  94. package/lib/utils.js.map +1 -1
  95. package/package.json +18 -9
  96. package/src/config.ts +16 -16
  97. package/src/error.ts +13 -13
  98. package/src/errorLogging.ts +31 -15
  99. package/src/eventEmitterWithErrorHandling.ts +3 -3
  100. package/src/index.ts +17 -12
  101. package/src/logger.ts +21 -21
  102. package/src/mockLogger.ts +71 -21
  103. package/src/sampledTelemetryHelper.ts +15 -5
  104. package/src/telemetryEventBatcher.ts +6 -47
  105. package/src/telemetryTypes.ts +38 -19
  106. package/src/thresholdCounter.ts +1 -1
  107. package/src/utils.ts +17 -2
  108. package/tsdoc.json +4 -0
@@ -6,10 +6,10 @@
6
6
  import { performance } from "@fluid-internal/client-utils";
7
7
  import type { IDisposable, ITelemetryBaseProperties } from "@fluidframework/core-interfaces";
8
8
 
9
- import {
10
- type ITelemetryGenericEventExt,
9
+ import type {
10
+ ITelemetryGenericEventExt,
11
11
  ITelemetryLoggerExt,
12
- type ITelemetryPerformanceEventExt,
12
+ ITelemetryPerformanceEventExt,
13
13
  } from "./telemetryTypes.js";
14
14
 
15
15
  /**
@@ -58,7 +58,14 @@ interface Measurements {
58
58
  * @internal
59
59
  */
60
60
  export class SampledTelemetryHelper implements IDisposable {
61
- disposed: boolean = false;
61
+ private _disposed: boolean = false;
62
+
63
+ /**
64
+ * {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed}
65
+ */
66
+ public get disposed(): boolean {
67
+ return this._disposed;
68
+ }
62
69
 
63
70
  private readonly measurementsMap = new Map<string, Measurements>();
64
71
 
@@ -145,6 +152,9 @@ export class SampledTelemetryHelper implements IDisposable {
145
152
  }
146
153
 
147
154
  public dispose(error?: Error | undefined): void {
148
- for (const [k] of this.measurementsMap.entries()) this.flushBucket(k);
155
+ for (const [k] of this.measurementsMap.entries()) {
156
+ this.flushBucket(k);
157
+ }
158
+ this._disposed = true;
149
159
  }
150
160
  }
@@ -3,8 +3,6 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { performance } from "@fluid-internal/client-utils";
7
-
8
6
  import { roundToDecimalPlaces } from "./mathTools.js";
9
7
  import type {
10
8
  ITelemetryGenericEventExt,
@@ -13,20 +11,10 @@ import type {
13
11
  } from "./telemetryTypes.js";
14
12
 
15
13
  /**
16
- * Expected type of the custom data passed into the logger.
17
- * @internal
18
- */
19
- export interface IMeasuredCodeResult<TKey extends string> {
20
- /**
21
- * Optional properties to log custom data. The set of properties must be the same for all calls to the `measure` function.
22
- */
23
- telemetryProperties?: { readonly [key in TKey]: number };
24
- }
25
-
26
- /**
27
- * Telemetry class that measures the execution time of a given piece of code and accumulates user defined telemetry metrics, to finally log an event through the {@link TelemetryEventBatcher.logger | logger} provided to this class when the number of calls to the {@link TelemetryEventBatcher.measure | measure} function reaches the specified by {@link TelemetryEventBatcher.threshold | threshold}.
14
+ * Telemetry class that accumulates measurements which are eventually logged in a telemetry event through the provided
15
+ * {@link TelemetryEventBatcher.logger | logger} when the number of calls to the function reaches the specified {@link TelemetryEventBatcher.threshold | threshold}.
28
16
  *
29
- * @remarks It is expected to be used for a single event type. If the set of `telemetryProperties` is different for different events, a separate `TelemetryEventBatcher` should be created for each event type.
17
+ * @remarks It is expected to be used for a single event type. If different properties should be logged at different times, a separate `TelemetryEventBatcher` should be created with separate `TMetrics` type.
30
18
  * @typeparam TMetrics - The set of keys that should be logged.
31
19
  * E.g., `keyof Foo` for logging properties `bar` and `baz` from `type Foo = { bar: number, baz: number }`.
32
20
  *
@@ -34,11 +22,6 @@ export interface IMeasuredCodeResult<TKey extends string> {
34
22
  * @internal
35
23
  */
36
24
  export class TelemetryEventBatcher<TMetrics extends string> {
37
- /**
38
- * Stores the accumulated duration of the code passed into the logger.
39
- */
40
- private accumulatedDuration: number = 0;
41
-
42
25
  /**
43
26
  * Stores the sum of the custom data passed into the logger.
44
27
  */
@@ -71,34 +54,13 @@ export class TelemetryEventBatcher<TMetrics extends string> {
71
54
  private readonly threshold: number,
72
55
  ) {}
73
56
 
74
- /**
75
- * Executes the specified code, keeping statistics of its execution time and the telemetry properties it returns, and when the {@link TelemetryEventBatcher.threshold} is reached it logs a performance event which includes the maxes and averages.
76
- * @param codeToMeasure - The code to be executed and measured.
77
- * @param customData - Custom data to be logged.
78
- *
79
- * @returns Whatever the passed-in code block returns.
80
- */
81
- public measure<T extends IMeasuredCodeResult<TMetrics>>(codeToMeasure: () => T): T {
82
- const start = performance.now();
83
- const returnValue = codeToMeasure();
84
- const duration = performance.now() - start;
85
-
86
- this.accumulatedDuration += duration;
87
-
88
- if (returnValue.telemetryProperties) {
89
- this.accumulateAndLog(returnValue.telemetryProperties);
90
- }
91
-
92
- return returnValue;
93
- }
94
-
95
57
  /**
96
58
  * Accumulates the custom data and sends it to the logger every {@link TelemetryEventBatcher.threshold} calls.
97
59
  *
98
60
  * @param customData -
99
61
  * A record storing the custom data to be accumulated and eventually logged.
100
62
  */
101
- private accumulateAndLog(customData: Record<TMetrics, number>): void {
63
+ public accumulateAndLog(customData: Record<TMetrics, number>): void {
102
64
  for (const key of Object.keys(customData) as TMetrics[]) {
103
65
  this.dataSums[key] = (this.dataSums[key] ?? 0) + customData[key];
104
66
  this.dataMaxes[key] = Math.max(
@@ -119,17 +81,15 @@ export class TelemetryEventBatcher<TMetrics extends string> {
119
81
  ...this.eventBase,
120
82
  };
121
83
 
122
- telemetryEvent.duration = this.accumulatedDuration /= this.counter;
123
-
124
84
  for (const key of Object.keys(this.dataSums) as TMetrics[]) {
125
85
  if (this.dataSums[key] !== undefined) {
126
- telemetryEvent[`avg${key}`] = roundToDecimalPlaces(
86
+ telemetryEvent[`avg_${key}`] = roundToDecimalPlaces(
127
87
  this.dataSums[key]! / this.counter,
128
88
  6,
129
89
  );
130
90
  }
131
91
  if (this.dataMaxes[key] !== undefined) {
132
- telemetryEvent[`max${key}`] = this.dataMaxes[key];
92
+ telemetryEvent[`max_${key}`] = this.dataMaxes[key];
133
93
  }
134
94
  }
135
95
 
@@ -137,7 +97,6 @@ export class TelemetryEventBatcher<TMetrics extends string> {
137
97
 
138
98
  // Reset the counter and the data.
139
99
  this.counter = 0;
140
- this.accumulatedDuration = 0;
141
100
  this.dataSums = {};
142
101
  this.dataMaxes = {};
143
102
  }
@@ -3,7 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ITelemetryBaseLogger, LogLevel, Tagged } from "@fluidframework/core-interfaces";
6
+ import type { ITelemetryBaseLogger, LogLevel, Tagged } from "@fluidframework/core-interfaces";
7
7
 
8
8
  /**
9
9
  * The categories FF uses when instrumenting the code.
@@ -31,10 +31,7 @@ export type TelemetryEventPropertyTypeExt =
31
31
  | boolean
32
32
  | undefined
33
33
  | (string | number | boolean)[]
34
- | {
35
- [key: string]: // Flat objects can have the same properties as the event itself
36
- string | number | boolean | undefined | (string | number | boolean)[];
37
- };
34
+ | Record<string, string | number | boolean | undefined | (string | number | boolean)[]>;
38
35
 
39
36
  /**
40
37
  * A property to be logged to telemetry containing both the value and a tag. Tags are generic strings that can be used
@@ -53,9 +50,10 @@ export interface ITaggedTelemetryPropertyTypeExt {
53
50
  * JSON-serializable properties, which will be logged with telemetry.
54
51
  * @alpha
55
52
  */
56
- export interface ITelemetryPropertiesExt {
57
- [index: string]: TelemetryEventPropertyTypeExt | Tagged<TelemetryEventPropertyTypeExt>;
58
- }
53
+ export type ITelemetryPropertiesExt = Record<
54
+ string,
55
+ TelemetryEventPropertyTypeExt | Tagged<TelemetryEventPropertyTypeExt>
56
+ >;
59
57
 
60
58
  /**
61
59
  * Interface for logging telemetry statements.
@@ -66,7 +64,14 @@ export interface ITelemetryPropertiesExt {
66
64
  * @internal
67
65
  */
68
66
  export interface ITelemetryEventExt extends ITelemetryPropertiesExt {
67
+ /**
68
+ * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseEvent.category}
69
+ */
69
70
  category: string;
71
+
72
+ /**
73
+ * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseEvent.eventName}
74
+ */
70
75
  eventName: string;
71
76
  }
72
77
 
@@ -76,7 +81,15 @@ export interface ITelemetryEventExt extends ITelemetryPropertiesExt {
76
81
  * @alpha
77
82
  */
78
83
  export interface ITelemetryGenericEventExt extends ITelemetryPropertiesExt {
84
+ /**
85
+ * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseEvent.eventName}
86
+ */
79
87
  eventName: string;
88
+
89
+ /**
90
+ * Optional event {@link @fluidframework/core-interfaces#ITelemetryBaseEvent.category}.
91
+ * @defaultValue "generic"
92
+ */
80
93
  category?: TelemetryEventCategory;
81
94
  }
82
95
 
@@ -86,6 +99,9 @@ export interface ITelemetryGenericEventExt extends ITelemetryPropertiesExt {
86
99
  * @alpha
87
100
  */
88
101
  export interface ITelemetryErrorEventExt extends ITelemetryPropertiesExt {
102
+ /**
103
+ * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseEvent.eventName}
104
+ */
89
105
  eventName: string;
90
106
  }
91
107
 
@@ -95,7 +111,10 @@ export interface ITelemetryErrorEventExt extends ITelemetryPropertiesExt {
95
111
  * @alpha
96
112
  */
97
113
  export interface ITelemetryPerformanceEventExt extends ITelemetryGenericEventExt {
98
- duration?: number; // Duration of event (optional)
114
+ /**
115
+ * Duration of event (optional)
116
+ */
117
+ duration?: number;
99
118
  }
100
119
 
101
120
  /**
@@ -108,10 +127,10 @@ export interface ITelemetryPerformanceEventExt extends ITelemetryGenericEventExt
108
127
  */
109
128
  export interface ITelemetryLoggerExt extends ITelemetryBaseLogger {
110
129
  /**
111
- * Send information telemetry event
112
- * @param event - Event to send
113
- * @param error - optional error object to log
114
- * @param logLevel - optional level of the log.
130
+ * Send an information telemetry event.
131
+ * @param event - Event to send.
132
+ * @param error - Optional error object to log.
133
+ * @param logLevel - Optional level of the log. Default: {@link @fluidframework/core-interfaces#LogLevel.default}.
115
134
  */
116
135
  sendTelemetryEvent(
117
136
  event: ITelemetryGenericEventExt,
@@ -120,17 +139,17 @@ export interface ITelemetryLoggerExt extends ITelemetryBaseLogger {
120
139
  ): void;
121
140
 
122
141
  /**
123
- * Send error telemetry event
124
- * @param event - Event to send
125
- * @param error - optional error object to log
142
+ * Send an error telemetry event.
143
+ * @param event - Event to send.
144
+ * @param error - Optional error object to log.
126
145
  */
127
146
  sendErrorEvent(event: ITelemetryErrorEventExt, error?: unknown): void;
128
147
 
129
148
  /**
130
- * Send performance telemetry event
149
+ * Send a performance telemetry event.
131
150
  * @param event - Event to send
132
- * @param error - optional error object to log
133
- * @param logLevel - optional level of the log.
151
+ * @param error - Optional error object to log.
152
+ * @param logLevel - Optional level of the log. Default: {@link @fluidframework/core-interfaces#LogLevel.default}.
134
153
  */
135
154
  sendPerformanceEvent(
136
155
  event: ITelemetryPerformanceEventExt,
@@ -3,7 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ITelemetryLoggerExt } from "./telemetryTypes.js";
6
+ import type { ITelemetryLoggerExt } from "./telemetryTypes.js";
7
7
 
8
8
  /**
9
9
  * Utility counter which will send event only if the provided value is above a configured threshold.
package/src/utils.ts CHANGED
@@ -3,10 +3,10 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ITelemetryBaseEvent } from "@fluidframework/core-interfaces";
6
+ import type { ITelemetryBaseEvent } from "@fluidframework/core-interfaces";
7
7
 
8
8
  import { loggerToMonitoringContext } from "./config.js";
9
- import { ITelemetryGenericEventExt, ITelemetryLoggerExt } from "./telemetryTypes.js";
9
+ import type { ITelemetryGenericEventExt, ITelemetryLoggerExt } from "./telemetryTypes.js";
10
10
 
11
11
  /**
12
12
  * An object that contains a callback used in conjunction with the {@link createSampledLogger} utility function to provide custom logic for sampling events.
@@ -104,3 +104,18 @@ export function createSampledLogger(
104
104
 
105
105
  return sampledLogger;
106
106
  }
107
+
108
+ /**
109
+ * Runs the specified function and returns an object with the time it took to run as well as any output from it.
110
+ * @remarks Useful in conjunction with {@link TelemetryEventBatcher}.
111
+ *
112
+ * @param codeToMeasure - The code to be executed and measured.
113
+ * @returns The total duration of the code execution and whatever the passed-in code block returns.
114
+ * @internal
115
+ */
116
+ export function measure<T>(codeToMeasure: () => T): { duration: number; output: T } {
117
+ const start = performance.now();
118
+ const output = codeToMeasure();
119
+ const duration = performance.now() - start;
120
+ return { duration, output };
121
+ }
package/tsdoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3
+ "extends": ["../../../common/build/build-common/tsdoc-base.json"]
4
+ }