github.com/newrelic/go-agent@v3.26.0+incompatible/internal/cat/appdata.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package cat 5 6 import ( 7 "bytes" 8 "encoding/json" 9 "errors" 10 11 "github.com/newrelic/go-agent/internal/jsonx" 12 ) 13 14 // AppDataHeader represents a decoded AppData header. 15 type AppDataHeader struct { 16 CrossProcessID string 17 TransactionName string 18 QueueTimeInSeconds float64 19 ResponseTimeInSeconds float64 20 ContentLength int64 21 TransactionGUID string 22 } 23 24 var ( 25 errInvalidAppDataJSON = errors.New("invalid transaction data JSON") 26 errInvalidAppDataCrossProcessID = errors.New("cross process ID is not a string") 27 errInvalidAppDataTransactionName = errors.New("transaction name is not a string") 28 errInvalidAppDataQueueTimeInSeconds = errors.New("queue time is not a float64") 29 errInvalidAppDataResponseTimeInSeconds = errors.New("response time is not a float64") 30 errInvalidAppDataContentLength = errors.New("content length is not a float64") 31 errInvalidAppDataTransactionGUID = errors.New("transaction GUID is not a string") 32 ) 33 34 // MarshalJSON marshalls an AppDataHeader as raw JSON. 35 func (appData *AppDataHeader) MarshalJSON() ([]byte, error) { 36 buf := bytes.NewBufferString("[") 37 38 jsonx.AppendString(buf, appData.CrossProcessID) 39 40 buf.WriteString(",") 41 jsonx.AppendString(buf, appData.TransactionName) 42 43 buf.WriteString(",") 44 jsonx.AppendFloat(buf, appData.QueueTimeInSeconds) 45 46 buf.WriteString(",") 47 jsonx.AppendFloat(buf, appData.ResponseTimeInSeconds) 48 49 buf.WriteString(",") 50 jsonx.AppendInt(buf, appData.ContentLength) 51 52 buf.WriteString(",") 53 jsonx.AppendString(buf, appData.TransactionGUID) 54 55 // The mysterious unused field. We don't need to round trip this, so we'll 56 // just hardcode it to false. 57 buf.WriteString(",false]") 58 return buf.Bytes(), nil 59 } 60 61 // UnmarshalJSON unmarshalls an AppDataHeader from raw JSON. 62 func (appData *AppDataHeader) UnmarshalJSON(data []byte) error { 63 var ok bool 64 var v interface{} 65 66 if err := json.Unmarshal(data, &v); err != nil { 67 return err 68 } 69 70 arr, ok := v.([]interface{}) 71 if !ok { 72 return errInvalidAppDataJSON 73 } 74 if len(arr) < 7 { 75 return errUnexpectedArraySize{ 76 label: "unexpected number of application data elements", 77 expected: 7, 78 actual: len(arr), 79 } 80 } 81 82 if appData.CrossProcessID, ok = arr[0].(string); !ok { 83 return errInvalidAppDataCrossProcessID 84 } 85 86 if appData.TransactionName, ok = arr[1].(string); !ok { 87 return errInvalidAppDataTransactionName 88 } 89 90 if appData.QueueTimeInSeconds, ok = arr[2].(float64); !ok { 91 return errInvalidAppDataQueueTimeInSeconds 92 } 93 94 if appData.ResponseTimeInSeconds, ok = arr[3].(float64); !ok { 95 return errInvalidAppDataResponseTimeInSeconds 96 } 97 98 cl, ok := arr[4].(float64) 99 if !ok { 100 return errInvalidAppDataContentLength 101 } 102 // Content length is specced as int32, but not all agents are consistent on 103 // this in practice. Let's handle it as int64 to maximise compatibility. 104 appData.ContentLength = int64(cl) 105 106 if appData.TransactionGUID, ok = arr[5].(string); !ok { 107 return errInvalidAppDataTransactionGUID 108 } 109 110 // As above, we don't bother decoding the unused field here. It just has to 111 // be present (which was checked earlier with the length check). 112 113 return nil 114 }