Skip to content

Commit

Permalink
refactor debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljackins committed Oct 11, 2024
1 parent 705aa7e commit b8e7ef7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class BasePage {
await this.page.evaluate(
({ signalSettings }) => {
window.signalsPlugin = new window.SignalsPlugin({
disableSignalsRedaction: true,
enableSignalsDebug: true,
flushInterval: 1000,
...signalSettings,
})
Expand Down
97 changes: 18 additions & 79 deletions packages/signals/signals/src/core/signals/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export type SignalsSettingsConfig = Pick<
| 'functionHost'
| 'flushAt'
| 'flushInterval'
| 'disableSignalsRedaction'
| 'signalsIngestion'
| 'sampleRate'
| 'enableSignalsDebug'
| 'networkSignalsAllowList'
| 'networkSignalsDisallowList'
| 'networkSignalsAllowSameDomain'
Expand All @@ -34,10 +32,9 @@ export class SignalGlobalSettings {
signalBuffer: SignalBufferSettingsConfig
ingestClient: SignalsIngestSettingsConfig
network: NetworkSettingsConfig
private sampleRate = 0

private redaction = new SignalRedactionSettings()
private ingestion = new SignalIngestionSettings()
private sampleRate = 0
private signalsDebug = new SignalsDebugSettings()

constructor(settings: SignalsSettingsConfig) {
if (settings.maxBufferSize && settings.signalStorage) {
Expand All @@ -46,9 +43,7 @@ export class SignalGlobalSettings {
)
}

this.redaction = new SignalRedactionSettings(
settings.disableSignalsRedaction
)
this.signalsDebug = new SignalsDebugSettings(settings.enableSignalsDebug)

this.signalBuffer = {
signalStorage: settings.signalStorage,
Expand All @@ -58,9 +53,9 @@ export class SignalGlobalSettings {
apiHost: settings.apiHost,
flushAt: settings.flushAt,
flushInterval: settings.flushInterval,
shouldDisableSignalRedaction: this.redaction.getDisableSignalRedaction,
shouldDisableSignalRedaction: this.signalsDebug.getSignalsDebug,
shouldIngestSignals: () => {
if (this.ingestion.getSignalIngestion()) {
if (this.signalsDebug.getSignalsDebug()) {
return true
}
if (Math.random() > this.sampleRate) {
Expand Down Expand Up @@ -108,99 +103,43 @@ export class SignalGlobalSettings {
}
}

class SignalRedactionSettings {
private static redactionKey = 'segment_signals_debug_redaction_disabled'
class SignalsDebugSettings {
private static key = 'segment_signals_debug'
constructor(initialValue?: boolean) {
if (typeof initialValue === 'boolean') {
this.setDisableSignalRedaction(initialValue)
this.setSignalsDebug(initialValue)
}

// setting ?segment_signals_debug=true will disable redaction, and set a key in local storage
// setting ?segment_signals_debug=true will disable redaction, enable ingestion, and set keys in local storage
// this setting will persist across page loads (even if there is no query string)
// in order to clear the setting, user must set ?segment_signals_debug=false
const debugModeInQs = parseDebugModeQueryString()
logger.debug('debugMode is set to true via query string')
if (typeof debugModeInQs === 'boolean') {
this.setDisableSignalRedaction(debugModeInQs)
this.setSignalsDebug(debugModeInQs)
}
}

setDisableSignalRedaction(shouldDisable: boolean) {
setSignalsDebug(shouldDisable: boolean) {
try {
if (shouldDisable) {
window.sessionStorage.setItem(
SignalRedactionSettings.redactionKey,
'true'
)
window.sessionStorage.setItem(SignalsDebugSettings.key, 'true')
} else {
logger.debug('Removing redaction key from storage')
window.sessionStorage.removeItem(SignalRedactionSettings.redactionKey)
logger.debug('Removing debug key from storage')
window.sessionStorage.removeItem(SignalsDebugSettings.key)
}
} catch (e) {
logger.debug('Storage error', e)
}
}

getDisableSignalRedaction() {
getSignalsDebug() {
try {
const isDisabled = Boolean(
window.sessionStorage.getItem(SignalRedactionSettings.redactionKey)
window.sessionStorage.getItem(SignalsDebugSettings.key)
)
if (isDisabled) {
logger.debug(
`${SignalRedactionSettings.redactionKey}=true (app. storage)`
)
return true
}
} catch (e) {
logger.debug('Storage error', e)
}
return false
}
}

class SignalIngestionSettings {
private static ingestionKey = 'segment_signals_debug_ingestion_enabled'
constructor(initialValue?: boolean) {
if (typeof initialValue === 'boolean') {
this.setSignalIngestion(initialValue)
}

// setting ?segment_signals_debug=true will disable redaction, and set a key in local storage
// this setting will persist across page loads (even if there is no query string)
// in order to clear the setting, user must set ?segment_signals_debug=false
const debugModeInQs = parseDebugModeQueryString()
logger.debug('debugMode is set to true via query string')
if (typeof debugModeInQs === 'boolean') {
this.setSignalIngestion(debugModeInQs)
}
}

setSignalIngestion(shouldEnable: boolean) {
try {
if (shouldEnable) {
window.sessionStorage.setItem(
SignalIngestionSettings.ingestionKey,
'true'
)
} else {
logger.debug('Removing ingestion key from storage')
window.sessionStorage.removeItem(SignalIngestionSettings.ingestionKey)
}
} catch (e) {
logger.debug('Storage error', e)
}
}

getSignalIngestion() {
try {
const isEnabled = Boolean(
window.sessionStorage.getItem(SignalIngestionSettings.ingestionKey)
)
if (isEnabled) {
logger.debug(
`${SignalIngestionSettings.ingestionKey}=true (app. storage)`
)
logger.debug(`${SignalsDebugSettings.key}=true (app. storage)`)
return true
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/signals/signals/src/plugin/signals-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
logger.debug('SignalsPlugin initializing', { settings })

this.signals = new Signals({
disableSignalsRedaction: settings.disableSignalsRedaction,
enableSignalsDebug: settings.enableSignalsDebug,
flushAt: settings.flushAt,
flushInterval: settings.flushInterval,
functionHost: settings.functionHost,
Expand Down
4 changes: 2 additions & 2 deletions packages/signals/signals/src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export interface SignalsPluginSettingsConfig {
enableDebugLogging?: boolean

/**
* Disable redaction of signals
* Enables signals debug mode (turns off redaction, enables ingestion)
* @default false
*/
disableSignalsRedaction?: boolean
enableSignalsDebug?: boolean

/**
* If signals ingestion is enabled/disabled
Expand Down

0 comments on commit b8e7ef7

Please sign in to comment.