Refactor logging in feed-feedback, add debug logging to metrics client

This commit is contained in:
Eric Bailey
2026-01-21 14:36:25 -06:00
parent 2d25f1caee
commit eeb3e90b1a
3 changed files with 53 additions and 40 deletions
+3
View File
@@ -131,6 +131,8 @@ export function AnalyticsContext({
combinedMetadata.base.sessionId = sessionId
combinedMetadata.geolocation = geolocation
const context: AnalyticsBaseContextType = {
...parentContext,
// TODO trim down metadata
logger: createLogger(Logger.Context.Default, combinedMetadata),
metadata: combinedMetadata,
metric: (event, payload, extraMetadata) => {
@@ -196,6 +198,7 @@ export function useAnalyticsBase() {
export function useAnalytics() {
const ctx = useContext(Context)
if (!('feature' in ctx) || !('Features' in ctx)) {
console.log(ctx)
throw new Error(
'useAnalytics must be used within an AnalyticsFeaturesContext',
)
+11
View File
@@ -1,5 +1,6 @@
import {onAppStateChange} from '#/lib/appState'
import {isNetworkError} from '#/lib/strings/errors'
import {Logger} from '#/logger'
import {Sentry} from '#/logger/sentry/lib'
import * as env from '#/env'
@@ -13,6 +14,7 @@ type Event<M extends Record<string, any>> = {
}
const TRACKING_ENDPOINT = env.METRICS_API_HOST + '/t'
const logger = Logger.create(Logger.Context.Metric, {})
export class MetricsClient<M extends Record<string, any>> {
private started: boolean = false
@@ -49,6 +51,11 @@ export class MetricsClient<M extends Record<string, any>> {
metadata,
})
logger.debug(`event: ${event as string}`, {
payload,
metadata,
})
if (this.queue.length > 100) {
this.flush()
}
@@ -62,6 +69,10 @@ export class MetricsClient<M extends Record<string, any>> {
}
private async sendBatch(events: Event<M>[], isRetry: boolean = false) {
logger.debug(`sendBatch: ${events.length}`, {
isRetry,
})
try {
const body = JSON.stringify(events)
if (env.IS_WEB && 'navigator' in globalThis && navigator.sendBeacon) {