@fluidframework/telemetry-utils 1.2.7 → 2.0.0-dev.1.3.0.96595

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 (70) hide show
  1. package/.mocharc.js +12 -0
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +2 -7
  4. package/dist/config.js.map +1 -1
  5. package/dist/errorLogging.d.ts +10 -1
  6. package/dist/errorLogging.d.ts.map +1 -1
  7. package/dist/errorLogging.js +75 -31
  8. package/dist/errorLogging.js.map +1 -1
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/logger.d.ts +1 -6
  14. package/dist/logger.d.ts.map +1 -1
  15. package/dist/logger.js +4 -8
  16. package/dist/logger.js.map +1 -1
  17. package/dist/mockLogger.d.ts +2 -0
  18. package/dist/mockLogger.d.ts.map +1 -1
  19. package/dist/mockLogger.js +12 -0
  20. package/dist/mockLogger.js.map +1 -1
  21. package/dist/packageVersion.d.ts +1 -1
  22. package/dist/packageVersion.d.ts.map +1 -1
  23. package/dist/packageVersion.js +1 -1
  24. package/dist/packageVersion.js.map +1 -1
  25. package/dist/sampledTelemetryHelper.d.ts +53 -0
  26. package/dist/sampledTelemetryHelper.d.ts.map +1 -0
  27. package/dist/sampledTelemetryHelper.js +92 -0
  28. package/dist/sampledTelemetryHelper.js.map +1 -0
  29. package/dist/thresholdCounter.d.ts +1 -1
  30. package/dist/thresholdCounter.js +1 -1
  31. package/dist/thresholdCounter.js.map +1 -1
  32. package/lib/config.d.ts.map +1 -1
  33. package/lib/config.js +2 -7
  34. package/lib/config.js.map +1 -1
  35. package/lib/errorLogging.d.ts +10 -1
  36. package/lib/errorLogging.d.ts.map +1 -1
  37. package/lib/errorLogging.js +74 -30
  38. package/lib/errorLogging.js.map +1 -1
  39. package/lib/index.d.ts +1 -0
  40. package/lib/index.d.ts.map +1 -1
  41. package/lib/index.js +1 -0
  42. package/lib/index.js.map +1 -1
  43. package/lib/logger.d.ts +1 -6
  44. package/lib/logger.d.ts.map +1 -1
  45. package/lib/logger.js +4 -8
  46. package/lib/logger.js.map +1 -1
  47. package/lib/mockLogger.d.ts +2 -0
  48. package/lib/mockLogger.d.ts.map +1 -1
  49. package/lib/mockLogger.js +12 -0
  50. package/lib/mockLogger.js.map +1 -1
  51. package/lib/packageVersion.d.ts +1 -1
  52. package/lib/packageVersion.d.ts.map +1 -1
  53. package/lib/packageVersion.js +1 -1
  54. package/lib/packageVersion.js.map +1 -1
  55. package/lib/sampledTelemetryHelper.d.ts +53 -0
  56. package/lib/sampledTelemetryHelper.d.ts.map +1 -0
  57. package/lib/sampledTelemetryHelper.js +88 -0
  58. package/lib/sampledTelemetryHelper.js.map +1 -0
  59. package/lib/thresholdCounter.d.ts +1 -1
  60. package/lib/thresholdCounter.js +1 -1
  61. package/lib/thresholdCounter.js.map +1 -1
  62. package/package.json +21 -10
  63. package/src/config.ts +2 -4
  64. package/src/errorLogging.ts +77 -30
  65. package/src/index.ts +1 -0
  66. package/src/logger.ts +4 -8
  67. package/src/mockLogger.ts +13 -0
  68. package/src/packageVersion.ts +1 -1
  69. package/src/sampledTelemetryHelper.ts +142 -0
  70. package/src/thresholdCounter.ts +1 -1
@@ -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
+ }
@@ -30,7 +30,7 @@ export class ThresholdCounter {
30
30
  }
31
31
 
32
32
  /**
33
- * Sends the value if it's above the treshold
33
+ * Sends the value if it's above the threshold
34
34
  * and a multiple of the threshold.
35
35
  *
36
36
  * To be used in scenarios where we'd like to record a