@fluidframework/telemetry-utils 2.0.0-internal.7.0.0 → 2.0.0-internal.7.1.0

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/src/utils.ts CHANGED
@@ -7,6 +7,8 @@ import {
7
7
  ITelemetryBaseLogger,
8
8
  ITelemetryGenericEvent,
9
9
  } from "@fluidframework/core-interfaces";
10
+ import { loggerToMonitoringContext } from "./config";
11
+ import { ITelemetryGenericEventExt, ITelemetryLoggerExt } from "./telemetryTypes";
10
12
 
11
13
  /**
12
14
  * Like assert, but logs only if the condition is false, rather than throwing
@@ -30,3 +32,86 @@ export function logIfFalse(
30
32
  logger.send(newEvent);
31
33
  return false;
32
34
  }
35
+
36
+ /**
37
+ * An object that contains a callback used in conjunction with the {@link createSampledLogger} utility function to provide custom logic for sampling events.
38
+ *
39
+ * @internal
40
+ */
41
+ export interface IEventSampler {
42
+ /**
43
+ * @returns true if the event should be sampled or false if not
44
+ */
45
+ sample: () => boolean | undefined;
46
+ }
47
+
48
+ /**
49
+ * A telemetry logger that has sampling capabilities
50
+ *
51
+ * @internal
52
+ */
53
+ export interface ISampledTelemetryLogger extends ITelemetryLoggerExt {
54
+ /**
55
+ * Indicates if the feature flag to disable sampling is set.
56
+ *
57
+ * @remarks Exposed to enable some advanced scenarios where the code using the sampled logger
58
+ * could take advantage of skipping the execution of some logic when it can determine
59
+ * it won't be necessary because the telemetry event that needs it wouldn't be
60
+ * emitted anyway.
61
+ */
62
+ isSamplingDisabled: boolean;
63
+ }
64
+
65
+ /**
66
+ * Wraps around an existing logger matching the {@link ITelemetryLoggerExt} interface and provides the ability to only log a subset of events using a sampling strategy provided by an ${@link IEventSampler}.
67
+ * You can chose to not provide an event sampler which is effectively a no-op, meaning that it will be treated as if the sampler always returns true.
68
+ *
69
+ * @remarks
70
+ * The sampling functionality uses the Fluid telemetry logging configuration along with the optionally provided event sampling callback to determine whether an event should
71
+ * be logged or not.
72
+ *
73
+ * Configuration object parameters:
74
+ * 'Fluid.Telemetry.DisableSampling': if this config value is set to true, all events will be unsampled and therefore logged.
75
+ * Otherwise only a sample will be logged according to the provided event sampler callback.
76
+ *
77
+ * Note that the same sampler is used for all APIs of the returned logger. If you want separate events flowing through the returned logger to be sampled separately, the {@link IEventSampler} you provide should track them separately.
78
+ *
79
+ * @internal
80
+ */
81
+ export function createSampledLogger(
82
+ logger: ITelemetryLoggerExt,
83
+ eventSampler?: IEventSampler,
84
+ ): ISampledTelemetryLogger {
85
+ const monitoringContext = loggerToMonitoringContext(logger);
86
+ const isSamplingDisabled =
87
+ monitoringContext.config.getBoolean("Fluid.Telemetry.DisableSampling") ?? false;
88
+
89
+ const sampledLogger = {
90
+ send: (event: ITelemetryBaseEvent): void => {
91
+ // The sampler uses the following logic for sending events:
92
+ // 1. If isSamplingDisabled is true, then this means events should be unsampled. Therefore we send the event without any checks.
93
+ // 2. If isSamplingDisabled is false, then event should be sampled using the event sampler, if the sampler is not defined just send all events, other use the eventSampler.sample() method.
94
+ if (isSamplingDisabled || eventSampler === undefined || eventSampler.sample()) {
95
+ logger.send(event);
96
+ }
97
+ },
98
+ sendTelemetryEvent: (event: ITelemetryGenericEventExt): void => {
99
+ if (isSamplingDisabled || eventSampler === undefined || eventSampler.sample()) {
100
+ logger.sendTelemetryEvent(event);
101
+ }
102
+ },
103
+ sendErrorEvent: (event: ITelemetryGenericEventExt): void => {
104
+ if (isSamplingDisabled || eventSampler === undefined || eventSampler.sample()) {
105
+ logger.sendErrorEvent(event);
106
+ }
107
+ },
108
+ sendPerformanceEvent: (event: ITelemetryGenericEventExt): void => {
109
+ if (isSamplingDisabled || eventSampler === undefined || eventSampler.sample()) {
110
+ logger.sendPerformanceEvent(event);
111
+ }
112
+ },
113
+ isSamplingDisabled,
114
+ };
115
+
116
+ return sampledLogger;
117
+ }