github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cross_process_http.go (about) 1 package internal 2 3 import ( 4 "net/http" 5 6 "github.com/lulzWill/go-agent/internal/cat" 7 ) 8 9 // InitFromHTTPRequest initialises the TxnCrossProcess from the given request. 10 // This is a convenience method to keep newTxn() as clean as possible, and to 11 // support unit tests. 12 func (txp *TxnCrossProcess) InitFromHTTPRequest(enabled bool, reply *ConnectReply, req *http.Request) error { 13 metadata := CrossProcessMetadata{} 14 if req != nil { 15 metadata = HTTPHeaderToMetadata(req.Header) 16 } 17 18 return txp.Init(enabled, reply, metadata) 19 } 20 21 // AppDataToHTTPHeader encapsulates the given appData value in the correct HTTP 22 // header. 23 func AppDataToHTTPHeader(appData string) http.Header { 24 header := http.Header{} 25 26 if appData != "" { 27 header.Add(cat.NewRelicAppDataName, appData) 28 } 29 30 return header 31 } 32 33 // HTTPHeaderToAppData gets the appData value from the correct HTTP header. 34 func HTTPHeaderToAppData(header http.Header) string { 35 if header == nil { 36 return "" 37 } 38 39 return header.Get(cat.NewRelicAppDataName) 40 } 41 42 // HTTPHeaderToMetadata gets the cross process metadata from the relevant HTTP 43 // headers. 44 func HTTPHeaderToMetadata(header http.Header) CrossProcessMetadata { 45 if header == nil { 46 return CrossProcessMetadata{} 47 } 48 49 return CrossProcessMetadata{ 50 ID: header.Get(cat.NewRelicIDName), 51 TxnData: header.Get(cat.NewRelicTxnName), 52 Synthetics: header.Get(cat.NewRelicSyntheticsName), 53 } 54 } 55 56 // MetadataToHTTPHeader creates a set of HTTP headers to represent the given 57 // cross process metadata. 58 func MetadataToHTTPHeader(metadata CrossProcessMetadata) http.Header { 59 header := http.Header{} 60 61 if metadata.ID != "" { 62 header.Add(cat.NewRelicIDName, metadata.ID) 63 } 64 65 if metadata.TxnData != "" { 66 header.Add(cat.NewRelicTxnName, metadata.TxnData) 67 } 68 69 if metadata.Synthetics != "" { 70 header.Add(cat.NewRelicSyntheticsName, metadata.Synthetics) 71 } 72 73 return header 74 }