Skip to content
Open
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
5 changes: 2 additions & 3 deletions benchmarks/src/testSetup/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

import { DefaultTimeProvider, RumActionType } from "@datadog/mobile-react-native";
import { ErrorSource } from "@datadog/mobile-react-native/lib/typescript/rum/types";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import was wrong and somehow slipped by us so I decided to fix it here.

import { DefaultTimeProvider, ErrorSource, RumActionType } from "@datadog/mobile-react-native";
import type { DdRumType, ResourceKind } from "@datadog/mobile-react-native/lib/typescript/rum/types";
import type { GestureResponderEvent } from "react-native/types";

Expand Down Expand Up @@ -72,4 +71,4 @@ export const Monitor: Pick<DdRumType, 'startView' | 'stopView' | 'addAction' | '
console.info("Monitor - addError", message, source, stacktrace, context, timestampMs, fingerprint);
return Promise.resolve();
},
};
};
4 changes: 2 additions & 2 deletions example/src/ddUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getDatadogConfig(trackingConsent: TrackingConsent) {
export function onDatadogInitialization() {
DdLogs.info('The RN Sdk was properly initialized')
DdSdkReactNative.setUserInfo({id: "1337", name: "Xavier", email: "[email protected]", extraInfo: { type: "premium" } })
DdSdkReactNative.setAttributes({campaign: "ad-network"})
DdSdkReactNative.addAttributes({campaign: "ad-network"})
}

// Legacy SDK Setup
Expand All @@ -54,6 +54,6 @@ export function initializeDatadog(trackingConsent: TrackingConsent) {
DdSdkReactNative.initialize(config).then(() => {
DdLogs.info('The RN Sdk was properly initialized')
DdSdkReactNative.setUserInfo({id: "1337", name: "Xavier", email: "[email protected]", extraInfo: { type: "premium" } })
DdSdkReactNative.setAttributes({campaign: "ad-network"})
DdSdkReactNative.addAttributes({campaign: "ad-network"})
});
}
13 changes: 11 additions & 2 deletions packages/codepush/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ actualRN.NativeModules.DdSdk = {
initialize: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['initialize']>,
setAttributes: jest.fn().mockImplementation(
addAttribute: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['setAttributes']>,
) as jest.MockedFunction<DdNativeSdkType['addAttribute']>,
removeAttribute: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['removeAttribute']>,
addAttributes: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['addAttributes']>,
removeAttributes: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['removeAttributes']>,
setTrackingConsent: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['setTrackingConsent']>,
Expand Down
13 changes: 11 additions & 2 deletions packages/core/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ actualRN.NativeModules.DdSdk = {
clearUserInfo: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['clearUserInfo']>,
setAttributes: jest.fn().mockImplementation(
addAttribute: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['setAttributes']>,
) as jest.MockedFunction<DdNativeSdkType['addAttribute']>,
removeAttribute: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['removeAttribute']>,
addAttributes: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['addAttributes']>,
removeAttributes: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['removeAttributes']>,
setTrackingConsent: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['setTrackingConsent']>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ internal class DatadogSDKWrapper : DatadogWrapper {
override fun clearUserInfo() {
Datadog.clearUserInfo()
}

override fun addRumGlobalAttribute(key: String, value: Any?) {
this.getRumMonitor().addAttribute(key, value)
}

override fun removeRumGlobalAttribute(key: String) {
this.getRumMonitor().removeAttribute(key)
}

override fun addRumGlobalAttributes(attributes: Map<String, Any?>) {
val rumMonitor = this.getRumMonitor()
for (attribute in attributes) {
rumMonitor.addAttribute(attribute.key, attribute.value)
this.addRumGlobalAttribute(attribute.key, attribute.value)
}
}

override fun removeRumGlobalAttributes(keys: Array<String>) {
for (key in keys) {
this.removeRumGlobalAttribute(key)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,35 @@ interface DatadogWrapper {
*/
fun clearUserInfo()


/** Adds a global attribute.
*
* @param key: Key that identifies the attribute.
* @param value: Value linked to the attribute.
*/
fun addRumGlobalAttribute(key: String, value: Any?)

/**
* Removes a global attribute.
*
* @param key: Key that identifies the attribute.
*/
fun removeRumGlobalAttribute(key: String)

/**
* Adds global attributes.
*
* @param attributes Attributes to add
*/
fun addRumGlobalAttributes(attributes: Map<String, Any?>)

/**
* Removes global attributes.
*
* @param keys Keys linked to the attributes to be removed
*/
fun removeRumGlobalAttributes(keys: Array<String>)

/**
* Sets tracking consent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.datadog.android.rum.configuration.VitalsUpdateFrequency
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import java.util.Locale
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -66,18 +67,62 @@ class DdSdkImplementation(
}

/**
* Sets the global context (set of attributes) attached with all future Logs, Spans and RUM
* Sets a specific attribute in the global context attached with all future Logs, Spans and RUM.
*
* @param key: Key that identifies the attribute.
* @param value: Value linked to the attribute.
*/
fun addAttribute(key: String, value: ReadableMap, promise: Promise) {
val attributeValue = value.toMap()["value"]
datadog.addRumGlobalAttribute(key, attributeValue)
GlobalState.addAttribute(key, attributeValue)
promise.resolve(null)
}

/**
* Removes an attribute from the global context attached with all future Logs, Spans and RUM events.
* @param key: They key associated with the attribute to be removed.
*/
fun removeAttribute(key: String, promise: Promise) {
datadog.removeRumGlobalAttribute(key)
GlobalState.removeAttribute(key)
promise.resolve(null)
}


/**
* Adds a set of attributes to the global context that is attached with all future Logs, Spans and RUM
* events.
* @param attributes The global context attributes.
* @param attributes: The global context attributes.
*/
fun setAttributes(attributes: ReadableMap, promise: Promise) {
fun addAttributes(attributes: ReadableMap, promise: Promise) {
datadog.addRumGlobalAttributes(attributes.toHashMap())
for ((k,v) in attributes.toHashMap()) {
GlobalState.addAttribute(k, v)
}
promise.resolve(null)
}

/**
* Removes a set of attributes from the global context that is attached with all future Logs, Spans and RUM
* events.
* @param keys: They keys associated with the attributes to be removed.
*/
fun removeAttributes(keys: ReadableArray, promise: Promise) {
val keysArray = mutableListOf<String>()
for (i in 0 until keys.size()) {
val key: String = keys.getString(i)
keysArray.add(key)
}
val keysStringArray = keysArray.toTypedArray()

datadog.removeRumGlobalAttributes(keysStringArray)
for (key in keysStringArray) {
GlobalState.removeAttribute(key)
}
promise.resolve(null)
}

/**
* Set the user information.
* @param userInfo The user object (use builtin attributes: 'id', 'email', 'name', and any custom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.DeviceEventManagerModule

/** The entry point to initialize Datadog's features. */
class DdSdk(
reactContext: ReactApplicationContext,
datadogWrapper: DatadogWrapper = DatadogSDKWrapper()
datadogWrapper: DatadogWrapper = DatadogSDKWrapper(),
ddTelemetry: DdTelemetry = DdTelemetry()
) : NativeDdSdkSpec(reactContext) {

Expand All @@ -40,13 +41,43 @@ class DdSdk(
}

/**
* Sets the global context (set of attributes) attached with all future Logs, Spans and RUM
* Sets a specific attribute in the global context attached with all future Logs, Spans and RUM
*
* @param key: Key that identifies the attribute.
* @param value: Value linked to the attribute.
*/
@ReactMethod
override fun addAttribute(key: String, value: ReadableMap, promise: Promise) {
implementation.addAttribute(key, value, promise)
}

/**
* Removes an attribute from the context attached with all future Logs, Spans and RUM events.
* @param key: They key associated with the attribute to be removed.
*/
@ReactMethod
override fun removeAttribute(key: String, promise: Promise) {
implementation.removeAttribute(key, promise)
}

/**
* Adds a set of attributes to the global context that is attached with all future Logs, Spans and RUM
* events.
* @param attributes The global context attributes.
*/
@ReactMethod
override fun setAttributes(attributes: ReadableMap, promise: Promise) {
implementation.setAttributes(attributes, promise)
override fun addAttributes(attributes: ReadableMap, promise: Promise) {
implementation.addAttributes(attributes, promise)
}

/**
* Removes a set of attributes from the global context that is attached with all future Logs, Spans and RUM
* events.
* @param keys: They keys associated with the attributes to be removed.
*/
@ReactMethod
override fun removeAttributes(keys: ReadableArray, promise: Promise) {
implementation.removeAttributes(keys, promise)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap

/** The entry point to initialize Datadog's features. */
Expand Down Expand Up @@ -66,13 +67,43 @@ class DdSdk(
}

/**
* Sets the global context (set of attributes) attached with all future Logs, Spans and RUM
* Sets a specific attribute in the global context attached with all future Logs, Spans and RUM
*
* @param key: Key that identifies the attribute.
* @param value: Value linked to the attribute.
*/
@ReactMethod
fun addAttribute(key: String, value: ReadableMap, promise: Promise) {
implementation.addAttribute(key, value, promise)
}

/**
* Removes an attribute from the context attached with all future Logs, Spans and RUM events.
* @param key: They key associated with the attribute to be removed.
*/
@ReactMethod
fun removeAttribute(key: String, promise: Promise) {
implementation.removeAttribute(key, promise)
}

/**
* Adds a set of attributes to the global context that is attached with all future Logs, Spans and RUM
* events.
* @param attributes The global context attributes.
*/
@ReactMethod
fun setAttributes(attributes: ReadableMap, promise: Promise) {
implementation.setAttributes(attributes, promise)
fun addAttributes(attributes: ReadableMap, promise: Promise) {
implementation.addAttributes(attributes, promise)
}

/**
* Removes a set of attributes from the global context that is attached with all future Logs, Spans and RUM
* events.
* @param keys: They keys associated with the attributes to be removed.
*/
@ReactMethod
fun removeAttributes(keys: ReadableArray, promise: Promise) {
implementation.removeAttributes(keys, promise)
}

/**
Expand Down
Loading