-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtransaction.go
385 lines (333 loc) · 14.5 KB
/
transaction.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package transaction
import (
"bytes"
"context"
"crypto/tls"
"expvar"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptrace"
"strconv"
"time"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
var (
// ForwarderExpvars is the root for expvars in the forwarder.
ForwarderExpvars = expvar.NewMap("forwarder")
// TransactionsExpvars the transactions Expvars
TransactionsExpvars = expvar.Map{}
connectionDNSSuccess = expvar.Int{}
connectionConnectSuccess = expvar.Int{}
transactionsConnectionEvents = expvar.Map{}
// TransactionsDropped is the number of transaction dropped.
TransactionsDropped = expvar.Int{}
// TransactionsDroppedByEndpoint is the number of transaction dropped by endpoint.
TransactionsDroppedByEndpoint = expvar.Map{}
// TransactionsSuccessByEndpoint is the number of transaction succeeded by endpoint.
TransactionsSuccessByEndpoint = expvar.Map{}
transactionsSuccessBytesByEndpoint = expvar.Map{}
transactionsSuccess = expvar.Int{}
transactionsErrors = expvar.Int{}
transactionsErrorsByType = expvar.Map{}
transactionsDNSErrors = expvar.Int{}
transactionsTLSErrors = expvar.Int{}
transactionsConnectionErrors = expvar.Int{}
transactionsWroteRequestErrors = expvar.Int{}
transactionsSentRequestErrors = expvar.Int{}
transactionsHTTPErrors = expvar.Int{}
transactionsHTTPErrorsByCode = expvar.Map{}
tlmConnectEvents = telemetry.NewCounter("transactions", "connection_events",
[]string{"connection_event_type"}, "Count of new connection events grouped by type of event")
// TlmTxDropped is a telemetry counter that counts the number transaction dropped.
TlmTxDropped = telemetry.NewCounter("transactions", "dropped",
[]string{"domain", "endpoint"}, "Transaction drop count")
tlmTxSuccessCount = telemetry.NewCounter("transactions", "success",
[]string{"domain", "endpoint"}, "Successful transaction count")
tlmTxSuccessBytes = telemetry.NewCounter("transactions", "success_bytes",
[]string{"domain", "endpoint"}, "Successful transaction sizes in bytes")
tlmTxErrors = telemetry.NewCounter("transactions", "errors",
[]string{"domain", "endpoint", "error_type"}, "Count of transactions errored grouped by type of error")
tlmTxHTTPErrors = telemetry.NewCounter("transactions", "http_errors",
[]string{"domain", "endpoint", "code"}, "Count of transactions http errors per http code")
)
// Trace is an httptrace.ClientTrace instance that traces the events within HTTP client requests.
var Trace = &httptrace.ClientTrace{
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
if dnsInfo.Err != nil {
transactionsDNSErrors.Add(1)
tlmTxErrors.Inc("unknown", "unknown", "dns_lookup_failure")
log.Debugf("DNS Lookup failure: %s", dnsInfo.Err)
return
}
connectionDNSSuccess.Add(1)
tlmConnectEvents.Inc("dns_lookup_success")
log.Tracef("DNS Lookup success, addresses: %s", dnsInfo.Addrs)
},
WroteRequest: func(wroteInfo httptrace.WroteRequestInfo) {
if wroteInfo.Err != nil {
transactionsWroteRequestErrors.Add(1)
tlmTxErrors.Inc("unknown", "unknown", "writing_failure")
log.Debugf("Request writing failure: %s", wroteInfo.Err)
}
},
ConnectDone: func(network, addr string, err error) {
if err != nil {
transactionsConnectionErrors.Add(1)
tlmTxErrors.Inc("unknown", "unknown", "connection_failure")
log.Debugf("Connection failure: %s", err)
return
}
connectionConnectSuccess.Add(1)
tlmConnectEvents.Inc("connection_success")
log.Tracef("New successful connection to address: %q", addr)
},
TLSHandshakeDone: func(tlsState tls.ConnectionState, err error) {
if err != nil {
transactionsTLSErrors.Add(1)
tlmTxErrors.Inc("unknown", "unknown", "tls_handshake_failure")
log.Errorf("TLS Handshake failure: %s", err)
}
},
}
// Compile-time check to ensure that HTTPTransaction conforms to the Transaction interface
var _ Transaction = &HTTPTransaction{}
// HTTPAttemptHandler is an event handler that will get called each time this transaction is attempted
type HTTPAttemptHandler func(transaction *HTTPTransaction)
// HTTPCompletionHandler is an event handler that will get called after this transaction has completed
type HTTPCompletionHandler func(transaction *HTTPTransaction, statusCode int, body []byte, err error)
var defaultAttemptHandler = func(transaction *HTTPTransaction) {}
var defaultCompletionHandler = func(transaction *HTTPTransaction, statusCode int, body []byte, err error) {}
func init() {
TransactionsExpvars.Init()
transactionsConnectionEvents.Init()
TransactionsDroppedByEndpoint.Init()
TransactionsSuccessByEndpoint.Init()
transactionsSuccessBytesByEndpoint.Init()
transactionsErrorsByType.Init()
transactionsHTTPErrorsByCode.Init()
ForwarderExpvars.Set("Transactions", &TransactionsExpvars)
transactionsConnectionEvents.Set("DNSSuccess", &connectionDNSSuccess)
transactionsConnectionEvents.Set("ConnectSuccess", &connectionConnectSuccess)
TransactionsExpvars.Set("ConnectionEvents", &transactionsConnectionEvents)
TransactionsExpvars.Set("Dropped", &TransactionsDropped)
TransactionsExpvars.Set("DroppedByEndpoint", &TransactionsDroppedByEndpoint)
TransactionsExpvars.Set("SuccessByEndpoint", &TransactionsSuccessByEndpoint)
TransactionsExpvars.Set("SuccessBytesByEndpoint", &transactionsSuccessBytesByEndpoint)
TransactionsExpvars.Set("Success", &transactionsSuccess)
TransactionsExpvars.Set("Errors", &transactionsErrors)
TransactionsExpvars.Set("ErrorsByType", &transactionsErrorsByType)
transactionsErrorsByType.Set("DNSErrors", &transactionsDNSErrors)
transactionsErrorsByType.Set("TLSErrors", &transactionsTLSErrors)
transactionsErrorsByType.Set("ConnectionErrors", &transactionsConnectionErrors)
transactionsErrorsByType.Set("WroteRequestErrors", &transactionsWroteRequestErrors)
transactionsErrorsByType.Set("SentRequestErrors", &transactionsSentRequestErrors)
TransactionsExpvars.Set("HTTPErrors", &transactionsHTTPErrors)
TransactionsExpvars.Set("HTTPErrorsByCode", &transactionsHTTPErrorsByCode)
}
// Priority defines the priority of a transaction
// Transactions with priority `TransactionPriorityNormal` are dropped from the retry queue
// before dropping transactions with priority `TransactionPriorityHigh`.
type Priority int
const (
// TransactionPriorityNormal defines a transaction with a normal priority
TransactionPriorityNormal Priority = iota
// TransactionPriorityHigh defines a transaction with an high priority
TransactionPriorityHigh Priority = iota
)
// HTTPTransaction represents one Payload for one Endpoint on one Domain.
type HTTPTransaction struct {
// Domain represents the domain target by the HTTPTransaction.
Domain string
// Endpoint is the API Endpoint used by the HTTPTransaction.
Endpoint Endpoint
// Headers are the HTTP headers used by the HTTPTransaction.
Headers http.Header
// Payload is the content delivered to the backend.
Payload *[]byte
// ErrorCount is the number of times this HTTPTransaction failed to be processed.
ErrorCount int
CreatedAt time.Time
// Retryable indicates whether this transaction can be retried
Retryable bool
// StorableOnDisk indicates whether this transaction can be stored on disk
StorableOnDisk bool
// AttemptHandler will be called with a transaction before the attempting to send the request
// This field is not restored when a transaction is deserialized from the disk (the default value is used).
AttemptHandler HTTPAttemptHandler
// CompletionHandler will be called with a transaction after it has been successfully sent
// This field is not restored when a transaction is deserialized from the disk (the default value is used).
CompletionHandler HTTPCompletionHandler
Priority Priority
}
// TransactionsSerializer serializes Transaction instances.
type TransactionsSerializer interface {
Add(transaction *HTTPTransaction) error
}
// Transaction represents the task to process for a Worker.
type Transaction interface {
Process(ctx context.Context, client *http.Client) error
GetCreatedAt() time.Time
GetTarget() string
GetPriority() Priority
GetEndpointName() string
GetPayloadSize() int
// This method serializes the transaction to `TransactionsSerializer`.
// It forces a new implementation of `Transaction` to define how to
// serialize the transaction to `TransactionsSerializer` as a `Transaction`
// must be serializable in domainForwarder.
SerializeTo(TransactionsSerializer) error
}
// NewHTTPTransaction returns a new HTTPTransaction.
func NewHTTPTransaction() *HTTPTransaction {
tr := &HTTPTransaction{
CreatedAt: time.Now(),
ErrorCount: 0,
Retryable: true,
StorableOnDisk: true,
Headers: make(http.Header),
}
tr.SetDefaultHandlers()
return tr
}
// SetDefaultHandlers sets the default handlers for AttemptHandler and CompletionHandler
func (t *HTTPTransaction) SetDefaultHandlers() {
t.AttemptHandler = defaultAttemptHandler
t.CompletionHandler = defaultCompletionHandler
}
// GetCreatedAt returns the creation time of the HTTPTransaction.
func (t *HTTPTransaction) GetCreatedAt() time.Time {
return t.CreatedAt
}
// GetTarget return the url used by the transaction
func (t *HTTPTransaction) GetTarget() string {
url := t.Domain + t.Endpoint.Route
return log.SanitizeURL(url) // sanitized url that can be logged
}
// GetPriority returns the priority
func (t *HTTPTransaction) GetPriority() Priority {
return t.Priority
}
// GetEndpointName returns the name of the endpoint used by the transaction
func (t *HTTPTransaction) GetEndpointName() string {
return t.Endpoint.Name
}
// GetPayloadSize returns the size of the payload.
func (t *HTTPTransaction) GetPayloadSize() int {
if t.Payload != nil {
return len(*t.Payload)
}
return 0
}
// Process sends the Payload of the transaction to the right Endpoint and Domain.
func (t *HTTPTransaction) Process(ctx context.Context, client *http.Client) error {
t.AttemptHandler(t)
statusCode, body, err := t.internalProcess(ctx, client)
if err == nil || !t.Retryable {
t.CompletionHandler(t, statusCode, body, err)
}
// If the txn is retryable, return the error (if present) to the worker to allow it to be retried
// Otherwise, return nil so the txn won't be retried.
if t.Retryable {
return err
}
return nil
}
// internalProcess does the work of actually sending the http request to the specified domain
// This will return (http status code, response body, error).
func (t *HTTPTransaction) internalProcess(ctx context.Context, client *http.Client) (int, []byte, error) {
reader := bytes.NewReader(*t.Payload)
url := t.Domain + t.Endpoint.Route
transactionEndpointName := t.GetEndpointName()
logURL := log.SanitizeURL(url) // sanitized url that can be logged
req, err := http.NewRequest("POST", url, reader)
if err != nil {
log.Errorf("Could not create request for transaction to invalid URL %q (dropping transaction): %s", logURL, err)
transactionsErrors.Add(1)
tlmTxErrors.Inc(t.Domain, transactionEndpointName, "invalid_request")
transactionsSentRequestErrors.Add(1)
return 0, nil, nil
}
req = req.WithContext(ctx)
req.Header = t.Headers
resp, err := client.Do(req)
if err != nil {
// Do not requeue transaction if that one was canceled
if ctx.Err() == context.Canceled {
return 0, nil, nil
}
t.ErrorCount++
transactionsErrors.Add(1)
tlmTxErrors.Inc(t.Domain, transactionEndpointName, "cant_send")
return 0, nil, fmt.Errorf("error while sending transaction, rescheduling it: %s", log.SanitizeURL(err.Error()))
}
defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Fail to read the response Body: %s", err)
return 0, nil, err
}
if resp.StatusCode >= 400 {
statusCode := strconv.Itoa(resp.StatusCode)
var codeCount *expvar.Int
if count := transactionsHTTPErrorsByCode.Get(statusCode); count == nil {
codeCount = &expvar.Int{}
transactionsHTTPErrorsByCode.Set(statusCode, codeCount)
} else {
codeCount = count.(*expvar.Int)
}
codeCount.Add(1)
transactionsHTTPErrors.Add(1)
tlmTxHTTPErrors.Inc(t.Domain, transactionEndpointName, statusCode)
}
if resp.StatusCode == 400 || resp.StatusCode == 404 || resp.StatusCode == 413 {
log.Errorf("Error code %q received while sending transaction to %q: %s, dropping it", resp.Status, logURL, string(body))
TransactionsDroppedByEndpoint.Add(transactionEndpointName, 1)
TransactionsDropped.Add(1)
TlmTxDropped.Inc(t.Domain, transactionEndpointName)
return resp.StatusCode, body, nil
} else if resp.StatusCode == 403 {
log.Errorf("API Key invalid, dropping transaction for %s", logURL)
TransactionsDroppedByEndpoint.Add(transactionEndpointName, 1)
TransactionsDropped.Add(1)
TlmTxDropped.Inc(t.Domain, transactionEndpointName)
return resp.StatusCode, body, nil
} else if resp.StatusCode > 400 {
t.ErrorCount++
transactionsErrors.Add(1)
tlmTxErrors.Inc(t.Domain, transactionEndpointName, "gt_400")
return resp.StatusCode, body, fmt.Errorf("error %q while sending transaction to %q, rescheduling it", resp.Status, logURL)
}
tlmTxSuccessCount.Inc(t.Domain, transactionEndpointName)
tlmTxSuccessBytes.Add(float64(t.GetPayloadSize()), t.Domain, transactionEndpointName)
TransactionsSuccessByEndpoint.Add(transactionEndpointName, 1)
transactionsSuccessBytesByEndpoint.Add(transactionEndpointName, int64(t.GetPayloadSize()))
transactionsSuccess.Add(1)
loggingFrequency := config.Datadog.GetInt64("logging_frequency")
if transactionsSuccess.Value() == 1 {
log.Infof("Successfully posted payload to %q, the agent will only log transaction success every %d transactions", logURL, loggingFrequency)
log.Tracef("Url: %q payload: %s", logURL, string(body))
return resp.StatusCode, body, nil
}
if transactionsSuccess.Value()%loggingFrequency == 0 {
log.Infof("Successfully posted payload to %q", logURL)
log.Tracef("Url: %q payload: %s", logURL, string(body))
return resp.StatusCode, body, nil
}
log.Tracef("Successfully posted payload to %q: %s", logURL, string(body))
return resp.StatusCode, body, nil
}
// SerializeTo serializes the transaction using TransactionsSerializer
func (t *HTTPTransaction) SerializeTo(serializer TransactionsSerializer) error {
if t.StorableOnDisk {
return serializer.Add(t)
}
log.Trace("The transaction is not stored on disk because `storableOnDisk` is false.")
return nil
}