mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Pipe statsig events to logger (#7141)

* Pipe statsig events to logger

* Log rich objects to bitdrift

* Fix tests

* Consolidate mocks, fix tests

* Reduce log trash on native

authored by danabra.mov and committed by

GitHub 32611391 07b72506

+46 -26
+16
jest/jestSetup.js
··· 105 105 return () => null 106 106 }), 107 107 })) 108 + 109 + jest.mock('expo-localization', () => ({ 110 + getLocales: () => [], 111 + })) 112 + 113 + jest.mock('statsig-react-native-expo', () => ({ 114 + Statsig: { 115 + initialize() {}, 116 + initializeCalled() { 117 + return false 118 + }, 119 + }, 120 + })) 121 + 122 + jest.mock('../src/lib/bitdrift', () => ({})) 123 + jest.mock('../src/lib/statsig/statsig', () => ({}))
+1
src/lib/bitdrift.ts
··· 1 1 import {init} from '@bitdrift/react-native' 2 2 import {Statsig} from 'statsig-react-native-expo' 3 + export {debug, error, info, warn} from '@bitdrift/react-native' 3 4 4 5 import {initPromise} from './statsig/statsig' 5 6
+4
src/lib/bitdrift.web.ts
··· 1 + export function debug() {} 2 + export function error() {} 3 + export function info() {} 4 + export function warn() {}
+23 -3
src/lib/statsig/statsig.tsx
··· 5 5 import {Statsig, StatsigProvider} from 'statsig-react-native-expo' 6 6 7 7 import {BUNDLE_DATE, BUNDLE_IDENTIFIER, IS_TESTFLIGHT} from '#/lib/app-info' 8 + import * as bitdrift from '#/lib/bitdrift' 8 9 import {logger} from '#/logger' 9 10 import {isWeb} from '#/platform/detection' 10 11 import * as persisted from '#/state/persisted' ··· 97 98 rawMetadata: LogEvents[E] & FlatJSONRecord, 98 99 ) { 99 100 try { 100 - const fullMetadata = { 101 - ...rawMetadata, 102 - } as Record<string, string> // Statsig typings are unnecessarily strict here. 101 + const fullMetadata = toStringRecord(rawMetadata) 103 102 fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)' 104 103 if (Statsig.initializeCalled()) { 105 104 Statsig.logEvent(eventName, null, fullMetadata) 106 105 } 106 + // Intentionally bypass the logger abstraction to log rich objects. 107 + console.groupCollapsed(eventName) 108 + console.log(fullMetadata) 109 + console.groupEnd() 110 + bitdrift.info(eventName, fullMetadata) 107 111 } catch (e) { 108 112 // A log should never interrupt the calling code, whatever happens. 109 113 logger.error('Failed to log an event', {message: e}) 110 114 } 115 + } 116 + 117 + function toStringRecord<E extends keyof LogEvents>( 118 + metadata: LogEvents[E] & FlatJSONRecord, 119 + ): Record<string, string> { 120 + const record: Record<string, string> = {} 121 + for (let key in metadata) { 122 + if (metadata.hasOwnProperty(key)) { 123 + if (typeof metadata[key] === 'string') { 124 + record[key] = metadata[key] 125 + } else { 126 + record[key] = JSON.stringify(metadata[key]) 127 + } 128 + } 129 + } 130 + return record 111 131 } 112 132 113 133 // We roll our own cache in front of Statsig because it is a singleton
+2 -3
src/logger/bitdriftTransport.ts
··· 3 3 error as bdError, 4 4 info as bdInfo, 5 5 warn as bdWarn, 6 - } from '@bitdrift/react-native' 7 - 6 + } from '../lib/bitdrift' 8 7 import {LogLevel, Transport} from './types' 9 8 10 9 export function createBitdriftTransport(): Transport { ··· 18 17 19 18 return (level, message) => { 20 19 const log = logFunctions[level] 21 - log(message.toString()) 20 + log('' + message) 22 21 } 23 22 }
-7
src/logger/bitdriftTransport.web.ts
··· 1 - import {Transport} from './index' 2 - 3 - export function createBitdriftTransport(): Transport { 4 - return (_level, _message) => { 5 - // noop 6 - } 7 - }
-13
src/state/session/__tests__/session-test.ts
··· 4 4 import {agentToSessionAccountOrThrow} from '../agent' 5 5 import {Action, getInitialState, reducer, State} from '../reducer' 6 6 7 - jest.mock('statsig-react-native-expo', () => ({ 8 - Statsig: { 9 - initialize() {}, 10 - initializeCalled() { 11 - return false 12 - }, 13 - }, 14 - })) 15 - 16 7 jest.mock('jwt-decode', () => ({ 17 8 jwtDecode(_token: string) { 18 9 return {} 19 10 }, 20 - })) 21 - 22 - jest.mock('expo-localization', () => ({ 23 - getLocales: () => [], 24 11 })) 25 12 26 13 describe('session', () => {