Skip to content

(WIP) Log Tracing #4827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: v7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ protected void getSentryAndroidOptions(
if (rnOptions.hasKey("enableNdk")) {
options.setEnableNdk(rnOptions.getBoolean("enableNdk"));
}
if (rnOptions.hasKey("_experiments")) {
var experiments = rnOptions.getMap("_experiments");
if (experiments.hasKey(("enableLogs"))) {
options.getLogs().setEnabled(experiments.getBoolean("enableLogs"));
}
}
if (rnOptions.hasKey("spotlight")) {
if (rnOptions.getType("spotlight") == ReadableType.Boolean) {
options.setEnableSpotlight(rnOptions.getBoolean("spotlight"));
Expand Down
14 changes: 7 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@
},
"dependencies": {
"@sentry/babel-plugin-component-annotate": "3.4.0",
"@sentry/browser": "9.12.0",
"@sentry/browser": "9.18.0",
"@sentry/cli": "2.43.0",
"@sentry/core": "9.12.0",
"@sentry/react": "9.12.0",
"@sentry/types": "9.12.0"
"@sentry/core": "9.18.0",
"@sentry/react": "9.18.0",
"@sentry/types": "9.18.0"
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@expo/metro-config": "0.19.5",
"@mswjs/interceptors": "^0.25.15",
"@react-native/babel-preset": "0.77.1",
"@sentry-internal/eslint-config-sdk": "9.12.0",
"@sentry-internal/eslint-plugin-sdk": "9.12.0",
"@sentry-internal/typescript": "9.12.0",
"@sentry-internal/eslint-config-sdk": "9.18.0",
"@sentry-internal/eslint-plugin-sdk": "9.18.0",
"@sentry-internal/typescript": "9.18.0",
"@sentry/wizard": "4.9.0",
"@testing-library/react-native": "^12.7.2",
"@types/jest": "^29.5.13",
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
UserFeedback,
} from '@sentry/core';
import {
_INTERNAL_flushLogsBuffer,
addAutoIpAddressToSession,
addAutoIpAddressToUser,
BaseClient,
Expand All @@ -32,6 +33,8 @@ import { mergeOutcomes } from './utils/outcome';
import { ReactNativeLibraries } from './utils/rnlibraries';
import { NATIVE } from './wrapper';

const DEFAULT_FLUSH_INTERVAL = 5000;

/**
* The Sentry React Native SDK Client.
*
Expand All @@ -40,6 +43,7 @@ import { NATIVE } from './wrapper';
*/
export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
private _outcomesBuffer: Outcome[];
private _logFlushIdleTimeout: ReturnType<typeof setTimeout> | undefined;

/**
* Creates a new React Native SDK instance.
Expand All @@ -60,6 +64,22 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
this.on('postprocessEvent', addAutoIpAddressToUser);
this.on('beforeSendSession', addAutoIpAddressToSession);
}

if (options._experiments?.enableLogs) {
this.on('flush', () => {
_INTERNAL_flushLogsBuffer(this);
});

this.on('afterCaptureLog', () => {
if (this._logFlushIdleTimeout) {
clearTimeout(this._logFlushIdleTimeout);
}

this._logFlushIdleTimeout = setTimeout(() => {
_INTERNAL_flushLogsBuffer(this);
}, DEFAULT_FLUSH_INTERVAL);
});
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/js/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { Platform } from 'react-native';
import type { TouchEventBoundaryProps } from './touchevents';
import { getExpoConstants } from './utils/expomodules';

import type { BrowserOptions } from '@sentry/react';

type ProfilerProps = React.ComponentProps<typeof Profiler>;
type BrowserTransportOptions = Parameters<typeof makeFetchTransport>[0];

type BrowserExperiments = NonNullable<BrowserOptions['_experiments']>;
type SharedExperimentsSubset = Pick<BrowserExperiments, 'enableLogs' | 'beforeSendLog'>;

export interface BaseReactNativeOptions {
/**
* Enables native transport + device info + offline caching.
Expand Down Expand Up @@ -245,7 +250,7 @@ export interface BaseReactNativeOptions {
/**
* Options which are in beta, or otherwise not guaranteed to be stable.
*/
_experiments?: {
_experiments?: SharedExperimentsSubset & {
[key: string]: unknown;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const NATIVE: SentryNativeWrapper = {
typeof itemHeader.content_type === 'string' ? itemHeader.content_type : 'application/octet-stream';
bytesPayload = itemPayload;
} else {
bytesContentType = 'application/json';
bytesContentType = 'application/vnd.sentry.items.log+json';
bytesPayload = utf8ToBytes(JSON.stringify(itemPayload));
if (!hardCrashed) {
hardCrashed = isHardCrash(itemPayload);
Expand Down
2 changes: 1 addition & 1 deletion samples/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@react-navigation/native": "^7.0.14",
"@react-navigation/native-stack": "^7.2.0",
"@react-navigation/stack": "^7.1.1",
"@sentry/core": "9.12.0",
"@sentry/core": "9.18.0",
"@sentry/react-native": "7.0.0-alpha.0",
"@shopify/flash-list": "^1.7.3",
"delay": "^6.0.0",
Expand Down
10 changes: 8 additions & 2 deletions samples/react-native/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Sentry.init({
didCallNativeInit,
);
},
_experiments: {
enableLogs: true,
beforeSendLog: (log) => {
return log;
},
},
enableUserInteractionTracing: true,
integrations(integrations) {
integrations.push(
Expand Down Expand Up @@ -149,8 +155,8 @@ Sentry.init({
// release: '[email protected]+1',
// dist: `1`,
profilesSampleRate: 1.0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 1.0,
replaysSessionSampleRate: 0.0,
replaysOnErrorSampleRate: 0.0,
spotlight: true,
// This should be disabled when manually initializing the native SDK
// Note that options from JS are not passed to the native SDKs when initialized manually
Expand Down
13 changes: 13 additions & 0 deletions samples/react-native/src/Screens/ErrorsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { FallbackRender } from '@sentry/react';
import NativeSampleModule from '../../tm/NativeSampleModule';
import NativePlatformSampleModule from '../../tm/NativePlatformSampleModule';
import { TimeToFullDisplay } from '../utils';
import { logger } from '@sentry/browser';

const { AssetsModule, CppModule, CrashModule } = NativeModules;

Expand Down Expand Up @@ -149,6 +150,18 @@ const ErrorsScreen = (_props: Props) => {
}
}}
/>
<Button
title="Log console"
onPress={() => {
logger.info('info log');
logger.trace('trace log');
logger.debug('debug log');
logger.warn('warn log');
logger.error('error log');

logger.info('info log with data', { database: 'admin', number: 123, obj: { password: 'admin'} });
}}
/>
{Platform.OS === 'android' && (
<>
<Button
Expand Down
Loading
Loading