@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.
- package/.eslintrc.cjs +1 -1
- package/api-extractor/api-extractor-lint-bundle.json +5 -0
- package/api-extractor/api-extractor-lint-legacy.cjs.json +5 -0
- package/api-extractor/api-extractor-lint-legacy.esm.json +5 -0
- package/api-extractor/api-extractor-lint-public.cjs.json +5 -0
- package/api-extractor/api-extractor-lint-public.esm.json +5 -0
- package/api-extractor.json +1 -1
- package/api-report/telemetry-utils.alpha.api.md +13 -28
- package/api-report/telemetry-utils.beta.api.md +7 -7
- package/api-report/telemetry-utils.public.api.md +7 -7
- package/dist/config.d.ts +2 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/error.d.ts +5 -5
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js.map +1 -1
- package/dist/errorLogging.d.ts +1 -1
- package/dist/errorLogging.d.ts.map +1 -1
- package/dist/errorLogging.js +21 -8
- package/dist/errorLogging.js.map +1 -1
- package/dist/eventEmitterWithErrorHandling.d.ts +2 -2
- package/dist/eventEmitterWithErrorHandling.d.ts.map +1 -1
- package/dist/eventEmitterWithErrorHandling.js.map +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/logger.d.ts +3 -5
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js.map +1 -1
- package/dist/mockLogger.d.ts +34 -7
- package/dist/mockLogger.d.ts.map +1 -1
- package/dist/mockLogger.js +31 -10
- package/dist/mockLogger.js.map +1 -1
- package/dist/sampledTelemetryHelper.d.ts +6 -2
- package/dist/sampledTelemetryHelper.d.ts.map +1 -1
- package/dist/sampledTelemetryHelper.js +10 -2
- package/dist/sampledTelemetryHelper.js.map +1 -1
- package/dist/telemetryEventBatcher.d.ts +4 -27
- package/dist/telemetryEventBatcher.d.ts.map +1 -1
- package/dist/telemetryEventBatcher.js +5 -28
- package/dist/telemetryEventBatcher.js.map +1 -1
- package/dist/telemetryTypes.d.ts +32 -18
- package/dist/telemetryTypes.d.ts.map +1 -1
- package/dist/telemetryTypes.js.map +1 -1
- package/dist/thresholdCounter.d.ts +1 -1
- package/dist/thresholdCounter.d.ts.map +1 -1
- package/dist/thresholdCounter.js.map +1 -1
- package/dist/utils.d.ts +13 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +16 -1
- package/dist/utils.js.map +1 -1
- package/lib/config.d.ts +2 -2
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js.map +1 -1
- package/lib/error.d.ts +5 -5
- package/lib/error.d.ts.map +1 -1
- package/lib/error.js.map +1 -1
- package/lib/errorLogging.d.ts +1 -1
- package/lib/errorLogging.d.ts.map +1 -1
- package/lib/errorLogging.js +21 -8
- package/lib/errorLogging.js.map +1 -1
- package/lib/eventEmitterWithErrorHandling.d.ts +2 -2
- package/lib/eventEmitterWithErrorHandling.d.ts.map +1 -1
- package/lib/eventEmitterWithErrorHandling.js.map +1 -1
- package/lib/index.d.ts +8 -8
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/logger.d.ts +3 -5
- package/lib/logger.d.ts.map +1 -1
- package/lib/logger.js.map +1 -1
- package/lib/mockLogger.d.ts +34 -7
- package/lib/mockLogger.d.ts.map +1 -1
- package/lib/mockLogger.js +29 -9
- package/lib/mockLogger.js.map +1 -1
- package/lib/sampledTelemetryHelper.d.ts +6 -2
- package/lib/sampledTelemetryHelper.d.ts.map +1 -1
- package/lib/sampledTelemetryHelper.js +10 -2
- package/lib/sampledTelemetryHelper.js.map +1 -1
- package/lib/telemetryEventBatcher.d.ts +4 -27
- package/lib/telemetryEventBatcher.d.ts.map +1 -1
- package/lib/telemetryEventBatcher.js +5 -28
- package/lib/telemetryEventBatcher.js.map +1 -1
- package/lib/telemetryTypes.d.ts +32 -18
- package/lib/telemetryTypes.d.ts.map +1 -1
- package/lib/telemetryTypes.js.map +1 -1
- package/lib/thresholdCounter.d.ts +1 -1
- package/lib/thresholdCounter.d.ts.map +1 -1
- package/lib/thresholdCounter.js.map +1 -1
- package/lib/utils.d.ts +13 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +14 -0
- package/lib/utils.js.map +1 -1
- package/package.json +18 -9
- package/src/config.ts +16 -16
- package/src/error.ts +13 -13
- package/src/errorLogging.ts +31 -15
- package/src/eventEmitterWithErrorHandling.ts +3 -3
- package/src/index.ts +17 -12
- package/src/logger.ts +21 -21
- package/src/mockLogger.ts +71 -21
- package/src/sampledTelemetryHelper.ts +15 -5
- package/src/telemetryEventBatcher.ts +6 -47
- package/src/telemetryTypes.ts +38 -19
- package/src/thresholdCounter.ts +1 -1
- package/src/utils.ts +17 -2
- 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
|
-
|
|
9
|
+
import type {
|
|
10
|
+
ITelemetryGenericEventExt,
|
|
11
11
|
ITelemetryLoggerExt,
|
|
12
|
-
|
|
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
|
-
|
|
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())
|
|
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
|
-
*
|
|
17
|
-
* @
|
|
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
|
|
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
|
-
|
|
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[`
|
|
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[`
|
|
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
|
}
|
package/src/telemetryTypes.ts
CHANGED
|
@@ -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
|
|
57
|
-
|
|
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
|
-
|
|
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 -
|
|
114
|
-
* @param logLevel -
|
|
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 -
|
|
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 -
|
|
133
|
-
* @param logLevel -
|
|
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,
|
package/src/thresholdCounter.ts
CHANGED
|
@@ -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