@luciq/react-native 19.2.2 → 19.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +87 -0
  3. package/android/native.gradle +1 -1
  4. package/android/src/main/java/ai/luciq/reactlibrary/RNLuciqAPMModule.java +202 -117
  5. package/android/src/main/java/ai/luciq/reactlibrary/RNLuciqReactnativeModule.java +20 -0
  6. package/dist/constants/Strings.d.ts +9 -0
  7. package/dist/constants/Strings.js +12 -0
  8. package/dist/index.d.ts +2 -1
  9. package/dist/index.js +2 -1
  10. package/dist/models/CustomSpan.d.ts +47 -0
  11. package/dist/models/CustomSpan.js +82 -0
  12. package/dist/modules/APM.d.ts +58 -0
  13. package/dist/modules/APM.js +62 -0
  14. package/dist/native/NativeAPM.d.ts +3 -0
  15. package/dist/native/NativeLuciq.d.ts +1 -0
  16. package/dist/utils/CustomSpansManager.d.ts +38 -0
  17. package/dist/utils/CustomSpansManager.js +173 -0
  18. package/ios/RNLuciq/LuciqAPMBridge.h +13 -0
  19. package/ios/RNLuciq/LuciqAPMBridge.m +55 -0
  20. package/ios/RNLuciq/LuciqReactBridge.m +12 -0
  21. package/ios/RNLuciq/Util/LCQAPM+PrivateAPIs.h +1 -0
  22. package/ios/native.rb +1 -1
  23. package/package.json +1 -1
  24. package/scripts/releases/changelog_to_slack_formatter.sh +9 -0
  25. package/scripts/releases/get_job_approver.sh +60 -0
  26. package/scripts/releases/get_release_notes.sh +22 -0
  27. package/scripts/releases/get_sdk_version.sh +5 -0
  28. package/scripts/releases/get_slack_id_from_username.sh +24 -0
  29. package/src/constants/Strings.ts +24 -0
  30. package/src/index.ts +2 -0
  31. package/src/models/CustomSpan.ts +102 -0
  32. package/src/modules/APM.ts +72 -0
  33. package/src/native/NativeAPM.ts +7 -0
  34. package/src/native/NativeLuciq.ts +1 -0
  35. package/src/utils/CustomSpansManager.ts +202 -0
