Open Suraj-Gupta-ML opened 2 years ago
Hi @suraj-dot-gupta,
At present, RUM agent is not supported on react-native and there are no plans yet for so.
Despite so, I would like to provide you with a workaround:
Disclaimer: there is no guarantee that this is going to work perfectly. It's not thoroughly tested, this is just to provide you a solution that works on the basic level. (E.g. page-load instrumentation have to be disabled)
--
elastic-apm-rum-react-native-adapter.js
// Setup that allow the agent initialisation
window.addEventListener = function noop() {};
window.location = {
href: 'https://write-here-some-meaningful-but-static-url.yours/'
};
global.document = {
currentScript: null,
location: {
href: 'https://write-here-some-meaningful-but-static-url.yours/'
},
getElementsByTagName() {
return {
length: 0
}
}
};
react native main file:
import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
// IMPORTS RELATED WITH ELASTIC START HERE:
// this is a small implementation of crypto.getRandomValues
// without this the transaction/span ids will not be generated
import 'react-native-get-random-values'
import './elastic-apm-rum-react-native-adapter.js'; // <--- load it before the RUM agent
import '@elastic/apm-rum';
elasticApm.init({
serviceName: "react-native-app-demo-rum-v1",
serverUrl: 'yourUrl',
disableInstrumentations: ['page-load']
})
export default function App() {
// Custom transaction
const transaction = elasticApm.startTransaction("my-transaction", "custom")
const span = transaction.startSpan("my-span", "my-span-type")
// just an example of how to use the API
setTimeout(() => {
span.end()
}, 300)
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Change code in the editor and watch it change on your phone! Save to get a shareable url.
</Text>
<Card>
<AssetExample />
</Card>
<Button
onPress={() => {
fetch('https://snack.expo.dev/') // just an example of http request
}}
title="Press Me"
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
You have also access to the react native example application through this link.
I have also recorded a video where you can see how the created transactions are visible in Kibana. You will see:
Thanks, Alberto
Thanks @devcorpio for your time. I tested by adding custom transactions in navigation using above code. It worked well. one doubt how can we create a function or service so that all route navigation and user actions are captured without explicity writing transactions for each actions.
Thanks for the help!!
Hi @suraj-dot-gupta,
Glad it worked!
how can we create a function or service so that all route navigation and user actions are captured without explicity writing transactions for each actions.
Something that you could check is if the react router/navigation library that you are using emits events similar to "navigationstarted" and "navigationended", then you could create your own listeners and write there a "generic" logic to handle the transactions creation.
Perhaps this article about the reactnavigation lifecycle helps you
Making your application available (or a sample application with the same flows as yours) in https://snack.expo.dev/ would be very helpful to investigate this scenario.
Also something that I would like to highlight: Given that RUM agent is not currently supported on a react-native-app there is still territory pending to explore for us on that area, that's why I cannot be more specific at the moment.
Thanks, Alberto
Sorry to ask on this thread, but i think better to ask here -
Hello @devcorpio ,
We are testing react-native integration here - seems it works for Expo (the workaround up there), trace data sent, but it's not working for pure react native (trace data didn't get received by APM server). Do you have any workaround for this case? Because we need to have full control of the app on RN pure.
Thank you.
Hi @freefiona85,
Do you have any workaround for this case?
Unfortunately, for this particular case, we don't have any specific workaround.
Have you tried to debug the application (with the workaround enabled) with something like React Native Debugger?
For instance, something that might help:
elasticApm.serviceFactory.instances.ConfigService.config
?elasticApm._disable
and elasticApm._initialized
Thanks, Alberto
elasticApm.serviceFactory.instances.ConfigService.config :
{serviceName: "
Is the network request to the APM server failing for any reason? There is no network request to the APM server failing. The request isn't reaching APM server on react-native, but works on expo. no alert that the network request is failing, logs are there.
Maybe you can help @devcorpio - the debug result is above (redacted specific things). Also we do tests using non-credentials at the moment.
Thank you.
Hi @freefiona85,
Thanks for looking at those details!
Looking at the data, I can see the agent is active and initialized.
The request isn't reaching APM server on react-native, but works on expo. no alert that the network request is failing, logs are there.
Unfortunately, I cannot help so much here since the RUM agent is not supported on react-native. Despite so, I would like to propose possible strategies for debugging in case help you:
On the networking docs of React Native I see a few warnings:
The docs give details of how to solve such issues.
Can one of those warnings be the cause of your issue? (For some reason they don't apply in Expo)
Thanks, Alberto
Ahh @devcorpio - so you do need the APM server to be on https too? At the moment, we do it on port 443 BUT not on https - though, the APM RUM server is hosted externally with HTTPS.
Is it possible that it needs the full traffic to be https, from the loadbalancer to the APM server? Because as far as react-native knows, it's on https (the external URL is HTTPS).
This explains some details though. We'll do more digging too.
Hello @devcorpio ,
At the moment we succeeded in sending data to APM RUM. The issue before is the clashing from XHR and Fetch, and clashing with debugging app. It's still http-request only though. We want to see if it's possible to capture user-interaction or page-load. Will let you know. Thanks for your help!
Hi @freefiona85,
Were you able to capture user-interaction and page-load, too?
Thanks, Alberto
Hi @devcorpio , how can we use the package in react-native.
Since react-native is not browser based, we cannot use this package.
i tried using this react-native package, but there router events and errors are not getting captured.
could you pls help in resolving the issue?.
Thanks!!