Skip to content

Commit e7296d9

Browse files
committed
limit telemetry of unhandled errors
1 parent 5002965 commit e7296d9

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/telemetry.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ const samplingConfigs: EventSamplingConfig[] = [
134134
propertyValue: "disconnect",
135135
samplingFactor: 0.2,
136136
},
137+
{
138+
eventName: "unhandlederror",
139+
samplingFactor: 0.01,
140+
},
137141
];
138142

139143
function shouldSampleEvent(
@@ -155,6 +159,25 @@ function shouldSampleEvent(
155159
return true;
156160
}
157161

162+
function applySampling(
163+
eventName: string,
164+
properties: TelemetryEventProperties | undefined,
165+
cb: (samplingFactor: number) => void,
166+
) {
167+
let samplingFactor = 1; // Default sampling factor
168+
169+
for (const config of samplingConfigs) {
170+
if (shouldSampleEvent(eventName, properties, config)) {
171+
samplingFactor = config.samplingFactor;
172+
break;
173+
}
174+
}
175+
176+
if (samplingFactor === 1 || Math.random() <= samplingFactor) {
177+
cb(samplingFactor);
178+
}
179+
}
180+
158181
export let reporter: TelemetryReporter;
159182

160183
class EnvironmentReporter extends TelemetryReporter {
@@ -171,22 +194,13 @@ class EnvironmentReporter extends TelemetryReporter {
171194
return;
172195
}
173196

174-
let samplingFactor = 1; // Default sampling factor
175-
176-
for (const config of samplingConfigs) {
177-
if (shouldSampleEvent(eventName, properties, config)) {
178-
samplingFactor = config.samplingFactor;
179-
break;
180-
}
181-
}
182-
183-
if (samplingFactor === 1 || Math.random() <= samplingFactor) {
197+
applySampling(eventName, properties, (samplingFactor) =>
184198
super.sendTelemetryEvent(
185199
eventName,
186200
properties,
187201
this.appendCount(eventName, samplingFactor, measurements),
188-
);
189-
}
202+
),
203+
);
190204
}
191205

192206
override sendTelemetryErrorEvent(
@@ -198,10 +212,12 @@ class EnvironmentReporter extends TelemetryReporter {
198212
return;
199213
}
200214

201-
super.sendTelemetryErrorEvent(
202-
eventName,
203-
properties,
204-
this.appendCount(eventName, 1, measurements),
215+
applySampling(eventName, properties, (samplingFactor) =>
216+
super.sendTelemetryErrorEvent(
217+
eventName,
218+
properties,
219+
this.appendCount(eventName, samplingFactor, measurements),
220+
),
205221
);
206222
}
207223

@@ -213,10 +229,13 @@ class EnvironmentReporter extends TelemetryReporter {
213229
if (process.env.ELS_TEST) {
214230
return;
215231
}
216-
super.sendRawTelemetryEvent(
217-
eventName,
218-
properties,
219-
this.appendCount(eventName, 1, measurements),
232+
233+
applySampling(eventName, properties, (samplingFactor) =>
234+
super.sendRawTelemetryEvent(
235+
eventName,
236+
properties,
237+
this.appendCount(eventName, samplingFactor, measurements),
238+
),
220239
);
221240
}
222241

@@ -228,10 +247,13 @@ class EnvironmentReporter extends TelemetryReporter {
228247
if (process.env.ELS_TEST) {
229248
return;
230249
}
231-
super.sendDangerousTelemetryErrorEvent(
232-
eventName,
233-
properties,
234-
this.appendCount(eventName, 1, measurements),
250+
251+
applySampling(eventName, properties, (samplingFactor) =>
252+
super.sendDangerousTelemetryErrorEvent(
253+
eventName,
254+
properties,
255+
this.appendCount(eventName, samplingFactor, measurements),
256+
),
235257
);
236258
}
237259

@@ -243,10 +265,13 @@ class EnvironmentReporter extends TelemetryReporter {
243265
if (process.env.ELS_TEST) {
244266
return;
245267
}
246-
super.sendDangerousTelemetryEvent(
247-
eventName,
248-
properties,
249-
this.appendCount(eventName, 1, measurements),
268+
269+
applySampling(eventName, properties, (samplingFactor) =>
270+
super.sendDangerousTelemetryEvent(
271+
eventName,
272+
properties,
273+
this.appendCount(eventName, samplingFactor, measurements),
274+
),
250275
);
251276
}
252277

0 commit comments

Comments
 (0)