@@ -0,0 +1,24 @@
1
+ github_username=$1
2
+
3
+ case $github_username in
4
+ 'mzelzoghbi')
5
+ sid='U5697F4EL'
6
+ ;;
7
+ 'AndrewAminInstabug')
8
+ sid='U06JVRNMKE1'
9
+ ;;
10
+ 'ahmedAlaaInstabug')
11
+ sid='U06AE2G1161'
12
+ ;;
13
+ 'kholood-ea')
14
+ sid='U06SU2QR280'
15
+ ;;
16
+ 'AyaMahmoud148')
17
+ sid='U07GZSURC8K'
18
+ ;;
19
+ 'MoKamall')
20
+ sid='U06JHDS3JJK'
21
+ ;;
22
+ *)
23
+ esac
24
+ echo "$sid"
@@ -0,0 +1,24 @@
1
+ export class LuciqStrings {
2
+ static readonly customSpanAPMDisabledMessage: string =
3
+ 'APM is disabled, custom span not created. Please enable APM by following the instructions at this link:\n' +
4
+ 'https://docs.luciq.ai/product-guides-and-integrations/product-guides/application-performance-monitoring';
5
+
6
+ static readonly customSpanDisabled: string =
7
+ 'Custom span is disabled, custom span not created. Please enable Custom Span by following the instructions at this link:\n' +
8
+ 'https://docs.luciq.ai/product-guides-and-integrations/product-guides/application-performance-monitoring';
9
+
10
+ static readonly customSpanSDKNotInitializedMessage: string =
11
+ 'Luciq API was called before the SDK is built. To build it, first by following the instructions at this link:\n' +
12
+ 'https://docs.luciq.ai/product-guides-and-integrations/product-guides/application-performance-monitoring';
13
+
14
+ static readonly customSpanNameEmpty: string =
15
+ 'Custom span name cannot be empty. Please provide a valid name for the custom span.';
16
+
17
+ static readonly customSpanEndTimeBeforeStartTime: string =
18
+ 'Custom span end time must be after start time. Please provide a valid start and end time for the custom span.';
19
+
20
+ static readonly customSpanNameTruncated: string = 'Custom span name truncated to 150 characters';
21
+
22
+ static readonly customSpanLimitReached: string =
23
+ 'Maximum number of concurrent custom spans (100) reached. Please end some spans before starting new ones.';
24
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import type { LuciqConfig } from './models/LuciqConfig';
3
3
  import Report from './models/Report';
4
4
  import type { ThemeConfig } from './models/ThemeConfig';
5
+ import { CustomSpan } from './models/CustomSpan';
5
6
  // Modules
6
7
  import * as APM from './modules/APM';
7
8
  import * as BugReporting from './modules/BugReporting';
@@ -23,6 +24,7 @@ import type { SessionMetadata } from './models/SessionMetadata';
23
24
  export * from './utils/Enums';
24
25
  export {
25
26
  Report,
27
+ CustomSpan,
26
28
  APM,
27
29
  BugReporting,
28
30
  CrashReporting,
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Callback to unregister a span from tracking
3
+ */
4
+ type UnregisterCallback = (span: CustomSpan) => void;
5
+
6
+ /**
7
+ * Callback to sync span data to native SDK
8
+ */
9
+ type SyncCallback = (name: string, startTimestamp: number, endTimestamp: number) => Promise<void>;
10
+
11
+ /**
12
+ * Represents a custom span for performance tracking.
13
+ * A span measures the duration of an operation and reports it to the native SDK.
14
+ */
15
+ export class CustomSpan {
16
+ private name: string;
17
+ private startTime: number; // Date.now() in milliseconds
18
+ private startMonotonic: number; // performance.now() in milliseconds
19
+ private endTime?: number;
20
+ private duration?: number;
21
+ private hasEnded: boolean = false;
22
+ private endPromise?: Promise<void>;
23
+ private unregisterCallback: UnregisterCallback;
24
+ private syncCallback: SyncCallback;
25
+
26
+ /**
27
+ * Creates a new custom span. The span starts immediately upon creation.
28
+ * @internal - Use APM.startCustomSpan() instead
29
+ */
30
+ constructor(name: string, unregisterCallback: UnregisterCallback, syncCallback: SyncCallback) {
31
+ this.name = name;
32
+ this.startTime = Date.now();
33
+ this.startMonotonic = performance.now();
34
+ this.unregisterCallback = unregisterCallback;
35
+ this.syncCallback = syncCallback;
36
+ }
37
+
38
+ /**
39
+ * Ends this custom span and reports it to the native SDK.
40
+ * This method is idempotent - calling it multiple times is safe.
41
+ * Subsequent calls will wait for the first call to complete.
42
+ */
43
+ async end(): Promise<void> {
44
+ // Thread-safe check using Promise-based locking
45
+ if (this.hasEnded) {
46
+ if (this.endPromise) {
47
+ await this.endPromise;
48
+ }
49
+ return;
50
+ }
51
+
52
+ // Create lock and mark as ended
53
+ let resolveEnd: () => void;
54
+ this.endPromise = new Promise((resolve) => {
55
+ resolveEnd = resolve;
56
+ });
57
+ this.hasEnded = true;
58
+
59
+ try {
60
+ // Unregister from active spans
61
+ this.unregisterCallback(this);
62
+
63
+ // Calculate duration using monotonic clock
64
+ const endMonotonic = performance.now();
65
+ this.duration = endMonotonic - this.startMonotonic;
66
+
67
+ // Calculate end time using wall clock
68
+ this.endTime = this.startTime + this.duration;
69
+
70
+ // Convert to microseconds for native SDK
71
+ const startMicros = this.startTime * 1000;
72
+ const endMicros = this.endTime * 1000;
73
+
74
+ // Send to native SDK
75
+ await this.syncCallback(this.name, startMicros, endMicros);
76
+ } finally {
77
+ // Release lock
78
+ resolveEnd!();
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Get the span name
84
+ */
85
+ getName(): string {
86
+ return this.name;
87
+ }
88
+
89
+ /**
90
+ * Check if the span has ended
91
+ */
92
+ isEnded(): boolean {
93
+ return this.hasEnded;
94
+ }
95
+
96
+ /**
97
+ * Get the span duration in milliseconds (only available after end())
98
+ */
99
+ getDuration(): number | undefined {
100
+ return this.duration;
101
+ }
102
+ }
@@ -2,6 +2,11 @@ import { Platform } from 'react-native';
2
2
 
3
3
  import { NativeAPM } from '../native/NativeAPM';
4
4
  import { NativeLuciq } from '../native/NativeLuciq';
5
+ import {
6
+ startCustomSpan as startCustomSpanInternal,
7
+ addCompletedCustomSpan as addCompletedCustomSpanInternal,
8
+ } from '../utils/CustomSpansManager';
9
+ import type { CustomSpan } from '../models/CustomSpan';
5
10
 
6
11
  /**
7
12
  * Enables or disables APM
@@ -123,3 +128,70 @@ export const _lcqSleep = () => {
123
128
  export const setScreenRenderingEnabled = (isEnabled: boolean) => {
124
129
  NativeAPM.setScreenRenderingEnabled(isEnabled);
125
130
  };
131
+
132
+ /**
133
+ * Starts a custom span for performance tracking.
134
+ *
135
+ * A custom span measures the duration of an arbitrary operation that is not
136
+ * automatically tracked by the SDK. The span must be manually ended by calling
137
+ * the `end()` method on the returned span object.
138
+ *
139
+ * @param name - The name of the span. Cannot be empty. Max 150 characters.
140
+ * Leading and trailing whitespace will be trimmed.
141
+ *
142
+ * @returns Promise<CustomSpan | null> - The span object to end later, or null if:
143
+ * - Name is empty after trimming
144
+ * - SDK is not initialized
145
+ * - APM is disabled
146
+ * - Custom spans feature is disabled
147
+ * - Maximum concurrent spans limit (100) reached
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const span = await APM.startCustomSpan('Load User Profile');
152
+ * if (span) {
153
+ * try {
154
+ * // ... perform operation ...
155
+ * } finally {
156
+ * await span.end();
157
+ * }
158
+ * }
159
+ * ```
160
+ */
161
+ export const startCustomSpan = async (name: string): Promise<CustomSpan | null> => {
162
+ return startCustomSpanInternal(name);
163
+ };
164
+
165
+ /**
166
+ * Records a completed custom span with pre-recorded timestamps.
167
+ *
168
+ * Use this method when you have already recorded the start and end times
169
+ * of an operation and want to report it retroactively.
170
+ *
171
+ * @param name - The name of the span. Cannot be empty. Max 150 characters.
172
+ * Leading and trailing whitespace will be trimmed.
173
+ * @param startDate - The start time of the operation
174
+ * @param endDate - The end time of the operation (must be after startDate)
175
+ *
176
+ * @returns Promise<void> - Resolves when the span has been recorded, or logs error if:
177
+ * - Name is empty after trimming
178
+ * - End date is not after start date
179
+ * - SDK is not initialized
180
+ * - APM is disabled
181
+ * - Custom spans feature is disabled
182
+ *
183
+ * @example
184
+ * ```typescript
185
+ * const start = new Date();
186
+ * // ... operation already completed ...
187
+ * const end = new Date();
188
+ * await APM.addCompletedCustomSpan('Cache Lookup', start, end);
189
+ * ```
190
+ */
191
+ export const addCompletedCustomSpan = async (
192
+ name: string,
193
+ startDate: Date,
194
+ endDate: Date,
195
+ ): Promise<void> => {
196
+ return addCompletedCustomSpanInternal(name, startDate, endDate);
197
+ };
@@ -47,6 +47,13 @@ export interface ApmNativeModule extends NativeModule {
47
47
 
48
48
  // Screen Rendering //
49
49
  setScreenRenderingEnabled(isEnabled: boolean): void;
50
+
51
+ // Custom Spans APIs //
52
+ syncCustomSpan(name: string, startTimestamp: number, endTimestamp: number): Promise<void>;
53
+
54
+ isCustomSpanEnabled(): Promise<boolean>;
55
+
56
+ isAPMEnabled(): Promise<boolean>;
50
57
  }
51
58
 
52
59
  export const NativeAPM = NativeModules.LCQAPM;
@@ -22,6 +22,7 @@ export interface LuciqNativeModule extends NativeModule {
22
22
 
23
23
  // Essential APIs //
24
24
  setEnabled(isEnabled: boolean): void;
25
+ isBuilt(): Promise<boolean>;
25
26
  init(
26
27
  token: string,
27
28
  invocationEvents: InvocationEvent[],
@@ -0,0 +1,202 @@
1
+ import { NativeAPM } from '../native/NativeAPM';
2
+ import { NativeLuciq } from '../native/NativeLuciq';
3
+ import { CustomSpan } from '../models/CustomSpan';
4
+ import { LuciqStrings } from '../constants/Strings';
5
+
6
+ /**
7
+ * Tracks currently active custom spans
8
+ * @internal
9
+ */
10
+ const activeSpans = new Set<CustomSpan>();
11
+
12
+ /**
13
+ * Maximum concurrent custom spans allowed at any time
14
+ * @internal
15
+ */
16
+ const MAX_CONCURRENT_SPANS = 100;
17
+
18
+ /**
19
+ * Internal: unregister a span from active tracking
20
+ * @internal
21
+ */
22
+ const unregisterSpan = (span: CustomSpan): void => {
23
+ activeSpans.delete(span);
24
+ };
25
+
26
+ /**
27
+ * Internal: sync custom span data to native SDK
28
+ * @internal
29
+ */
30
+ const syncCustomSpan = async (
31
+ name: string,
32
+ startTimestamp: number,
33
+ endTimestamp: number,
34
+ ): Promise<void> => {
35
+ // Validate inputs (safety net)
36
+ if (!name || name.trim().length === 0) {
37
+ console.error(LuciqStrings.customSpanNameEmpty);
38
+ return;
39
+ }
40
+
41
+ if (endTimestamp <= startTimestamp) {
42
+ console.error(LuciqStrings.customSpanEndTimeBeforeStartTime);
43
+ return;
44
+ }
45
+
46
+ // Truncate name if needed (safety net)
47
+ let spanName = name.trim();
48
+ if (spanName.length > 150) {
49
+ spanName = spanName.substring(0, 150);
50
+ }
51
+
52
+ await NativeAPM.syncCustomSpan(spanName, startTimestamp, endTimestamp);
53
+ };
54
+
55
+ /**
56
+ * Starts a custom span for performance tracking.
57
+ *
58
+ * A custom span measures the duration of an arbitrary operation that is not
59
+ * automatically tracked by the SDK. The span must be manually ended by calling
60
+ * the `end()` method on the returned span object.
61
+ *
62
+ * @param name - The name of the span. Cannot be empty. Max 150 characters.
63
+ * Leading and trailing whitespace will be trimmed.
64
+ *
65
+ * @returns Promise<CustomSpan | null> - The span object to end later, or null if:
66
+ * - Name is empty after trimming
67
+ * - SDK is not initialized
68
+ * - APM is disabled
69
+ * - Custom spans feature is disabled
70
+ * - Maximum concurrent spans limit (100) reached
71
+ */
72
+ export const startCustomSpan = async (name: string): Promise<CustomSpan | null> => {
73
+ try {
74
+ // Validate name
75
+ const trimmedName = name.trim();
76
+ if (trimmedName.length === 0) {
77
+ console.error(LuciqStrings.customSpanNameEmpty);
78
+ return null;
79
+ }
80
+
81
+ // Check SDK initialization
82
+ const isInitialized = await NativeLuciq.isBuilt();
83
+ if (!isInitialized) {
84
+ console.error(LuciqStrings.customSpanSDKNotInitializedMessage);
85
+ return null;
86
+ }
87
+
88
+ // Check APM enabled
89
+ const isAPMEnabled = await NativeAPM.isAPMEnabled();
90
+ if (!isAPMEnabled) {
91
+ console.log(LuciqStrings.customSpanAPMDisabledMessage);
92
+ return null;
93
+ }
94
+
95
+ // Check custom spans enabled
96
+ const isCustomSpanEnabled = await NativeAPM.isCustomSpanEnabled();
97
+ if (!isCustomSpanEnabled) {
98
+ console.log(LuciqStrings.customSpanDisabled);
99
+ return null;
100
+ }
101
+
102
+ // Check concurrent span limit
103
+ if (activeSpans.size >= MAX_CONCURRENT_SPANS) {
104
+ console.error(LuciqStrings.customSpanLimitReached);
105
+ return null;
106
+ }
107
+
108
+ // Truncate name if needed
109
+ let spanName = trimmedName;
110
+ if (spanName.length > 150) {
111
+ spanName = spanName.substring(0, 150);
112
+ console.log(LuciqStrings.customSpanNameTruncated);
113
+ }
114
+
115
+ // Create and register span with callbacks
116
+ const span = new CustomSpan(spanName, unregisterSpan, syncCustomSpan);
117
+ activeSpans.add(span);
118
+ return span;
119
+ } catch (error) {
120
+ console.error('[CustomSpan] Error starting span:', error);
121
+ return null;
122
+ }
123
+ };
124
+
125
+ /**
126
+ * Records a completed custom span with pre-recorded timestamps.
127
+ *
128
+ * Use this method when you have already recorded the start and end times
129
+ * of an operation and want to report it retroactively.
130
+ *
131
+ * @param name - The name of the span. Cannot be empty. Max 150 characters.
132
+ * Leading and trailing whitespace will be trimmed.
133
+ * @param startDate - The start time of the operation
134
+ * @param endDate - The end time of the operation (must be after startDate)
135
+ *
136
+ * @returns Promise<void>
137
+ */
138
+ export const addCompletedCustomSpan = async (
139
+ name: string,
140
+ startDate: Date,
141
+ endDate: Date,
142
+ ): Promise<void> => {
143
+ try {
144
+ // Validate name
145
+ const trimmedName = name.trim();
146
+ if (trimmedName.length === 0) {
147
+ console.error(LuciqStrings.customSpanNameEmpty);
148
+ return;
149
+ }
150
+
151
+ // Validate timestamps
152
+ if (endDate <= startDate) {
153
+ console.error(LuciqStrings.customSpanEndTimeBeforeStartTime);
154
+ return;
155
+ }
156
+
157
+ // Check SDK initialization
158
+ const isInitialized = await NativeLuciq.isBuilt();
159
+ if (!isInitialized) {
160
+ console.error(LuciqStrings.customSpanSDKNotInitializedMessage);
161
+ return;
162
+ }
163
+
164
+ // Check APM enabled
165
+ const isAPMEnabled = await NativeAPM.isAPMEnabled();
166
+ if (!isAPMEnabled) {
167
+ console.log(LuciqStrings.customSpanAPMDisabledMessage);
168
+ return;
169
+ }
170
+
171
+ // Check custom spans enabled
172
+ const isCustomSpanEnabled = await NativeAPM.isCustomSpanEnabled();
173
+ if (!isCustomSpanEnabled) {
174
+ console.log(LuciqStrings.customSpanDisabled);
175
+ return;
176
+ }
177
+
178
+ // Truncate name if needed
179
+ let spanName = trimmedName;
180
+ if (spanName.length > 150) {
181
+ spanName = spanName.substring(0, 150);
182
+ console.log(LuciqStrings.customSpanNameTruncated);
183
+ }
184
+
185
+ // Convert to microseconds
186
+ const startMicros = startDate.getTime() * 1000;
187
+ const endMicros = endDate.getTime() * 1000;
188
+
189
+ // Send to native SDK
190
+ await syncCustomSpan(spanName, startMicros, endMicros);
191
+ } catch (error) {
192
+ console.error('[CustomSpan] Error adding completed span:', error);
193
+ }
194
+ };
195
+
196
+ /**
197
+ * Test-only helper to clear active spans between tests.
198
+ * @internal
199
+ */
200
+ export const __resetCustomSpansForTests = (): void => {
201
+ activeSpans.clear();
202
+ };