@fluidframework/telemetry-utils 1.2.2 → 2.0.0-internal.1.0.0.81589

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 (44) hide show
  1. package/dist/errorLogging.d.ts.map +1 -1
  2. package/dist/errorLogging.js +39 -18
  3. package/dist/errorLogging.js.map +1 -1
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/logger.d.ts +0 -5
  9. package/dist/logger.d.ts.map +1 -1
  10. package/dist/logger.js +3 -7
  11. package/dist/logger.js.map +1 -1
  12. package/dist/packageVersion.d.ts +1 -1
  13. package/dist/packageVersion.d.ts.map +1 -1
  14. package/dist/packageVersion.js +1 -1
  15. package/dist/packageVersion.js.map +1 -1
  16. package/dist/sampledTelemetryHelper.d.ts +53 -0
  17. package/dist/sampledTelemetryHelper.d.ts.map +1 -0
  18. package/dist/sampledTelemetryHelper.js +92 -0
  19. package/dist/sampledTelemetryHelper.js.map +1 -0
  20. package/lib/errorLogging.d.ts.map +1 -1
  21. package/lib/errorLogging.js +39 -18
  22. package/lib/errorLogging.js.map +1 -1
  23. package/lib/index.d.ts +1 -0
  24. package/lib/index.d.ts.map +1 -1
  25. package/lib/index.js +1 -0
  26. package/lib/index.js.map +1 -1
  27. package/lib/logger.d.ts +0 -5
  28. package/lib/logger.d.ts.map +1 -1
  29. package/lib/logger.js +3 -7
  30. package/lib/logger.js.map +1 -1
  31. package/lib/packageVersion.d.ts +1 -1
  32. package/lib/packageVersion.d.ts.map +1 -1
  33. package/lib/packageVersion.js +1 -1
  34. package/lib/packageVersion.js.map +1 -1
  35. package/lib/sampledTelemetryHelper.d.ts +53 -0
  36. package/lib/sampledTelemetryHelper.d.ts.map +1 -0
  37. package/lib/sampledTelemetryHelper.js +88 -0
  38. package/lib/sampledTelemetryHelper.js.map +1 -0
  39. package/package.json +14 -6
  40. package/src/errorLogging.ts +41 -17
  41. package/src/index.ts +1 -0
  42. package/src/logger.ts +3 -7
  43. package/src/packageVersion.ts +1 -1
  44. package/src/sampledTelemetryHelper.ts +142 -0
