Skip to content

Commit 81fd553

Browse files
committed
add debug
1 parent e107a98 commit 81fd553

File tree

7 files changed

+89
-76
lines changed

7 files changed

+89
-76
lines changed

packages/signals/signals/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ You can *turn off debugging* by doing:
7777
https://my-website.com?segment_signals_debug=false
7878
```
7979

80+
* This also logs all signals to the js console.
81+
82+
#### Alternative method of enabling debug mode
83+
In your JS console:
84+
```js
85+
SegmentSignalsPlugin.debug()
86+
```
87+
8088
### Advanced
8189

8290
#### Listening to signals
@@ -109,3 +117,5 @@ Network signals emit when an HTTP Request is made, or an HTTP Response is receiv
109117
- First party domain (e.g if on `foo.com`, then `foo.com/api/products`, but not `bar.com/api/products`)
110118
- Contains the content-type: `application/json`
111119

120+
121+

packages/signals/signals/src/core/debug-mode/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ export const parseDebugModeQueryString = (): boolean | undefined => {
1414
return undefined
1515
}
1616

17-
export const parseDebugLoggingQueryString = (): boolean | undefined => {
17+
/**
18+
* This turns on advanced logging for signals!
19+
*/
20+
export const parseSignalsLoggingAdvancedQueryString = ():
21+
| boolean
22+
| undefined => {
1823
const queryParams = new URLSearchParams(window.location.search)
1924

2025
const val =
21-
queryParams.get('segment_signals_logging') ||
22-
queryParams.get('seg_signals_logging')
26+
queryParams.get('segment_signals_logging_advanced') ||
27+
queryParams.get('seg_signals_logging_advanced')
2328
if (val === 'true' || val === 'false') {
2429
return val === 'true'
2530
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ export interface EmitSignal {
66
emit: (signal: Signal) => void
77
}
88

9+
interface SignalEmitterSettings {
10+
shouldLogSignals: () => boolean
11+
}
12+
913
export class SignalEmitter implements EmitSignal {
1014
private emitter = new Emitter<{ add: [Signal] }>()
1115
private listeners = new Set<(signal: Signal) => void>()
12-
16+
private settings?: SignalEmitterSettings
17+
constructor(settings?: SignalEmitterSettings) {
18+
this.settings = settings
19+
}
1320
emit(signal: Signal) {
14-
logger.debug('New signal:', signal.type, signal.data)
21+
if (this.settings?.shouldLogSignals()) {
22+
logger.log('New signal:', signal.type, signal.data)
23+
}
1524
this.emitter.emit('add', signal)
1625
}
1726

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

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SignalsIngestSettingsConfig } from '../client'
55
import { SandboxSettingsConfig } from '../processor/sandbox'
66
import { NetworkSettingsConfig } from '../signal-generators/network-gen'
77
import { SignalsPluginSettingsConfig } from '../../types'
8+
import { DebugStorage } from '../../lib/storage/debug-storage'
89

910
export type SignalsSettingsConfig = Pick<
1011
SignalsPluginSettingsConfig,
@@ -111,17 +112,24 @@ export class SignalGlobalSettings {
111112
}
112113

113114
export class SignalsDebugSettings {
114-
private storageType = 'sessionStorage' as const
115115
private static redactionKey = 'segment_signals_debug_redaction_disabled'
116116
private static ingestionKey = 'segment_signals_debug_ingestion_enabled'
117-
private static logSignals = 'segment_signals_debug_log_signals_enabled'
117+
private static logSignals = 'segment_signals_log_signals_enabled'
118+
storage: DebugStorage
118119

119120
constructor(disableRedaction?: boolean, enableIngestion?: boolean) {
121+
this.storage = new DebugStorage('sessionStorage')
120122
if (typeof disableRedaction === 'boolean') {
121-
this.setDebugKey(SignalsDebugSettings.redactionKey, disableRedaction)
123+
this.storage.setDebugKey(
124+
SignalsDebugSettings.redactionKey,
125+
disableRedaction
126+
)
122127
}
123128
if (typeof enableIngestion === 'boolean') {
124-
this.setDebugKey(SignalsDebugSettings.ingestionKey, enableIngestion)
129+
this.storage.setDebugKey(
130+
SignalsDebugSettings.ingestionKey,
131+
enableIngestion
132+
)
125133
}
126134

127135
const debugModeInQs = parseDebugModeQueryString()
@@ -132,46 +140,20 @@ export class SignalsDebugSettings {
132140
}
133141

134142
setAllDebugging = (boolean: boolean) => {
135-
this.setDebugKey(SignalsDebugSettings.redactionKey, boolean)
136-
this.setDebugKey(SignalsDebugSettings.ingestionKey, boolean)
137-
this.setDebugKey(SignalsDebugSettings.logSignals, boolean)
138-
}
139-
140-
private setDebugKey = (key: string, enable: boolean): void => {
141-
try {
142-
if (enable) {
143-
window[this.storageType].setItem(key, 'true')
144-
} else {
145-
logger.debug(`Removing debug key ${key} from storage`)
146-
window.sessionStorage.removeItem(key)
147-
}
148-
} catch (e) {
149-
logger.debug('Storage error', e)
150-
}
151-
}
152-
153-
private getDebugKey = (key: string): boolean => {
154-
try {
155-
const isEnabled = Boolean(window[this.storageType].getItem(key))
156-
if (isEnabled) {
157-
logger.debug(`${key}=true (app. storage)`)
158-
return true
159-
}
160-
} catch (e) {
161-
logger.debug('Storage error', e)
162-
}
163-
return false
143+
this.storage.setDebugKey(SignalsDebugSettings.redactionKey, boolean)
144+
this.storage.setDebugKey(SignalsDebugSettings.ingestionKey, boolean)
145+
this.storage.setDebugKey(SignalsDebugSettings.logSignals, boolean)
164146
}
165147

166148
getDisableSignalsRedaction = (): boolean => {
167-
return this.getDebugKey(SignalsDebugSettings.redactionKey)
149+
return this.storage.getDebugKey(SignalsDebugSettings.redactionKey)
168150
}
169151

170152
getEnableSignalsIngestion = (): boolean => {
171-
return this.getDebugKey(SignalsDebugSettings.ingestionKey)
153+
return this.storage.getDebugKey(SignalsDebugSettings.ingestionKey)
172154
}
173155

174156
getEnableLogSignals = (): boolean => {
175-
return this.getDebugKey(SignalsDebugSettings.logSignals)
157+
return this.storage.getDebugKey(SignalsDebugSettings.logSignals)
176158
}
177159
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export class Signals implements ISignals {
3838
private globalSettings: SignalGlobalSettings
3939
constructor(settingsConfig: SignalsSettingsConfig = {}) {
4040
this.globalSettings = new SignalGlobalSettings(settingsConfig)
41-
this.signalEmitter = new SignalEmitter()
41+
this.signalEmitter = new SignalEmitter({
42+
shouldLogSignals: () =>
43+
this.globalSettings.signalsDebug.getEnableLogSignals(),
44+
})
4245
this.signalsClient = new SignalsIngestClient(
4346
this.globalSettings.ingestClient
4447
)
@@ -129,16 +132,11 @@ export class Signals implements ISignals {
129132
void this.buffer.clear()
130133
}
131134

132-
// create a reference so we prevent duplicate subscriptions
133-
private logSignal = (signal: Signal) => {
134-
logger.log(signal.type, signal.data, signal.metadata)
135-
}
136135
/**
137136
* Disable redaction, ingestion of signals, and other debug logging.
138137
*/
139138
debug(boolean = true): void {
140139
this.globalSettings.signalsDebug.setAllDebugging(boolean)
141-
this.signalEmitter.subscribe(this.logSignal)
142140
}
143141

144142
/**

packages/signals/signals/src/lib/logger/index.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,28 @@
1-
import { parseDebugLoggingQueryString } from '../../core/debug-mode'
1+
import { parseSignalsLoggingAdvancedQueryString } from '../../core/debug-mode'
2+
import { DebugStorage } from '../storage/debug-storage'
23

34
class Logger {
45
private storageType = 'sessionStorage' as const
5-
private static loggingKey = 'segment_signals_debug_logging'
6+
private static advancedLogging = 'segment_signals_logging_advanced'
67

8+
storage = new DebugStorage(this.storageType)
79
constructor() {
8-
const val = parseDebugLoggingQueryString()
10+
const val = parseSignalsLoggingAdvancedQueryString()
911
if (typeof val === 'boolean') {
10-
this.setDebugKey(Logger.loggingKey, val)
12+
this.storage.setDebugKey(Logger.advancedLogging, val)
1113
}
1214
}
1315

1416
private debugLoggingEnabled = (): boolean => {
15-
try {
16-
const isEnabled = Boolean(
17-
globalThis[this.storageType].getItem(Logger.loggingKey)
18-
)
19-
if (isEnabled) {
20-
return true
21-
}
22-
} catch (e) {
23-
console.warn('Storage error', e)
24-
}
25-
return false
26-
}
27-
28-
private setDebugKey = (key: string, enable: boolean) => {
29-
try {
30-
if (enable) {
31-
globalThis[this.storageType].setItem(key, 'true')
32-
} else {
33-
globalThis[this.storageType].removeItem(key)
34-
}
35-
} catch (e) {
36-
console.warn('Storage error', e)
37-
}
17+
return this.storage.getDebugKey(Logger.advancedLogging)
3818
}
3919

4020
enableDebugLogging = (bool = true) => {
41-
this.setDebugKey(Logger.loggingKey, bool)
21+
this.storage.setDebugKey(Logger.advancedLogging, bool)
4222
}
4323

4424
log = (...args: any[]): void => {
45-
console.log('[signals]', ...args)
25+
console.log('[signals log]', ...args)
4626
}
4727

4828
debug = (...args: any[]): void => {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export class DebugStorage {
2+
private storageType: 'localStorage' | 'sessionStorage'
3+
constructor(type: 'localStorage' | 'sessionStorage') {
4+
this.storageType = type
5+
}
6+
public setDebugKey = (key: string, enable: boolean): void => {
7+
try {
8+
if (enable) {
9+
window[this.storageType].setItem(key, 'true')
10+
} else {
11+
window.sessionStorage.removeItem(key)
12+
}
13+
} catch (e) {
14+
console.warn('Storage error', e)
15+
}
16+
}
17+
18+
public getDebugKey = (key: string): boolean => {
19+
try {
20+
const isEnabled = Boolean(window[this.storageType].getItem(key))
21+
if (isEnabled) {
22+
return true
23+
}
24+
} catch (e) {
25+
console.warn('Storage error', e)
26+
}
27+
return false
28+
}
29+
}

0 commit comments

Comments
 (0)