forked from newrelic/go-agent
/
distributed_tracing.go
206 lines (172 loc) · 5.63 KB
/
distributed_tracing.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"encoding/base64"
"encoding/json"
"fmt"
"time"
)
type distTraceVersion [2]int
func (v distTraceVersion) major() int { return v[0] }
func (v distTraceVersion) minor() int { return v[1] }
const (
// CallerType is the Type field's value for outbound payloads.
CallerType = "App"
)
var (
currentDistTraceVersion = distTraceVersion([2]int{0 /* Major */, 1 /* Minor */})
callerUnknown = payloadCaller{Type: "Unknown", App: "Unknown", Account: "Unknown", TransportType: "Unknown"}
)
// timestampMillis allows raw payloads to use exact times, and marshalled
// payloads to use times in millis.
type timestampMillis time.Time
func (tm *timestampMillis) UnmarshalJSON(data []byte) error {
var millis uint64
if err := json.Unmarshal(data, &millis); nil != err {
return err
}
*tm = timestampMillis(timeFromUnixMilliseconds(millis))
return nil
}
func (tm timestampMillis) MarshalJSON() ([]byte, error) {
return json.Marshal(TimeToUnixMilliseconds(tm.Time()))
}
func (tm timestampMillis) Time() time.Time { return time.Time(tm) }
func (tm *timestampMillis) Set(t time.Time) { *tm = timestampMillis(t) }
// Payload is the distributed tracing payload.
type Payload struct {
payloadCaller
TransactionID string `json:"tx,omitempty"`
ID string `json:"id,omitempty"`
TracedID string `json:"tr"`
Priority Priority `json:"pr"`
Sampled *bool `json:"sa"`
Timestamp timestampMillis `json:"ti"`
TransportDuration time.Duration `json:"-"`
}
type payloadCaller struct {
TransportType string `json:"-"`
Type string `json:"ty"`
App string `json:"ap"`
Account string `json:"ac"`
TrustedAccountKey string `json:"tk,omitempty"`
}
// IsValid validates the payload data by looking for missing fields.
// Returns an error if there's a problem, nil if everything's fine
func (p Payload) IsValid() error {
// If a payload is missing both `guid` and `transactionId` is received,
// a ParseException supportability metric should be generated.
if "" == p.TransactionID && "" == p.ID {
return ErrPayloadMissingField{message: "missing both guid/id and TransactionId/tx"}
}
if "" == p.Type {
return ErrPayloadMissingField{message: "missing Type/ty"}
}
if "" == p.Account {
return ErrPayloadMissingField{message: "missing Account/ac"}
}
if "" == p.App {
return ErrPayloadMissingField{message: "missing App/ap"}
}
if "" == p.TracedID {
return ErrPayloadMissingField{message: "missing TracedID/tr"}
}
if p.Timestamp.Time().IsZero() || 0 == p.Timestamp.Time().Unix() {
return ErrPayloadMissingField{message: "missing Timestamp/ti"}
}
return nil
}
func (p Payload) text(v distTraceVersion) []byte {
js, _ := json.Marshal(struct {
Version distTraceVersion `json:"v"`
Data Payload `json:"d"`
}{
Version: v,
Data: p,
})
return js
}
// Text implements newrelic.DistributedTracePayload.
func (p Payload) Text() string {
t := p.text(currentDistTraceVersion)
return string(t)
}
// HTTPSafe implements newrelic.DistributedTracePayload.
func (p Payload) HTTPSafe() string {
t := p.text(currentDistTraceVersion)
return base64.StdEncoding.EncodeToString(t)
}
// SetSampled lets us set a value for our *bool,
// which we can't do directly since a pointer
// needs something to point at.
func (p *Payload) SetSampled(sampled bool) {
p.Sampled = &sampled
}
// ErrPayloadParse indicates that the payload was malformed.
type ErrPayloadParse struct{ err error }
func (e ErrPayloadParse) Error() string {
return fmt.Sprintf("unable to parse inbound payload: %s", e.err.Error())
}
// ErrPayloadMissingField indicates there's a required field that's missing
type ErrPayloadMissingField struct{ message string }
func (e ErrPayloadMissingField) Error() string {
return fmt.Sprintf("payload is missing required fields: %s", e.message)
}
// ErrUnsupportedPayloadVersion indicates that the major version number is
// unknown.
type ErrUnsupportedPayloadVersion struct{ version int }
func (e ErrUnsupportedPayloadVersion) Error() string {
return fmt.Sprintf("unsupported major version number %d", e.version)
}
// AcceptPayload parses the inbound distributed tracing payload.
func AcceptPayload(p interface{}) (*Payload, error) {
var payload Payload
if byteSlice, ok := p.([]byte); ok {
p = string(byteSlice)
}
switch v := p.(type) {
case string:
if "" == v {
return nil, nil
}
var decoded []byte
if '{' == v[0] {
decoded = []byte(v)
} else {
var err error
decoded, err = base64.StdEncoding.DecodeString(v)
if nil != err {
return nil, ErrPayloadParse{err: err}
}
}
envelope := struct {
Version distTraceVersion `json:"v"`
Data json.RawMessage `json:"d"`
}{}
if err := json.Unmarshal(decoded, &envelope); nil != err {
return nil, ErrPayloadParse{err: err}
}
if 0 == envelope.Version.major() && 0 == envelope.Version.minor() {
return nil, ErrPayloadMissingField{message: "missing v"}
}
if envelope.Version.major() > currentDistTraceVersion.major() {
return nil, ErrUnsupportedPayloadVersion{
version: envelope.Version.major(),
}
}
if err := json.Unmarshal(envelope.Data, &payload); nil != err {
return nil, ErrPayloadParse{err: err}
}
case Payload:
payload = v
default:
// Could be a shim payload (if the app is not yet connected).
return nil, nil
}
// Ensure that we don't have a reference to the input payload: we don't
// want to change it, it could be used multiple times.
alloc := new(Payload)
*alloc = payload
return alloc, nil
}