3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
import { FEATURE_NAMES } from '../features/features'
6
- import { getInfo , setInfo } from '../../common/config/info'
7
- import { getRuntime } from '../../common/config/runtime'
6
+ import { setInfo } from '../../common/config/info'
8
7
import { handle } from '../../common/event-emitter/handle'
9
- import { ee } from '../../common/event-emitter/contextual-ee'
10
8
import { drain , registerDrain } from '../../common/drain/drain'
11
9
import { onWindowLoad } from '../../common/window/load'
12
10
import { isBrowserScope } from '../../common/constants/runtime'
@@ -31,11 +29,11 @@ export function setTopLevelCallers () {
31
29
32
30
function caller ( fnName , ...args ) {
33
31
let returnVals = [ ]
34
- Object . values ( nr . initializedAgents ) . forEach ( val => {
35
- if ( ! val || ! val . api || ! val . runtime ) {
32
+ Object . values ( nr . initializedAgents ) . forEach ( agt => {
33
+ if ( ! agt || ! agt . runtime ) {
36
34
warn ( 38 , fnName )
37
- } else if ( val . exposed && val . api [ fnName ] && val . runtime . loaderType !== 'micro-agent' ) {
38
- returnVals . push ( val . api [ fnName ] ( ...args ) )
35
+ } else if ( agt . exposed && agt [ fnName ] && agt . runtime . loaderType !== 'micro-agent' ) {
36
+ returnVals . push ( agt [ fnName ] ( ...args ) )
39
37
}
40
38
} )
41
39
return returnVals [ 0 ]
@@ -44,42 +42,40 @@ export function setTopLevelCallers () {
44
42
45
43
const replayRunning = { }
46
44
47
- export function setAPI ( agentIdentifier , forceDrain , runSoftNavOverSpa = false ) {
48
- if ( ! forceDrain ) registerDrain ( agentIdentifier , 'api' )
49
- const apiInterface = { }
50
- var instanceEE = ee . get ( agentIdentifier )
51
- var tracerEE = instanceEE . get ( 'tracer' )
45
+ export function setAPI ( agent , forceDrain ) {
46
+ if ( ! forceDrain ) registerDrain ( agent . agentIdentifier , 'api' )
47
+ const tracerEE = agent . ee . get ( 'tracer' )
52
48
53
- replayRunning [ agentIdentifier ] = MODE . OFF
49
+ replayRunning [ agent . agentIdentifier ] = MODE . OFF
54
50
55
- instanceEE . on ( SR_EVENT_EMITTER_TYPES . REPLAY_RUNNING , ( isRunning ) => {
56
- replayRunning [ agentIdentifier ] = isRunning
51
+ agent . ee . on ( SR_EVENT_EMITTER_TYPES . REPLAY_RUNNING , ( isRunning ) => {
52
+ replayRunning [ agent . agentIdentifier ] = isRunning
57
53
} )
58
54
59
- var prefix = 'api-'
60
- var spaPrefix = prefix + 'ixn-'
55
+ const prefix = 'api-'
56
+ const spaPrefix = prefix + 'ixn-'
61
57
62
- apiInterface . log = function ( message , { customAttributes = { } , level = LOG_LEVELS . INFO } = { } ) {
63
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/log/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
64
- bufferLog ( instanceEE , message , customAttributes , level )
58
+ agent . log = function ( message , { customAttributes = { } , level = LOG_LEVELS . INFO } = { } ) {
59
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/log/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
60
+ bufferLog ( agent . ee , message , customAttributes , level )
65
61
}
66
62
67
- apiInterface . wrapLogger = ( parent , functionName , { customAttributes = { } , level = LOG_LEVELS . INFO } = { } ) => {
68
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/wrapLogger/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
69
- wrapLogger ( instanceEE , parent , functionName , { customAttributes, level } )
63
+ agent . wrapLogger = ( parent , functionName , { customAttributes = { } , level = LOG_LEVELS . INFO } = { } ) => {
64
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/wrapLogger/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
65
+ wrapLogger ( agent . ee , parent , functionName , { customAttributes, level } )
70
66
}
71
67
72
68
// Setup stub functions that queue calls for later processing.
73
- asyncApiMethods . forEach ( fnName => { apiInterface [ fnName ] = apiCall ( prefix , fnName , true , 'api' ) } )
69
+ asyncApiMethods . forEach ( fnName => { agent [ fnName ] = apiCall ( prefix , fnName , true , 'api' ) } )
74
70
75
- apiInterface . addPageAction = apiCall ( prefix , 'addPageAction' , true , FEATURE_NAMES . genericEvents )
71
+ agent . addPageAction = apiCall ( prefix , 'addPageAction' , true , FEATURE_NAMES . genericEvents )
76
72
77
- apiInterface . recordCustomEvent = apiCall ( prefix , 'recordCustomEvent' , true , FEATURE_NAMES . genericEvents )
73
+ agent . recordCustomEvent = apiCall ( prefix , 'recordCustomEvent' , true , FEATURE_NAMES . genericEvents )
78
74
79
- apiInterface . setPageViewName = function ( name , host ) {
75
+ agent . setPageViewName = function ( name , host ) {
80
76
if ( typeof name !== 'string' ) return
81
77
if ( name . charAt ( 0 ) !== '/' ) name = '/' + name
82
- getRuntime ( agentIdentifier ) . customTransaction = ( host || 'http://custom.transaction' ) + name
78
+ agent . runtime . customTransaction = ( host || 'http://custom.transaction' ) + name
83
79
return apiCall ( prefix , 'setPageViewName' , true ) ( )
84
80
}
85
81
@@ -92,15 +88,15 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
92
88
* @returns @see apiCall
93
89
*/
94
90
function appendJsAttribute ( key , value , apiName , addToBrowserStorage ) {
95
- const currentInfo = getInfo ( agentIdentifier )
91
+ const currentInfo = agent . info
96
92
if ( value === null ) {
97
93
delete currentInfo . jsAttributes [ key ]
98
94
} else {
99
- setInfo ( agentIdentifier , { ...currentInfo , jsAttributes : { ...currentInfo . jsAttributes , [ key ] : value } } )
95
+ setInfo ( agent . agentIdentifier , { ...currentInfo , jsAttributes : { ...currentInfo . jsAttributes , [ key ] : value } } )
100
96
}
101
97
return apiCall ( prefix , apiName , true , ( ! ! addToBrowserStorage || value === null ) ? 'session' : undefined ) ( key , value )
102
98
}
103
- apiInterface . setCustomAttribute = function ( name , value , persistAttribute = false ) {
99
+ agent . setCustomAttribute = function ( name , value , persistAttribute = false ) {
104
100
if ( typeof name !== 'string' ) {
105
101
warn ( 39 , typeof name )
106
102
return
@@ -116,7 +112,7 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
116
112
* @param {string } value - unique user identifier; a null user id suggests none should exist
117
113
* @returns @see apiCall
118
114
*/
119
- apiInterface . setUserId = function ( value ) {
115
+ agent . setUserId = function ( value ) {
120
116
if ( ! ( typeof value === 'string' || value === null ) ) {
121
117
warn ( 41 , typeof value )
122
118
return
@@ -129,34 +125,34 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
129
125
* @param {string|null } value - Application version -- if null, will "unset" the value
130
126
* @returns @see apiCall
131
127
*/
132
- apiInterface . setApplicationVersion = function ( value ) {
128
+ agent . setApplicationVersion = function ( value ) {
133
129
if ( ! ( typeof value === 'string' || value === null ) ) {
134
130
warn ( 42 , typeof value )
135
131
return
136
132
}
137
133
return appendJsAttribute ( 'application.version' , value , 'setApplicationVersion' , false )
138
134
}
139
135
140
- apiInterface . start = ( ) => {
136
+ agent . start = ( ) => {
141
137
try {
142
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/start/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
143
- instanceEE . emit ( 'manual-start-all' )
138
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/start/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
139
+ agent . ee . emit ( 'manual-start-all' )
144
140
} catch ( err ) {
145
141
warn ( 23 , err )
146
142
}
147
143
}
148
144
149
- apiInterface [ SR_EVENT_EMITTER_TYPES . RECORD ] = function ( ) {
150
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/recordReplay/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
151
- handle ( SR_EVENT_EMITTER_TYPES . RECORD , [ ] , undefined , FEATURE_NAMES . sessionReplay , instanceEE )
145
+ agent [ SR_EVENT_EMITTER_TYPES . RECORD ] = function ( ) {
146
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/recordReplay/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
147
+ handle ( SR_EVENT_EMITTER_TYPES . RECORD , [ ] , undefined , FEATURE_NAMES . sessionReplay , agent . ee )
152
148
}
153
149
154
- apiInterface [ SR_EVENT_EMITTER_TYPES . PAUSE ] = function ( ) {
155
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/pauseReplay/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
156
- handle ( SR_EVENT_EMITTER_TYPES . PAUSE , [ ] , undefined , FEATURE_NAMES . sessionReplay , instanceEE )
150
+ agent [ SR_EVENT_EMITTER_TYPES . PAUSE ] = function ( ) {
151
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/pauseReplay/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
152
+ handle ( SR_EVENT_EMITTER_TYPES . PAUSE , [ ] , undefined , FEATURE_NAMES . sessionReplay , agent . ee )
157
153
}
158
154
159
- apiInterface . interaction = function ( options ) {
155
+ agent . interaction = function ( options ) {
160
156
return new InteractionHandle ( ) . get ( typeof options === 'object' ? options : { } )
161
157
}
162
158
@@ -167,9 +163,9 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
167
163
var contextStore = { }
168
164
var ixn = this
169
165
var hasCb = typeof cb === 'function'
170
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/createTracer/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
166
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/createTracer/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
171
167
// Soft navigations won't support Tracer nodes, but this fn should still work the same otherwise (e.g., run the orig cb).
172
- if ( ! runSoftNavOverSpa ) handle ( spaPrefix + 'tracer' , [ now ( ) , name , contextStore ] , ixn , FEATURE_NAMES . spa , instanceEE )
168
+ if ( ! agent . runSoftNavOverSpa ) handle ( spaPrefix + 'tracer' , [ now ( ) , name , contextStore ] , ixn , FEATURE_NAMES . spa , agent . ee )
173
169
return function ( ) {
174
170
tracerEE . emit ( ( hasCb ? '' : 'no-' ) + 'fn-start' , [ now ( ) , ixn , hasCb ] , contextStore )
175
171
if ( hasCb ) {
@@ -189,30 +185,30 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
189
185
}
190
186
191
187
; [ 'actionText' , 'setName' , 'setAttribute' , 'save' , 'ignore' , 'onEnd' , 'getContext' , 'end' , 'get' ] . forEach ( name => {
192
- InteractionApiProto [ name ] = apiCall ( spaPrefix , name , undefined , runSoftNavOverSpa ? FEATURE_NAMES . softNav : FEATURE_NAMES . spa )
188
+ InteractionApiProto [ name ] = apiCall ( spaPrefix , name , undefined , agent . runSoftNavOverSpa ? FEATURE_NAMES . softNav : FEATURE_NAMES . spa )
193
189
} )
194
- apiInterface . setCurrentRouteName = runSoftNavOverSpa ? apiCall ( spaPrefix , 'routeName' , undefined , FEATURE_NAMES . softNav ) : apiCall ( prefix , 'routeName' , true , FEATURE_NAMES . spa )
190
+ agent . setCurrentRouteName = agent . runSoftNavOverSpa ? apiCall ( spaPrefix , 'routeName' , undefined , FEATURE_NAMES . softNav ) : apiCall ( prefix , 'routeName' , true , FEATURE_NAMES . spa )
195
191
196
192
function apiCall ( prefix , name , notSpa , bufferGroup ) {
197
193
return function ( ) {
198
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/' + name + '/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
194
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/' + name + '/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
199
195
dispatchGlobalEvent ( {
200
- agentIdentifier,
201
- loaded : ! ! activatedFeatures ?. [ agentIdentifier ] ,
196
+ agentIdentifier : agent . agentIdentifier ,
197
+ loaded : ! ! activatedFeatures ?. [ agent . agentIdentifier ] ,
202
198
type : 'data' ,
203
199
name : 'api' ,
204
200
feature : prefix + name ,
205
201
data : { notSpa, bufferGroup }
206
202
} )
207
- if ( bufferGroup ) handle ( prefix + name , [ notSpa ? now ( ) : performance . now ( ) , ...arguments ] , notSpa ? null : this , bufferGroup , instanceEE ) // no bufferGroup means only the SM is emitted
203
+ if ( bufferGroup ) handle ( prefix + name , [ notSpa ? now ( ) : performance . now ( ) , ...arguments ] , notSpa ? null : this , bufferGroup , agent . ee ) // no bufferGroup means only the SM is emitted
208
204
return notSpa ? undefined : this // returns the InteractionHandle which allows these methods to be chained
209
205
}
210
206
}
211
207
212
- apiInterface . noticeError = function ( err , customAttributes ) {
208
+ agent . noticeError = function ( err , customAttributes ) {
213
209
if ( typeof err === 'string' ) err = new Error ( err )
214
- handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/noticeError/called' ] , undefined , FEATURE_NAMES . metrics , instanceEE )
215
- handle ( 'err' , [ err , now ( ) , false , customAttributes , ! ! replayRunning [ agentIdentifier ] ] , undefined , FEATURE_NAMES . jserrors , instanceEE )
210
+ handle ( SUPPORTABILITY_METRIC_CHANNEL , [ 'API/noticeError/called' ] , undefined , FEATURE_NAMES . metrics , agent . ee )
211
+ handle ( 'err' , [ err , now ( ) , false , customAttributes , ! ! replayRunning [ agent . agentIdentifier ] ] , undefined , FEATURE_NAMES . jserrors , agent . ee )
216
212
}
217
213
218
214
// theres no window.load event on non-browser scopes, lazy load immediately
@@ -221,14 +217,14 @@ export function setAPI (agentIdentifier, forceDrain, runSoftNavOverSpa = false)
221
217
else onWindowLoad ( ( ) => lazyLoad ( ) , true )
222
218
223
219
function lazyLoad ( ) {
224
- import ( /* webpackChunkName: "async-api" */ './apiAsync' ) . then ( ( { setAPI } ) => {
225
- setAPI ( agentIdentifier )
226
- drain ( agentIdentifier , 'api' )
220
+ import ( /* webpackChunkName: "async-api" */ './apiAsync' ) . then ( ( { setAsyncAPI } ) => {
221
+ setAsyncAPI ( agent )
222
+ drain ( agent . agentIdentifier , 'api' )
227
223
} ) . catch ( ( err ) => {
228
224
warn ( 27 , err )
229
- instanceEE . abort ( )
225
+ agent . ee . abort ( )
230
226
} )
231
227
}
232
228
233
- return apiInterface
229
+ return true
234
230
}
0 commit comments