@fluidframework/telemetry-utils 2.0.0-rc.4.0.6 → 2.0.0-rc.5.0.1
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/CHANGELOG.md +27 -0
- 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 +107 -0
- package/api-report/telemetry-utils.beta.api.md +29 -0
- package/api-report/telemetry-utils.public.api.md +29 -0
- package/biome.jsonc +4 -0
- 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 +2 -6
- package/dist/errorLogging.d.ts.map +1 -1
- package/dist/errorLogging.js +22 -29
- 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/events.js.map +1 -1
- package/dist/fluidErrorBase.d.ts +0 -6
- package/dist/fluidErrorBase.d.ts.map +1 -1
- package/dist/fluidErrorBase.js +1 -12
- package/dist/fluidErrorBase.js.map +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- 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/mathTools.d.ts +13 -0
- package/dist/mathTools.d.ts.map +1 -0
- package/dist/mathTools.js +20 -0
- package/dist/mathTools.js.map +1 -0
- package/dist/mockLogger.d.ts +99 -19
- package/dist/mockLogger.d.ts.map +1 -1
- package/dist/mockLogger.js +120 -33
- 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 +11 -3
- package/dist/sampledTelemetryHelper.js.map +1 -1
- package/dist/telemetryEventBatcher.d.ts +64 -0
- package/dist/telemetryEventBatcher.d.ts.map +1 -0
- package/dist/telemetryEventBatcher.js +86 -0
- package/dist/telemetryEventBatcher.js.map +1 -0
- 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 +14 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +30 -2
- 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 +2 -6
- package/lib/errorLogging.d.ts.map +1 -1
- package/lib/errorLogging.js +23 -30
- 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/events.js.map +1 -1
- package/lib/fluidErrorBase.d.ts +0 -6
- package/lib/fluidErrorBase.d.ts.map +1 -1
- package/lib/fluidErrorBase.js +0 -10
- package/lib/fluidErrorBase.js.map +1 -1
- package/lib/index.d.ts +8 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -3
- 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/mathTools.d.ts +13 -0
- package/lib/mathTools.d.ts.map +1 -0
- package/lib/mathTools.js +16 -0
- package/lib/mathTools.js.map +1 -0
- package/lib/mockLogger.d.ts +99 -19
- package/lib/mockLogger.d.ts.map +1 -1
- package/lib/mockLogger.js +118 -32
- 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 +11 -3
- package/lib/sampledTelemetryHelper.js.map +1 -1
- package/lib/telemetryEventBatcher.d.ts +64 -0
- package/lib/telemetryEventBatcher.d.ts.map +1 -0
- package/lib/telemetryEventBatcher.js +82 -0
- package/lib/telemetryEventBatcher.js.map +1 -0
- 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/tsdoc-metadata.json +1 -1
- package/lib/utils.d.ts +14 -2
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +28 -1
- package/lib/utils.js.map +1 -1
- package/package.json +30 -16
- package/src/config.ts +16 -16
- package/src/error.ts +13 -13
- package/src/errorLogging.ts +32 -46
- package/src/eventEmitterWithErrorHandling.ts +3 -3
- package/src/fluidErrorBase.ts +0 -15
- package/src/index.ts +17 -16
- package/src/logger.ts +21 -21
- package/src/mathTools.ts +16 -0
- package/src/mockLogger.ts +165 -35
- package/src/sampledTelemetryHelper.ts +15 -5
- package/src/telemetryEventBatcher.ts +103 -0
- package/src/telemetryTypes.ts +38 -19
- package/src/thresholdCounter.ts +1 -1
- package/src/utils.ts +31 -2
- package/tsconfig.json +2 -0
- package/tsdoc.json +4 -0
- package/api-report/telemetry-utils.api.md +0 -425
package/lib/mockLogger.js
CHANGED
|
@@ -2,27 +2,54 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
+
import { LogLevel, } from "@fluidframework/core-interfaces";
|
|
5
6
|
import { assert } from "@fluidframework/core-utils/internal";
|
|
6
7
|
import { createChildLogger } from "./logger.js";
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
9
|
+
* Mock {@link @fluidframework/core-interfaces#ITelemetryBaseLogger} implementation.
|
|
10
|
+
*
|
|
11
|
+
* Records events sent to it, and then can walk back over those events, searching for a set of expected events to
|
|
12
|
+
* match against the logged events.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated
|
|
15
|
+
*
|
|
16
|
+
* This class is not intended for use outside of the `fluid-framework` repo, and will be removed from
|
|
17
|
+
* package exports in the near future.
|
|
18
|
+
*
|
|
19
|
+
* Please migrate usages by either creating your own mock {@link @fluidframework/core-interfaces#ITelemetryBaseLogger}
|
|
20
|
+
* implementation, or by copying this code as-is into your own repo.
|
|
21
|
+
*
|
|
22
|
+
* @privateRemarks TODO: When we are ready, this type should be made `internal`, and the deprecation notice should be removed.
|
|
10
23
|
*
|
|
11
24
|
* @alpha
|
|
12
25
|
*/
|
|
13
26
|
export class MockLogger {
|
|
27
|
+
/**
|
|
28
|
+
* Gets an immutable copy of the events logged thus far.
|
|
29
|
+
*/
|
|
30
|
+
get events() {
|
|
31
|
+
return [...this._events];
|
|
32
|
+
}
|
|
14
33
|
constructor(minLogLevel) {
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
34
|
+
this._events = [];
|
|
35
|
+
this.minLogLevel = minLogLevel ?? LogLevel.default;
|
|
17
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Clears the events logged thus far.
|
|
39
|
+
*/
|
|
18
40
|
clear() {
|
|
19
|
-
this.
|
|
41
|
+
this._events = [];
|
|
20
42
|
}
|
|
21
43
|
toTelemetryLogger() {
|
|
22
44
|
return createChildLogger({ logger: this });
|
|
23
45
|
}
|
|
24
|
-
|
|
25
|
-
|
|
46
|
+
/**
|
|
47
|
+
* {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseLogger.send}
|
|
48
|
+
*/
|
|
49
|
+
send(event, logLevel) {
|
|
50
|
+
if (logLevel ?? LogLevel.default >= this.minLogLevel) {
|
|
51
|
+
this._events.push(event);
|
|
52
|
+
}
|
|
26
53
|
}
|
|
27
54
|
/**
|
|
28
55
|
* Search events logged since the last time matchEvents was called, looking for the given expected
|
|
@@ -31,19 +58,30 @@ export class MockLogger {
|
|
|
31
58
|
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
32
59
|
* These event objects may be subsets of the logged events.
|
|
33
60
|
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
61
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
62
|
+
* Default: true.
|
|
34
63
|
*/
|
|
35
|
-
matchEvents(expectedEvents, inlineDetailsProp = false) {
|
|
36
|
-
const matchedExpectedEventCount = this.getMatchedEventsCount(expectedEvents, inlineDetailsProp);
|
|
64
|
+
matchEvents(expectedEvents, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
65
|
+
const matchedExpectedEventCount = this.getMatchedEventsCount(expectedEvents, inlineDetailsProp, clearEventsAfterCheck);
|
|
37
66
|
// How many expected events were left over? Hopefully none.
|
|
38
67
|
const unmatchedExpectedEventCount = expectedEvents.length - matchedExpectedEventCount;
|
|
39
68
|
return unmatchedExpectedEventCount === 0;
|
|
40
69
|
}
|
|
41
70
|
/**
|
|
42
|
-
* Asserts
|
|
71
|
+
* Asserts {@link MockLogger.matchEvents} is `true` for the given events.
|
|
72
|
+
* @param expectedEvents - The events expected to appear.
|
|
73
|
+
* @param message - Optional error message to include in the thrown error, if the condition is not satisfied.
|
|
74
|
+
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
75
|
+
* These event objects may be subsets of the logged events.
|
|
76
|
+
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
77
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
78
|
+
* Default: true.
|
|
79
|
+
* @throws An error containing the actual/expected event data if the condition is not satisfied.
|
|
43
80
|
*/
|
|
44
|
-
assertMatch(expectedEvents, message, inlineDetailsProp = false) {
|
|
81
|
+
assertMatch(expectedEvents, message, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
82
|
+
// Use copy to ensure events aren't cleared out from under us before we (potentially) throw
|
|
45
83
|
const actualEvents = this.events;
|
|
46
|
-
if (!this.matchEvents(expectedEvents, inlineDetailsProp)) {
|
|
84
|
+
if (!this.matchEvents(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {
|
|
47
85
|
throw new Error(`${message ?? "Logs don't match"}
|
|
48
86
|
expected:
|
|
49
87
|
${JSON.stringify(expectedEvents)}
|
|
@@ -61,16 +99,25 @@ ${JSON.stringify(actualEvents)}`);
|
|
|
61
99
|
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
62
100
|
* @returns if any of the expected events is found.
|
|
63
101
|
*/
|
|
64
|
-
matchAnyEvent(expectedEvents, inlineDetailsProp = false) {
|
|
65
|
-
const matchedExpectedEventCount = this.getMatchedEventsCount(expectedEvents, inlineDetailsProp);
|
|
102
|
+
matchAnyEvent(expectedEvents, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
103
|
+
const matchedExpectedEventCount = this.getMatchedEventsCount(expectedEvents, inlineDetailsProp, clearEventsAfterCheck);
|
|
66
104
|
return matchedExpectedEventCount > 0;
|
|
67
105
|
}
|
|
68
106
|
/**
|
|
69
|
-
* Asserts
|
|
107
|
+
* Asserts {@link MockLogger.matchAnyEvent} is `true` for the given events.
|
|
108
|
+
* @param expectedEvents - The events expected to appear.
|
|
109
|
+
* @param message - Optional error message to include in the thrown error, if the condition is not satisfied.
|
|
110
|
+
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
111
|
+
* These event objects may be subsets of the logged events.
|
|
112
|
+
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
113
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
114
|
+
* Default: true.
|
|
115
|
+
* @throws An error containing the actual/expected event data if the condition is not satisfied.
|
|
70
116
|
*/
|
|
71
|
-
assertMatchAny(expectedEvents, message, inlineDetailsProp = false) {
|
|
117
|
+
assertMatchAny(expectedEvents, message, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
118
|
+
// Use copy to ensure events aren't cleared out from under us before we (potentially) throw
|
|
72
119
|
const actualEvents = this.events;
|
|
73
|
-
if (!this.matchAnyEvent(expectedEvents, inlineDetailsProp)) {
|
|
120
|
+
if (!this.matchAnyEvent(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {
|
|
74
121
|
throw new Error(`${message ?? "Logs don't match"}
|
|
75
122
|
expected:
|
|
76
123
|
${JSON.stringify(expectedEvents)}
|
|
@@ -86,17 +133,34 @@ ${JSON.stringify(actualEvents)}`);
|
|
|
86
133
|
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
87
134
|
* These event objects may be subsets of the logged events.
|
|
88
135
|
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
136
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
137
|
+
* Default: true.
|
|
89
138
|
*/
|
|
90
|
-
matchEventStrict(expectedEvents, inlineDetailsProp = false) {
|
|
91
|
-
|
|
92
|
-
|
|
139
|
+
matchEventStrict(expectedEvents, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
140
|
+
if (expectedEvents.length !== this._events.length) {
|
|
141
|
+
if (clearEventsAfterCheck) {
|
|
142
|
+
this.clear();
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
// `events` will be cleared by the below check if requested.
|
|
147
|
+
return this.matchEvents(expectedEvents, inlineDetailsProp, clearEventsAfterCheck);
|
|
93
148
|
}
|
|
94
149
|
/**
|
|
95
|
-
* Asserts
|
|
150
|
+
* Asserts {@link MockLogger.matchEvents} is `true` for the given events.
|
|
151
|
+
* @param expectedEvents - The events expected to appear.
|
|
152
|
+
* @param message - Optional error message to include in the thrown error, if the condition is not satisfied.
|
|
153
|
+
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
154
|
+
* These event objects may be subsets of the logged events.
|
|
155
|
+
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
156
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
157
|
+
* Default: true.
|
|
158
|
+
* @throws An error containing the actual/expected event data if the condition is not satisfied.
|
|
96
159
|
*/
|
|
97
|
-
assertMatchStrict(expectedEvents, message, inlineDetailsProp = false) {
|
|
160
|
+
assertMatchStrict(expectedEvents, message, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
161
|
+
// Use copy to ensure events aren't cleared out from under us before we (potentially) throw
|
|
98
162
|
const actualEvents = this.events;
|
|
99
|
-
if (!this.matchEventStrict(expectedEvents, inlineDetailsProp)) {
|
|
163
|
+
if (!this.matchEventStrict(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {
|
|
100
164
|
throw new Error(`${message ?? "Logs don't match"}
|
|
101
165
|
expected:
|
|
102
166
|
${JSON.stringify(expectedEvents)}
|
|
@@ -106,11 +170,20 @@ ${JSON.stringify(actualEvents)}`);
|
|
|
106
170
|
}
|
|
107
171
|
}
|
|
108
172
|
/**
|
|
109
|
-
* Asserts
|
|
173
|
+
* Asserts {@link MockLogger.matchAnyEvent} is `false` for the given events.
|
|
174
|
+
* @param disallowedEvents - The events expected to not appear.
|
|
175
|
+
* @param message - Optional error message to include in the thrown error, if the condition is not satisfied.
|
|
176
|
+
* @param inlineDetailsProp - true if the "details" property in the actual event should be extracted and inlined.
|
|
177
|
+
* These event objects may be subsets of the logged events.
|
|
178
|
+
* Note: category is omitted from the type because it's usually uninteresting and tedious to type.
|
|
179
|
+
* @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.
|
|
180
|
+
* Default: true.
|
|
181
|
+
* @throws An error containing the actual/expected event data if the condition is not satisfied.
|
|
110
182
|
*/
|
|
111
|
-
assertMatchNone(disallowedEvents, message, inlineDetailsProp = false) {
|
|
183
|
+
assertMatchNone(disallowedEvents, message, inlineDetailsProp = false, clearEventsAfterCheck = true) {
|
|
184
|
+
// Use copy to ensure events aren't cleared out from under us before we (potentially) throw
|
|
112
185
|
const actualEvents = this.events;
|
|
113
|
-
if (this.matchAnyEvent(disallowedEvents, inlineDetailsProp)) {
|
|
186
|
+
if (this.matchAnyEvent(disallowedEvents, inlineDetailsProp, clearEventsAfterCheck)) {
|
|
114
187
|
throw new Error(`${message ?? "Logs don't match"}
|
|
115
188
|
disallowed events:
|
|
116
189
|
${JSON.stringify(disallowedEvents)}
|
|
@@ -119,9 +192,9 @@ actual:
|
|
|
119
192
|
${JSON.stringify(actualEvents)}`);
|
|
120
193
|
}
|
|
121
194
|
}
|
|
122
|
-
getMatchedEventsCount(expectedEvents, inlineDetailsProp) {
|
|
195
|
+
getMatchedEventsCount(expectedEvents, inlineDetailsProp, clearEventsAfterCheck) {
|
|
123
196
|
let iExpectedEvent = 0;
|
|
124
|
-
for (const event of this.
|
|
197
|
+
for (const event of this._events) {
|
|
125
198
|
if (iExpectedEvent < expectedEvents.length &&
|
|
126
199
|
MockLogger.eventsMatch(event, expectedEvents[iExpectedEvent], inlineDetailsProp)) {
|
|
127
200
|
// We found the next expected event; increment
|
|
@@ -129,7 +202,9 @@ ${JSON.stringify(actualEvents)}`);
|
|
|
129
202
|
}
|
|
130
203
|
}
|
|
131
204
|
// Remove the events so far; next call will just compare subsequent events from here
|
|
132
|
-
|
|
205
|
+
if (clearEventsAfterCheck) {
|
|
206
|
+
this.clear();
|
|
207
|
+
}
|
|
133
208
|
// Return the count of matched events.
|
|
134
209
|
return iExpectedEvent;
|
|
135
210
|
}
|
|
@@ -142,9 +217,7 @@ ${JSON.stringify(actualEvents)}`);
|
|
|
142
217
|
// Some of the properties in the expected event may be inside "details". So, if inlineDetailsProp is true,
|
|
143
218
|
// extract the properties from "details" in the actual event and inline them in the actual event.
|
|
144
219
|
if (inlineDetailsProp && details !== undefined) {
|
|
145
|
-
assert(typeof details === "string",
|
|
146
|
-
// eslint-disable-next-line unicorn/numeric-separators-style
|
|
147
|
-
0x6c9 /* Details should a JSON stringified string if inlineDetailsProp is true */);
|
|
220
|
+
assert(typeof details === "string", 0x6c9 /* Details should a JSON stringified string if inlineDetailsProp is true */);
|
|
148
221
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
149
222
|
const detailsExpanded = JSON.parse(details);
|
|
150
223
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
@@ -172,4 +245,17 @@ function matchObjects(actual, expected) {
|
|
|
172
245
|
}
|
|
173
246
|
return true;
|
|
174
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Creates an {@link IMockLoggerExt}.
|
|
250
|
+
*
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
253
|
+
export function createMockLoggerExt(minLogLevel) {
|
|
254
|
+
const mockLogger = new MockLogger(minLogLevel);
|
|
255
|
+
const childLogger = createChildLogger({ logger: mockLogger });
|
|
256
|
+
Object.assign(childLogger, {
|
|
257
|
+
events: () => mockLogger.events.map((e) => e),
|
|
258
|
+
});
|
|
259
|
+
return childLogger;
|
|
260
|
+
}
|
|
175
261
|
//# sourceMappingURL=mockLogger.js.map
|
package/lib/mockLogger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mockLogger.js","sourceRoot":"","sources":["../src/mockLogger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,O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,QAAQ,CAAC,CAAC;IACvC,CAAC;CACD;AAED,SAAS,YAAY,CAAC,MAA+B,EAAE,QAAiC;IACvF,KAAK,MAAM,CAAC,WAAW,EAAE,aAAa,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QACpE,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;QACxC,IACC,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC;YAC7B,aAAa,KAAK,IAAI;YACtB,OAAO,aAAa,KAAK,QAAQ,EAChC;YACD,IACC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC;gBAC1B,WAAW,KAAK,IAAI;gBACpB,OAAO,WAAW,KAAK,QAAQ;gBAC/B,CAAC,YAAY,CACZ,WAAsC,EACtC,aAAwC,CACxC,EACA;gBACD,OAAO,KAAK,CAAC;aACb;SACD;aAAM,IAAI,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE;YACzE,OAAO,KAAK,CAAC;SACb;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n\tITelemetryBaseEvent,\n\tITelemetryBaseLogger,\n\tLogLevel,\n} from \"@fluidframework/core-interfaces\";\nimport { assert } from \"@fluidframework/core-utils/internal\";\n\nimport { createChildLogger } from \"./logger.js\";\nimport { ITelemetryLoggerExt, ITelemetryPropertiesExt } from \"./telemetryTypes.js\";\n\n/**\n * The MockLogger records events sent to it, and then can walk back over those events\n * searching for a set of expected events to match against the logged events.\n *\n * @alpha\n */\nexport class MockLogger implements ITelemetryBaseLogger {\n\tevents: ITelemetryBaseEvent[] = [];\n\n\tconstructor(public readonly minLogLevel?: LogLevel) {}\n\n\tclear(): void {\n\t\tthis.events = [];\n\t}\n\n\ttoTelemetryLogger(): ITelemetryLoggerExt {\n\t\treturn createChildLogger({ logger: this });\n\t}\n\n\tsend(event: ITelemetryBaseEvent): void {\n\t\tthis.events.push(event);\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking for the given expected\n\t * events in order.\n\t * @param expectedEvents - events in order that are expected to appear in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t */\n\tmatchEvents(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t): boolean {\n\t\tconst matchedExpectedEventCount = this.getMatchedEventsCount(\n\t\t\texpectedEvents,\n\t\t\tinlineDetailsProp,\n\t\t);\n\t\t// How many expected events were left over? Hopefully none.\n\t\tconst unmatchedExpectedEventCount = expectedEvents.length - matchedExpectedEventCount;\n\t\treturn unmatchedExpectedEventCount === 0;\n\t}\n\n\t/**\n\t * Asserts that matchEvents is true, and prints the actual/expected output if not.\n\t */\n\tassertMatch(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t): void {\n\t\tconst actualEvents = this.events;\n\t\tif (!this.matchEvents(expectedEvents, inlineDetailsProp)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking for any of the given\n\t * expected events.\n\t * @param expectedEvents - events that are expected to appear in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @returns if any of the expected events is found.\n\t */\n\tmatchAnyEvent(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t): boolean {\n\t\tconst matchedExpectedEventCount = this.getMatchedEventsCount(\n\t\t\texpectedEvents,\n\t\t\tinlineDetailsProp,\n\t\t);\n\t\treturn matchedExpectedEventCount > 0;\n\t}\n\n\t/**\n\t * Asserts that matchAnyEvent is true, and prints the actual/expected output if not.\n\t */\n\tassertMatchAny(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t): void {\n\t\tconst actualEvents = this.events;\n\t\tif (!this.matchAnyEvent(expectedEvents, inlineDetailsProp)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking only for the given expected\n\t * events in order.\n\t * @param expectedEvents - events in order that are expected to be the only events in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t */\n\tmatchEventStrict(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t): boolean {\n\t\treturn (\n\t\t\texpectedEvents.length === this.events.length &&\n\t\t\tthis.matchEvents(expectedEvents, inlineDetailsProp)\n\t\t);\n\t}\n\n\t/**\n\t * Asserts that matchEvents is true, and prints the actual/expected output if not\n\t */\n\tassertMatchStrict(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t): void {\n\t\tconst actualEvents = this.events;\n\t\tif (!this.matchEventStrict(expectedEvents, inlineDetailsProp)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Asserts that matchAnyEvent is false for the given events, and prints the actual/expected output if not\n\t */\n\tassertMatchNone(\n\t\tdisallowedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t): void {\n\t\tconst actualEvents = this.events;\n\t\tif (this.matchAnyEvent(disallowedEvents, inlineDetailsProp)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\ndisallowed events:\n${JSON.stringify(disallowedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\tprivate getMatchedEventsCount(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean,\n\t): number {\n\t\tlet iExpectedEvent = 0;\n\t\tfor (const event of this.events) {\n\t\t\tif (\n\t\t\t\tiExpectedEvent < expectedEvents.length &&\n\t\t\t\tMockLogger.eventsMatch(event, expectedEvents[iExpectedEvent], inlineDetailsProp)\n\t\t\t) {\n\t\t\t\t// We found the next expected event; increment\n\t\t\t\t++iExpectedEvent;\n\t\t\t}\n\t\t}\n\n\t\t// Remove the events so far; next call will just compare subsequent events from here\n\t\tthis.events = [];\n\n\t\t// Return the count of matched events.\n\t\treturn iExpectedEvent;\n\t}\n\n\t/**\n\t * Ensure the expected event is a strict subset of the actual event\n\t */\n\tprivate static eventsMatch(\n\t\tactual: ITelemetryBaseEvent,\n\t\texpected: Omit<ITelemetryBaseEvent, \"category\">,\n\t\tinlineDetailsProp: boolean,\n\t): boolean {\n\t\tconst { details, ...actualForMatching } = actual;\n\t\t// \"details\" is used in a lot of telemetry logs to group a bunch of properties together and stringify them.\n\t\t// Some of the properties in the expected event may be inside \"details\". So, if inlineDetailsProp is true,\n\t\t// extract the properties from \"details\" in the actual event and inline them in the actual event.\n\t\tif (inlineDetailsProp && details !== undefined) {\n\t\t\tassert(\n\t\t\t\ttypeof details === \"string\",\n\t\t\t\t// eslint-disable-next-line unicorn/numeric-separators-style\n\t\t\t\t0x6c9 /* Details should a JSON stringified string if inlineDetailsProp is true */,\n\t\t\t);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\tconst detailsExpanded = JSON.parse(details);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\treturn matchObjects({ ...actualForMatching, ...detailsExpanded }, expected);\n\t\t}\n\t\treturn matchObjects(actual, expected);\n\t}\n}\n\nfunction matchObjects(actual: ITelemetryPropertiesExt, expected: ITelemetryPropertiesExt): boolean {\n\tfor (const [expectedKey, expectedValue] of Object.entries(expected)) {\n\t\tconst actualValue = actual[expectedKey];\n\t\tif (\n\t\t\t!Array.isArray(expectedValue) &&\n\t\t\texpectedValue !== null &&\n\t\t\ttypeof expectedValue === \"object\"\n\t\t) {\n\t\t\tif (\n\t\t\t\tArray.isArray(actualValue) ||\n\t\t\t\tactualValue === null ||\n\t\t\t\ttypeof actualValue !== \"object\" ||\n\t\t\t\t!matchObjects(\n\t\t\t\t\tactualValue as ITelemetryPropertiesExt,\n\t\t\t\t\texpectedValue as ITelemetryPropertiesExt,\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t} else if (JSON.stringify(actualValue) !== JSON.stringify(expectedValue)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"mockLogger.js","sourceRoot":"","sources":["../src/mockLogger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAGN,QAAQ,GACR,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAOhD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,UAAU;IACtB;;OAEG;IACH,IAAW,MAAM;QAChB,OAAO,CAAC,GAAG,IAAI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AC;QACF,CAAC;aAAM,IAAI,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,CAAC;YAC1E,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAgBD;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,WAAsB;IACzD,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IAC/C,MAAM,WAAW,GAAG,iBAAiB,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAC9D,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE;QAC1B,MAAM,EAAE,GAAkC,EAAE,CAC3C,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAuB,CAAC;KACtD,CAAC,CAAC;IACH,OAAO,WAA6B,CAAC;AACtC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n\ttype ITelemetryBaseEvent,\n\ttype ITelemetryBaseLogger,\n\tLogLevel,\n} from \"@fluidframework/core-interfaces\";\nimport { assert } from \"@fluidframework/core-utils/internal\";\n\nimport { createChildLogger } from \"./logger.js\";\nimport type {\n\tITelemetryEventExt,\n\tITelemetryLoggerExt,\n\tITelemetryPropertiesExt,\n} from \"./telemetryTypes.js\";\n\n/**\n * Mock {@link @fluidframework/core-interfaces#ITelemetryBaseLogger} implementation.\n *\n * Records events sent to it, and then can walk back over those events, searching for a set of expected events to\n * match against the logged events.\n *\n * @deprecated\n *\n * This class is not intended for use outside of the `fluid-framework` repo, and will be removed from\n * package exports in the near future.\n *\n * Please migrate usages by either creating your own mock {@link @fluidframework/core-interfaces#ITelemetryBaseLogger}\n * implementation, or by copying this code as-is into your own repo.\n *\n * @privateRemarks TODO: When we are ready, this type should be made `internal`, and the deprecation notice should be removed.\n *\n * @alpha\n */\nexport class MockLogger implements ITelemetryBaseLogger {\n\t/**\n\t * Gets an immutable copy of the events logged thus far.\n\t */\n\tpublic get events(): readonly ITelemetryBaseEvent[] {\n\t\treturn [...this._events];\n\t}\n\n\tprivate _events: ITelemetryBaseEvent[] = [];\n\n\t/**\n\t * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseLogger.minLogLevel}\n\t */\n\tpublic readonly minLogLevel: LogLevel;\n\n\tpublic constructor(minLogLevel?: LogLevel) {\n\t\tthis.minLogLevel = minLogLevel ?? LogLevel.default;\n\t}\n\n\t/**\n\t * Clears the events logged thus far.\n\t */\n\tpublic clear(): void {\n\t\tthis._events = [];\n\t}\n\n\tpublic toTelemetryLogger(): ITelemetryLoggerExt {\n\t\treturn createChildLogger({ logger: this });\n\t}\n\n\t/**\n\t * {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseLogger.send}\n\t */\n\tpublic send(event: ITelemetryBaseEvent, logLevel?: LogLevel): void {\n\t\tif (logLevel ?? LogLevel.default >= this.minLogLevel) {\n\t\t\tthis._events.push(event);\n\t\t}\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking for the given expected\n\t * events in order.\n\t * @param expectedEvents - events in order that are expected to appear in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t */\n\tpublic matchEvents(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): boolean {\n\t\tconst matchedExpectedEventCount = this.getMatchedEventsCount(\n\t\t\texpectedEvents,\n\t\t\tinlineDetailsProp,\n\t\t\tclearEventsAfterCheck,\n\t\t);\n\t\t// How many expected events were left over? Hopefully none.\n\t\tconst unmatchedExpectedEventCount = expectedEvents.length - matchedExpectedEventCount;\n\t\treturn unmatchedExpectedEventCount === 0;\n\t}\n\n\t/**\n\t * Asserts {@link MockLogger.matchEvents} is `true` for the given events.\n\t * @param expectedEvents - The events expected to appear.\n\t * @param message - Optional error message to include in the thrown error, if the condition is not satisfied.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t * @throws An error containing the actual/expected event data if the condition is not satisfied.\n\t */\n\tpublic assertMatch(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): void {\n\t\t// Use copy to ensure events aren't cleared out from under us before we (potentially) throw\n\t\tconst actualEvents = this.events;\n\n\t\tif (!this.matchEvents(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking for any of the given\n\t * expected events.\n\t * @param expectedEvents - events that are expected to appear in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @returns if any of the expected events is found.\n\t */\n\tpublic matchAnyEvent(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): boolean {\n\t\tconst matchedExpectedEventCount = this.getMatchedEventsCount(\n\t\t\texpectedEvents,\n\t\t\tinlineDetailsProp,\n\t\t\tclearEventsAfterCheck,\n\t\t);\n\t\treturn matchedExpectedEventCount > 0;\n\t}\n\n\t/**\n\t * Asserts {@link MockLogger.matchAnyEvent} is `true` for the given events.\n\t * @param expectedEvents - The events expected to appear.\n\t * @param message - Optional error message to include in the thrown error, if the condition is not satisfied.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t * @throws An error containing the actual/expected event data if the condition is not satisfied.\n\t */\n\tpublic assertMatchAny(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): void {\n\t\t// Use copy to ensure events aren't cleared out from under us before we (potentially) throw\n\t\tconst actualEvents = this.events;\n\n\t\tif (!this.matchAnyEvent(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Search events logged since the last time matchEvents was called, looking only for the given expected\n\t * events in order.\n\t * @param expectedEvents - events in order that are expected to be the only events in the recorded log.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t */\n\tpublic matchEventStrict(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): boolean {\n\t\tif (expectedEvents.length !== this._events.length) {\n\t\t\tif (clearEventsAfterCheck) {\n\t\t\t\tthis.clear();\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\t// `events` will be cleared by the below check if requested.\n\t\treturn this.matchEvents(expectedEvents, inlineDetailsProp, clearEventsAfterCheck);\n\t}\n\n\t/**\n\t * Asserts {@link MockLogger.matchEvents} is `true` for the given events.\n\t * @param expectedEvents - The events expected to appear.\n\t * @param message - Optional error message to include in the thrown error, if the condition is not satisfied.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t * @throws An error containing the actual/expected event data if the condition is not satisfied.\n\t */\n\tpublic assertMatchStrict(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): void {\n\t\t// Use copy to ensure events aren't cleared out from under us before we (potentially) throw\n\t\tconst actualEvents = this.events;\n\n\t\tif (!this.matchEventStrict(expectedEvents, inlineDetailsProp, clearEventsAfterCheck)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\nexpected:\n${JSON.stringify(expectedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Asserts {@link MockLogger.matchAnyEvent} is `false` for the given events.\n\t * @param disallowedEvents - The events expected to not appear.\n\t * @param message - Optional error message to include in the thrown error, if the condition is not satisfied.\n\t * @param inlineDetailsProp - true if the \"details\" property in the actual event should be extracted and inlined.\n\t * These event objects may be subsets of the logged events.\n\t * Note: category is omitted from the type because it's usually uninteresting and tedious to type.\n\t * @param clearEventsAfterCheck - Whether or not to clear the logger's {@link MockLogger.events} after performing the check.\n\t * Default: true.\n\t * @throws An error containing the actual/expected event data if the condition is not satisfied.\n\t */\n\tpublic assertMatchNone(\n\t\tdisallowedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tmessage?: string,\n\t\tinlineDetailsProp: boolean = false,\n\t\tclearEventsAfterCheck: boolean = true,\n\t): void {\n\t\t// Use copy to ensure events aren't cleared out from under us before we (potentially) throw\n\t\tconst actualEvents = this.events;\n\n\t\tif (this.matchAnyEvent(disallowedEvents, inlineDetailsProp, clearEventsAfterCheck)) {\n\t\t\tthrow new Error(`${message ?? \"Logs don't match\"}\ndisallowed events:\n${JSON.stringify(disallowedEvents)}\n\nactual:\n${JSON.stringify(actualEvents)}`);\n\t\t}\n\t}\n\n\tprivate getMatchedEventsCount(\n\t\texpectedEvents: Omit<ITelemetryBaseEvent, \"category\">[],\n\t\tinlineDetailsProp: boolean,\n\t\tclearEventsAfterCheck: boolean,\n\t): number {\n\t\tlet iExpectedEvent = 0;\n\t\tfor (const event of this._events) {\n\t\t\tif (\n\t\t\t\tiExpectedEvent < expectedEvents.length &&\n\t\t\t\tMockLogger.eventsMatch(event, expectedEvents[iExpectedEvent], inlineDetailsProp)\n\t\t\t) {\n\t\t\t\t// We found the next expected event; increment\n\t\t\t\t++iExpectedEvent;\n\t\t\t}\n\t\t}\n\n\t\t// Remove the events so far; next call will just compare subsequent events from here\n\t\tif (clearEventsAfterCheck) {\n\t\t\tthis.clear();\n\t\t}\n\n\t\t// Return the count of matched events.\n\t\treturn iExpectedEvent;\n\t}\n\n\t/**\n\t * Ensure the expected event is a strict subset of the actual event\n\t */\n\tprivate static eventsMatch(\n\t\tactual: ITelemetryBaseEvent,\n\t\texpected: Omit<ITelemetryBaseEvent, \"category\">,\n\t\tinlineDetailsProp: boolean,\n\t): boolean {\n\t\tconst { details, ...actualForMatching } = actual;\n\t\t// \"details\" is used in a lot of telemetry logs to group a bunch of properties together and stringify them.\n\t\t// Some of the properties in the expected event may be inside \"details\". So, if inlineDetailsProp is true,\n\t\t// extract the properties from \"details\" in the actual event and inline them in the actual event.\n\t\tif (inlineDetailsProp && details !== undefined) {\n\t\t\tassert(\n\t\t\t\ttypeof details === \"string\",\n\t\t\t\t0x6c9 /* Details should a JSON stringified string if inlineDetailsProp is true */,\n\t\t\t);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\tconst detailsExpanded = JSON.parse(details);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\treturn matchObjects({ ...actualForMatching, ...detailsExpanded }, expected);\n\t\t}\n\t\treturn matchObjects(actual, expected);\n\t}\n}\n\nfunction matchObjects(\n\tactual: ITelemetryPropertiesExt,\n\texpected: ITelemetryPropertiesExt,\n): boolean {\n\tfor (const [expectedKey, expectedValue] of Object.entries(expected)) {\n\t\tconst actualValue = actual[expectedKey];\n\t\tif (\n\t\t\t!Array.isArray(expectedValue) &&\n\t\t\texpectedValue !== null &&\n\t\t\ttypeof expectedValue === \"object\"\n\t\t) {\n\t\t\tif (\n\t\t\t\tArray.isArray(actualValue) ||\n\t\t\t\tactualValue === null ||\n\t\t\t\ttypeof actualValue !== \"object\" ||\n\t\t\t\t!matchObjects(\n\t\t\t\t\tactualValue as ITelemetryPropertiesExt,\n\t\t\t\t\texpectedValue as ITelemetryPropertiesExt,\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t} else if (JSON.stringify(actualValue) !== JSON.stringify(expectedValue)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/**\n * Mock {@link ITelemetryLoggerExt} implementation.\n *\n * @remarks Can be created via {@link createMockLoggerExt}.\n *\n * @internal\n */\nexport interface IMockLoggerExt extends ITelemetryLoggerExt {\n\t/**\n\t * Gets the events that have been logged so far.\n\t */\n\tevents(): readonly ITelemetryEventExt[];\n}\n\n/**\n * Creates an {@link IMockLoggerExt}.\n *\n * @internal\n */\nexport function createMockLoggerExt(minLogLevel?: LogLevel): IMockLoggerExt {\n\tconst mockLogger = new MockLogger(minLogLevel);\n\tconst childLogger = createChildLogger({ logger: mockLogger });\n\tObject.assign(childLogger, {\n\t\tevents: (): readonly ITelemetryEventExt[] =>\n\t\t\tmockLogger.events.map((e) => e as ITelemetryEventExt),\n\t});\n\treturn childLogger as IMockLoggerExt;\n}\n"]}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
import type { IDisposable, ITelemetryBaseProperties } from "@fluidframework/core-interfaces";
|
|
6
|
-
import {
|
|
6
|
+
import type { ITelemetryGenericEventExt, ITelemetryLoggerExt } from "./telemetryTypes.js";
|
|
7
7
|
/**
|
|
8
8
|
* Helper class that executes a specified code block and writes an
|
|
9
9
|
* {@link @fluidframework/core-interfaces#ITelemetryPerformanceEvent} to a specified logger every time a specified
|
|
@@ -21,7 +21,11 @@ export declare class SampledTelemetryHelper implements IDisposable {
|
|
|
21
21
|
private readonly sampleThreshold;
|
|
22
22
|
private readonly includeAggregateMetrics;
|
|
23
23
|
private readonly perBucketProperties;
|
|
24
|
-
|
|
24
|
+
private _disposed;
|
|
25
|
+
/**
|
|
26
|
+
* {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed}
|
|
27
|
+
*/
|
|
28
|
+
get disposed(): boolean;
|
|
25
29
|
private readonly measurementsMap;
|
|
26
30
|
/**
|
|
27
31
|
* @param eventBase -
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sampledTelemetryHelper.d.ts","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAE7F,OAAO,
|
|
1
|
+
{"version":3,"file":"sampledTelemetryHelper.d.ts","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAE7F,OAAO,KAAK,EACX,yBAAyB,EACzB,mBAAmB,EAEnB,MAAM,qBAAqB,CAAC;AAoC7B;;;;;;;;;;GAUG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IA+BxD,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAChC,OAAO,CAAC,QAAQ,CAAC,uBAAuB;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB;IAlCrC,OAAO,CAAC,SAAS,CAAkB;IAEnC;;OAEG;IACH,IAAW,QAAQ,IAAI,OAAO,CAE7B;IAED,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAmC;IAEnE;;;;;;;;;;;;;;;;;OAiBG;gBAEe,SAAS,EAAE,yBAAyB,EACpC,MAAM,EAAE,mBAAmB,EAC3B,eAAe,EAAE,MAAM,EACvB,uBAAuB,GAAE,OAAe,EACxC,mBAAmB,wCAA8C;IAGnF;;;;;;;;;OASG;IACI,OAAO,CAAC,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,EAAE,MAAM,GAAE,MAAW,GAAG,CAAC;IA0BjE,OAAO,CAAC,WAAW;IAoBZ,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,GAAG,IAAI;CAM/C"}
|
|
@@ -15,6 +15,12 @@ import { performance } from "@fluid-internal/client-utils";
|
|
|
15
15
|
* @internal
|
|
16
16
|
*/
|
|
17
17
|
export class SampledTelemetryHelper {
|
|
18
|
+
/**
|
|
19
|
+
* {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed}
|
|
20
|
+
*/
|
|
21
|
+
get disposed() {
|
|
22
|
+
return this._disposed;
|
|
23
|
+
}
|
|
18
24
|
/**
|
|
19
25
|
* @param eventBase -
|
|
20
26
|
* Custom properties to include in the telemetry performance event when it is written.
|
|
@@ -39,7 +45,7 @@ export class SampledTelemetryHelper {
|
|
|
39
45
|
this.sampleThreshold = sampleThreshold;
|
|
40
46
|
this.includeAggregateMetrics = includeAggregateMetrics;
|
|
41
47
|
this.perBucketProperties = perBucketProperties;
|
|
42
|
-
this.
|
|
48
|
+
this._disposed = false;
|
|
43
49
|
this.measurementsMap = new Map();
|
|
44
50
|
}
|
|
45
51
|
/**
|
|
@@ -82,7 +88,7 @@ export class SampledTelemetryHelper {
|
|
|
82
88
|
const bucketProperties = this.perBucketProperties.get(bucket);
|
|
83
89
|
const telemetryEvent = {
|
|
84
90
|
...this.eventBase,
|
|
85
|
-
...bucketProperties,
|
|
91
|
+
...bucketProperties, // If the bucket doesn't exist and this is undefined, things work as expected
|
|
86
92
|
...measurements,
|
|
87
93
|
};
|
|
88
94
|
this.logger.sendPerformanceEvent(telemetryEvent);
|
|
@@ -90,8 +96,10 @@ export class SampledTelemetryHelper {
|
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
dispose(error) {
|
|
93
|
-
for (const [k] of this.measurementsMap.entries())
|
|
99
|
+
for (const [k] of this.measurementsMap.entries()) {
|
|
94
100
|
this.flushBucket(k);
|
|
101
|
+
}
|
|
102
|
+
this._disposed = true;
|
|
95
103
|
}
|
|
96
104
|
}
|
|
97
105
|
//# sourceMappingURL=sampledTelemetryHelper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sampledTelemetryHelper.js","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AA2C3D;;;;;;;;;;GAUG;AACH,MAAM,OAAO,sBAAsB;
|
|
1
|
+
{"version":3,"file":"sampledTelemetryHelper.js","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AA2C3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Content":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { performance } from \"@fluid-internal/client-utils\";\nimport type { IDisposable, ITelemetryBaseProperties } from \"@fluidframework/core-interfaces\";\n\nimport type {\n\tITelemetryGenericEventExt,\n\tITelemetryLoggerExt,\n\tITelemetryPerformanceEventExt,\n} from \"./telemetryTypes.js\";\n\n/**\n * @privateRemarks\n *\n * The names of the properties in this interface are the ones that will get stamped in the\n * telemetry event, changes should be considered carefully. The optional properties should\n * only be populated if 'includeAggregateMetrics' is true.\n */\ninterface Measurements {\n\t/**\n\t * The duration of the latest execution.\n\t */\n\tduration: number;\n\n\t/**\n\t * The number of executions since the last time an event was generated.\n\t */\n\tcount: number;\n\n\t/**\n\t * Total duration across all the executions since the last event was generated.\n\t */\n\ttotalDuration?: number;\n\n\t/**\n\t * Min duration across all the executions since the last event was generated.\n\t */\n\tminDuration?: number;\n\n\t/**\n\t * Max duration across all the executions since the last event was generated.\n\t */\n\tmaxDuration?: number;\n}\n\n/**\n * Helper class that executes a specified code block and writes an\n * {@link @fluidframework/core-interfaces#ITelemetryPerformanceEvent} to a specified logger every time a specified\n * number of executions is reached (or when the class is disposed).\n *\n * The `duration` field in the telemetry event is the duration of the latest execution (sample) of the specified\n * function. See the documentation of the `includeAggregateMetrics` parameter for additional details that can be\n * included.\n *\n * @internal\n */\nexport class SampledTelemetryHelper implements IDisposable {\n\tprivate _disposed: boolean = false;\n\n\t/**\n\t * {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed}\n\t */\n\tpublic get disposed(): boolean {\n\t\treturn this._disposed;\n\t}\n\n\tprivate readonly measurementsMap = new Map<string, Measurements>();\n\n\t/**\n\t * @param eventBase -\n\t * Custom properties to include in the telemetry performance event when it is written.\n\t * @param logger -\n\t * The logger to use to write the telemetry performance event.\n\t * @param sampleThreshold -\n\t * Telemetry performance events will be generated every time we hit this many executions of the code block.\n\t * @param includeAggregateMetrics -\n\t * If set to `true`, the telemetry performance event will include aggregated metrics (total duration, min duration,\n\t * max duration) for all the executions in between generated events.\n\t * @param perBucketProperties -\n\t * Map of strings that represent different buckets (which can be specified when calling the 'measure' method), to\n\t * properties which should be added to the telemetry event for that bucket. If a bucket being measured does not\n\t * have an entry in this map, no additional properties will be added to its telemetry events. The following keys are\n\t * reserved for use by this class: \"duration\", \"count\", \"totalDuration\", \"minDuration\", \"maxDuration\". If any of\n\t * them is specified as a key in one of the ITelemetryBaseProperties objects in this map, that key-value pair will be\n\t * ignored.\n\t */\n\tpublic constructor(\n\t\tprivate readonly eventBase: ITelemetryGenericEventExt,\n\t\tprivate readonly logger: ITelemetryLoggerExt,\n\t\tprivate readonly sampleThreshold: number,\n\t\tprivate readonly includeAggregateMetrics: boolean = false,\n\t\tprivate readonly perBucketProperties = new Map<string, ITelemetryBaseProperties>(),\n\t) {}\n\n\t/**\n\t * Executes the specified code and keeps track of execution time statistics.\n\t * If it's been called enough times (the sampleThreshold for the class) then it generates a log message with the necessary information.\n\t *\n\t * @param codeToMeasure - The code to be executed and measured.\n\t * @param bucket - A key to track executions of the code block separately.\n\t * Each different value of this parameter has a separate set of executions and metrics tracked by the class.\n\t * If no such distinction needs to be made, do not provide a value.\n\t * @returns Whatever the passed-in code block returns.\n\t */\n\tpublic measure<T>(codeToMeasure: () => T, bucket: string = \"\"): T {\n\t\tconst start = performance.now();\n\t\tconst returnValue = codeToMeasure();\n\t\tconst duration = performance.now() - start;\n\n\t\tlet m = this.measurementsMap.get(bucket);\n\t\tif (m === undefined) {\n\t\t\tm = { count: 0, duration: -1 };\n\t\t\tthis.measurementsMap.set(bucket, m);\n\t\t}\n\t\tm.count++;\n\t\tm.duration = duration;\n\n\t\tif (this.includeAggregateMetrics) {\n\t\t\tm.totalDuration = (m.totalDuration ?? 0) + duration;\n\t\t\tm.minDuration = Math.min(m.minDuration ?? duration, duration);\n\t\t\tm.maxDuration = Math.max(m.maxDuration ?? 0, duration);\n\t\t}\n\n\t\tif (m.count >= this.sampleThreshold) {\n\t\t\tthis.flushBucket(bucket);\n\t\t}\n\n\t\treturn returnValue;\n\t}\n\n\tprivate flushBucket(bucket: string): void {\n\t\tconst measurements = this.measurementsMap.get(bucket);\n\t\tif (measurements === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (measurements.count !== 0) {\n\t\t\tconst bucketProperties = this.perBucketProperties.get(bucket);\n\n\t\t\tconst telemetryEvent: ITelemetryPerformanceEventExt = {\n\t\t\t\t...this.eventBase,\n\t\t\t\t...bucketProperties, // If the bucket doesn't exist and this is undefined, things work as expected\n\t\t\t\t...measurements,\n\t\t\t};\n\n\t\t\tthis.logger.sendPerformanceEvent(telemetryEvent);\n\t\t\tthis.measurementsMap.delete(bucket);\n\t\t}\n\t}\n\n\tpublic dispose(error?: Error | undefined): void {\n\t\tfor (const [k] of this.measurementsMap.entries()) {\n\t\t\tthis.flushBucket(k);\n\t\t}\n\t\tthis._disposed = true;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { ITelemetryGenericEventExt, ITelemetryLoggerExt } from "./telemetryTypes.js";
|
|
6
|
+
/**
|
|
7
|
+
* Telemetry class that accumulates measurements which are eventually logged in a telemetry event through the provided
|
|
8
|
+
* {@link TelemetryEventBatcher.logger | logger} when the number of calls to the function reaches the specified {@link TelemetryEventBatcher.threshold | threshold}.
|
|
9
|
+
*
|
|
10
|
+
* @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.
|
|
11
|
+
* @typeparam TMetrics - The set of keys that should be logged.
|
|
12
|
+
* E.g., `keyof Foo` for logging properties `bar` and `baz` from `type Foo = { bar: number, baz: number }`.
|
|
13
|
+
*
|
|
14
|
+
* @sealed
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class TelemetryEventBatcher<TMetrics extends string> {
|
|
18
|
+
/**
|
|
19
|
+
* Custom properties to include in the telemetry performance event when it is written.
|
|
20
|
+
*/
|
|
21
|
+
private readonly eventBase;
|
|
22
|
+
/**
|
|
23
|
+
* The logger to use to write the telemetry performance event.
|
|
24
|
+
*/
|
|
25
|
+
private readonly logger;
|
|
26
|
+
/**
|
|
27
|
+
* The number of logs to accumulate before sending the data to the logger.
|
|
28
|
+
*/
|
|
29
|
+
private readonly threshold;
|
|
30
|
+
/**
|
|
31
|
+
* Stores the sum of the custom data passed into the logger.
|
|
32
|
+
*/
|
|
33
|
+
private dataSums;
|
|
34
|
+
/**
|
|
35
|
+
* Stores the maximum value of the custom data passed into the logger.
|
|
36
|
+
*/
|
|
37
|
+
private dataMaxes;
|
|
38
|
+
/**
|
|
39
|
+
* Counter to keep track of the number of times the log function is called.
|
|
40
|
+
*/
|
|
41
|
+
private counter;
|
|
42
|
+
constructor(
|
|
43
|
+
/**
|
|
44
|
+
* Custom properties to include in the telemetry performance event when it is written.
|
|
45
|
+
*/
|
|
46
|
+
eventBase: ITelemetryGenericEventExt,
|
|
47
|
+
/**
|
|
48
|
+
* The logger to use to write the telemetry performance event.
|
|
49
|
+
*/
|
|
50
|
+
logger: ITelemetryLoggerExt,
|
|
51
|
+
/**
|
|
52
|
+
* The number of logs to accumulate before sending the data to the logger.
|
|
53
|
+
*/
|
|
54
|
+
threshold: number);
|
|
55
|
+
/**
|
|
56
|
+
* Accumulates the custom data and sends it to the logger every {@link TelemetryEventBatcher.threshold} calls.
|
|
57
|
+
*
|
|
58
|
+
* @param customData -
|
|
59
|
+
* A record storing the custom data to be accumulated and eventually logged.
|
|
60
|
+
*/
|
|
61
|
+
accumulateAndLog(customData: Record<TMetrics, number>): void;
|
|
62
|
+
private sendData;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=telemetryEventBatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telemetryEventBatcher.d.ts","sourceRoot":"","sources":["../src/telemetryEventBatcher.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACX,yBAAyB,EACzB,mBAAmB,EAEnB,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;;;;GAUG;AACH,qBAAa,qBAAqB,CAAC,QAAQ,SAAS,MAAM;IAiBxD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS;IAE1B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS;IA7B3B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAsC;IAEtD;;OAEG;IACH,OAAO,CAAC,SAAS,CAAsC;IAEvD;;OAEG;IACH,OAAO,CAAC,OAAO,CAAK;;IAGnB;;OAEG;IACc,SAAS,EAAE,yBAAyB;IAErD;;OAEG;IACc,MAAM,EAAE,mBAAmB;IAE5C;;OAEG;IACc,SAAS,EAAE,MAAM;IAGnC;;;;;OAKG;IACI,gBAAgB,CAAC,UAAU,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,IAAI;IAgBnE,OAAO,CAAC,QAAQ;CAwBhB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { roundToDecimalPlaces } from "./mathTools.js";
|
|
6
|
+
/**
|
|
7
|
+
* Telemetry class that accumulates measurements which are eventually logged in a telemetry event through the provided
|
|
8
|
+
* {@link TelemetryEventBatcher.logger | logger} when the number of calls to the function reaches the specified {@link TelemetryEventBatcher.threshold | threshold}.
|
|
9
|
+
*
|
|
10
|
+
* @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.
|
|
11
|
+
* @typeparam TMetrics - The set of keys that should be logged.
|
|
12
|
+
* E.g., `keyof Foo` for logging properties `bar` and `baz` from `type Foo = { bar: number, baz: number }`.
|
|
13
|
+
*
|
|
14
|
+
* @sealed
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export class TelemetryEventBatcher {
|
|
18
|
+
constructor(
|
|
19
|
+
/**
|
|
20
|
+
* Custom properties to include in the telemetry performance event when it is written.
|
|
21
|
+
*/
|
|
22
|
+
eventBase,
|
|
23
|
+
/**
|
|
24
|
+
* The logger to use to write the telemetry performance event.
|
|
25
|
+
*/
|
|
26
|
+
logger,
|
|
27
|
+
/**
|
|
28
|
+
* The number of logs to accumulate before sending the data to the logger.
|
|
29
|
+
*/
|
|
30
|
+
threshold) {
|
|
31
|
+
this.eventBase = eventBase;
|
|
32
|
+
this.logger = logger;
|
|
33
|
+
this.threshold = threshold;
|
|
34
|
+
/**
|
|
35
|
+
* Stores the sum of the custom data passed into the logger.
|
|
36
|
+
*/
|
|
37
|
+
this.dataSums = {};
|
|
38
|
+
/**
|
|
39
|
+
* Stores the maximum value of the custom data passed into the logger.
|
|
40
|
+
*/
|
|
41
|
+
this.dataMaxes = {};
|
|
42
|
+
/**
|
|
43
|
+
* Counter to keep track of the number of times the log function is called.
|
|
44
|
+
*/
|
|
45
|
+
this.counter = 0;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Accumulates the custom data and sends it to the logger every {@link TelemetryEventBatcher.threshold} calls.
|
|
49
|
+
*
|
|
50
|
+
* @param customData -
|
|
51
|
+
* A record storing the custom data to be accumulated and eventually logged.
|
|
52
|
+
*/
|
|
53
|
+
accumulateAndLog(customData) {
|
|
54
|
+
for (const key of Object.keys(customData)) {
|
|
55
|
+
this.dataSums[key] = (this.dataSums[key] ?? 0) + customData[key];
|
|
56
|
+
this.dataMaxes[key] = Math.max(this.dataMaxes[key] ?? Number.NEGATIVE_INFINITY, customData[key]);
|
|
57
|
+
}
|
|
58
|
+
this.counter++;
|
|
59
|
+
if (this.counter >= this.threshold) {
|
|
60
|
+
this.sendData();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
sendData() {
|
|
64
|
+
const telemetryEvent = {
|
|
65
|
+
...this.eventBase,
|
|
66
|
+
};
|
|
67
|
+
for (const key of Object.keys(this.dataSums)) {
|
|
68
|
+
if (this.dataSums[key] !== undefined) {
|
|
69
|
+
telemetryEvent[`avg_${key}`] = roundToDecimalPlaces(this.dataSums[key] / this.counter, 6);
|
|
70
|
+
}
|
|
71
|
+
if (this.dataMaxes[key] !== undefined) {
|
|
72
|
+
telemetryEvent[`max_${key}`] = this.dataMaxes[key];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
this.logger.sendPerformanceEvent(telemetryEvent);
|
|
76
|
+
// Reset the counter and the data.
|
|
77
|
+
this.counter = 0;
|
|
78
|
+
this.dataSums = {};
|
|
79
|
+
this.dataMaxes = {};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=telemetryEventBatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telemetryEventBatcher.js","sourceRoot":"","sources":["../src/telemetryEventBatcher.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAOtD;;;;;;;;;;GAUG;AACH,MAAM,OAAO,qBAAqB;IAgBjC;IACC;;OAEG;IACc,SAAoC;IAErD;;OAEG;IACc,MAA2B;IAE5C;;OAEG;IACc,SAAiB;QAVjB,cAAS,GAAT,SAAS,CAA2B;QAKpC,WAAM,GAAN,MAAM,CAAqB;QAK3B,cAAS,GAAT,SAAS,CAAQ;QA7BnC;;WAEG;QACK,aAAQ,GAAmC,EAAE,CAAC;QAEtD;;WAEG;QACK,cAAS,GAAmC,EAAE,CAAC;QAEvD;;WAEG;QACK,YAAO,GAAG,CAAC,CAAC;IAiBjB,CAAC;IAEJ;;;;;OAKG;IACI,gBA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,CAAC,CAAC;QAEjD,kCAAkC;QAClC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACrB,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { roundToDecimalPlaces } from \"./mathTools.js\";\nimport type {\n\tITelemetryGenericEventExt,\n\tITelemetryLoggerExt,\n\tITelemetryPerformanceEventExt,\n} from \"./telemetryTypes.js\";\n\n/**\n * Telemetry class that accumulates measurements which are eventually logged in a telemetry event through the provided\n * {@link TelemetryEventBatcher.logger | logger} when the number of calls to the function reaches the specified {@link TelemetryEventBatcher.threshold | threshold}.\n *\n * @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.\n * @typeparam TMetrics - The set of keys that should be logged.\n * E.g., `keyof Foo` for logging properties `bar` and `baz` from `type Foo = { bar: number, baz: number }`.\n *\n * @sealed\n * @internal\n */\nexport class TelemetryEventBatcher<TMetrics extends string> {\n\t/**\n\t * Stores the sum of the custom data passed into the logger.\n\t */\n\tprivate dataSums: { [key in TMetrics]?: number } = {};\n\n\t/**\n\t * Stores the maximum value of the custom data passed into the logger.\n\t */\n\tprivate dataMaxes: { [key in TMetrics]?: number } = {};\n\n\t/**\n\t * Counter to keep track of the number of times the log function is called.\n\t */\n\tprivate counter = 0;\n\n\tpublic constructor(\n\t\t/**\n\t\t * Custom properties to include in the telemetry performance event when it is written.\n\t\t */\n\t\tprivate readonly eventBase: ITelemetryGenericEventExt,\n\n\t\t/**\n\t\t * The logger to use to write the telemetry performance event.\n\t\t */\n\t\tprivate readonly logger: ITelemetryLoggerExt,\n\n\t\t/**\n\t\t * The number of logs to accumulate before sending the data to the logger.\n\t\t */\n\t\tprivate readonly threshold: number,\n\t) {}\n\n\t/**\n\t * Accumulates the custom data and sends it to the logger every {@link TelemetryEventBatcher.threshold} calls.\n\t *\n\t * @param customData -\n\t * A record storing the custom data to be accumulated and eventually logged.\n\t */\n\tpublic accumulateAndLog(customData: Record<TMetrics, number>): void {\n\t\tfor (const key of Object.keys(customData) as TMetrics[]) {\n\t\t\tthis.dataSums[key] = (this.dataSums[key] ?? 0) + customData[key];\n\t\t\tthis.dataMaxes[key] = Math.max(\n\t\t\t\tthis.dataMaxes[key] ?? Number.NEGATIVE_INFINITY,\n\t\t\t\tcustomData[key],\n\t\t\t);\n\t\t}\n\n\t\tthis.counter++;\n\n\t\tif (this.counter >= this.threshold) {\n\t\t\tthis.sendData();\n\t\t}\n\t}\n\n\tprivate sendData(): void {\n\t\tconst telemetryEvent: ITelemetryPerformanceEventExt = {\n\t\t\t...this.eventBase,\n\t\t};\n\n\t\tfor (const key of Object.keys(this.dataSums) as TMetrics[]) {\n\t\t\tif (this.dataSums[key] !== undefined) {\n\t\t\t\ttelemetryEvent[`avg_${key}`] = roundToDecimalPlaces(\n\t\t\t\t\tthis.dataSums[key]! / this.counter,\n\t\t\t\t\t6,\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (this.dataMaxes[key] !== undefined) {\n\t\t\t\ttelemetryEvent[`max_${key}`] = this.dataMaxes[key];\n\t\t\t}\n\t\t}\n\n\t\tthis.logger.sendPerformanceEvent(telemetryEvent);\n\n\t\t// Reset the counter and the data.\n\t\tthis.counter = 0;\n\t\tthis.dataSums = {};\n\t\tthis.dataMaxes = {};\n\t}\n}\n"]}
|