@@ -0,0 +1,53 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { IDisposable, ITelemetryGenericEvent, ITelemetryLogger, ITelemetryProperties } from "@fluidframework/common-definitions";
6
+ /**
7
+ * Helper class that executes a specified code block and writes an
8
+ * {@link @fluidframework/common-definitions#ITelemetryPerformanceEvent} to a specified logger every time a specified
9
+ * number of executions is reached (or when the class is disposed). The `duration` field in the telemetry event is
10
+ * the duration of the latest execution (sample) of the specified function. See the documentation of the
11
+ * `includeAggregateMetrics` parameter for additional details that can be included.
12
+ */
13
+ export declare class SampledTelemetryHelper implements IDisposable {
14
+ private readonly eventBase;
15
+ private readonly logger;
16
+ private readonly sampleThreshold;
17
+ private readonly includeAggregateMetrics;
18
+ private readonly perBucketProperties;
19
+ disposed: boolean;
20
+ private readonly measurementsMap;
21
+ /**
22
+ * @param eventBase -
23
+ * Custom properties to include in the telemetry performance event when it is written.
24
+ * @param logger -
25
+ * The logger to use to write the telemetry performance event.
26
+ * @param sampleThreshold -
27
+ * Telemetry performance events will be generated every time we hit this many executions of the code block.
28
+ * @param includeAggregateMetrics -
29
+ * If set to `true`, the telemetry performance event will include aggregated metrics (total duration, min duration,
30
+ * max duration) for all the executions in between generated events.
31
+ * @param perBucketProperties -
32
+ * Map of strings that represent different buckets (which can be specified when calling the 'measure' method), to
33
+ * properties which should be added to the telemetry event for that bucket. If a bucket being measured does not
34
+ * have an entry in this map, no additional properties will be added to its telemetry events. The following keys are
35
+ * reserved for use by this class: "duration", "count", "totalDuration", "minDuration", "maxDuration". If any of
36
+ * them is specified as a key in one of the ITelemetryProperties objects in this map, that key-value pair will be
37
+ * ignored.
38
+ */
39
+ constructor(eventBase: ITelemetryGenericEvent, logger: ITelemetryLogger, sampleThreshold: number, includeAggregateMetrics?: boolean, perBucketProperties?: Map<string, ITelemetryProperties>);
40
+ /**
41
+ * @param codeToMeasure -
42
+ * The code to be executed and measured.
43
+ * @param bucket -
44
+ * A key to track executions of the code block separately. Each different value of this parameter has a separate
45
+ * set of executions and metrics tracked by the class. If no such distinction needs to be made, do not provide a
46
+ * value.
47
+ * @returns Whatever the passed-in code block returns.
48
+ */
49
+ measure<T>(codeToMeasure: () => T, bucket?: string): T;
50
+ private flushBucket;
51
+ dispose(error?: Error | undefined): void;
52
+ }
53
+ //# sourceMappingURL=sampledTelemetryHelper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sampledTelemetryHelper.d.ts","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,WAAW,EACX,sBAAsB,EACtB,gBAAgB,EAEhB,oBAAoB,EACvB,MAAM,oCAAoC,CAAC;AAkC5C;;;;;;GAMG;AACF,qBAAa,sBAAuB,YAAW,WAAW;IAwBnD,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;IA3BxC,QAAQ,EAAE,OAAO,CAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAmC;IAEnE;;;;;;;;;;;;;;;;;OAiBG;gBAEkB,SAAS,EAAE,sBAAsB,EACjC,MAAM,EAAE,gBAAgB,EACxB,eAAe,EAAE,MAAM,EACvB,uBAAuB,GAAE,OAAe,EACxC,mBAAmB,oCAA0C;IAGlF;;;;;;;;OAQG;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;CAGlD"}
@@ -0,0 +1,88 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { performance } from "@fluidframework/common-utils";
6
+ /**
7
+ * Helper class that executes a specified code block and writes an
8
+ * {@link @fluidframework/common-definitions#ITelemetryPerformanceEvent} to a specified logger every time a specified
9
+ * number of executions is reached (or when the class is disposed). The `duration` field in the telemetry event is
10
+ * the duration of the latest execution (sample) of the specified function. See the documentation of the
11
+ * `includeAggregateMetrics` parameter for additional details that can be included.
12
+ */
13
+ export class SampledTelemetryHelper {
14
+ /**
15
+ * @param eventBase -
16
+ * Custom properties to include in the telemetry performance event when it is written.
17
+ * @param logger -
18
+ * The logger to use to write the telemetry performance event.
19
+ * @param sampleThreshold -
20
+ * Telemetry performance events will be generated every time we hit this many executions of the code block.
21
+ * @param includeAggregateMetrics -
22
+ * If set to `true`, the telemetry performance event will include aggregated metrics (total duration, min duration,
23
+ * max duration) for all the executions in between generated events.
24
+ * @param perBucketProperties -
25
+ * Map of strings that represent different buckets (which can be specified when calling the 'measure' method), to
26
+ * properties which should be added to the telemetry event for that bucket. If a bucket being measured does not
27
+ * have an entry in this map, no additional properties will be added to its telemetry events. The following keys are
28
+ * reserved for use by this class: "duration", "count", "totalDuration", "minDuration", "maxDuration". If any of
29
+ * them is specified as a key in one of the ITelemetryProperties objects in this map, that key-value pair will be
30
+ * ignored.
31
+ */
32
+ constructor(eventBase, logger, sampleThreshold, includeAggregateMetrics = false, perBucketProperties = new Map()) {
33
+ this.eventBase = eventBase;
34
+ this.logger = logger;
35
+ this.sampleThreshold = sampleThreshold;
36
+ this.includeAggregateMetrics = includeAggregateMetrics;
37
+ this.perBucketProperties = perBucketProperties;
38
+ this.disposed = false;
39
+ this.measurementsMap = new Map();
40
+ }
41
+ /**
42
+ * @param codeToMeasure -
43
+ * The code to be executed and measured.
44
+ * @param bucket -
45
+ * A key to track executions of the code block separately. Each different value of this parameter has a separate
46
+ * set of executions and metrics tracked by the class. If no such distinction needs to be made, do not provide a
47
+ * value.
48
+ * @returns Whatever the passed-in code block returns.
49
+ */
50
+ measure(codeToMeasure, bucket = "") {
51
+ var _a, _b, _c;
52
+ const start = performance.now();
53
+ const returnValue = codeToMeasure();
54
+ const duration = performance.now() - start;
55
+ let m = this.measurementsMap.get(bucket);
56
+ if (m === undefined) {
57
+ m = { count: 0, duration: -1 };
58
+ this.measurementsMap.set(bucket, m);
59
+ }
60
+ m.count++;
61
+ m.duration = duration;
62
+ if (this.includeAggregateMetrics) {
63
+ m.totalDuration = ((_a = m.totalDuration) !== null && _a !== void 0 ? _a : 0) + duration;
64
+ m.minDuration = Math.min((_b = m.minDuration) !== null && _b !== void 0 ? _b : duration, duration);
65
+ m.maxDuration = Math.max((_c = m.maxDuration) !== null && _c !== void 0 ? _c : 0, duration);
66
+ }
67
+ if (m.count >= this.sampleThreshold) {
68
+ this.flushBucket(bucket);
69
+ }
70
+ return returnValue;
71
+ }
72
+ flushBucket(bucket) {
73
+ const measurements = this.measurementsMap.get(bucket);
74
+ if (measurements === undefined) {
75
+ return;
76
+ }
77
+ if (measurements.count !== 0) {
78
+ const bucketProperties = this.perBucketProperties.get(bucket);
79
+ const telemetryEvent = Object.assign(Object.assign(Object.assign({}, this.eventBase), bucketProperties), measurements);
80
+ this.logger.sendPerformanceEvent(telemetryEvent);
81
+ this.measurementsMap.delete(bucket);
82
+ }
83
+ }
84
+ dispose(error) {
85
+ this.measurementsMap.forEach((_, k) => this.flushBucket(k));
86
+ }
87
+ }
88
+ //# sourceMappingURL=sampledTelemetryHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sampledTelemetryHelper.js","sourceRoot":"","sources":["../src/sampledTelemetryHelper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAiC3D;;;;;;GAMG;AACF,MAAM,OAAO,sBAAsB;IAKhC;;;;;;;;;;;;;;;;;OAiBG;IACH,YACqB,SAAiC,EACjC,MAAwB,EACxB,eAAuB,EACvB,0BAAmC,KAAK,EACxC,sBAAsB,IAAI,GAAG,EAAgC;QAJ7D,cAAS,GAAT,SAAS,CAAwB;QACjC,WAAM,GAAN,MAAM,CAAkB;QACxB,oBAAe,GAAf,eAAe,CAAQ;QACvB,4BAAuB,GAAvB,uBAAuB,CAAiB;QACxC,wBAAmB,GAAnB,mBAAmB,CAA0C;QA3BlF,aAAQ,GAAY,KAAK,CAAC;QAET,oBAAe,GAAG,IAAI,GAAG,EAAwB,CAAC;IA0BnE,CAAC;IAED;;;;;;;;OAQG;IACI,OAAO,CAAI,aAAsB,EAAE,SAAiB,EAAE;;QACzD,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAChC,MAAM,WAAW,GAAG,aAAa,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;QAE3C,IAAI,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACzC,IAAI,CAAC,KAAK,SAAS,EAAE;YACjB,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC;YAC/B,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;SACvC;QACD,CAAC,CAAC,KAAK,EAAE,CAAC;QACV,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAEtB,IAAI,IAAI,CAAC,uBAAuB,EAAE;YAC9B,CAAC,CAAC,aAAa,GAAG,CAAC,MAAA,CAAC,CAAC,aAAa,mCAAI,CAAC,CAAC,GAAG,QAAQ,CAAC;YACpD,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,MAAA,CAAC,CAAC,WAAW,mCAAI,QAAQ,EAAE,QAAQ,CAAC,CAAC;YAC9D,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,MAAA,CAAC,CAAC,WAAW,mCAAI,CAAC,EAAE,QAAQ,CAAC,CAAC;SAC1D;QAED,IAAI,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,eAAe,EAAE;YACjC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;SAC5B;QAED,OAAO,WAAW,CAAC;IACvB,CAAC;IAEO,WAAW,CAAC,MAAc;QAC9B,MAAM,YAAY,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,SAAS,EAAE;YAC5B,OAAO;SACV;QAED,IAAI,YAAY,CAAC,KAAK,KAAK,CAAC,EAAE;YAC1B,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YAE9D,MAAM,cAAc,iDACb,IAAI,CAAC,SAAS,GACd,gBAAgB,GAChB,YAAY,CAClB,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC,cAAc,CAAC,CAAC;YACjD,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;SACvC;IACL,CAAC;IAEM,OAAO,CAAC,KAAyB;QACpC,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IAChE,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n IDisposable,\n ITelemetryGenericEvent,\n ITelemetryLogger,\n ITelemetryPerformanceEvent,\n ITelemetryProperties,\n} from \"@fluidframework/common-definitions\";\nimport { performance } from \"@fluidframework/common-utils\";\n\ninterface Measurements {\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\n /**\n * The duration of the latest execution.\n */\n duration: number;\n\n /**\n * The number of executions since the last time an event was generated.\n */\n count: number;\n\n /**\n * Total duration across all the executions since the last event was generated.\n */\n totalDuration?: number;\n\n /**\n * Min duration across all the executions since the last event was generated.\n */\n minDuration?: number;\n\n /**\n * Max duration across all the executions since the last event was generated.\n */\n maxDuration?: number;\n}\n\n/**\n * Helper class that executes a specified code block and writes an\n * {@link @fluidframework/common-definitions#ITelemetryPerformanceEvent} to a specified logger every time a specified\n * number of executions is reached (or when the class is disposed). The `duration` field in the telemetry event is\n * the duration of the latest execution (sample) of the specified function. See the documentation of the\n * `includeAggregateMetrics` parameter for additional details that can be included.\n */\n export class SampledTelemetryHelper implements IDisposable {\n disposed: boolean = false;\n\n private readonly measurementsMap = new Map<string, Measurements>();\n\n /**\n * @param eventBase -\n * Custom properties to include in the telemetry performance event when it is written.\n * @param logger -\n * The logger to use to write the telemetry performance event.\n * @param sampleThreshold -\n * Telemetry performance events will be generated every time we hit this many executions of the code block.\n * @param includeAggregateMetrics -\n * If set to `true`, the telemetry performance event will include aggregated metrics (total duration, min duration,\n * max duration) for all the executions in between generated events.\n * @param perBucketProperties -\n * Map of strings that represent different buckets (which can be specified when calling the 'measure' method), to\n * properties which should be added to the telemetry event for that bucket. If a bucket being measured does not\n * have an entry in this map, no additional properties will be added to its telemetry events. The following keys are\n * reserved for use by this class: \"duration\", \"count\", \"totalDuration\", \"minDuration\", \"maxDuration\". If any of\n * them is specified as a key in one of the ITelemetryProperties objects in this map, that key-value pair will be\n * ignored.\n */\n public constructor(\n private readonly eventBase: ITelemetryGenericEvent,\n private readonly logger: ITelemetryLogger,\n private readonly sampleThreshold: number,\n private readonly includeAggregateMetrics: boolean = false,\n private readonly perBucketProperties = new Map<string, ITelemetryProperties>()) {\n }\n\n /**\n * @param codeToMeasure -\n * The code to be executed and measured.\n * @param bucket -\n * A key to track executions of the code block separately. Each different value of this parameter has a separate\n * set of executions and metrics tracked by the class. If no such distinction needs to be made, do not provide a\n * value.\n * @returns Whatever the passed-in code block returns.\n */\n public measure<T>(codeToMeasure: () => T, bucket: string = \"\"): T {\n const start = performance.now();\n const returnValue = codeToMeasure();\n const duration = performance.now() - start;\n\n let m = this.measurementsMap.get(bucket);\n if (m === undefined) {\n m = { count: 0, duration: -1 };\n this.measurementsMap.set(bucket, m);\n }\n m.count++;\n m.duration = duration;\n\n if (this.includeAggregateMetrics) {\n m.totalDuration = (m.totalDuration ?? 0) + duration;\n m.minDuration = Math.min(m.minDuration ?? duration, duration);\n m.maxDuration = Math.max(m.maxDuration ?? 0, duration);\n }\n\n if (m.count >= this.sampleThreshold) {\n this.flushBucket(bucket);\n }\n\n return returnValue;\n }\n\n private flushBucket(bucket: string) {\n const measurements = this.measurementsMap.get(bucket);\n if (measurements === undefined) {\n return;\n }\n\n if (measurements.count !== 0) {\n const bucketProperties = this.perBucketProperties.get(bucket);\n\n const telemetryEvent: ITelemetryPerformanceEvent = {\n ...this.eventBase,\n ...bucketProperties, // If the bucket doesn't exist and this is undefined, things work as expected\n ...measurements,\n };\n\n this.logger.sendPerformanceEvent(telemetryEvent);\n this.measurementsMap.delete(bucket);\n }\n }\n\n public dispose(error?: Error | undefined): void {\n this.measurementsMap.forEach((_, k) => this.flushBucket(k));\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/telemetry-utils",
3
- "version": "1.2.2",
3
+ "version": "2.0.0-internal.1.0.0.81589",
4
4
  "description": "Collection of telemetry relates utilities for Fluid",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -72,10 +72,10 @@
