Skip to content

Commit b9b9966

Browse files
committed
RUMM-2050 add logs for all bridge calls
1 parent f4befb8 commit b9b9966

File tree

5 files changed

+101
-79
lines changed

5 files changed

+101
-79
lines changed

example/src/ddUtils.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
2+
DdLogs,
23
DdSdkReactNative,
34
DdSdkReactNativeConfiguration,
4-
DdLogs,
5+
SdkVerbosity,
56
TrackingConsent
67
} from '@datadog/mobile-react-native';
78

8-
import { CLIENT_TOKEN, ENVIRONMENT, APPLICATION_ID } from './ddCredentials';
9-
import { getTrackingConsent } from './utils';
9+
import {APPLICATION_ID, CLIENT_TOKEN, ENVIRONMENT} from './ddCredentials';
1010

1111
export function initializeDatadog(trackingConsent: TrackingConsent) {
1212

@@ -22,6 +22,7 @@ export function initializeDatadog(trackingConsent: TrackingConsent) {
2222
config.nativeCrashReportEnabled = true
2323
config.sampleRate = 100
2424
config.serviceName = "com.datadoghq.reactnative.sample"
25+
config.verbosity = SdkVerbosity.DEBUG;
2526

2627
DdSdkReactNative.initialize(config).then(() => {
2728
DdLogs.info('The RN Sdk was properly initialized')

packages/core/src/DdSdkReactNative.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {DdRumErrorTracking} from './rum/instrumentation/DdRumErrorTracking'
1212
import {DdRumResourceTracking} from './rum/instrumentation/DdRumResourceTracking'
1313
import type {TrackingConsent} from "./TrackingConsent"
1414
import {ProxyType} from "./ProxyConfiguration"
15-
import {InternalLog} from "./InternalLog"
1615
import {version as sdkVersion} from './version';
16+
import {InternalLog} from "./InternalLog"
1717
import {SdkVerbosity} from "./SdkVerbosity";
1818

1919
/**
@@ -115,6 +115,7 @@ export class DdSdkReactNative {
115115
*/
116116
// eslint-disable-next-line @typescript-eslint/ban-types
117117
static setAttributes(attributes: object): Promise<void> {
118+
InternalLog.log("Setting attributes " + JSON.stringify(attributes), SdkVerbosity.DEBUG);
118119
return DdSdk.setAttributes(attributes)
119120
}
120121

@@ -125,6 +126,7 @@ export class DdSdkReactNative {
125126
*/
126127
// eslint-disable-next-line @typescript-eslint/ban-types
127128
static setUser(user: object): Promise<void> {
129+
InternalLog.log("Setting user " + JSON.stringify(user), SdkVerbosity.DEBUG);
128130
return DdSdk.setUser(user)
129131
}
130132

@@ -134,6 +136,7 @@ export class DdSdkReactNative {
134136
* @returns a Promise.
135137
*/
136138
static setTrackingConsent(consent: TrackingConsent): Promise<void> {
139+
InternalLog.log("Setting consent " + consent, SdkVerbosity.DEBUG);
137140
return DdSdk.setTrackingConsent(consent)
138141
}
139142

packages/core/src/InternalLog.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,37 @@ export class InternalLog {
1010

1111
public static verbosity: SdkVerbosity|undefined = undefined
1212

13+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1314
public static log (message: any, verbosity: SdkVerbosity) {
15+
const prefixedMessage = "DATADOG: " + message
1416
if (verbosity == SdkVerbosity.ERROR && (
1517
(InternalLog.verbosity == SdkVerbosity.ERROR) ||
1618
(InternalLog.verbosity == SdkVerbosity.WARN) ||
1719
(InternalLog.verbosity == SdkVerbosity.INFO) ||
1820
(InternalLog.verbosity == SdkVerbosity.DEBUG)
1921
)) {
20-
console.error(message)
22+
console.error(prefixedMessage)
2123
}
2224

2325
if (verbosity == SdkVerbosity.WARN && (
2426
(InternalLog.verbosity == SdkVerbosity.WARN) ||
2527
(InternalLog.verbosity == SdkVerbosity.INFO) ||
2628
(InternalLog.verbosity == SdkVerbosity.DEBUG)
2729
)) {
28-
console.warn(message)
30+
console.warn(prefixedMessage)
2931
}
3032

3133
if (verbosity == SdkVerbosity.INFO && (
3234
(InternalLog.verbosity == SdkVerbosity.INFO) ||
3335
(InternalLog.verbosity == SdkVerbosity.DEBUG)
3436
)) {
35-
console.log(message)
37+
console.log(prefixedMessage)
3638
}
3739

3840
if (verbosity == SdkVerbosity.DEBUG && (
3941
(InternalLog.verbosity == SdkVerbosity.DEBUG)
4042
)) {
41-
console.log(message)
43+
console.log(prefixedMessage)
4244
}
4345
}
4446
}

packages/core/src/__tests__/InternalLog.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ it('M output debug W log(debug) (DEBUG+ allowed)', async () => {
7171
expect(baseConsoleLogCalled).toStrictEqual(true);
7272
expect(baseConsoleWarnCalled).toStrictEqual(false);
7373
expect(baseConsoleErrorCalled).toStrictEqual(false);
74-
expect(baseConsoleLogArg).toStrictEqual([message]);
74+
expect(baseConsoleLogArg).toStrictEqual(["DATADOG: " + message]);
7575
})
7676

7777
it('M output info W log(info) (DEBUG+ allowed)', async () => {
@@ -86,7 +86,7 @@ it('M output info W log(info) (DEBUG+ allowed)', async () => {
8686
expect(baseConsoleLogCalled).toStrictEqual(true);
8787
expect(baseConsoleWarnCalled).toStrictEqual(false);
8888
expect(baseConsoleErrorCalled).toStrictEqual(false);
89-
expect(baseConsoleLogArg).toStrictEqual([message]);
89+
expect(baseConsoleLogArg).toStrictEqual(["DATADOG: " + message]);
9090
})
9191

9292
it('M output warn W log(warn) (DEBUG+ allowed)', async () => {
@@ -101,7 +101,7 @@ it('M output warn W log(warn) (DEBUG+ allowed)', async () => {
101101
expect(baseConsoleLogCalled).toStrictEqual(false);
102102
expect(baseConsoleWarnCalled).toStrictEqual(true);
103103
expect(baseConsoleErrorCalled).toStrictEqual(false);
104-
expect(baseConsoleWarnArg).toStrictEqual([message]);
104+
expect(baseConsoleWarnArg).toStrictEqual(["DATADOG: " + message]);
105105
})
106106

107107
it('M output error W log(error) (DEBUG+ allowed)', async () => {
@@ -116,7 +116,7 @@ it('M output error W log(error) (DEBUG+ allowed)', async () => {
116116
expect(baseConsoleLogCalled).toStrictEqual(false);
117117
expect(baseConsoleWarnCalled).toStrictEqual(false);
118118
expect(baseConsoleErrorCalled).toStrictEqual(true);
119-
expect(baseConsoleErrorArg).toStrictEqual([message]);
119+
expect(baseConsoleErrorArg).toStrictEqual(["DATADOG: " + message]);
120120
})
121121

122122
it('M not output debug W log(debug) (INFO+ allowed)', async () => {
@@ -145,7 +145,7 @@ it('M output info W log(info) (INFO+ allowed)', async () => {
145145
expect(baseConsoleLogCalled).toStrictEqual(true);
146146
expect(baseConsoleWarnCalled).toStrictEqual(false);
147147
expect(baseConsoleErrorCalled).toStrictEqual(false);
148-
expect(baseConsoleLogArg).toStrictEqual([message]);
148+
expect(baseConsoleLogArg).toStrictEqual(["DATADOG: " + message]);
149149
})
150150

151151
it('M output warn W log(warn) (INFO+ allowed)', async () => {
@@ -160,7 +160,7 @@ it('M output warn W log(warn) (INFO+ allowed)', async () => {
160160
expect(baseConsoleLogCalled).toStrictEqual(false);
161161
expect(baseConsoleWarnCalled).toStrictEqual(true);
162162
expect(baseConsoleErrorCalled).toStrictEqual(false);
163-
expect(baseConsoleWarnArg).toStrictEqual([message]);
163+
expect(baseConsoleWarnArg).toStrictEqual(["DATADOG: " + message]);
164164
})
165165

166166
it('M output error W log(error) (INFO+ allowed)', async () => {
@@ -175,7 +175,7 @@ it('M output error W log(error) (INFO+ allowed)', async () => {
175175
expect(baseConsoleLogCalled).toStrictEqual(false);
176176
expect(baseConsoleWarnCalled).toStrictEqual(false);
177177
expect(baseConsoleErrorCalled).toStrictEqual(true);
178-
expect(baseConsoleErrorArg).toStrictEqual([message]);
178+
expect(baseConsoleErrorArg).toStrictEqual(["DATADOG: " + message]);
179179
})
180180

181181
it('M not output debug W log(debug) (WARN+ allowed)', async () => {
@@ -219,7 +219,7 @@ it('M output warn W log(warn) (WARN+ allowed)', async () => {
219219
expect(baseConsoleLogCalled).toStrictEqual(false);
220220
expect(baseConsoleWarnCalled).toStrictEqual(true);
221221
expect(baseConsoleErrorCalled).toStrictEqual(false);
222-
expect(baseConsoleWarnArg).toStrictEqual([message]);
222+
expect(baseConsoleWarnArg).toStrictEqual(["DATADOG: " + message]);
223223
})
224224

225225
it('M output error W log(error) (WARN+ allowed)', async () => {
@@ -234,7 +234,7 @@ it('M output error W log(error) (WARN+ allowed)', async () => {
234234
expect(baseConsoleLogCalled).toStrictEqual(false);
235235
expect(baseConsoleWarnCalled).toStrictEqual(false);
236236
expect(baseConsoleErrorCalled).toStrictEqual(true);
237-
expect(baseConsoleErrorArg).toStrictEqual([message]);
237+
expect(baseConsoleErrorArg).toStrictEqual(["DATADOG: " + message]);
238238
})
239239

240240
it('M not output debug W log(debug) (ERROR+ allowed)', async () => {
@@ -291,7 +291,7 @@ it('M output error W log(error) (ERROR+ allowed)', async () => {
291291
expect(baseConsoleLogCalled).toStrictEqual(false);
292292
expect(baseConsoleWarnCalled).toStrictEqual(false);
293293
expect(baseConsoleErrorCalled).toStrictEqual(true);
294-
expect(baseConsoleErrorArg).toStrictEqual([message]);
294+
expect(baseConsoleErrorArg).toStrictEqual(["DATADOG: " + message]);
295295
})
296296

297297
it('M not output debug W log(debug) (none allowed)', async () => {

packages/core/src/foundation.tsx

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,85 +6,101 @@
66

77
import { NativeModules } from 'react-native';
88
import { DdSdkConfiguration, DdSdkType, DdLogsType, DdTraceType, DdRumType } from './types';
9+
import {InternalLog} from "./InternalLog"
10+
import {SdkVerbosity} from "./SdkVerbosity";
911

1012
class DdLogsWrapper implements DdLogsType {
1113

12-
private nativeLogs: DdLogsType = NativeModules.DdLogs;
14+
private nativeLogs: DdLogsType = NativeModules.DdLogs;
1315

14-
debug(message: string, context: object = {}): Promise<void> {
15-
return this.nativeLogs.debug(message, context);
16-
}
16+
debug(message: string, context: object = {}): Promise<void> {
17+
InternalLog.log("Tracking debug log “" + message + "”", SdkVerbosity.DEBUG);
18+
return this.nativeLogs.debug(message, context);
19+
}
1720

18-
info(message: string, context: object = {}): Promise<void> {
19-
return this.nativeLogs.info(message, context);
20-
}
21+
info(message: string, context: object = {}): Promise<void> {
22+
InternalLog.log("Tracking info log “" + message + "”", SdkVerbosity.DEBUG);
23+
return this.nativeLogs.info(message, context);
24+
}
2125

22-
warn(message: string, context: object = {}): Promise<void> {
23-
return this.nativeLogs.warn(message, context);
24-
}
26+
warn(message: string, context: object = {}): Promise<void> {
27+
InternalLog.log("Tracking warn log “" + message + "”", SdkVerbosity.DEBUG);
28+
return this.nativeLogs.warn(message, context);
29+
}
2530

26-
error(message: string, context: object = {}): Promise<void> {
27-
return this.nativeLogs.error(message, context);
28-
}
31+
error(message: string, context: object = {}): Promise<void> {
32+
InternalLog.log("Tracking error log “" + message + "”", SdkVerbosity.DEBUG);
33+
return this.nativeLogs.error(message, context);
34+
}
2935

3036
}
3137

3238
class DdTraceWrapper implements DdTraceType {
3339

34-
private nativeTrace: DdTraceType = NativeModules.DdTrace;
40+
private nativeTrace: DdTraceType = NativeModules.DdTrace;
3541

36-
startSpan(operation: string, context: object = {}, timestampMs: number = Date.now()): Promise<string> {
37-
return this.nativeTrace.startSpan(operation, context, timestampMs);
38-
}
39-
40-
finishSpan(spanId: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
41-
return this.nativeTrace.finishSpan(spanId, context, timestampMs);
42-
}
42+
startSpan(operation: string, context: object = {}, timestampMs: number = Date.now()): Promise<string> {
43+
let spanId = this.nativeTrace.startSpan(operation, context, timestampMs);
44+
InternalLog.log("Starting span “" + operation + "” #" + spanId, SdkVerbosity.DEBUG);
45+
return spanId
46+
}
4347

48+
finishSpan(spanId: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
49+
InternalLog.log("Finishing span #" + spanId, SdkVerbosity.DEBUG);
50+
return this.nativeTrace.finishSpan(spanId, context, timestampMs);
51+
}
4452
}
4553

4654
class DdRumWrapper implements DdRumType {
4755

48-
private nativeRum: DdRumType = NativeModules.DdRum;
49-
50-
startView(key: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
51-
return this.nativeRum.startView(key, name, context, timestampMs);
52-
}
53-
54-
stopView(key: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
55-
return this.nativeRum.stopView(key, context, timestampMs);
56-
}
57-
58-
startAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
59-
return this.nativeRum.startAction(type, name, context, timestampMs);
60-
}
61-
62-
stopAction(context: object = {}, timestampMs: number = Date.now()): Promise<void> {
63-
return this.nativeRum.stopAction(context, timestampMs);
64-
}
65-
66-
addAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
67-
return this.nativeRum.addAction(type, name, context, timestampMs);
68-
}
69-
70-
startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
71-
return this.nativeRum.startResource(key, method, url, context, timestampMs);
72-
}
73-
74-
stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
75-
return this.nativeRum.stopResource(key, statusCode, kind, size, context, timestampMs);
76-
}
77-
78-
addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
79-
let updatedContext: any = context;
80-
updatedContext["_dd.error.source_type"] = "react-native";
81-
return this.nativeRum.addError(message, source, stacktrace, updatedContext, timestampMs);
82-
}
83-
84-
addTiming(name: string): Promise<void> {
85-
return this.nativeRum.addTiming(name);
86-
}
87-
56+
private nativeRum: DdRumType = NativeModules.DdRum;
57+
58+
startView(key: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
59+
InternalLog.log("Starting RUM View “" + name + "” #" + key, SdkVerbosity.DEBUG);
60+
return this.nativeRum.startView(key, name, context, timestampMs);
61+
}
62+
63+
stopView(key: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
64+
InternalLog.log("Stopping RUM View #" + key, SdkVerbosity.DEBUG);
65+
return this.nativeRum.stopView(key, context, timestampMs);
66+
}
67+
68+
startAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
69+
InternalLog.log("Starting RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
70+
return this.nativeRum.startAction(type, name, context, timestampMs);
71+
}
72+
73+
stopAction(context: object = {}, timestampMs: number = Date.now()): Promise<void> {
74+
InternalLog.log("Stopping current RUM Action", SdkVerbosity.DEBUG);
75+
return this.nativeRum.stopAction(context, timestampMs);
76+
}
77+
78+
addAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
79+
InternalLog.log("Adding RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
80+
return this.nativeRum.addAction(type, name, context, timestampMs);
81+
}
82+
83+
startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
84+
InternalLog.log("Starting RUM Resource #" + key + " " + method + ": " + url, SdkVerbosity.DEBUG);
85+
return this.nativeRum.startResource(key, method, url, context, timestampMs);
86+
}
87+
88+
stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
89+
InternalLog.log("Stopping RUM Resource #" + key + " status:" + statusCode, SdkVerbosity.DEBUG);
90+
return this.nativeRum.stopResource(key, statusCode, kind, size, context, timestampMs);
91+
}
92+
93+
addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
94+
InternalLog.log("Adding RUM Error “" + message + "”", SdkVerbosity.DEBUG);
95+
let updatedContext: any = context;
96+
updatedContext["_dd.error.source_type"] = "react-native";
97+
return this.nativeRum.addError(message, source, stacktrace, updatedContext, timestampMs);
98+
}
99+
100+
addTiming(name: string): Promise<void> {
101+
InternalLog.log("Adding timing “" + name + "” to RUM View", SdkVerbosity.DEBUG);
102+
return this.nativeRum.addTiming(name);
103+
}
88104
}
89105

90106
const DdSdk: DdSdkType = NativeModules.DdSdk;

0 commit comments

Comments
 (0)