/
cross_process_http.go
70 lines (54 loc) · 1.72 KB
/
cross_process_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"net/http"
"github.com/newrelic/go-agent/internal/cat"
)
// InboundHTTPRequest adds the inbound request metadata to the TxnCrossProcess.
func (txp *TxnCrossProcess) InboundHTTPRequest(hdr http.Header) error {
return txp.handleInboundRequestHeaders(HTTPHeaderToMetadata(hdr))
}
// AppDataToHTTPHeader encapsulates the given appData value in the correct HTTP
// header.
func AppDataToHTTPHeader(appData string) http.Header {
header := http.Header{}
if appData != "" {
header.Add(cat.NewRelicAppDataName, appData)
}
return header
}
// HTTPHeaderToAppData gets the appData value from the correct HTTP header.
func HTTPHeaderToAppData(header http.Header) string {
if header == nil {
return ""
}
return header.Get(cat.NewRelicAppDataName)
}
// HTTPHeaderToMetadata gets the cross process metadata from the relevant HTTP
// headers.
func HTTPHeaderToMetadata(header http.Header) CrossProcessMetadata {
if header == nil {
return CrossProcessMetadata{}
}
return CrossProcessMetadata{
ID: header.Get(cat.NewRelicIDName),
TxnData: header.Get(cat.NewRelicTxnName),
Synthetics: header.Get(cat.NewRelicSyntheticsName),
}
}
// MetadataToHTTPHeader creates a set of HTTP headers to represent the given
// cross process metadata.
func MetadataToHTTPHeader(metadata CrossProcessMetadata) http.Header {
header := http.Header{}
if metadata.ID != "" {
header.Add(cat.NewRelicIDName, metadata.ID)
}
if metadata.TxnData != "" {
header.Add(cat.NewRelicTxnName, metadata.TxnData)
}
if metadata.Synthetics != "" {
header.Add(cat.NewRelicSyntheticsName, metadata.Synthetics)
}
return header
}