72
72
  },
73
73
  "devDependencies": {
74
74
  "@fluidframework/build-common": "^0.24.0",
75
- "@fluidframework/build-tools": "^0.2.74327",
75
+ "@fluidframework/build-tools": "^0.3.0-0",
76
76
  "@fluidframework/eslint-config-fluid": "^0.28.2000",
77
- "@fluidframework/mocha-test-setup": "^1.2.2",
78
- "@fluidframework/telemetry-utils-previous": "npm:@fluidframework/telemetry-utils@1.2.1",
77
+ "@fluidframework/mocha-test-setup": "2.0.0-internal.1.0.0.81589",
78
+ "@fluidframework/telemetry-utils-previous": "npm:@fluidframework/telemetry-utils@^1.0.0",
79
79
  "@microsoft/api-extractor": "^7.22.2",
80
80
  "@rushstack/eslint-config": "^2.5.1",
81
81
  "@types/debug": "^4.1.5",
@@ -93,7 +93,15 @@
93
93
  "typescript": "~4.5.5"
94
94
  },
95
95
  "typeValidation": {
96
- "version": "1.2.2",
97
- "broken": {}
96
+ "version": "2.0.0",
97
+ "broken": {
98
+ "RemovedFunctionDeclaration_originatedAsExternalError": {
99
+ "forwardCompat": false,
100
+ "backCompat": false
101
+ },
102
+ "EnumDeclaration_TelemetryDataTag": {
103
+ "forwardCompat": false
104
+ }
105
+ }
98
106
  }
