@@ -55,28 +55,39 @@ function resolveHttpPromise(httpEvent, resolveFunction, startTime) {
55
55
56
56
57
57
/**
58
- * Set the duration of the event, and resolves the promise using the given function .
58
+ * Attempts to json parse the data and set it at key on the event's metadata .
59
59
* @param {object } httpEvent The current event
60
60
* @param {string } key name in metadata
61
61
* @param {string } data data to jsonify
62
62
* @param {string } encoding data encoding from the headers
63
63
*/
64
64
function setJsonPayload ( httpEvent , key , data , encoding ) {
65
65
try {
66
- let jsonData = data ;
66
+ let decodedData = data ;
67
67
if ( config . getConfig ( ) . decodeHTTP && ENCODING_FUNCTIONS [ encoding ] ) {
68
68
try {
69
- jsonData = ENCODING_FUNCTIONS [ encoding ] ( data ) ;
69
+ decodedData = ENCODING_FUNCTIONS [ encoding ] ( data ) ;
70
70
} catch ( err ) {
71
71
utils . debugLog ( `Could decode ${ key } with ${ encoding } in http` ) ;
72
72
}
73
73
}
74
- JSON . parse ( jsonData ) ;
74
+ const jsonData = decodedData ;
75
+ try {
76
+ JSON . parse ( jsonData ) ;
77
+ eventInterface . addToMetadata ( httpEvent , { } , {
78
+ [ key ] : jsonData . toString ( ) ,
79
+ } ) ;
80
+ } catch ( err ) {
81
+ utils . debugLog ( `Could not parse JSON ${ key } in http` ) ;
82
+ eventInterface . addToMetadata ( httpEvent , { } , {
83
+ [ key ] : decodedData . toString ( 'utf-8' ) ,
84
+ } ) ;
85
+ }
86
+ } catch ( err ) {
87
+ utils . debugLog ( `Could not decode data and parse JSON ${ key } in http` ) ;
75
88
eventInterface . addToMetadata ( httpEvent , { } , {
76
- [ key ] : jsonData . toString ( ) ,
89
+ [ key ] : data ,
77
90
} ) ;
78
- } catch ( err ) {
79
- utils . debugLog ( `Could not parse JSON ${ key } in http` ) ;
80
91
}
81
92
}
82
93
0 commit comments