Skip to content

Commit 705aa7e

Browse files
committed
Update and move sampling logic
1 parent cf6480d commit 705aa7e

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

packages/signals/signals/src/core/client/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ export class SignalsIngestSettings {
88
flushInterval: number
99
apiHost: string
1010
shouldDisableSignalRedaction: () => boolean
11-
signalIngestion: () => boolean
11+
shouldIngestSignals: () => boolean
1212
writeKey?: string
13-
sampleRate: number
1413
constructor(settings: SignalsIngestSettingsConfig) {
1514
this.flushAt = settings.flushAt ?? 5
1615
this.apiHost = settings.apiHost ?? 'signals.segment.io/v1'
1716
this.flushInterval = settings.flushInterval ?? 2000
1817
this.shouldDisableSignalRedaction =
1918
settings.shouldDisableSignalRedaction ?? (() => false)
20-
this.signalIngestion = settings.signalIngestion ?? (() => false)
21-
this.sampleRate = settings.sampleRate ?? 0
19+
this.shouldIngestSignals = settings.shouldIngestSignals ?? (() => false)
2220
}
2321
}
2422

@@ -27,8 +25,7 @@ export interface SignalsIngestSettingsConfig {
2725
flushAt?: number
2826
flushInterval?: number
2927
shouldDisableSignalRedaction?: () => boolean
30-
signalIngestion?: () => boolean
31-
sampleRate?: number
28+
shouldIngestSignals?: () => boolean
3229
}
3330
/**
3431
* This currently just uses the Segment analytics-next library to send signals.
@@ -75,23 +72,11 @@ export class SignalsIngestClient {
7572
return analytics
7673
}
7774

78-
private shouldIngestSignals(): boolean {
79-
let shouldIngest = false
80-
if (Math.random() > this.settings.sampleRate) {
81-
// currently a no-op, but will be used once signals are sent outside of debug mode
82-
shouldIngest = false
83-
}
84-
if (this.settings.signalIngestion()) {
85-
shouldIngest = true
86-
}
87-
return shouldIngest
88-
}
89-
9075
private sendTrackCall(signal: Signal) {
9176
if (!this.analytics) {
9277
throw new Error('Please initialize before calling this method.')
9378
}
94-
if (!this.shouldIngestSignals()) {
79+
if (!this.settings.shouldIngestSignals()) {
9580
return
9681
}
9782
const disableRedaction = this.settings.shouldDisableSignalRedaction()

packages/signals/signals/src/core/signals/settings.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class SignalGlobalSettings {
3434
signalBuffer: SignalBufferSettingsConfig
3535
ingestClient: SignalsIngestSettingsConfig
3636
network: NetworkSettingsConfig
37+
private sampleRate = 0
3738

3839
private redaction = new SignalRedactionSettings()
3940
private ingestion = new SignalIngestionSettings()
@@ -58,8 +59,15 @@ export class SignalGlobalSettings {
5859
flushAt: settings.flushAt,
5960
flushInterval: settings.flushInterval,
6061
shouldDisableSignalRedaction: this.redaction.getDisableSignalRedaction,
61-
signalIngestion: this.ingestion.getSignalIngestion,
62-
sampleRate: settings.sampleRate,
62+
shouldIngestSignals: () => {
63+
if (this.ingestion.getSignalIngestion()) {
64+
return true
65+
}
66+
if (Math.random() > this.sampleRate) {
67+
return false
68+
}
69+
return false
70+
},
6371
}
6472
this.sandbox = {
6573
functionHost: settings.functionHost,
@@ -86,7 +94,7 @@ export class SignalGlobalSettings {
8694
*/
8795
disallowListURLs: (string | undefined)[]
8896
/**
89-
* The sample rate pulled from CDN settings
97+
* Sample rate to determine sending signals
9098
*/
9199
sampleRate: number
92100
}): void {
@@ -96,7 +104,7 @@ export class SignalGlobalSettings {
96104
Boolean(val)
97105
)
98106
)
99-
this.ingestClient.sampleRate = sampleRate
107+
this.sampleRate = sampleRate
100108
}
101109
}
102110

packages/signals/signals/src/core/signals/signals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class Signals implements ISignals {
9393
],
9494
sampleRate:
9595
analyticsService.instance.settings.cdnSettings
96-
.autoInstrumentationSettings?.sampleRate || 0,
96+
.autoInstrumentationSettings?.sampleRate ?? 0,
9797
})
9898

9999
const sandbox = new Sandbox(

0 commit comments

Comments
 (0)