99
107
  }
@@ -8,6 +8,7 @@ import {
8
8
  ITaggedTelemetryPropertyType,
9
9
  ITelemetryLogger,
10
10
  ITelemetryProperties,
11
+ TelemetryEventPropertyType,
11
12
  } from "@fluidframework/common-definitions";
12
13
  import { v4 as uuid } from "uuid";
13
14
  import {
@@ -262,9 +263,39 @@ export function isExternalError(e: any): boolean {
262
263
  * Type guard to identify if a particular value (loosely) appears to be a tagged telemetry property
263
264
  */
264
265
  export function isTaggedTelemetryPropertyValue(x: any): x is ITaggedTelemetryPropertyType {
265
- return (typeof (x?.value) !== "object" && typeof (x?.tag) === "string");
266
+ return typeof (x?.tag) === "string";
266
267
  }
267
268
 
269
+ /**
270
+ * Filter serializable telemetry properties
271
+ * @param x - any telemetry prop
272
+ * @returns - as-is if x is primitive. returns stringified if x is an array of primitive.
273
+ * otherwise returns null since this is what we support at the moment.
274
+ */
275
+ function filterValidTelemetryProps(x: any, key: string): TelemetryEventPropertyType {
276
+ if (Array.isArray(x) && x.every((val) => isTelemetryEventPropertyValue(val))) {
277
+ return JSON.stringify(x);
278
+ }
279
+ if (isTelemetryEventPropertyValue(x)) {
280
+ return x;
281
+ }
282
+ // We don't support logging arbitrary objects
283
+ console.error(`UnSupported Format of Logging Error Property for key ${key}:`, x);
284
+ return "REDACTED (arbitrary object)";
285
+ }
286
+
287
+ // checking type of x, returns false if x is null
288
+ function isTelemetryEventPropertyValue(x: any): x is TelemetryEventPropertyType {
289
+ switch (typeof x) {
290
+ case "string":
291
+ case "number":
292
+ case "boolean":
293
+ case "undefined":
294
+ return true;
295
+ default:
296
+ return false;
297
+ }
298
+ }
268
299
  /**
269
300
  * Walk an object's enumerable properties to find those fit for telemetry.
270
301
  */
@@ -275,22 +306,15 @@ function getValidTelemetryProps(obj: any, keysToOmit: Set<string>): ITelemetryPr
275
306
  continue;
276
307
  }
277
308
  const val = obj[key];
278
- switch (typeof val) {
279
- case "string":
280
- case "number":
281
- case "boolean":
282
- case "undefined":
283
- props[key] = val;
284
- break;
285
- default: {
286
- if (isTaggedTelemetryPropertyValue(val)) {
287
- props[key] = val;
288
- } else {
289
- // We don't support logging arbitrary objects
290
- props[key] = "REDACTED (arbitrary object)";
291
- }
292
- break;
293
- }
309
+
310
+ // ensure only valid props get logged, since props of logging error could be in any shape
311
+ if (isTaggedTelemetryPropertyValue(val)) {
312
+ props[key] = {
313
+ value: filterValidTelemetryProps(val.value, key),
314
+ tag: val.tag,
315
+ };
316
+ } else {
317
+ props[key] = filterValidTelemetryProps(val, key);
294
318
  }
295
319
  }
296
320
  return props;
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./logger";
11
11
  export * from "./mockLogger";
12
12
  export * from "./thresholdCounter";
13
13
  export * from "./utils";
14
+ export * from "./sampledTelemetryHelper";
14
15
  export {
15
16
  MonitoringContext,
16
17
  IConfigProviderBase,
package/src/logger.ts CHANGED
@@ -32,11 +32,6 @@ import {
32
32
  * Please do not modify existing entries for backwards compatibility.
33
33
  */
34
34
  export enum TelemetryDataTag {
35
- /**
36
- * Data containing terms from code packages that may have been dynamically loaded
37
- * @deprecated 1.0, will be removed in next release (see issue #6603). Use `TelemetryDataTag.CodeArtifact` instead.
38
- */
39
- PackageData = "PackageData",
40
35
  /** Data containing terms or IDs from code packages that may have been dynamically loaded */
41
36
  CodeArtifact = "CodeArtifact",
42
37
  /** Personal data of a variety of classifications that pertains to the user */
@@ -249,8 +244,9 @@ export abstract class TelemetryLogger implements ITelemetryLogger {
249
244
  // No tag means we can log plainly
250
245
  newEvent[key] = value;
251
246
  break;
252
- case TelemetryDataTag.PackageData:
253
- // For Microsoft applications, PackageData is safe for now
247
+ case "PackageData": // For back-compat
248
+ case TelemetryDataTag.CodeArtifact:
249
+ // For Microsoft applications, CodeArtifact is safe for now
254
250
  // (we don't load 3P code in 1P apps)
255
251
  newEvent[key] = value;
256
252
  break;
@@ -6,4 +6,4 @@
6
6
  */
7
7
 
8
8
  export const pkgName = "@fluidframework/telemetry-utils";
9
- export const pkgVersion = "1.2.2";
9
+ export const pkgVersion = "2.0.0-internal.1.0.0.81589";
@@ -0,0 +1,142 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ IDisposable,
8
+ ITelemetryGenericEvent,
9
+ ITelemetryLogger,
10
+ ITelemetryPerformanceEvent,
11
+ ITelemetryProperties,
12
+ } from "@fluidframework/common-definitions";
13
+ import { performance } from "@fluidframework/common-utils";
14
+
15
+ interface Measurements {
16
+ // The names of the properties in this interface are the ones that will get stamped in the
17
+ // telemetry event, changes should be considered carefully. The optional properties should
18
+ // only be populated if 'includeAggregateMetrics' is true.
19
+
20
+ /**
21
+ * The duration of the latest execution.
22
+ */
23
+ duration: number;
24
+
25
+ /**
26
+ * The number of executions since the last time an event was generated.
27
+ */
28
+ count: number;
29
+
30
+ /**
31
+ * Total duration across all the executions since the last event was generated.
32
+ */
33
+ totalDuration?: number;
34
+
35
+ /**
36
+ * Min duration across all the executions since the last event was generated.
37
+ */
38
+ minDuration?: number;
39
+
40
+ /**
41
+ * Max duration across all the executions since the last event was generated.
42
+ */
43
+ maxDuration?: number;
44
+ }
45
+
46
+ /**
47
+ * Helper class that executes a specified code block and writes an
48
+ * {@link @fluidframework/common-definitions#ITelemetryPerformanceEvent} to a specified logger every time a specified
49
+ * number of executions is reached (or when the class is disposed). The `duration` field in the telemetry event is
50
+ * the duration of the latest execution (sample) of the specified function. See the documentation of the
51
+ * `includeAggregateMetrics` parameter for additional details that can be included.
52
+ */
53
+ export class SampledTelemetryHelper implements IDisposable {
54
+ disposed: boolean = false;
55
+
56
+ private readonly measurementsMap = new Map<string, Measurements>();
57
+
58
+ /**
59
+ * @param eventBase -
60
+ * Custom properties to include in the telemetry performance event when it is written.
61
+ * @param logger -
62
+ * The logger to use to write the telemetry performance event.
63
+ * @param sampleThreshold -
64
+ * Telemetry performance events will be generated every time we hit this many executions of the code block.
65
+ * @param includeAggregateMetrics -
66
+ * If set to `true`, the telemetry performance event will include aggregated metrics (total duration, min duration,
67
+ * max duration) for all the executions in between generated events.
68
+ * @param perBucketProperties -
69
+ * Map of strings that represent different buckets (which can be specified when calling the 'measure' method), to
70
+ * properties which should be added to the telemetry event for that bucket. If a bucket being measured does not
71
+ * have an entry in this map, no additional properties will be added to its telemetry events. The following keys are
72
+ * reserved for use by this class: "duration", "count", "totalDuration", "minDuration", "maxDuration". If any of
73
+ * them is specified as a key in one of the ITelemetryProperties objects in this map, that key-value pair will be
74
+ * ignored.
75
+ */
76
+ public constructor(
77
+ private readonly eventBase: ITelemetryGenericEvent,
78
+ private readonly logger: ITelemetryLogger,
79
+ private readonly sampleThreshold: number,
80
+ private readonly includeAggregateMetrics: boolean = false,
81
+ private readonly perBucketProperties = new Map<string, ITelemetryProperties>()) {
82
+ }
83
+
84
+ /**
85
+ * @param codeToMeasure -
86
+ * The code to be executed and measured.
87
+ * @param bucket -
88
+ * A key to track executions of the code block separately. Each different value of this parameter has a separate
89
+ * set of executions and metrics tracked by the class. If no such distinction needs to be made, do not provide a
90
+ * value.
91
+ * @returns Whatever the passed-in code block returns.
92
+ */
93
+ public measure<T>(codeToMeasure: () => T, bucket: string = ""): T {
94
+ const start = performance.now();
95
+ const returnValue = codeToMeasure();
96
+ const duration = performance.now() - start;
97
+
98
+ let m = this.measurementsMap.get(bucket);
99
+ if (m === undefined) {
100
+ m = { count: 0, duration: -1 };
101
+ this.measurementsMap.set(bucket, m);
102
+ }
103
+ m.count++;
104
+ m.duration = duration;
105
+
106
+ if (this.includeAggregateMetrics) {
107
+ m.totalDuration = (m.totalDuration ?? 0) + duration;
108
+ m.minDuration = Math.min(m.minDuration ?? duration, duration);
109
+ m.maxDuration = Math.max(m.maxDuration ?? 0, duration);
110
+ }
111
+
112
+ if (m.count >= this.sampleThreshold) {
113
+ this.flushBucket(bucket);
114
+ }
115
+
116
+ return returnValue;
117
+ }
118
+
119
+ private flushBucket(bucket: string) {
120
+ const measurements = this.measurementsMap.get(bucket);
121
+ if (measurements === undefined) {
122
+ return;
123
+ }
124
+
125
+ if (measurements.count !== 0) {
126
+ const bucketProperties = this.perBucketProperties.get(bucket);
127
+
128
+ const telemetryEvent: ITelemetryPerformanceEvent = {
129
+ ...this.eventBase,
130
+ ...bucketProperties, // If the bucket doesn't exist and this is undefined, things work as expected
131
+ ...measurements,
132
+ };
133
+
134
+ this.logger.sendPerformanceEvent(telemetryEvent);
135
+ this.measurementsMap.delete(bucket);
136
+ }
137
+ }
138
+
139
+ public dispose(error?: Error | undefined): void {
140
+ this.measurementsMap.forEach((_, k) => this.flushBucket(k));
141
+ }
142
+ }