Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

## Unreleased

## Features

- Logs now contains more attributes like release, os and device information ([#5032](https://github.com/getsentry/sentry-react-native/pull/5032))

### Dependencies

- Bump Android SDK from v8.17.0 to v8.18.0 ([#5034](https://github.com/getsentry/sentry-react-native/pull/5034))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,13 @@ private String readStringFromFile(File path) throws IOException {
}
}

public void fetchNativeLogAttributes(Promise promise) {
final @NotNull SentryOptions options = ScopesAdapter.getInstance().getOptions();
final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
final @Nullable IScope currentScope = InternalSentrySdk.getCurrentScope();
fetchNativeLogContexts(promise, options, context, currentScope);
}

public void fetchNativeDeviceContexts(Promise promise) {
final @NotNull SentryOptions options = ScopesAdapter.getInstance().getOptions();
final @Nullable Context context = this.getReactApplicationContext().getApplicationContext();
Expand Down Expand Up @@ -1025,6 +1032,47 @@ protected void fetchNativeDeviceContexts(
promise.resolve(deviceContext);
}

// Basically fetchNativeDeviceContexts but filtered to only get contexts info.
protected void fetchNativeLogContexts(
Promise promise,
final @NotNull SentryOptions options,
final @Nullable Context osContext,
final @Nullable IScope currentScope) {
if (!(options instanceof SentryAndroidOptions) || osContext == null) {
promise.resolve(null);
return;
}

Object contextsObj =
InternalSentrySdk.serializeScope(osContext, (SentryAndroidOptions) options, currentScope)
.get("contexts");

if (!(contextsObj instanceof Map)) {
promise.resolve(null);
return;
}

@SuppressWarnings("unchecked")
Map<String, Object> contextsMap = (Map<String, Object>) contextsObj;

Map<String, Object> contextItems = new HashMap<>();
if (contextsMap.containsKey("os")) {
contextItems.put("os", contextsMap.get("os"));
}

if (contextsMap.containsKey("device")) {
contextItems.put("device", contextsMap.get("device"));
}

contextItems.put("release", options.getRelease());

Map<String, Object> logContext = new HashMap<>();
logContext.put("contexts", contextItems);
Object filteredContext = RNSentryMapConverter.convertToWritable(logContext);

promise.resolve(filteredContext);
}

public void fetchNativeSdkInfo(Promise promise) {
final @Nullable SdkVersion sdkVersion =
ScopesAdapter.getInstance().getOptions().getSdkVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public void disableNativeFramesTracking() {
this.impl.disableNativeFramesTracking();
}

@Override
public void fetchNativeLogAttributes(Promise promise) {
this.impl.fetchNativeLogAttributes(promise);
}

@Override
public void fetchNativeDeviceContexts(Promise promise) {
this.impl.fetchNativeDeviceContexts(promise);
Expand Down
52 changes: 52 additions & 0 deletions packages/core/ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,58 @@ - (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAd
return [self fetchNativeStackFramesBy:instructionsAddr symbolicate:dladdr];
}

RCT_EXPORT_METHOD(fetchNativeLogAttributes
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject)
{
__block NSMutableDictionary<NSString *, id> *result = [NSMutableDictionary new];

[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) {
// Serialize to get contexts dictionary
NSDictionary *serializedScope = [scope serialize];
NSDictionary *allContexts = serializedScope[@"context"]; // It's singular here, annoyingly

NSMutableDictionary *contexts = [NSMutableDictionary new];

NSDictionary *device = allContexts[@"device"];
if ([device isKindOfClass:[NSDictionary class]]) {
contexts[@"device"] = device;
}

NSDictionary *os = allContexts[@"os"];
if ([os isKindOfClass:[NSDictionary class]]) {
contexts[@"os"] = os;
}

NSString *releaseName = [SentrySDK options].releaseName;
if (releaseName) {
contexts[@"release"] = releaseName;
}
// Merge extra context
NSDictionary *extraContext = [PrivateSentrySDKOnly getExtraContext];

if (extraContext) {
NSDictionary *extraDevice = extraContext[@"device"];
if ([extraDevice isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *mergedDevice =
[contexts[@"device"] mutableCopy] ?: [NSMutableDictionary new];
[mergedDevice addEntriesFromDictionary:extraDevice];
contexts[@"device"] = mergedDevice;
}

NSDictionary *extraOS = extraContext[@"os"];
if ([extraOS isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *mergedOS =
[contexts[@"os"] mutableCopy] ?: [NSMutableDictionary new];
[mergedOS addEntriesFromDictionary:extraOS];
contexts[@"os"] = mergedOS;
}
}
result[@"contexts"] = contexts;
}];
resolve(result);
}

RCT_EXPORT_METHOD(fetchNativeDeviceContexts
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/js/NativeRNSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Spec extends TurboModule {
fetchNativeRelease(): Promise<NativeReleaseResponse>;
fetchNativeSdkInfo(): Promise<Package | null>;
fetchNativeDeviceContexts(): Promise<NativeDeviceContextsResponse | null>;
fetchNativeLogAttributes(): Promise<NativeDeviceContextsResponse | null>;
fetchNativeAppStart(): Promise<NativeAppStartResponse | null>;
fetchNativeFrames(): Promise<NativeFramesResponse | null>;
initNativeSdk(options: UnsafeObject): Promise<boolean>;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/js/integrations/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
httpClientIntegration,
httpContextIntegration,
inboundFiltersIntegration,
logEnricherIntegration,
mobileReplayIntegration,
modulesLoaderIntegration,
nativeLinkedErrorsIntegration,
Expand Down Expand Up @@ -84,6 +85,9 @@ export function getDefaultIntegrations(options: ReactNativeClientOptions): Integ
if (options.enableNative) {
integrations.push(deviceContextIntegration());
integrations.push(modulesLoaderIntegration());
if (options._experiments?.enableLogs) {
integrations.push(logEnricherIntegration());
}
if (options.attachScreenshot) {
integrations.push(screenshotIntegration());
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/js/integrations/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { appRegistryIntegration } from './appRegistry';
export { timeToDisplayIntegration } from '../tracing/integrations/timeToDisplayIntegration';
export { breadcrumbsIntegration } from './breadcrumbs';
export { primitiveTagIntegration } from './primitiveTagIntegration';
export { logEnricherIntegration } from './logEnricherIntegration';

export {
browserApiErrorsIntegration,
Expand Down
67 changes: 67 additions & 0 deletions packages/core/src/js/integrations/logEnricherIntegration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable complexity */
import type { Integration, Log } from '@sentry/core';
import { logger } from '@sentry/core';
import type { ReactNativeClient } from '../client';
import { NATIVE } from '../wrapper';

const INTEGRATION_NAME = 'LogEnricher';

export const logEnricherIntegration = (): Integration => {
return {
name: INTEGRATION_NAME,
setup(client: ReactNativeClient) {
client.on('afterInit', () => {
cacheLogContext().then(
() => {
client.on('beforeCaptureLog', (log: Log) => {
processLog(log);
});
},
reason => {
logger.log(reason);
},
);
});
},
};
};

let NativeCache: Record<string, unknown> | undefined = undefined;

async function cacheLogContext(): Promise<void> {
try {
const response = await NATIVE.fetchNativeLogAttributes();

NativeCache = {
...(response?.contexts?.device && {
brand: response.contexts.device?.brand,
model: response.contexts.device?.model,
family: response.contexts.device?.family,
}),
...(response?.contexts?.os && {
os: response.contexts.os.name,
version: response.contexts.os.version,
}),
...(response?.contexts?.release && {
release: response.contexts.release,
}),
};
} catch (e) {
return Promise.reject(`[LOGS]: Failed to prepare attributes from Native Layer: ${e}`);
}
return Promise.resolve();
}

function processLog(log: Log): void {
if (NativeCache === undefined) {
return;
}

log.attributes = log.attributes ?? {};
NativeCache.brand && (log.attributes['device.brand'] = NativeCache.brand);
NativeCache.model && (log.attributes['device.model'] = NativeCache.model);
NativeCache.family && (log.attributes['device.family'] = NativeCache.family);
NativeCache.os && (log.attributes['os.name'] = NativeCache.os);
NativeCache.version && (log.attributes['os.version'] = NativeCache.version);
NativeCache.release && (log.attributes['sentry.release'] = NativeCache.release);
}
14 changes: 14 additions & 0 deletions packages/core/src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ interface SentryNativeWrapper {

fetchNativeRelease(): PromiseLike<NativeReleaseResponse>;
fetchNativeDeviceContexts(): PromiseLike<NativeDeviceContextsResponse | null>;
fetchNativeLogAttributes(): Promise<NativeDeviceContextsResponse | null>;
fetchNativeAppStart(): PromiseLike<NativeAppStartResponse | null>;
fetchNativeFrames(): PromiseLike<NativeFramesResponse | null>;
fetchNativeSdkInfo(): PromiseLike<Package | null>;
Expand Down Expand Up @@ -282,6 +283,19 @@ export const NATIVE: SentryNativeWrapper = {
return nativeIsReady;
},

/**
* Fetches the attributes to be set into logs from Native
*/
async fetchNativeLogAttributes(): Promise<NativeDeviceContextsResponse | null> {
if (!this.enableNative) {
throw this._DisabledNativeError;
}
if (!this._isModuleLoaded(RNSentry)) {
throw this._NativeClientError;
}

return RNSentry.fetchNativeLogAttributes();
},
/**
* Fetches the release from native
*/
Expand Down
Loading
Loading