Skip to content

Commit

Permalink
Pipe statsig events to logger (#7141)
Browse files Browse the repository at this point in the history
* Pipe statsig events to logger

* Log rich objects to bitdrift

* Fix tests

* Consolidate mocks, fix tests

* Reduce log trash on native
  • Loading branch information
gaearon authored Dec 17, 2024
1 parent 07b7250 commit 3261139
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
16 changes: 16 additions & 0 deletions jest/jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ jest.mock('expo-modules-core', () => ({
return () => null
}),
}))

jest.mock('expo-localization', () => ({
getLocales: () => [],
}))

jest.mock('statsig-react-native-expo', () => ({
Statsig: {
initialize() {},
initializeCalled() {
return false
},
},
}))

jest.mock('../src/lib/bitdrift', () => ({}))
jest.mock('../src/lib/statsig/statsig', () => ({}))
1 change: 1 addition & 0 deletions src/lib/bitdrift.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {init} from '@bitdrift/react-native'
import {Statsig} from 'statsig-react-native-expo'
export {debug, error, info, warn} from '@bitdrift/react-native'

import {initPromise} from './statsig/statsig'

Expand Down
4 changes: 4 additions & 0 deletions src/lib/bitdrift.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function debug() {}
export function error() {}
export function info() {}
export function warn() {}
26 changes: 23 additions & 3 deletions src/lib/statsig/statsig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {sha256} from 'js-sha256'
import {Statsig, StatsigProvider} from 'statsig-react-native-expo'

import {BUNDLE_DATE, BUNDLE_IDENTIFIER, IS_TESTFLIGHT} from '#/lib/app-info'
import * as bitdrift from '#/lib/bitdrift'
import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted'
Expand Down Expand Up @@ -97,19 +98,38 @@ export function logEvent<E extends keyof LogEvents>(
rawMetadata: LogEvents[E] & FlatJSONRecord,
) {
try {
const fullMetadata = {
...rawMetadata,
} as Record<string, string> // Statsig typings are unnecessarily strict here.
const fullMetadata = toStringRecord(rawMetadata)
fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)'
if (Statsig.initializeCalled()) {
Statsig.logEvent(eventName, null, fullMetadata)
}
// Intentionally bypass the logger abstraction to log rich objects.
console.groupCollapsed(eventName)
console.log(fullMetadata)
console.groupEnd()
bitdrift.info(eventName, fullMetadata)
} catch (e) {
// A log should never interrupt the calling code, whatever happens.
logger.error('Failed to log an event', {message: e})
}
}

function toStringRecord<E extends keyof LogEvents>(
metadata: LogEvents[E] & FlatJSONRecord,
): Record<string, string> {
const record: Record<string, string> = {}
for (let key in metadata) {
if (metadata.hasOwnProperty(key)) {
if (typeof metadata[key] === 'string') {
record[key] = metadata[key]
} else {
record[key] = JSON.stringify(metadata[key])
}
}
}
return record
}

// We roll our own cache in front of Statsig because it is a singleton
// and it's been difficult to get it to behave in a predictable way.
// Our own cache ensures consistent evaluation within a single session.
Expand Down
5 changes: 2 additions & 3 deletions src/logger/bitdriftTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
error as bdError,
info as bdInfo,
warn as bdWarn,
} from '@bitdrift/react-native'

} from '../lib/bitdrift'
import {LogLevel, Transport} from './types'

export function createBitdriftTransport(): Transport {
Expand All @@ -18,6 +17,6 @@ export function createBitdriftTransport(): Transport {

return (level, message) => {
const log = logFunctions[level]
log(message.toString())
log('' + message)
}
}
7 changes: 0 additions & 7 deletions src/logger/bitdriftTransport.web.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/state/session/__tests__/session-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@ import {describe, expect, it, jest} from '@jest/globals'
import {agentToSessionAccountOrThrow} from '../agent'
import {Action, getInitialState, reducer, State} from '../reducer'

jest.mock('statsig-react-native-expo', () => ({
Statsig: {
initialize() {},
initializeCalled() {
return false
},
},
}))

jest.mock('jwt-decode', () => ({
jwtDecode(_token: string) {
return {}
},
}))

jest.mock('expo-localization', () => ({
getLocales: () => [],
}))

describe('session', () => {
it('can log in and out', () => {
let state = getInitialState([])
Expand Down

0 comments on commit 3261139

Please sign in to comment.