Skip to content

Commit 8082af1

Browse files
alyezzAliAbdelfattah
authored andcommitted
Add new APM module
1 parent bb7aefd commit 8082af1

File tree

6 files changed

+294
-0
lines changed

6 files changed

+294
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
package com.instabug.reactlibrary;
3+
4+
import android.os.Handler;
5+
import android.os.Looper;
6+
7+
import com.facebook.react.bridge.Callback;
8+
import com.facebook.react.bridge.ReactApplicationContext;
9+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
10+
import com.facebook.react.bridge.ReactMethod;
11+
import com.instabug.apm.APM;
12+
import com.instabug.apm.model.Trace;
13+
import com.instabug.bug.BugReporting;
14+
import com.instabug.chat.Chats;
15+
import com.instabug.library.Feature;
16+
import com.instabug.reactlibrary.utils.InstabugUtil;
17+
import com.instabug.reactlibrary.utils.MainThreadHandler;
18+
19+
import java.util.HashMap;
20+
21+
import javax.annotation.Nonnull;
22+
23+
public class RNInstabugAPMModule extends ReactContextBaseJavaModule {
24+
25+
public RNInstabugAPMModule(ReactApplicationContext reactApplicationContext) {
26+
super(reactApplicationContext);
27+
}
28+
HashMap<String, Trace> traces = new HashMap<String, Trace>();
29+
30+
@Nonnull
31+
@Override
32+
public String getName() {
33+
return "IBGAPM";
34+
}
35+
36+
/**
37+
* Enables or disables the feature Chats.
38+
* @param isEnabled boolean indicating enabled or disabled.
39+
*/
40+
@ReactMethod
41+
public void setEnabled(final boolean isEnabled) {
42+
MainThreadHandler.runOnMainThread(new Runnable() {
43+
@Override
44+
public void run() {
45+
try {
46+
APM.setEnabled(isEnabled);
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
});
52+
}
53+
54+
/**
55+
* Enables or disables the feature Chats.
56+
* @param isEnabled boolean indicating enabled or disabled.
57+
*/
58+
@ReactMethod
59+
public void setAppLaunchEnabled(final boolean isEnabled) {
60+
MainThreadHandler.runOnMainThread(new Runnable() {
61+
@Override
62+
public void run() {
63+
try {
64+
APM.setAppLaunchEnabled(isEnabled);
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
});
70+
}
71+
72+
/**
73+
* Starts a trace
74+
* @param name string name of the trace.
75+
*/
76+
@ReactMethod
77+
public void startTrace(final String name, final String id) {
78+
MainThreadHandler.runOnMainThread(new Runnable() {
79+
@Override
80+
public void run() {
81+
try {
82+
Trace trace = APM.startTrace(name);
83+
traces.put(id,trace);
84+
} catch (Exception e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
});
89+
}
90+
91+
/**
92+
* Starts a trace
93+
* @param id String id of the trace.
94+
*/
95+
@ReactMethod
96+
public void setTraceAttribute(final String id, final String key, final String value) {
97+
MainThreadHandler.runOnMainThread(new Runnable() {
98+
@Override
99+
public void run() {
100+
try {
101+
traces.get(id).setAttribute(key, value);
102+
} catch (Exception e) {
103+
e.printStackTrace();
104+
}
105+
}
106+
});
107+
}
108+
109+
/**
110+
* Starts a trace
111+
* @param id string id of the trace.
112+
*/
113+
@ReactMethod
114+
public void endTrace(final String id) {
115+
MainThreadHandler.runOnMainThread(new Runnable() {
116+
@Override
117+
public void run() {
118+
try {
119+
traces.get(id).end();
120+
} catch (Exception e) {
121+
e.printStackTrace();
122+
}
123+
}
124+
});
125+
}
126+
127+
128+
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativePackage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
137137
modules.add(new RNInstabugFeatureRequestsModule(reactContext));
138138
modules.add(new RNInstabugRepliesModule(reactContext));
139139
modules.add(new RNInstabugChatsModule(reactContext));
140+
modules.add(new RNInstabugAPMModule(reactContext));
140141
return modules;
141142
}
142143

ios/RNInstabug/InstabugAPMBridge.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#import <Foundation/Foundation.h>
3+
#import <React/RCTBridgeModule.h>
4+
#import <React/RCTEventEmitter.h>
5+
#import <Instabug/IBGTypes.h>
6+
7+
@interface InstabugAPMBridge : RCTEventEmitter <RCTBridgeModule>
8+
/*
9+
+------------------------------------------------------------------------+
10+
| APM Module |
11+
+------------------------------------------------------------------------+
12+
*/
13+
14+
- (void)setEnabled:(BOOL)isEnabled;
15+
- (void)setAppLaunchEnabled:(BOOL)isEnabled;
16+
- (void)startTrace:(NSString *)name :(NSString *)id;
17+
- (void)setTraceAttribute:(NSString *)id :(NSString *)key :(NSString *)value;
18+
- (void)endTrace:(NSString *)id;
19+
20+
extern NSMutableDictionary *traces;
21+
22+
@end
23+

ios/RNInstabug/InstabugAPMBridge.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
#import "InstabugAPMBridge.h"
4+
#import <Instabug/IBGAPM.h>
5+
#import <Instabug/IBGTrace.h>
6+
#import <asl.h>
7+
#import <React/RCTLog.h>
8+
#import <os/log.h>
9+
#import <Instabug/IBGTypes.h>
10+
#import <React/RCTUIManager.h>
11+
12+
@implementation InstabugAPMBridge
13+
14+
- (dispatch_queue_t)methodQueue {
15+
return dispatch_get_main_queue();
16+
}
17+
18+
+ (BOOL)requiresMainQueueSetup
19+
{
20+
return NO;
21+
}
22+
23+
- (NSArray<NSString *> *)supportedEvents {
24+
return @[];
25+
}
26+
27+
RCT_EXPORT_MODULE(IBGAPM)
28+
29+
NSMutableDictionary *traces;
30+
31+
- (id) init
32+
{
33+
self = [super init];
34+
traces = [[NSMutableDictionary alloc] init];
35+
return self;
36+
}
37+
38+
RCT_EXPORT_METHOD(setEnabled:(BOOL)isEnabled) {
39+
IBGAPM.enabled = isEnabled;
40+
}
41+
42+
RCT_EXPORT_METHOD(setAppLaunchEnabled:(BOOL)isEnabled) {
43+
IBGAPM.appLaunchEnabled = isEnabled;
44+
}
45+
46+
RCT_EXPORT_METHOD(startTrace:(NSString *)name :(NSString *)id) {
47+
IBGTrace *trace = [IBGAPM startTraceWithName:name];
48+
[traces setObject: trace forKey: id];
49+
}
50+
51+
RCT_EXPORT_METHOD(setTraceAttribute:(NSString *)id :(NSString *)key :(NSString *)value) {
52+
IBGTrace *trace = [traces objectForKey:id];
53+
[trace setAttributeWithKey:key value:value];
54+
}
55+
56+
RCT_EXPORT_METHOD(endTrace:(NSString *)id) {
57+
IBGTrace *trace = [traces objectForKey:id];
58+
[trace end];
59+
}
60+
61+
62+
@synthesize description;
63+
64+
@synthesize hash;
65+
66+
@synthesize superclass;
67+
68+
@end
69+

models/Trace.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NativeModules } from 'react-native';
2+
let { IBGAPM } = NativeModules;
3+
4+
export default class Trace {
5+
6+
constructor(
7+
id,
8+
name,
9+
attributes,
10+
) {
11+
this.id = id;
12+
this.name = name ? name : "";
13+
this.attributes = attributes ? attributes : {};
14+
}
15+
16+
/**
17+
* Add an attribute with key and value to the Trace to be sent.
18+
* @param {string} key
19+
* @param {string} value
20+
*/
21+
setAttribute(key, value) {
22+
IBGAPM.setTraceAttribute(this.id, key, value);
23+
this.attributes[key] = value;
24+
}
25+
26+
/**
27+
* Add an attribute with key and value to the Trace to be sent.
28+
* @param {string} key
29+
* @param {string} value
30+
*/
31+
end() {
32+
IBGAPM.endTrace(this.id);
33+
}
34+
}

modules/APM.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
NativeModules
3+
} from 'react-native';
4+
import Trace from '../models/Trace';
5+
let { IBGAPM } = NativeModules;
6+
7+
/**
8+
* APM
9+
* @exports APM
10+
*/
11+
export default {
12+
13+
/**
14+
* Enables and disables APM
15+
* @param {boolean} isEnabled
16+
*/
17+
setEnabled(isEnabled) {
18+
IBGAPM.setEnabled(isEnabled);
19+
},
20+
21+
/**
22+
* Enables and disables App Launch
23+
* @param {boolean} isEnabled
24+
*/
25+
setAppLaunchEnabled(isEnabled) {
26+
IBGAPM.setEnabled(isEnabled);
27+
},
28+
29+
/**
30+
* Enables and disables App Launch
31+
* @param {boolean} isEnabled
32+
*/
33+
startTrace(name) {
34+
const id = Date.now() + "";
35+
IBGAPM.startTrace(name, id);
36+
return new Trace(id, name);
37+
},
38+
39+
}

0 commit comments

Comments
 (0)