Skip to content

Commit b03c38f

Browse files
committed
Make initializePerformance() idempotent
1 parent 3871f26 commit b03c38f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages-exp/performance-exp/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ export function initializePerformance(
7979
// throw if an instance was already created.
8080
// It could happen if initializePerformance() is called more than once, or getPerformance() is called first.
8181
if (provider.isInitialized()) {
82-
throw ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
82+
const existingInstance = provider.getImmediate();
83+
const initialSettings = provider.getOptions() as PerformanceSettings;
84+
if (
85+
settings?.dataCollectionEnabled === initialSettings?.dataCollectionEnabled &&
86+
settings?.instrumentationEnabled === initialSettings?.instrumentationEnabled
87+
) {
88+
return existingInstance;
89+
} else {
90+
throw ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
91+
}
8392
}
8493

8594
const perfInstance = provider.initialize({

packages-exp/performance-exp/src/utils/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
6060
'Custom metric name {$customMetricName} is invalid',
6161
[ErrorCode.INVALID_STRING_MERGER_PARAMETER]:
6262
'Input for String merger is invalid, contact support team to resolve.',
63-
[ErrorCode.ALREADY_INITIALIZED]: 'Performance can only be initialized once.'
63+
[ErrorCode.ALREADY_INITIALIZED]:
64+
'initializePerformance() has already been called with ' +
65+
'different options. To avoid this error, call initializePerformance() with the ' +
66+
'same options as when it was originally called, or call getPerformance() to return the' +
67+
' already initialized instance.'
6468
};
6569

6670
interface ErrorParams {

0 commit comments

Comments
 (0)