diff --git a/packages/react-native-vega/CMakeLists.txt b/packages/react-native-vega/CMakeLists.txt index 25ac5d37b..e7d6c159c 100644 --- a/packages/react-native-vega/CMakeLists.txt +++ b/packages/react-native-vega/CMakeLists.txt @@ -50,6 +50,9 @@ endif() FetchContent_MakeAvailable(Datadog) set(HEADERS + kepler/turbo-modules/generated/DdRumSpec.h + kepler/turbo-modules/DdRum.h + kepler/turbo-modules/RumBridge.h kepler/turbo-modules/generated/DdSdkSpec.h kepler/turbo-modules/DdSdk.h kepler/turbo-modules/EnumParsers.h @@ -59,6 +62,8 @@ set(HEADERS ) set(SOURCES + kepler/turbo-modules/generated/DdRumSpec.cpp + kepler/turbo-modules/DdRum.cpp kepler/turbo-modules/generated/DdSdkSpec.cpp kepler/turbo-modules/DdSdk.cpp kepler/turbo-modules/EnumParsers.cpp diff --git a/packages/react-native-vega/README.md b/packages/react-native-vega/README.md index 4a13533d2..6507fc803 100644 --- a/packages/react-native-vega/README.md +++ b/packages/react-native-vega/README.md @@ -3,8 +3,8 @@ This private package provides the native bridge and HTTP transport for Vega OS. The bridge uses the Datadog C++ SDK and Kepler TurboModules. Native upload requests are forwarded through JavaScript `fetch`; responses are returned to the -native upload thread. SDK initialization and feature APIs will be added -separately. +native upload thread. Manual RUM and global attribute APIs are available; SDK +initialization and automatic instrumentation are added separately. `startHttpProxy()` resolves to a cleanup function. Await it before starting the native SDK so the JavaScript listener and native emitter are ready for uploads. @@ -38,3 +38,39 @@ Build the native module with the Vega SDK installed: cd packages/react-native-vega vega build -b Release ``` + +## Manual RUM and global attributes + +Import `DdRum` and `DdSdkReactNative` from `@datadog/mobile-react-native-vega`. +After RUM is initialized, use `DdRum` to start and stop views, record discrete or +continuous actions, start and stop resources, report errors, update view +attributes, stop a session, report full app display, and track feature operations. + +`DdSdkReactNative.addAttribute`, `addAttributes`, `removeAttribute`, and +`removeAttributes` update global RUM attributes and the shared JavaScript context. +View attributes take precedence over global attributes until removed or the view +ends. JSON-compatible values retain their types, including nested objects, +arrays, numbers, booleans, and null. Singular attribute calls wrap the value for +the bridge; that wrapper is removed before passing it to the C++ SDK. + +Calls resolve safely when the native module or RUM feature is unavailable. They +are not buffered for later initialization. Native calls keep bridge order and use +the C++ SDK's existing event queue. + +### C++ SDK 0.7.0 limitations + +- `addTiming`, `addViewLoadingTime`, and `addFeatureFlagEvaluation` are safe no-ops. +- `getCurrentSessionId` resolves to `undefined`; the SDK does not expose a getter. +- Event timestamps come from the native SDK. Optional `timestampMs` arguments are + accepted for compatibility but do not override that clock. +- Custom error fingerprints are accepted but are not supported by the native SDK. +- `BACK` actions map to the C++ SDK's `Custom` action type. +- Event mappers, JavaScript error enrichment, and tracing helpers are not provided + by this manual wrapper. + +Attribute conversion tests can run on the host with dd-sdk-cpp 0.7.0 and the Vega +SDK headers installed. Pass `DATADOG_CPP_SOURCE_DIR`, `KEPLER_INCLUDE_DIR` (a root +containing `Kepler/` and `JsonContainer/`), and `NODE_API_INCLUDE_DIR` to the test +CMake configuration. Keep platform system headers separate from those roots when +building on macOS. The default host test command still runs the transport tests +without these SDK dependencies. diff --git a/packages/react-native-vega/__tests__/DdRumVega.test.ts b/packages/react-native-vega/__tests__/DdRumVega.test.ts new file mode 100644 index 000000000..794798552 --- /dev/null +++ b/packages/react-native-vega/__tests__/DdRumVega.test.ts @@ -0,0 +1,204 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import { AttributesSingleton } from '@datadog/mobile-react-native/internal'; + +import { DdRum, DdSdkReactNative } from '../src'; + +const mockRum = Object.fromEntries( + [ + 'startView', + 'stopView', + 'startAction', + 'stopAction', + 'addAction', + 'startResource', + 'stopResource', + 'addError', + 'addViewAttribute', + 'removeViewAttribute', + 'addViewAttributes', + 'removeViewAttributes', + 'reportAppFullyDisplayed', + 'stopSession', + 'startFeatureOperation', + 'succeedFeatureOperation', + 'failFeatureOperation' + ].map(name => [name, jest.fn()]) +); +const mockSdk = Object.fromEntries( + [ + 'addAttribute', + 'removeAttribute', + 'addAttributes', + 'removeAttributes' + ].map(name => [name, jest.fn()]) +); +const mockGet = jest.fn(); +jest.mock('@amazon-devices/keplerscript-turbomodule-api', () => ({ + TurboModuleRegistry: { get: (...args: unknown[]) => mockGet(...args) } +})); + +const context = { + number: 42.5, + bool: false, + nil: null, + list: [1, 'two'], + object: { value: 3 } +}; + +describe('Vega manual RUM APIs', () => { + beforeEach(() => { + jest.resetAllMocks(); + AttributesSingleton.reset(); + jest.spyOn(Date, 'now').mockReturnValue(1234); + mockGet.mockImplementation(name => + name === 'DdRum' ? mockRum : mockSdk + ); + }); + afterEach(() => jest.restoreAllMocks()); + + it.each([ + ['startView', ['key', 'name'], ['key', 'name', {}, 1234]], + ['stopView', ['key'], ['key', {}, 1234]], + ['startAction', ['TAP', 'name'], ['TAP', 'name', {}, 1234]], + ['stopAction', ['CUSTOM', 'name'], ['CUSTOM', 'name', {}, 1234]], + ['addAction', ['SCROLL', 'name'], ['SCROLL', 'name', {}, 1234]], + [ + 'startResource', + ['key', 'POST', 'https://example.com'], + ['key', 'POST', 'https://example.com', {}, 1234] + ], + [ + 'stopResource', + ['key', 201, 'xhr'], + ['key', 201, 'xhr', -1, {}, 1234] + ], + [ + 'addError', + ['message', 'SOURCE', 'stack'], + ['message', 'SOURCE', 'stack', {}, 1234, ''] + ], + [ + 'startView', + ['key', 'name', context, 99], + ['key', 'name', context, 99] + ], + [ + 'stopResource', + ['key', 202, 'fetch', 128, context, 99], + ['key', 202, 'fetch', 128, context, 99] + ], + [ + 'addError', + ['message', 'NETWORK', 'stack', context, 99, 'group'], + ['message', 'NETWORK', 'stack', context, 99, 'group'] + ], + ['addViewAttributes', [context], [context]], + ['removeViewAttribute', ['key'], ['key']], + ['removeViewAttributes', [['key', 'other']], [['key', 'other']]], + ['reportAppFullyDisplayed', [], []], + ['stopSession', [], []], + ['startFeatureOperation', ['login', null], ['login', '', {}]], + [ + 'succeedFeatureOperation', + ['login', 'key', context], + ['login', 'key', context] + ], + [ + 'failFeatureOperation', + ['login', null, 'ABANDONED', context], + ['login', '', 'ABANDONED', context] + ] + ])('forwards %s arguments', async (method, args, expected) => { + await (DdRum[method as keyof typeof DdRum] as ( + ...values: unknown[] + ) => Promise)(...args); + expect(mockGet).toHaveBeenCalledWith('DdRum'); + expect(mockRum[method]).toHaveBeenCalledWith(...expected); + }); + + it.each([null, false, 0, 'text', [1, null], { value: { nested: true } }])( + 'wraps a singular view attribute without changing its value: %p', + async value => { + await DdRum.addViewAttribute('key', value); + expect(mockRum.addViewAttribute).toHaveBeenCalledWith('key', { + value + }); + } + ); + + it('handles unavailable, throwing, and rejecting bridges without failing the app', async () => { + mockGet.mockReturnValueOnce(null).mockImplementationOnce(() => { + throw new Error('lookup'); + }); + await expect(DdRum.startView('key', 'name')).resolves.toBeUndefined(); + await expect(DdRum.stopSession()).resolves.toBeUndefined(); + mockRum.addAction.mockImplementationOnce(() => { + throw new Error('bridge'); + }); + await expect( + DdRum.addAction('CUSTOM', 'action') + ).resolves.toBeUndefined(); + mockRum.stopSession.mockRejectedValueOnce(new Error('native')); + await expect(DdRum.stopSession()).resolves.toBeUndefined(); + await DdRum.startView('retry', 'retry'); + expect(mockRum.startView).toHaveBeenCalledWith( + 'retry', + 'retry', + {}, + 1234 + ); + }); + + it('keeps unsupported APIs safe and does not resolve a native module for them', async () => { + await DdRum.addTiming('ready'); + await DdRum.addViewLoadingTime(true); + await DdRum.addFeatureFlagEvaluation('flag', true); + await expect(DdRum.getCurrentSessionId()).resolves.toBeUndefined(); + expect(mockGet).not.toHaveBeenCalled(); + }); + + it('adds, replaces, and removes global attributes in native and shared JS context', async () => { + await DdSdkReactNative.addAttributes(context); + expect(mockSdk.addAttributes).toHaveBeenCalledWith(context); + await DdSdkReactNative.addAttribute('number', { value: false }); + expect(mockSdk.addAttribute).toHaveBeenCalledWith('number', { + value: { value: false } + }); + expect(AttributesSingleton.getInstance().getAttributes()).toEqual({ + ...context, + number: { value: false } + }); + await DdSdkReactNative.removeAttribute('number'); + await DdSdkReactNative.removeAttributes(['list', 'object']); + expect(mockSdk.removeAttribute).toHaveBeenCalledWith('number'); + expect(mockSdk.removeAttributes).toHaveBeenCalledWith([ + 'list', + 'object' + ]); + expect(AttributesSingleton.getInstance().getAttributes()).toEqual({ + bool: false, + nil: null + }); + }); + + it('does not change shared attributes when native cannot accept the update', async () => { + mockGet.mockReturnValueOnce(null); + await DdSdkReactNative.addAttribute('missing', true); + mockSdk.addAttributes.mockResolvedValueOnce(false); + await DdSdkReactNative.addAttributes(context); + mockSdk.addAttribute.mockRejectedValueOnce(new Error('native')); + await DdSdkReactNative.addAttribute('failed', true); + expect(AttributesSingleton.getInstance().getAttributes()).toEqual({}); + await DdSdkReactNative.addAttribute('keep', true); + mockSdk.removeAttributes.mockResolvedValueOnce(false); + await DdSdkReactNative.removeAttributes(['keep']); + expect(AttributesSingleton.getInstance().getAttributes()).toEqual({ + keep: true + }); + }); +}); diff --git a/packages/react-native-vega/kepler/AutoLinkInit.cpp b/packages/react-native-vega/kepler/AutoLinkInit.cpp index 32b1e7764..f7b68dfe8 100644 --- a/packages/react-native-vega/kepler/AutoLinkInit.cpp +++ b/packages/react-native-vega/kepler/AutoLinkInit.cpp @@ -6,10 +6,12 @@ #include #include "turbo-modules/DdSdk.h" +#include "turbo-modules/DdRum.h" extern "C" { __attribute__((visibility("default"))) void autoLinkKeplerTurboModulesV1() noexcept { KEPLER_REGISTER_TURBO_MODULE(DdSdkTurboModule, DdSdk); + KEPLER_REGISTER_TURBO_MODULE(DdRumTurboModule, DdRum); } } diff --git a/packages/react-native-vega/kepler/turbo-modules/DdRum.cpp b/packages/react-native-vega/kepler/turbo-modules/DdRum.cpp new file mode 100644 index 000000000..1bbd4887d --- /dev/null +++ b/packages/react-native-vega/kepler/turbo-modules/DdRum.cpp @@ -0,0 +1,150 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// +// This product includes software developed at Datadog +// (https://www.datadoghq.com/). Copyright 2016-Present Datadog, Inc. + +#include "DdRum.h" +#include "AttributeConverter.h" +#include "EnumParsers.h" +#include "RumBridge.h" + +#include + +using namespace com::amazon::kepler::turbomodule; +using namespace datadog_rn_vega; + +namespace DdRumTurboModule { + +Promise DdRum::startView(std::string key, std::string name, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.StartView(key, name, jsObjectToAttribute(context)); + }); +} + +Promise DdRum::stopView(std::string key, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.StopView(key, jsObjectToAttribute(context)); + }); +} + +Promise DdRum::startAction(std::string type, std::string name, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.StartAction(parseActionType(type), name, + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::stopAction(std::string type, std::string name, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.StopAction(parseActionType(type), name, + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::addAction(std::string type, std::string name, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.AddAction(parseActionType(type), name, + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::startResource(std::string key, std::string method, + std::string url, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + rum.StartResource(key, parseResourceMethod(method), url, + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::stopResource(std::string key, double statusCode, + std::string kind, double size, JSObject context, + double /*timestampMs*/) { + return withRum([=](datadog::Rum &rum) { + const auto status = + std::isfinite(statusCode) && statusCode >= 0 && statusCode <= 599 + ? static_cast(statusCode) + : 0; + const auto bytes = + std::isfinite(size) && size >= 0 && size < 9223372036854775808.0 + ? static_cast(size) + : -1; + rum.StopResource(key, status, bytes, parseResourceType(kind), + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::addError(std::string message, std::string source, + std::string stacktrace, JSObject context, + double /*timestampMs*/, std::string /*fingerprint*/) { + return withRum([=](datadog::Rum &rum) { + rum.AddError(parseErrorSource(source), message, "", stacktrace, + jsObjectToAttribute(context)); + }); +} + +Promise DdRum::addViewAttribute(std::string key, JSObject value) { + return withRum([=](datadog::Rum &rum) { + rum.AddViewAttribute(key, jsWrappedValueToAttribute(value)); + }); +} + +Promise DdRum::removeViewAttribute(std::string key) { + return withRum([=](datadog::Rum &rum) { rum.RemoveViewAttribute(key); }); +} + +Promise DdRum::addViewAttributes(JSObject attributes) { + return withRum([=](datadog::Rum &rum) { + for (const auto &[key, value] : attributes) { + rum.AddViewAttribute(key, jsValueToAttribute(value)); + } + }); +} + +Promise DdRum::removeViewAttributes(std::vector keys) { + return withRum([=](datadog::Rum &rum) { + for (const auto &key : keys) { + rum.RemoveViewAttribute(key); + } + }); +} + +Promise DdRum::reportAppFullyDisplayed() { + return withRum([=](datadog::Rum &rum) { rum.ReportAppFullyDisplayed(); }); +} + +Promise DdRum::stopSession() { + return withRum([=](datadog::Rum &rum) { rum.StopSession(); }); +} + +Promise DdRum::startFeatureOperation(std::string name, std::string operationKey, + JSObject attributes) { + return withRum([=](datadog::Rum &rum) { + rum.StartOperation(name, operationKey, jsObjectToAttribute(attributes)); + }); +} + +Promise DdRum::succeedFeatureOperation(std::string name, + std::string operationKey, + JSObject attributes) { + return withRum([=](datadog::Rum &rum) { + rum.SucceedOperation(name, operationKey, + jsObjectToAttribute(attributes)); + }); +} + +Promise DdRum::failFeatureOperation(std::string name, std::string operationKey, + std::string reason, JSObject attributes) { + return withRum([=](datadog::Rum &rum) { + rum.FailOperation(name, parseOperationFailureReason(reason), + operationKey, jsObjectToAttribute(attributes)); + }); +} + +} // namespace DdRumTurboModule diff --git a/packages/react-native-vega/kepler/turbo-modules/DdRum.h b/packages/react-native-vega/kepler/turbo-modules/DdRum.h new file mode 100644 index 000000000..42a2246c8 --- /dev/null +++ b/packages/react-native-vega/kepler/turbo-modules/DdRum.h @@ -0,0 +1,74 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// +// This product includes software developed at Datadog +// (https://www.datadoghq.com/). Copyright 2016-Present Datadog, Inc. + +#pragma once + +#include "generated/DdRumSpec.h" + +namespace DdRumTurboModule { + +class DdRum : public DdRumSpec { + public: + DdRum() = default; + ~DdRum() noexcept override = default; + + com::amazon::kepler::turbomodule::Promise + startView(std::string key, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + stopView(std::string key, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + startAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + stopAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + addAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + startResource(std::string key, std::string method, std::string url, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + stopResource(std::string key, double statusCode, std::string kind, + double size, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) override; + com::amazon::kepler::turbomodule::Promise + addError(std::string message, std::string source, std::string stacktrace, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs, std::string fingerprint) override; + com::amazon::kepler::turbomodule::Promise + addViewAttribute(std::string key, + com::amazon::kepler::turbomodule::JSObject value) override; + com::amazon::kepler::turbomodule::Promise + removeViewAttribute(std::string key) override; + com::amazon::kepler::turbomodule::Promise addViewAttributes( + com::amazon::kepler::turbomodule::JSObject attributes) override; + com::amazon::kepler::turbomodule::Promise + removeViewAttributes(std::vector keys) override; + com::amazon::kepler::turbomodule::Promise + reportAppFullyDisplayed() override; + com::amazon::kepler::turbomodule::Promise stopSession() override; + com::amazon::kepler::turbomodule::Promise startFeatureOperation( + std::string name, std::string operationKey, + com::amazon::kepler::turbomodule::JSObject attributes) override; + com::amazon::kepler::turbomodule::Promise succeedFeatureOperation( + std::string name, std::string operationKey, + com::amazon::kepler::turbomodule::JSObject attributes) override; + com::amazon::kepler::turbomodule::Promise failFeatureOperation( + std::string name, std::string operationKey, std::string reason, + com::amazon::kepler::turbomodule::JSObject attributes) override; +}; + +} // namespace DdRumTurboModule diff --git a/packages/react-native-vega/kepler/turbo-modules/DdSdk.cpp b/packages/react-native-vega/kepler/turbo-modules/DdSdk.cpp index d8b43b47f..cc4c13fa8 100644 --- a/packages/react-native-vega/kepler/turbo-modules/DdSdk.cpp +++ b/packages/react-native-vega/kepler/turbo-modules/DdSdk.cpp @@ -5,7 +5,9 @@ // (https://www.datadoghq.com/). Copyright 2016-Present Datadog, Inc. #include "DdSdk.h" +#include "AttributeConverter.h" #include "HttpJsProxy.h" +#include "RumBridge.h" using namespace com::amazon::kepler::turbomodule; @@ -13,6 +15,34 @@ namespace DdSdkTurboModule { DdSdk::DdSdk() = default; +Promise DdSdk::addAttribute(std::string key, JSObject value) { + return datadog_rn_vega::withRum([=](datadog::Rum &rum) { + rum.AddAttribute(key, + datadog_rn_vega::jsWrappedValueToAttribute(value)); + }); +} + +Promise DdSdk::removeAttribute(std::string key) { + return datadog_rn_vega::withRum( + [=](datadog::Rum &rum) { rum.RemoveAttribute(key); }); +} + +Promise DdSdk::addAttributes(JSObject attributes) { + return datadog_rn_vega::withRum([=](datadog::Rum &rum) { + for (const auto &[key, value] : attributes) { + rum.AddAttribute(key, datadog_rn_vega::jsValueToAttribute(value)); + } + }); +} + +Promise DdSdk::removeAttributes(std::vector keys) { + return datadog_rn_vega::withRum([=](datadog::Rum &rum) { + for (const auto &key : keys) { + rum.RemoveAttribute(key); + } + }); +} + Promise DdSdk::startHttpProxy() { return Promise([this](const std::shared_ptr &promise) { datadog_rn_vega::HttpRequestBridge::getInstance().setEmitFunction( diff --git a/packages/react-native-vega/kepler/turbo-modules/DdSdk.h b/packages/react-native-vega/kepler/turbo-modules/DdSdk.h index 687793672..d19b63b5f 100644 --- a/packages/react-native-vega/kepler/turbo-modules/DdSdk.h +++ b/packages/react-native-vega/kepler/turbo-modules/DdSdk.h @@ -15,6 +15,15 @@ class DdSdk : public DdSdkSpec { DdSdk(); ~DdSdk() noexcept override; + com::amazon::kepler::turbomodule::Promise + addAttribute(std::string key, + com::amazon::kepler::turbomodule::JSObject value) override; + com::amazon::kepler::turbomodule::Promise + removeAttribute(std::string key) override; + com::amazon::kepler::turbomodule::Promise addAttributes( + com::amazon::kepler::turbomodule::JSObject attributes) override; + com::amazon::kepler::turbomodule::Promise + removeAttributes(std::vector keys) override; com::amazon::kepler::turbomodule::Promise startHttpProxy() override; com::amazon::kepler::turbomodule::Promise stopHttpProxy() override; com::amazon::kepler::turbomodule::Promise diff --git a/packages/react-native-vega/kepler/turbo-modules/EnumParsers.cpp b/packages/react-native-vega/kepler/turbo-modules/EnumParsers.cpp index ba973b3f0..c6cbf0eeb 100644 --- a/packages/react-native-vega/kepler/turbo-modules/EnumParsers.cpp +++ b/packages/react-native-vega/kepler/turbo-modules/EnumParsers.cpp @@ -7,8 +7,19 @@ #include "EnumParsers.h" namespace datadog_rn_vega { +namespace { +std::string uppercaseAscii(std::string value) { + for (char& c : value) { + if (c >= 'a' && c <= 'z') { + c = static_cast(c - 'a' + 'A'); + } + } + return value; +} +} // namespace -datadog::RumActionType parseActionType(const std::string& type) { +datadog::RumActionType parseActionType(const std::string& input) { + const auto type = uppercaseAscii(input); if (type == "TAP") return datadog::RumActionType::Tap; if (type == "SCROLL") return datadog::RumActionType::Scroll; if (type == "SWIPE") return datadog::RumActionType::Swipe; @@ -18,7 +29,8 @@ datadog::RumActionType parseActionType(const std::string& type) { return datadog::RumActionType::Custom; } -datadog::RumResourceMethod parseResourceMethod(const std::string& method) { +datadog::RumResourceMethod parseResourceMethod(const std::string& input) { + const auto method = uppercaseAscii(input); if (method == "GET") return datadog::RumResourceMethod::Get; if (method == "POST") return datadog::RumResourceMethod::Post; if (method == "PUT") return datadog::RumResourceMethod::Put; @@ -46,7 +58,8 @@ datadog::RumResourceType parseResourceType(const std::string& kind) { return datadog::RumResourceType::Unknown; } -datadog::RumErrorSource parseErrorSource(const std::string& source) { +datadog::RumErrorSource parseErrorSource(const std::string& input) { + const auto source = uppercaseAscii(input); if (source == "NETWORK") return datadog::RumErrorSource::Network; if (source == "SOURCE") return datadog::RumErrorSource::Source; if (source == "CONSOLE") return datadog::RumErrorSource::Console; diff --git a/packages/react-native-vega/kepler/turbo-modules/RumBridge.h b/packages/react-native-vega/kepler/turbo-modules/RumBridge.h new file mode 100644 index 000000000..fc3d69b7d --- /dev/null +++ b/packages/react-native-vega/kepler/turbo-modules/RumBridge.h @@ -0,0 +1,36 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// +// This product includes software developed at Datadog +// (https://www.datadoghq.com/). Copyright 2016-Present Datadog, Inc. + +#pragma once + +#include "DatadogGlobalState.h" +#include +#include + +namespace datadog_rn_vega { + +// Rum enqueues events itself. Calling it in bridge order avoids a detached +// thread per event reordering start/stop calls and attribute changes. +template +com::amazon::kepler::turbomodule::Promise withRum(Operation operation) { + using com::amazon::kepler::turbomodule::Promise; + return Promise([operation = std::move(operation)]( + const std::shared_ptr &promise) { + bool accepted = false; + try { + auto rum = DatadogGlobalState::getInstance().getRum(); + if (rum) { + operation(*rum); + accepted = true; + } + } catch (...) { + // An SDK failure must not escape into the host app. + } + promise->resolve(accepted); + }); +} + +} // namespace datadog_rn_vega diff --git a/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.cpp b/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.cpp new file mode 100644 index 000000000..cff3b1aae --- /dev/null +++ b/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.cpp @@ -0,0 +1,49 @@ +/** + * This code was generated by the Kepler Codegen tool. + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost when the code is regenerated. + * + * @generated by GenerateModuleSpecCpp.js + */ + +#include "DdRumSpec.h" + +using namespace com::amazon::kepler::turbomodule; + +namespace DdRumTurboModule { + +DdRumSpec::DdRumSpec() : KeplerTurboModule("DdRum") {} + +DdRumSpec::~DdRumSpec() = default; + +void DdRumSpec::aggregateMethods( + MethodAggregator &methodAggregator) const noexcept { + methodAggregator.addMethod("startView", &DdRumSpec::startView); + methodAggregator.addMethod("stopView", &DdRumSpec::stopView); + methodAggregator.addMethod("startAction", &DdRumSpec::startAction); + methodAggregator.addMethod("stopAction", &DdRumSpec::stopAction); + methodAggregator.addMethod("addAction", &DdRumSpec::addAction); + methodAggregator.addMethod("startResource", &DdRumSpec::startResource); + methodAggregator.addMethod("stopResource", &DdRumSpec::stopResource); + methodAggregator.addMethod("addError", &DdRumSpec::addError); + methodAggregator.addMethod("addViewAttribute", + &DdRumSpec::addViewAttribute); + methodAggregator.addMethod("removeViewAttribute", + &DdRumSpec::removeViewAttribute); + methodAggregator.addMethod("addViewAttributes", + &DdRumSpec::addViewAttributes); + methodAggregator.addMethod("removeViewAttributes", + &DdRumSpec::removeViewAttributes); + methodAggregator.addMethod("reportAppFullyDisplayed", + &DdRumSpec::reportAppFullyDisplayed); + methodAggregator.addMethod("stopSession", &DdRumSpec::stopSession); + methodAggregator.addMethod("startFeatureOperation", + &DdRumSpec::startFeatureOperation); + methodAggregator.addMethod("succeedFeatureOperation", + &DdRumSpec::succeedFeatureOperation); + methodAggregator.addMethod("failFeatureOperation", + &DdRumSpec::failFeatureOperation); +} + +} // namespace DdRumTurboModule diff --git a/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.h b/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.h new file mode 100644 index 000000000..9b49f2da1 --- /dev/null +++ b/packages/react-native-vega/kepler/turbo-modules/generated/DdRumSpec.h @@ -0,0 +1,93 @@ +/** + * This code was generated by the Kepler Codegen tool. + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost when the code is regenerated. + * + * @generated by GenerateModuleSpecH.js + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace DdRumTurboModule { + +class DdRumSpec : public com::amazon::kepler::turbomodule::KeplerTurboModule { + protected: + DdRumSpec(); + virtual ~DdRumSpec() = 0; + + public: + void aggregateMethods( + com::amazon::kepler::turbomodule::MethodAggregator + &methodAggregator) const noexcept override; + + virtual com::amazon::kepler::turbomodule::Promise + startView(std::string key, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + stopView(std::string key, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + startAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + stopAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + addAction(std::string type, std::string name, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + startResource(std::string key, std::string method, std::string url, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + stopResource(std::string key, double statusCode, std::string kind, + double size, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs) = 0; + virtual com::amazon::kepler::turbomodule::Promise + addError(std::string message, std::string source, std::string stacktrace, + com::amazon::kepler::turbomodule::JSObject context, + double timestampMs, std::string fingerprint) = 0; + virtual com::amazon::kepler::turbomodule::Promise + addViewAttribute(std::string key, + com::amazon::kepler::turbomodule::JSObject value) = 0; + virtual com::amazon::kepler::turbomodule::Promise + removeViewAttribute(std::string key) = 0; + virtual com::amazon::kepler::turbomodule::Promise addViewAttributes( + com::amazon::kepler::turbomodule::JSObject attributes) = 0; + virtual com::amazon::kepler::turbomodule::Promise + removeViewAttributes(std::vector keys) = 0; + virtual com::amazon::kepler::turbomodule::Promise + reportAppFullyDisplayed() = 0; + virtual com::amazon::kepler::turbomodule::Promise stopSession() = 0; + virtual com::amazon::kepler::turbomodule::Promise startFeatureOperation( + std::string name, std::string operationKey, + com::amazon::kepler::turbomodule::JSObject attributes) = 0; + virtual com::amazon::kepler::turbomodule::Promise succeedFeatureOperation( + std::string name, std::string operationKey, + com::amazon::kepler::turbomodule::JSObject attributes) = 0; + virtual com::amazon::kepler::turbomodule::Promise failFeatureOperation( + std::string name, std::string operationKey, std::string reason, + com::amazon::kepler::turbomodule::JSObject attributes) = 0; +}; + +} // namespace DdRumTurboModule diff --git a/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.cpp b/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.cpp index caf5a13bc..0e4dd8636 100644 --- a/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.cpp +++ b/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.cpp @@ -19,6 +19,11 @@ DdSdkSpec::~DdSdkSpec() = default; void DdSdkSpec::aggregateMethods( MethodAggregator &methodAggregator) const noexcept { + methodAggregator.addMethod("addAttribute", &DdSdkSpec::addAttribute); + methodAggregator.addMethod("removeAttribute", &DdSdkSpec::removeAttribute); + methodAggregator.addMethod("addAttributes", &DdSdkSpec::addAttributes); + methodAggregator.addMethod("removeAttributes", + &DdSdkSpec::removeAttributes); methodAggregator.addMethod("startHttpProxy", &DdSdkSpec::startHttpProxy); methodAggregator.addMethod("stopHttpProxy", &DdSdkSpec::stopHttpProxy); methodAggregator.addMethod("httpResponse", &DdSdkSpec::httpResponse); diff --git a/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.h b/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.h index 905f5f57c..909e792a8 100644 --- a/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.h +++ b/packages/react-native-vega/kepler/turbo-modules/generated/DdSdkSpec.h @@ -34,6 +34,15 @@ class DdSdkSpec : public com::amazon::kepler::turbomodule::KeplerTurboModule { com::amazon::kepler::turbomodule::MethodAggregator &methodAggregator) const noexcept override; + virtual com::amazon::kepler::turbomodule::Promise + addAttribute(std::string key, + com::amazon::kepler::turbomodule::JSObject value) = 0; + virtual com::amazon::kepler::turbomodule::Promise + removeAttribute(std::string key) = 0; + virtual com::amazon::kepler::turbomodule::Promise + addAttributes(com::amazon::kepler::turbomodule::JSObject attributes) = 0; + virtual com::amazon::kepler::turbomodule::Promise + removeAttributes(std::vector keys) = 0; virtual com::amazon::kepler::turbomodule::Promise startHttpProxy() = 0; virtual com::amazon::kepler::turbomodule::Promise stopHttpProxy() = 0; virtual com::amazon::kepler::turbomodule::Promise diff --git a/packages/react-native-vega/package.json b/packages/react-native-vega/package.json index 13d5ae47f..dfc12dbd2 100644 --- a/packages/react-native-vega/package.json +++ b/packages/react-native-vega/package.json @@ -44,7 +44,7 @@ "test": "jest", "lint": "eslint src __tests__", "prepare": "bob build", - "codegen": "keplerscript-turbomodule-api codegen src/turbo-modules/NativeDdSdk.ts -o kepler/turbo-modules/ --namespace DdSdkTurboModule --className DdSdk", + "codegen": "keplerscript-turbomodule-api codegen src/turbo-modules/NativeDdSdk.ts -o kepler/turbo-modules/ --namespace DdSdkTurboModule --className DdSdk && keplerscript-turbomodule-api codegen src/turbo-modules/NativeDdRum.ts -o kepler/turbo-modules/ --namespace DdRumTurboModule --className DdRum", "test:native": "cmake -S tests -B build/host-tests && cmake --build build/host-tests && ctest --test-dir build/host-tests --output-on-failure" }, "dependencies": { diff --git a/packages/react-native-vega/src/DdRumVega.ts b/packages/react-native-vega/src/DdRumVega.ts new file mode 100644 index 000000000..9f99da405 --- /dev/null +++ b/packages/react-native-vega/src/DdRumVega.ts @@ -0,0 +1,205 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import type { + ErrorSource, + FeatureOperationFailure +} from '@datadog/mobile-react-native'; + +import { callNative } from './callNative'; +import { getNativeDdRum } from './turbo-modules/NativeDdRum'; +import type { DdRum as NativeDdRum } from './turbo-modules/NativeDdRum'; + +const callRum = (call: (native: NativeDdRum) => Promise): Promise => + callNative(getNativeDdRum, call).then(() => undefined); + +/** Manual RUM APIs for Vega. Unsupported C++ SDK APIs are documented in README.md. */ +class DdRumVega { + startView = async ( + key: string, + name: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + await callRum(native => + native.startView(key, name, context, timestampMs) + ); + }; + + stopView = ( + key: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => native.stopView(key, context, timestampMs)); + }; + + startAction = ( + type: string, + name: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => + native.startAction(type, name, context, timestampMs) + ); + }; + + stopAction = ( + type: string, + name: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => + native.stopAction(type, name, context, timestampMs) + ); + }; + + addAction = ( + type: string, + name: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => + native.addAction(type, name, context, timestampMs) + ); + }; + + startResource = ( + key: string, + method: string, + url: string, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => + native.startResource(key, method, url, context, timestampMs) + ); + }; + + stopResource = ( + key: string, + statusCode: number, + kind: string, + size: number = -1, + context: object = {}, + timestampMs: number = Date.now() + ): Promise => { + return callRum(native => + native.stopResource( + key, + statusCode, + kind, + size, + context, + timestampMs + ) + ); + }; + + addError = ( + message: string, + source: ErrorSource, + stacktrace: string, + context: object = {}, + timestampMs: number = Date.now(), + fingerprint: string = '' + ): Promise => { + return callRum(native => + native.addError( + message, + source, + stacktrace, + context, + timestampMs, + fingerprint + ) + ); + }; + + addTiming = (_name: string): Promise => { + return Promise.resolve(); + }; + + addViewAttribute = (key: string, value: unknown): Promise => { + return callRum(native => native.addViewAttribute(key, { value })); + }; + + removeViewAttribute = (key: string): Promise => { + return callRum(native => native.removeViewAttribute(key)); + }; + + addViewAttributes = (attributes: object): Promise => { + return callRum(native => native.addViewAttributes(attributes)); + }; + + removeViewAttributes = (keys: string[]): Promise => { + return callRum(native => native.removeViewAttributes(keys)); + }; + + addViewLoadingTime = (_overwrite: boolean): Promise => { + return Promise.resolve(); + }; + + reportAppFullyDisplayed = (): Promise => { + return callRum(native => native.reportAppFullyDisplayed()); + }; + + stopSession = (): Promise => { + return callRum(native => native.stopSession()); + }; + + addFeatureFlagEvaluation = ( + _name: string, + _value: unknown + ): Promise => { + return Promise.resolve(); + }; + + getCurrentSessionId = async (): Promise => { + return undefined; + }; + + startFeatureOperation = ( + name: string, + operationKey: string | null, + attributes: object = {} + ): Promise => { + return callRum(native => + native.startFeatureOperation(name, operationKey ?? '', attributes) + ); + }; + + succeedFeatureOperation = ( + name: string, + operationKey: string | null, + attributes: object = {} + ): Promise => { + return callRum(native => + native.succeedFeatureOperation(name, operationKey ?? '', attributes) + ); + }; + + failFeatureOperation = ( + name: string, + operationKey: string | null, + reason: FeatureOperationFailure, + attributes: object = {} + ): Promise => { + return callRum(native => + native.failFeatureOperation( + name, + operationKey ?? '', + reason, + attributes + ) + ); + }; +} + +export const DdRum = new DdRumVega(); diff --git a/packages/react-native-vega/src/DdSdkReactNativeVega.ts b/packages/react-native-vega/src/DdSdkReactNativeVega.ts new file mode 100644 index 000000000..508acb176 --- /dev/null +++ b/packages/react-native-vega/src/DdSdkReactNativeVega.ts @@ -0,0 +1,57 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import { AttributesSingleton } from '@datadog/mobile-react-native/internal'; +import type { Attributes } from '@datadog/mobile-react-native/internal'; + +import { callNative } from './callNative'; +import { getNativeDdSdk } from './turbo-modules/NativeDdSdk'; + +/** Global attributes for future events; SDK initialization is added separately. */ +export class DdSdkReactNative { + static addAttribute = async ( + key: string, + value: unknown + ): Promise => { + if ( + await callNative(getNativeDdSdk, native => + native.addAttribute(key, { value }) + ) + ) { + AttributesSingleton.getInstance().addAttribute(key, value); + } + }; + + static removeAttribute = async (key: string): Promise => { + if ( + await callNative(getNativeDdSdk, native => + native.removeAttribute(key) + ) + ) { + AttributesSingleton.getInstance().removeAttribute(key); + } + }; + + static addAttributes = async (attributes: Attributes): Promise => { + if ( + await callNative(getNativeDdSdk, native => + native.addAttributes(attributes) + ) + ) { + AttributesSingleton.getInstance().addAttributes(attributes); + } + }; + + static removeAttributes = async (keys: string[]): Promise => { + if ( + await callNative(getNativeDdSdk, native => + native.removeAttributes(keys) + ) + ) { + AttributesSingleton.getInstance().removeAttributes(keys); + } + }; +} diff --git a/packages/react-native-vega/src/callNative.ts b/packages/react-native-vega/src/callNative.ts new file mode 100644 index 000000000..5734afaf4 --- /dev/null +++ b/packages/react-native-vega/src/callNative.ts @@ -0,0 +1,21 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +/** Keep a missing or failing native bridge from breaking the host app. */ +export const callNative = async ( + resolve: () => T | null, + call: (native: T) => Promise +): Promise => { + try { + const native = resolve(); + if (!native) { + return false; + } + return (await call(native)) !== false; + } catch { + return false; + } +}; diff --git a/packages/react-native-vega/src/index.ts b/packages/react-native-vega/src/index.ts index 4a21237d2..eb5473d93 100644 --- a/packages/react-native-vega/src/index.ts +++ b/packages/react-native-vega/src/index.ts @@ -5,3 +5,5 @@ */ export { startHttpProxy } from './HttpProxy'; +export { DdRum } from './DdRumVega'; +export { DdSdkReactNative } from './DdSdkReactNativeVega'; diff --git a/packages/react-native-vega/src/turbo-modules/NativeDdRum.ts b/packages/react-native-vega/src/turbo-modules/NativeDdRum.ts new file mode 100644 index 000000000..8e78f3f30 --- /dev/null +++ b/packages/react-native-vega/src/turbo-modules/NativeDdRum.ts @@ -0,0 +1,85 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import type { KeplerTurboModule } from '@amazon-devices/keplerscript-turbomodule-api'; +import { TurboModuleRegistry } from '@amazon-devices/keplerscript-turbomodule-api'; + +/* eslint-disable @typescript-eslint/ban-types */ +export interface DdRum extends KeplerTurboModule { + startView( + key: string, + name: string, + context: Object, + timestampMs: number + ): Promise; + stopView(key: string, context: Object, timestampMs: number): Promise; + startAction( + type: string, + name: string, + context: Object, + timestampMs: number + ): Promise; + stopAction( + type: string, + name: string, + context: Object, + timestampMs: number + ): Promise; + addAction( + type: string, + name: string, + context: Object, + timestampMs: number + ): Promise; + startResource( + key: string, + method: string, + url: string, + context: Object, + timestampMs: number + ): Promise; + stopResource( + key: string, + statusCode: number, + kind: string, + size: number, + context: Object, + timestampMs: number + ): Promise; + addError( + message: string, + source: string, + stacktrace: string, + context: Object, + timestampMs: number, + fingerprint: string + ): Promise; + addViewAttribute(key: string, value: Object): Promise; + removeViewAttribute(key: string): Promise; + addViewAttributes(attributes: Object): Promise; + removeViewAttributes(keys: string[]): Promise; + reportAppFullyDisplayed(): Promise; + stopSession(): Promise; + startFeatureOperation( + name: string, + operationKey: string, + attributes: Object + ): Promise; + succeedFeatureOperation( + name: string, + operationKey: string, + attributes: Object + ): Promise; + failFeatureOperation( + name: string, + operationKey: string, + reason: string, + attributes: Object + ): Promise; +} + +export const getNativeDdRum = (): DdRum | null => + TurboModuleRegistry.get('DdRum') ?? null; diff --git a/packages/react-native-vega/src/turbo-modules/NativeDdSdk.ts b/packages/react-native-vega/src/turbo-modules/NativeDdSdk.ts index 8d3106dac..3cc5c1e9f 100644 --- a/packages/react-native-vega/src/turbo-modules/NativeDdSdk.ts +++ b/packages/react-native-vega/src/turbo-modules/NativeDdSdk.ts @@ -7,7 +7,12 @@ import type { KeplerTurboModule } from '@amazon-devices/keplerscript-turbomodule-api'; import { TurboModuleRegistry } from '@amazon-devices/keplerscript-turbomodule-api'; +/* eslint-disable @typescript-eslint/ban-types */ export interface DdSdk extends KeplerTurboModule { + addAttribute(key: string, value: Object): Promise; + removeAttribute(key: string): Promise; + addAttributes(attributes: Object): Promise; + removeAttributes(keys: string[]): Promise; startHttpProxy(): Promise; stopHttpProxy(): Promise; httpResponse(requestId: string, statusCode: number): Promise; diff --git a/packages/react-native-vega/tests/AttributeConverterTest.cpp b/packages/react-native-vega/tests/AttributeConverterTest.cpp new file mode 100644 index 000000000..b38ab8467 --- /dev/null +++ b/packages/react-native-vega/tests/AttributeConverterTest.cpp @@ -0,0 +1,107 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// +// This product includes software developed at Datadog +// (https://www.datadoghq.com/). Copyright 2016-Present Datadog, Inc. + +#include "AttributeConverter.h" +#include "EnumParsers.h" +#include +#include +#include +#include +#include + +using namespace com::amazon::kepler::turbomodule; +using namespace datadog_rn_vega; +using datadog::ValueType; + +void check(bool condition) { + if (!condition) throw std::runtime_error("Attribute conversion mismatch"); +} + +template +void checkCasings(Parser parse, + std::initializer_list> cases) { + for (const auto& [token, expected] : cases) { + std::string lowercase = token; + std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(), + [](unsigned char c) { return std::tolower(c); }); + std::string mixed = lowercase; + mixed.front() = token.front(); + for (const auto& spelling : {token, lowercase, mixed}) { + if (parse(spelling) != expected) { + throw std::runtime_error( + "Unexpected enum mapping for: " + spelling); + } + } + } +} + +int main() { + check(jsValueToAttribute(nullptr).GetType() == ValueType::Null); + check(jsValueToAttribute(false).GetType() == ValueType::Bool); + check(!jsValueToAttribute(false).GetBoolValue()); + check(jsValueToAttribute(true).GetBoolValue()); + for (const JSValue& value : {JSValue(int8_t(-2)), JSValue(int16_t(-2)), + JSValue(int32_t(-2)), JSValue(int64_t(-2))}) { + const auto attribute = jsValueToAttribute(value); + check(attribute.GetType() == ValueType::Int); + check(attribute.GetIntValue() == -2); + } + for (const JSValue& value : {JSValue(uint8_t(2)), JSValue(uint16_t(2)), + JSValue(uint32_t(2))}) { + check(jsValueToAttribute(value).GetType() == ValueType::UInt); + check(jsValueToAttribute(value).GetUIntValue() == 2); + } + check(jsValueToAttribute(1.25f).GetDoubleValue() == 1.25); + check(jsValueToAttribute(2.5).GetDoubleValue() == 2.5); + check(jsValueToAttribute(std::string("text")).GetStringValue() == "text"); + const JSObject nested{{"value", false}}; + const JSArray values{nullptr, 0.0, std::string("text"), nested}; + const auto converted = jsObjectToAttribute({{"array", values}, {"object", nested}}); + const auto array = converted.GetObjectProperty("array"); + check(array.GetType() == ValueType::Array && array.GetArrayLen() == 4); + check(array.GetArrayItem(0).GetType() == ValueType::Null); + check(array.GetArrayItem(1).GetDoubleValue() == 0.0); + check(array.GetArrayItem(2).GetStringValue() == "text"); + check(array.GetArrayItem(3).GetObjectProperty("value").GetType() == ValueType::Bool); + check(converted.GetObjectProperty("object").GetObjectProperty("value").GetType() == ValueType::Bool); + check(jsWrappedValueToAttribute({{"value", nested}}).GetObjectProperty("value").GetType() == ValueType::Bool); + check(jsWrappedValueToAttribute({{"value", nullptr}}).GetType() == ValueType::Null); + check(jsWrappedValueToAttribute({}).GetType() == ValueType::Object); + check(jsValueToAttribute(JSArray{}).GetArrayLen() == 0); + check(jsObjectToAttribute({}).GetObjectPropertyCount() == 0); + + using Action = datadog::RumActionType; + checkCasings(parseActionType, { + {"TAP", Action::Tap}, {"SCROLL", Action::Scroll}, + {"SWIPE", Action::Swipe}, {"BACK", Action::Custom}, + {"CUSTOM", Action::Custom}, {"CLICK", Action::Click} + }); + using Method = datadog::RumResourceMethod; + checkCasings(parseResourceMethod, { + {"GET", Method::Get}, {"POST", Method::Post}, {"PUT", Method::Put}, + {"DELETE", Method::Delete}, {"HEAD", Method::Head}, + {"PATCH", Method::Patch}, {"OPTIONS", Method::Options}, + {"CONNECT", Method::Connect}, {"TRACE", Method::Trace} + }); + using Source = datadog::RumErrorSource; + checkCasings(parseErrorSource, { + {"NETWORK", Source::Network}, {"SOURCE", Source::Source}, + {"CONSOLE", Source::Console}, {"WEBVIEW", Source::Webview}, + {"CUSTOM", Source::Custom}, {"LOGGER", Source::Logger}, + {"AGENT", Source::Agent}, {"REPORT", Source::Report} + }); + for (const auto& unknown : {"", "unknown", "PoSt-invalid", "\xC3\xA9"}) { + check(parseActionType(unknown) == Action::Custom); + check(parseResourceMethod(unknown) == Method::Get); + check(parseErrorSource(unknown) == Source::Custom); + } + check(parseResourceType("fetch") == datadog::RumResourceType::Fetch); + check(parseResourceType("xhr") == datadog::RumResourceType::Xhr); + check(parseResourceType("invalid") == datadog::RumResourceType::Unknown); + check(parseOperationFailureReason("ERROR") == datadog::RumOperationFailureReason::Error); + check(parseOperationFailureReason("ABANDONED") == datadog::RumOperationFailureReason::Abandoned); + check(parseOperationFailureReason("OTHER") == datadog::RumOperationFailureReason::Other); +} diff --git a/packages/react-native-vega/tests/CMakeLists.txt b/packages/react-native-vega/tests/CMakeLists.txt index f75eca9b6..0ba678da1 100644 --- a/packages/react-native-vega/tests/CMakeLists.txt +++ b/packages/react-native-vega/tests/CMakeLists.txt @@ -16,3 +16,29 @@ target_compile_features(HttpJsProxyTest PRIVATE cxx_std_17) target_link_libraries(HttpJsProxyTest PRIVATE Threads::Threads) add_test(NAME HttpJsProxy COMMAND HttpJsProxyTest) set_tests_properties(HttpJsProxy PROPERTIES TIMEOUT 10) + +# Optional host coverage with the real SDK sources and Kepler headers. Keep the +# transport tests usable without either SDK installed. +set(DATADOG_CPP_SOURCE_DIR "" CACHE PATH "Path to dd-sdk-cpp 0.7.0 sources") +set(KEPLER_INCLUDE_DIR "" CACHE PATH "Include root containing Kepler and JsonContainer") +set(NODE_API_INCLUDE_DIR "" CACHE PATH "Include root containing node_api.h") +if(DATADOG_CPP_SOURCE_DIR AND KEPLER_INCLUDE_DIR AND NODE_API_INCLUDE_DIR) + add_executable(AttributeConverterTest + AttributeConverterTest.cpp + ../kepler/turbo-modules/AttributeConverter.cpp + ../kepler/turbo-modules/EnumParsers.cpp + ${DATADOG_CPP_SOURCE_DIR}/src/datadog/cpp/attribute.cpp + ${DATADOG_CPP_SOURCE_DIR}/src/datadog/cpp/uuid.cpp + ../kepler/turbo-modules/uuid_urandom.cpp + ${DATADOG_CPP_SOURCE_DIR}/src/datadog/impl/core/attribute/cow.cpp) + target_compile_features(AttributeConverterTest PRIVATE cxx_std_17) + target_compile_definitions(AttributeConverterTest PRIVATE NDEBUG) + target_include_directories(AttributeConverterTest PRIVATE + ../kepler/turbo-modules + ${DATADOG_CPP_SOURCE_DIR}/include-cpp + ${DATADOG_CPP_SOURCE_DIR}/src + ${KEPLER_INCLUDE_DIR} + ${KEPLER_INCLUDE_DIR}/Kepler/turbomodule + ${NODE_API_INCLUDE_DIR}) + add_test(NAME AttributeConverter COMMAND AttributeConverterTest) +endif() diff --git a/packages/react-native-vega/tsconfig.build.json b/packages/react-native-vega/tsconfig.build.json index 9bf5b455b..e405210de 100644 --- a/packages/react-native-vega/tsconfig.build.json +++ b/packages/react-native-vega/tsconfig.build.json @@ -1,4 +1,10 @@ { "extends": "./tsconfig", - "include": ["src"] + "include": [ + "src" + ], + "compilerOptions": { + "rootDir": "src", + "paths": {} + } }