@copilotkit/shared 1.71.2 → 1.72.1-canary.sp-longthread-perf

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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.cjs +6 -0
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +3 -2
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +3 -2
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +3 -2
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/index.umd.js +126 -2
  11. package/dist/index.umd.js.map +1 -1
  12. package/dist/package.cjs +1 -1
  13. package/dist/package.mjs +1 -1
  14. package/dist/telemetry/events.d.cts +33 -0
  15. package/dist/telemetry/events.d.cts.map +1 -1
  16. package/dist/telemetry/events.d.mts +33 -0
  17. package/dist/telemetry/events.d.mts.map +1 -1
  18. package/dist/telemetry/index.cjs +3 -0
  19. package/dist/telemetry/index.d.cts +2 -2
  20. package/dist/telemetry/index.d.mts +2 -2
  21. package/dist/telemetry/index.mjs +2 -2
  22. package/dist/telemetry/model-host.cjs +104 -0
  23. package/dist/telemetry/model-host.cjs.map +1 -0
  24. package/dist/telemetry/model-host.d.cts +26 -0
  25. package/dist/telemetry/model-host.d.cts.map +1 -0
  26. package/dist/telemetry/model-host.d.mts +26 -0
  27. package/dist/telemetry/model-host.d.mts.map +1 -0
  28. package/dist/telemetry/model-host.mjs +102 -0
  29. package/dist/telemetry/model-host.mjs.map +1 -0
  30. package/dist/telemetry/sampling.cjs +22 -0
  31. package/dist/telemetry/sampling.cjs.map +1 -1
  32. package/dist/telemetry/sampling.d.cts +22 -2
  33. package/dist/telemetry/sampling.d.cts.map +1 -1
  34. package/dist/telemetry/sampling.d.mts +22 -2
  35. package/dist/telemetry/sampling.d.mts.map +1 -1
  36. package/dist/telemetry/sampling.mjs +20 -1
  37. package/dist/telemetry/sampling.mjs.map +1 -1
  38. package/dist/telemetry/telemetry-client.cjs +18 -11
  39. package/dist/telemetry/telemetry-client.cjs.map +1 -1
  40. package/dist/telemetry/telemetry-client.d.cts +3 -3
  41. package/dist/telemetry/telemetry-client.d.cts.map +1 -1
  42. package/dist/telemetry/telemetry-client.d.mts +3 -3
  43. package/dist/telemetry/telemetry-client.d.mts.map +1 -1
  44. package/dist/telemetry/telemetry-client.mjs +19 -12
  45. package/dist/telemetry/telemetry-client.mjs.map +1 -1
  46. package/package.json +1 -1
  47. package/src/index.ts +3 -0
  48. package/src/telemetry/events.ts +33 -0
  49. package/src/telemetry/model-host.test.ts +145 -0
  50. package/src/telemetry/model-host.ts +155 -0
  51. package/src/telemetry/sampling.ts +39 -6
  52. package/src/telemetry/telemetry-blank-license-identity.test.ts +11 -4
  53. package/src/telemetry/telemetry-client.test.ts +107 -39
  54. package/src/telemetry/telemetry-client.ts +48 -27
