@@ -6,12 +6,13 @@ import { registerHandler } from '../../../common/event-emitter/register-handler'
66import { stringify } from '../../../common/util/stringify'
77import { handle } from '../../../common/event-emitter/handle'
88import { setDenyList , shouldCollectEvent } from '../../../common/deny-list/deny-list'
9- import { AJAX_ID , FEATURE_NAME } from '../constants'
9+ import { FEATURE_NAME , AJAX_ID } from '../constants'
1010import { FEATURE_NAMES } from '../../../loaders/features/features'
1111import { AggregateBase } from '../../utils/aggregate-base'
12- import { parseGQL } from './gql'
1312import { nullable , numeric , getAddStringContext , addCustomAttributes } from '../../../common/serialize/bel-serializer'
1413import { gosNREUMOriginals } from '../../../common/window/nreum'
14+ import { hasGQLErrors , parseGQL } from './gql'
15+ import { canCapturePayload , isLikelyHumanReadable , parseQueryString , createStringAdders } from '../../../common/payloads/payloads'
1516import { Obfuscator } from '../../../common/util/obfuscate'
1617import { getVersion2Attributes , getVersion2DuplicationAttributes , shouldDuplicate } from '../../../common/v2/utils'
1718import { EVENT_TYPES } from '../../../common/constants/events'
@@ -101,6 +102,24 @@ export class Aggregate extends AggregateBase {
101102 [ AJAX_ID ] : generateUuid ( ) // all AjaxRequest events should have a unique identifier to allow for easier grouping and analysis in the UI
102103 }
103104
105+ event . gql = params . gql = parseGQL ( {
106+ body : ctx . requestBody ,
107+ query : ctx . parsedOrigin ?. search
108+ } )
109+ if ( event . gql ) event . gql . operationHasErrors = params . gql . operationHasErrors = hasGQLErrors ( ctx . responseBody )
110+
111+ const capturePayloadSetting = this . agentRef . init . ajax . capture_payloads
112+ const shouldCapturePayload = canCapturePayload ( capturePayloadSetting , params . status , event . gql ?. operationHasErrors )
113+
114+ if ( shouldCapturePayload ) {
115+ // Store raw data; obfuscation and truncation will happen in the serializer
116+ params . requestQuery = event . requestQuery = parseQueryString ( ctx . parsedOrigin ?. search )
117+ params . requestHeaders = event . requestHeaders = ctx . requestHeaders
118+ params . responseHeaders = event . responseHeaders = ctx . responseHeaders
119+ if ( isLikelyHumanReadable ( ctx . requestHeaders , ctx . requestBody ) ) params . requestBody = event . requestBody = ctx . requestBody
120+ if ( isLikelyHumanReadable ( ctx . responseHeaders , ctx . responseBody ) ) params . responseBody = event . responseBody = ctx . responseBody
121+ }
122+
104123 if ( ctx . dt ) {
105124 event . spanId = ctx . dt . spanId
106125 event . traceId = ctx . dt . traceId
@@ -109,11 +128,6 @@ export class Aggregate extends AggregateBase {
109128 )
110129 }
111130
112- // parsed from the AJAX body, looking for operationName param & parsing query for operationType
113- event . gql = params . gql = parseGQL ( {
114- body : ctx . body ,
115- query : ctx . parsedOrigin ?. search
116- } )
117131 if ( event . gql ) this . reportSupportabilityMetric ( 'Ajax/Events/GraphQL/Bytes-Added' , stringify ( event . gql ) . length )
118132
119133 /** make a copy of the event for the MFE target if it exists */
@@ -136,7 +150,9 @@ export class Aggregate extends AggregateBase {
136150
137151 serializer ( eventBuffer ) {
138152 if ( ! eventBuffer . length ) return
139- const addString = getAddStringContext ( this . obfuscator )
153+
154+ const { addString, addStringWithTruncation } = createStringAdders ( getAddStringContext , this . obfuscator )
155+
140156 let payload = 'bel.7;'
141157
142158 let firstTimestamp = 0
@@ -170,15 +186,24 @@ export class Aggregate extends AggregateBase {
170186 // Since configuration objects (like info) are created new each time they are set, we have to grab the current pointer to the attr object here.
171187 const jsAttributes = this . agentRef . info . jsAttributes
172188
173- // add custom attributes
174- // gql decorators are added as custom attributes to alleviate need for new BEL schema
175- const attrParts = addCustomAttributes ( {
189+ // Regular attributes: obfuscate only
190+ const regularAttrs = addCustomAttributes ( {
176191 ...( jsAttributes || { } ) ,
177- ... ( event . gql || { } ) ,
192+ [ AJAX_ID ] : event [ AJAX_ID ] , // all AjaxRequest events should have a unique identifier to allow for easier grouping and analysis in the UI
178193 ...( event . targetAttributes || { } ) , // used to supply the version 2 attributes, either MFE target or duplication attributes for the main agent app
179- [ AJAX_ID ] : event [ AJAX_ID ] // all AjaxRequest events should have a unique identifier to allow for easier grouping and analysis in the UI
194+ ... ( event . gql || { } )
180195 } , addString )
181196
197+ // Payload attributes: obfuscate then truncate
198+ const payloadAttrs = addCustomAttributes ( {
199+ ...( event . requestBody ? { requestBody : event . requestBody } : { } ) ,
200+ ...( event . requestHeaders ? { requestHeaders : event . requestHeaders } : { } ) ,
201+ ...( event . requestQuery ? { requestQuery : event . requestQuery } : { } ) ,
202+ ...( event . responseBody ? { responseBody : event . responseBody } : { } ) ,
203+ ...( event . responseHeaders ? { responseHeaders : event . responseHeaders } : { } )
204+ } , addStringWithTruncation )
205+
206+ const attrParts = [ ...regularAttrs , ...payloadAttrs ]
182207 fields . unshift ( numeric ( attrParts . length ) )
183208
184209 insert += fields . join ( ',' )
0 commit comments