@@ -122,12 +122,23 @@ describe("v1 TelemetryClient", () => {
122
122
  "cloud.api_key_provided": false,
123
123
  } as const;
124
124
 
125
+ /** The block every unsampled wire carries, whatever the configured rate. */
126
+ const unsampledMeta = {
127
+ sampleRate: 1,
128
+ sampleRateAdjustmentFactor: 0,
129
+ sampleWeight: 1,
130
+ } as const;
131
+
125
132
  /**
126
- * Verifies the shared event shape and sampling metadata sent to both sinks.
133
+ * Verifies the shared event shape and per-wire sampling metadata.
134
+ *
135
+ * The two wires are gated separately, so they carry different sampling
136
+ * blocks for the same capture: the lambda copy is never sampled, while the
137
+ * Segment copy reports the rate its own gate used.
127
138
  *
128
139
  * @param expectedIdentity - Identity fields expected only on the Lambda send.
129
- * @param samplingMeta - Effective sampling metadata for this capture.
130
- * @param telemetryIdentified - Whether a license-derived identity bypassed sampling.
140
+ * @param segmentSamplingMeta - Sampling metadata expected on the Segment copy.
141
+ * @param telemetryIdentified - Whether a license-derived identity was present.
131
142
  * @param identityValues - Raw identity values that must not leak into payloads.
132
143
  */
133
144
  function expectBothSinksReceivedEvent(
@@ -135,7 +146,7 @@ describe("v1 TelemetryClient", () => {
135
146
  Parameters<typeof lambdaClient.send>[0],
136
147
  "licenseToken" | "telemetryId"
137
148
  >,
138
- samplingMeta: {
149
+ segmentSamplingMeta: {
139
150
  sampleRate: number;
140
151
  sampleRateAdjustmentFactor: number;
141
152
  sampleWeight: number;
@@ -147,14 +158,15 @@ describe("v1 TelemetryClient", () => {
147
158
  const lambdaEvent = lambdaSpy.mock.calls[0][0];
148
159
  expect(lambdaEvent).toMatchObject({
149
160
  ...expectedIdentity,
150
- globalProperties: samplingMeta,
161
+ globalProperties: unsampledMeta,
151
162
  });
152
163
  expect(lambdaEvent.properties).toEqual(flattenedBaseInstanceEvent);
153
164
  expect(lambdaEvent.globalProperties).toEqual({
154
165
  "copilotkit.package.name": "@copilotkit/shared",
155
166
  "copilotkit.package.version": "1.0.0",
156
- ...samplingMeta,
167
+ ...unsampledMeta,
157
168
  telemetry_emitter: "v1-shared",
169
+ telemetry_surface: "v1",
158
170
  telemetry_event_id: expect.any(String),
159
171
  telemetry_identified: telemetryIdentified,
160
172
  telemetry_transport: "lambda",
@@ -169,8 +181,9 @@ describe("v1 TelemetryClient", () => {
169
181
  ...flattenedBaseInstanceEvent,
170
182
  "copilotkit.package.name": "@copilotkit/shared",
171
183
  "copilotkit.package.version": "1.0.0",
172
- ...samplingMeta,
184
+ ...segmentSamplingMeta,
173
185
  telemetry_emitter: "v1-shared",
186
+ telemetry_surface: "v1",
174
187
  telemetry_event_id: expect.any(String),
175
188
  telemetry_identified: telemetryIdentified,
176
189
  telemetry_transport: "segment",
@@ -188,7 +201,7 @@ describe("v1 TelemetryClient", () => {
188
201
  }
189
202
  }
190
203
 
191
- test("capture sends to both sinks when sampled in (anonymous, one decision gates both)", async () => {
204
+ test("capture sends to both sinks when the anonymous Segment gate opens", async () => {
192
205
  vi.spyOn(Math, "random").mockReturnValue(0);
193
206
  const client = makeClient({ sampleRate: 0.05 });
194
207
 
@@ -201,15 +214,53 @@ describe("v1 TelemetryClient", () => {
201
214
  });
202
215
  });
203
216
 
204
- test("capture skips both sinks when anonymous and sampled out", async () => {
205
- // Math.random=0.99 vs sampleRate=0.05 — anonymous caller is gated out;
206
- // neither sink should fire under the new one-decision-both-sinks model.
217
+ test("an anonymous event sampled out of Segment still reaches the lambda sink", async () => {
218
+ // Math.random=0.99 vs sampleRate=0.05. The gate is Segment's alone now:
219
+ // the lambda sink is ours and takes every event, so a rate that drops
220
+ // 95% of the Segment copies must drop none of the lambda copies. This is
221
+ // the whole point of the change — if it ever reverts, this fails first.
207
222
  vi.spyOn(Math, "random").mockReturnValue(0.99);
208
223
  const client = makeClient({ sampleRate: 0.05 });
209
224
 
210
225
  await client.capture("oss.runtime.instance_created", baseInstanceEvent);
211
226
 
212
- expect(lambdaSpy).not.toHaveBeenCalled();
227
+ expect(lambdaSpy).toHaveBeenCalledTimes(1);
228
+ expect(segmentTrackMock).not.toHaveBeenCalled();
229
+ });
230
+
231
+ test("the lambda copy reports rate 1 while the Segment copy of the same capture reports the gated rate", async () => {
232
+ // One capture, two wires, two different weights. A shared block would
233
+ // make the unsampled lambda copy claim it stands for 20 events.
234
+ vi.spyOn(Math, "random").mockReturnValue(0);
235
+ const client = makeClient({ sampleRate: 0.05 });
236
+
237
+ await client.capture("oss.runtime.instance_created", baseInstanceEvent);
238
+
239
+ expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
240
+ sampleRate: 1,
241
+ sampleRateAdjustmentFactor: 0,
242
+ sampleWeight: 1,
243
+ });
244
+ expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
245
+ properties: expect.objectContaining({
246
+ sampleRate: 0.05,
247
+ sampleRateAdjustmentFactor: 0.95,
248
+ sampleWeight: 20,
249
+ }),
250
+ });
251
+ });
252
+
253
+ test("COPILOTKIT_TELEMETRY_SAMPLE_RATE=0 silences Segment without silencing the lambda sink", async () => {
254
+ // The env var survives as a Segment-only lever. Setting it to 0 is the
255
+ // strongest form of that lever and still must not be mistaken for an
256
+ // opt-out: COPILOTKIT_TELEMETRY_DISABLED is the opt-out.
257
+ vi.spyOn(Math, "random").mockReturnValue(0);
258
+ process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE = "0";
259
+ const client = makeClient();
260
+
261
+ await client.capture("oss.runtime.instance_created", baseInstanceEvent);
262
+
263
+ expect(lambdaSpy).toHaveBeenCalledTimes(1);
213
264
  expect(segmentTrackMock).not.toHaveBeenCalled();
214
265
  });
215
266
 
@@ -220,7 +271,7 @@ describe("v1 TelemetryClient", () => {
220
271
  priorLicenseToken: jwtWith({ telemetry_id: "legacy-telemetry-id" }),
221
272
  },
222
273
  ])(
223
- "standalone identity remains sample-gated when $label",
274
+ "standalone identity stays behind the Segment gate when $label",
224
275
  async ({ priorLicenseToken }) => {
225
276
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0.99);
226
277
  const client = makeClient({ sampleRate: 0.05 });
@@ -234,12 +285,12 @@ describe("v1 TelemetryClient", () => {
234
285
  await client.capture("oss.runtime.instance_created", baseInstanceEvent);
235
286
 
236
287
  expect(randomSpy).toHaveBeenCalledTimes(1);
237
- expect(lambdaSpy).not.toHaveBeenCalled();
288
+ expect(lambdaSpy).toHaveBeenCalledTimes(1);
238
289
  expect(segmentTrackMock).not.toHaveBeenCalled();
239
290
  },
240
291
  );
241
292
 
242
- test("sampled standalone identity remains a transport claim with anonymous sampling metadata", async () => {
293
+ test("standalone identity remains a transport claim with anonymous Segment sampling metadata", async () => {
243
294
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
244
295
  const client = makeClient({ sampleRate: 0.05 });
245
296
  client.setTelemetryIdentity({
@@ -301,7 +352,7 @@ describe("v1 TelemetryClient", () => {
301
352
  },
302
353
  );
303
354
 
304
- test("legacy license identity bypasses sampling for both sinks with the effective rate", async () => {
355
+ test("legacy license identity bypasses the Segment gate and weighs 1 on both wires", async () => {
305
356
  const telemetryId = "legacy-telemetry-id";
306
357
  const licenseToken = jwtWith({ telemetry_id: telemetryId });
307
358
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0.99);
@@ -323,7 +374,7 @@ describe("v1 TelemetryClient", () => {
323
374
  );
324
375
  });
325
376
 
326
- test("clearing telemetry identity restores one anonymous sampling decision for both sinks", async () => {
377
+ test("clearing telemetry identity restores the anonymous Segment decision", async () => {
327
378
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
328
379
  const client = makeClient({ sampleRate: 0.05 });
329
380
  client.setTelemetryIdentity({ telemetryId: "standalone-telemetry-id" });
@@ -336,11 +387,7 @@ describe("v1 TelemetryClient", () => {
336
387
  expect(lambdaSpy.mock.calls[0][0]).toMatchObject({
337
388
  licenseToken: undefined,
338
389
  telemetryId: undefined,
339
- globalProperties: {
340
- sampleRate: 0.05,
341
- sampleRateAdjustmentFactor: 0.95,
342
- sampleWeight: 20,
343
- },
390
+ globalProperties: unsampledMeta,
344
391
  });
345
392
  expect(segmentTrackMock).toHaveBeenCalledTimes(1);
346
393
  expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
@@ -377,7 +424,7 @@ describe("v1 TelemetryClient", () => {
377
424
  );
378
425
 
379
426
  test.each(["", " \t "])(
380
- "blank standalone identity %j without a legacy identity remains anonymously sampled",
427
+ "blank standalone identity %j without a legacy identity stays anonymous",
381
428
  async (blankTelemetryId) => {
382
429
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
383
430
  const client = makeClient({ sampleRate: 0.05 });
@@ -390,13 +437,19 @@ describe("v1 TelemetryClient", () => {
390
437
  expect.objectContaining({
391
438
  licenseToken: undefined,
392
439
  telemetryId: undefined,
393
- globalProperties: expect.objectContaining({ sampleRate: 0.05 }),
440
+ globalProperties: expect.objectContaining({
441
+ sampleRate: 1,
442
+ telemetry_identified: false,
443
+ }),
394
444
  }),
395
445
  );
446
+ expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
447
+ properties: expect.objectContaining({ sampleRate: 0.05 }),
448
+ });
396
449
  },
397
450
  );
398
451
 
399
- test("identified callers bypass the sample gate (lambda + segment fire even when Math.random would fail)", async () => {
452
+ test("identified callers bypass the Segment gate (both wires fire even when Math.random would fail)", async () => {
400
453
  vi.spyOn(Math, "random").mockReturnValue(0.99);
401
454
  const client = makeClient({ sampleRate: 0.05 });
402
455
  client.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
@@ -480,17 +533,16 @@ describe("v1 TelemetryClient", () => {
480
533
  expect(warn).not.toHaveBeenCalled();
481
534
  });
482
535
 
483
- test("identified events carry sampleWeight=1 (anonymous events carry 1/sampleRate)", async () => {
484
- // Anonymous: sampleWeight should be 1 / sampleRate so downstream
485
- // weight-based extrapolation reconstructs true volume.
486
- // Identified: bypassing the gate means each event represents itself,
487
- // so sampleWeight must be 1 — not the population's 1/sampleRate.
536
+ test("Segment keeps sampleWeight=1/sampleRate for anonymous events and 1 for identified ones", async () => {
537
+ // On the Segment wire the weight still has work to do: anonymous events
538
+ // stand for 1 / sampleRate of them, identified events stand for
539
+ // themselves. The lambda wire is unsampled, so its weight is always 1.
488
540
  vi.spyOn(Math, "random").mockReturnValue(0);
489
541
  const anonClient = makeClient({ sampleRate: 0.05 });
490
542
  await anonClient.capture("oss.runtime.instance_created", baseInstanceEvent);
491
543
  expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
492
- sampleRate: 0.05,
493
- sampleWeight: 20,
544
+ sampleRate: 1,
545
+ sampleWeight: 1,
494
546
  });
495
547
  expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
496
548
  properties: expect.objectContaining({
@@ -514,7 +566,7 @@ describe("v1 TelemetryClient", () => {
514
566
  });
515
567
  });
516
568
 
517
- test("malformed license token stays anonymous and remains sample-gated", async () => {
569
+ test("malformed license token stays anonymous and stays behind the Segment gate", async () => {
518
570
  // parseTelemetryIdFromLicense returns null for any of: empty token,
519
571
  // wrong-shape (not three dot-separated segments), base64/JSON parse
520
572
  // failure. A misconfigured customer must not flip to identified-bypass.
@@ -525,11 +577,13 @@ describe("v1 TelemetryClient", () => {
525
577
  client.setLicenseToken("not-a-jwt");
526
578
  await client.capture("oss.runtime.instance_created", baseInstanceEvent);
527
579
 
528
- expect(lambdaSpy).not.toHaveBeenCalled();
529
580
  expect(segmentTrackMock).not.toHaveBeenCalled();
581
+ expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
582
+ telemetry_identified: false,
583
+ });
530
584
  });
531
585
 
532
- test("illegal base64url license payload cannot bypass sampleRate 0", async () => {
586
+ test("illegal base64url license payload cannot bypass Segment sampleRate 0", async () => {
533
587
  const randomSpy = vi.spyOn(Math, "random").mockReturnValue(0);
534
588
  vi.spyOn(console, "warn").mockImplementation(() => undefined);
535
589
  const client = makeClient({ sampleRate: 0 });
@@ -538,8 +592,12 @@ describe("v1 TelemetryClient", () => {
538
592
  await client.capture("oss.runtime.instance_created", baseInstanceEvent);
539
593
 
540
594
  expect(randomSpy).toHaveBeenCalledTimes(1);
541
- expect(lambdaSpy).not.toHaveBeenCalled();
542
595
  expect(segmentTrackMock).not.toHaveBeenCalled();
596
+ // The lambda copy still goes, and still says it is anonymous: an
597
+ // unparseable token must not buy identified status on either wire.
598
+ expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
599
+ telemetry_identified: false,
600
+ });
543
601
  });
544
602
 
545
603
  test("setLicenseToken cache is overwritable (good token replaced by bad → back to anonymous gate)", async () => {
@@ -554,8 +612,10 @@ describe("v1 TelemetryClient", () => {
554
612
  client.setLicenseToken(jwtWith({ license_id: "no-telemetry-id" }));
555
613
  await client.capture("oss.runtime.instance_created", baseInstanceEvent);
556
614
 
557
- expect(lambdaSpy).not.toHaveBeenCalled();
558
615
  expect(segmentTrackMock).not.toHaveBeenCalled();
616
+ expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
617
+ telemetry_identified: false,
618
+ });
559
619
  });
560
620
 
561
621
  test("setCloudConfiguration writes cloud keys into globalProperties for both sinks", async () => {
@@ -647,7 +707,11 @@ describe("v1 TelemetryClient", () => {
647
707
  expect(first).not.toBe(second);
648
708
  });
649
709
 
650
- test("both copies are stamped as emitted by the v1 client", async () => {
710
+ test("both copies are stamped as emitted by the v1 client, on the v1 surface", async () => {
711
+ // telemetry_emitter names the client library; telemetry_surface names
712
+ // the API the developer built against. They are separate because six
713
+ // emitters across four languages report only two surfaces, and the
714
+ // question anyone asks of this data is how much traffic is still v1.
651
715
  vi.spyOn(Math, "random").mockReturnValue(0);
652
716
  const client = makeClient({ sampleRate: 0.05 });
653
717
 
@@ -655,9 +719,13 @@ describe("v1 TelemetryClient", () => {
655
719
 
656
720
  expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
657
721
  telemetry_emitter: "v1-shared",
722
+ telemetry_surface: "v1",
658
723
  });
659
724
  expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
660
- properties: expect.objectContaining({ telemetry_emitter: "v1-shared" }),
725
+ properties: expect.objectContaining({
726
+ telemetry_emitter: "v1-shared",
727
+ telemetry_surface: "v1",
728
+ }),
661
729
  });
662
730
  });
663
731
 
@@ -7,7 +7,12 @@ import {
7
7
  lambdaClient,
8
8
  parseAndWarnTelemetryId,
9
9
  } from "./lambda-client";
10
- import { computeSamplingMeta, TELEMETRY_EMITTER_V1 } from "./sampling";
10
+ import {
11
+ computeSamplingMeta,
12
+ TELEMETRY_EMITTER_V1,
13
+ TELEMETRY_SURFACE_V1,
14
+ UNSAMPLED_RATE,
15
+ } from "./sampling";
11
16
  import { isTelemetryDisabled } from "./telemetry-disabled";
12
17
 
13
18
  export { isTelemetryDisabled };
@@ -45,16 +50,18 @@ export class TelemetryClient {
45
50
  // the Lambda transport.
46
51
  private telemetryId: string | null = null;
47
52
  // License-derived identity used only as sampling authority. A standalone
48
- // telemetry id remains a transport claim and does not bypass sampleRate.
53
+ // telemetry id remains a transport claim and does not bypass the
54
+ // Segment gate.
49
55
  private licenseTelemetryId: string | null = null;
50
56
  packageName: string;
51
57
  packageVersion: string;
52
58
  private telemetryDisabled: boolean = false;
53
- // Client-side sampling rate for anonymous events. Identified events
54
- // (those whose license token yielded a telemetry_id) bypass the gate
55
- // entirely. Applied uniformly to both the lambda sink and Segment —
56
- // one dice roll per capture, both sinks see the same decision.
57
- private sampleRate: number = 0.05;
59
+ // Client-side sampling rate for anonymous events on the Segment wire
60
+ // only. The lambda sink is ours and takes every event; Segment is
61
+ // billed per event, so the anonymous population stays capped there.
62
+ // Identified events (those whose license token yielded a telemetry_id)
63
+ // bypass the gate on both wires.
64
+ private segmentSampleRate: number = 0.05;
58
65
  private anonymousId = `anon_${uuidv4()}`;
59
66
 
60
67
  constructor({
@@ -68,6 +75,8 @@ export class TelemetryClient {
68
75
  packageVersion: string;
69
76
  telemetryDisabled?: boolean;
70
77
  telemetryBaseUrl?: string;
78
+ // Anonymous sampling rate for the Segment wire. Named without the
79
+ // prefix for backward compatibility; the lambda sink ignores it.
71
80
  sampleRate?: number;
72
81
  }) {
73
82
  this.packageName = packageName;
@@ -95,9 +104,9 @@ export class TelemetryClient {
95
104
  });
96
105
  }
97
106
 
98
- private shouldSendEvent() {
107
+ private shouldSendToSegment() {
99
108
  const randomNumber = Math.random();
100
- return randomNumber < this.sampleRate;
109
+ return randomNumber < this.segmentSampleRate;
101
110
  }
102
111
 
103
112
  async capture<K extends keyof AnalyticsEvents>(
@@ -120,19 +129,29 @@ export class TelemetryClient {
120
129
  return;
121
130
  }
122
131
 
123
- // Callers without license-derived sampling authority are gated by
124
- // sampleRate. Legacy license tokens with telemetry_id always send —
125
- // the volume is bounded by paying-customer count and full fidelity
126
- // per identified customer is worth the marginal cost.
127
- if (!identity.licenseTelemetryId && !this.shouldSendEvent()) {
128
- return;
129
- }
132
+ // The two wires are gated separately. The lambda sink takes every
133
+ // event: it is CopilotKit-owned, so anonymous volume costs nothing
134
+ // per event there and a real count beats one extrapolated from 5% of
135
+ // the population. Segment is billed per event, so it keeps the gate —
136
+ // callers without license-derived sampling authority are dropped from
137
+ // that copy at segmentSampleRate. Legacy license tokens with a
138
+ // telemetry_id send on both wires: the volume is bounded by
139
+ // paying-customer count and full fidelity per identified customer is
140
+ // worth the marginal cost.
141
+ const sendToSegment =
142
+ Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
130
143
 
131
144
  // Sampling metadata is computed in ./sampling so this client and the
132
145
  // v2 runtime client can't drift apart again — see the note there.
133
- const samplingMeta = computeSamplingMeta({
146
+ // One call per wire, because the two wires were gated differently and
147
+ // a shared block would overweight the lambda copy by 1/sampleRate.
148
+ const lambdaSamplingMeta = computeSamplingMeta({
149
+ telemetryId: identity.licenseTelemetryId,
150
+ sampleRate: UNSAMPLED_RATE,
151
+ });
152
+ const segmentSamplingMeta = computeSamplingMeta({
134
153
  telemetryId: identity.licenseTelemetryId,
135
- sampleRate: this.sampleRate,
154
+ sampleRate: this.segmentSampleRate,
136
155
  });
137
156
 
138
157
  // Everything below travels identically on both copies of this event.
@@ -142,8 +161,8 @@ export class TelemetryClient {
142
161
  // duplication from $lib or from which fields happen to be present
143
162
  // (OSS-1019).
144
163
  const eventMeta = {
145
- ...samplingMeta,
146
164
  telemetry_emitter: TELEMETRY_EMITTER_V1,
165
+ telemetry_surface: TELEMETRY_SURFACE_V1,
147
166
  telemetry_event_id: uuidv4(),
148
167
  };
149
168
 
@@ -151,6 +170,7 @@ export class TelemetryClient {
151
170
  const propertiesWithGlobal: Record<string, any> = {
152
171
  ...this.globalProperties,
153
172
  ...eventMeta,
173
+ ...segmentSamplingMeta,
154
174
  telemetry_transport: "segment",
155
175
  ...flattenedProperties,
156
176
  };
@@ -170,6 +190,7 @@ export class TelemetryClient {
170
190
  globalProperties: {
171
191
  ...this.globalProperties,
172
192
  ...eventMeta,
193
+ ...lambdaSamplingMeta,
173
194
  telemetry_transport: "lambda",
174
195
  },
175
196
  packageName: this.packageName,
@@ -178,7 +199,7 @@ export class TelemetryClient {
178
199
  licenseToken: identity.licenseToken ?? undefined,
179
200
  });
180
201
 
181
- if (this.segment) {
202
+ if (this.segment && sendToSegment) {
182
203
  this.segment.track({
183
204
  anonymousId: this.anonymousId,
184
205
  event,
@@ -238,7 +259,7 @@ export class TelemetryClient {
238
259
  * Create an immutable capture scope for one runtime.
239
260
  *
240
261
  * The scope shares this client's sinks, process-wide opt-out, global
241
- * properties, and sampling settings, but snapshots transport identity and
262
+ * properties, and Segment sampling rate, but snapshots transport identity and
242
263
  * license-derived sampling authority. Constructing another runtime cannot
243
264
  * rewrite an existing scope.
244
265
  *
@@ -290,16 +311,16 @@ export class TelemetryClient {
290
311
 
291
312
  // Number.isNaN guards against parseFloat("nonsense") slipping past the
292
313
  // range check (all NaN comparisons are false), which would silently
293
- // drop every anonymous event with no signal — especially important
294
- // since the default is now 0.05, making env-var overrides more common.
314
+ // drop every anonymous Segment event with no signal.
295
315
  if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {
296
316
  throw new Error("Sample rate must be between 0 and 1");
297
317
  }
298
318
 
299
- this.sampleRate = _sampleRate;
319
+ this.segmentSampleRate = _sampleRate;
300
320
  // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/
301
- // sampleWeight) is computed per capture() in ./sampling. Only license-
302
- // authorized events get effectiveSampleRate=1; standalone transport
303
- // identity stays in the sampled population.
321
+ // sampleWeight) is computed per capture() in ./sampling, once per
322
+ // wire. Only license-authorized events get effectiveSampleRate=1 on
323
+ // the Segment copy; standalone transport identity stays in the
324
+ // sampled population there. The lambda copy is always unsampled.
304
325
  }
305
326
  }