-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathclient.go
356 lines (303 loc) · 10.9 KB
/
client.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
package telemetry
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
crypto_rand "crypto/rand"
math_rand "math/rand"
"github.com/newrelic/newrelic-lambda-extension/lambda/logserver"
"github.com/newrelic/newrelic-lambda-extension/util"
)
const (
InfraEndpointEU string = "https://cloud-collector.eu01.nr-data.net/aws/lambda/v1"
InfraEndpointUS string = "https://cloud-collector.newrelic.com/aws/lambda/v1"
LogEndpointEU string = "https://log-api.eu.newrelic.com/log/v1"
LogEndpointUS string = "https://log-api.newrelic.com/log/v1"
// WIP (configuration options?)
SendTimeoutRetryBase time.Duration = 200 * time.Millisecond
SendTimeoutMaxRetries int = 20
SendTimeoutMaxBackOff time.Duration = 3 * time.Second
httpClientTimeout time.Duration = 2400 * time.Millisecond
)
type Client struct {
httpClient *http.Client
batch *Batch
timeout time.Duration
licenseKey string
telemetryEndpoint string
logEndpoint string
functionName string
collectTraceID bool
}
// New creates a telemetry client with sensible defaults
func New(functionName string, licenseKey string, telemetryEndpointOverride string, logEndpointOverride string, batch *Batch, collectTraceID bool, clientTimeout time.Duration) *Client {
httpClient := &http.Client{
Timeout: httpClientTimeout,
}
// Create random seed for timeout to avoid instances created at the same time
// from creating a wall of retry requests to the collector
var b [8]byte
_, err := crypto_rand.Read(b[:])
if err != nil {
log.Fatal("cannot seed math/rand package with cryptographically secure random number generator")
}
math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
return NewWithHTTPClient(httpClient, functionName, licenseKey, telemetryEndpointOverride, logEndpointOverride, batch, collectTraceID, clientTimeout)
}
// NewWithHTTPClient is just like New, but the HTTP client can be overridden
func NewWithHTTPClient(httpClient *http.Client, functionName string, licenseKey string, telemetryEndpointOverride string, logEndpointOverride string, batch *Batch, collectTraceID bool, clientTimeout time.Duration) *Client {
telemetryEndpoint := getInfraEndpointURL(licenseKey, telemetryEndpointOverride)
logEndpoint := getLogEndpointURL(licenseKey, logEndpointOverride)
return &Client{
httpClient: httpClient,
licenseKey: licenseKey,
telemetryEndpoint: telemetryEndpoint,
logEndpoint: logEndpoint,
functionName: functionName,
batch: batch,
collectTraceID: collectTraceID,
timeout: clientTimeout,
}
}
// getInfraEndpointURL returns the Vortex endpoint for the provided license key
func getInfraEndpointURL(licenseKey string, telemetryEndpointOverride string) string {
if telemetryEndpointOverride != "" {
return telemetryEndpointOverride
}
if strings.HasPrefix(licenseKey, "eu") {
return InfraEndpointEU
}
return InfraEndpointUS
}
// getLogEndpointURL returns the Vortex endpoint for the provided license key
func getLogEndpointURL(licenseKey string, logEndpointOverride string) string {
if logEndpointOverride != "" {
return logEndpointOverride
}
if strings.HasPrefix(licenseKey, "eu") {
return LogEndpointEU
}
return LogEndpointUS
}
func (c *Client) SendTelemetry(ctx context.Context, invokedFunctionARN string, telemetry [][]byte) (error, int) {
util.Debugf("SendTelemetry: sending telemetry to New Relic...")
start := time.Now()
logEvents := make([]LogsEvent, 0, len(telemetry))
for _, payload := range telemetry {
logEvent := LogsEventForBytes(payload)
logEvents = append(logEvents, logEvent)
}
util.Debugf("SendTelemetry: compressing telemetry payloads...")
compressedPayloads, err := CompressedPayloadsForLogEvents(logEvents, c.functionName, invokedFunctionARN)
if err != nil {
return err, 0
}
var builder requestBuilder = func(buffer *bytes.Buffer) (*http.Request, error) {
return BuildVortexRequest(ctx, c.telemetryEndpoint, buffer, util.Name, c.licenseKey)
}
transmitStart := time.Now()
successCount, sentBytes := c.sendPayloads(compressedPayloads, builder)
end := time.Now()
totalTime := end.Sub(start)
transmissionTime := end.Sub(transmitStart)
util.Logf(
"Sent %d/%d New Relic Telemetry payload batches with %d log events successfully with certainty in %.3fms (%dms to transmit %.1fkB).\n",
successCount,
len(compressedPayloads),
len(telemetry),
float64(totalTime.Microseconds())/1000.0,
transmissionTime.Milliseconds(),
float64(sentBytes)/1024.0,
)
return nil, successCount
}
type requestBuilder func(buffer *bytes.Buffer) (*http.Request, error)
func (c *Client) sendPayloads(compressedPayloads []*bytes.Buffer, builder requestBuilder) (successCount int, sentBytes int) {
successCount = 0
sentBytes = 0
sendPayloadsStartTime := time.Now()
for _, p := range compressedPayloads {
payloadSize := p.Len()
sentBytes += payloadSize
currentPayloadBytes := p.Bytes()
var response AttemptData
// buffer this chanel to allow succesful attempts to go through if possible
data := make(chan AttemptData, 1)
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
go c.attemptSend(ctx, currentPayloadBytes, builder, data)
select {
case <-ctx.Done():
response.Error = fmt.Errorf("failed to send data within user defined timeout period: %s", c.timeout.String())
case response = <-data:
}
if response.Error != nil {
util.Logf("Telemetry client error: %s, payload size: %d bytes", response.Error, payloadSize)
sentBytes -= payloadSize
} else if response.Response.StatusCode >= 300 {
util.Logf("Telemetry client response: [%s] %s", response.Response.Status, response.ResponseBody)
} else {
successCount += 1
}
}
util.Debugf("sendPayloads: took %s to finish sending all payloads", time.Since(sendPayloadsStartTime).String())
return successCount, sentBytes
}
type AttemptData struct {
Error error
ResponseBody string
Response *http.Response
}
func (c *Client) attemptSend(ctx context.Context, currentPayloadBytes []byte, builder requestBuilder, dataChan chan AttemptData) {
baseSleepTime := SendTimeoutRetryBase
for attempts := 0; attempts < SendTimeoutMaxRetries; attempts++ {
select {
case <-ctx.Done():
util.Debugln("attemptSend: thread was quit by context timeout")
return
default:
// Construct request for this try
req, err := builder(bytes.NewBuffer(currentPayloadBytes))
if err != nil {
dataChan <- AttemptData{
Error: err,
}
return
}
//Make request, check for timeout
res, err := c.httpClient.Do(req)
// send response data and exit
if err == nil {
// Success. Process response and exit retry loop
defer util.Close(res.Body)
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
dataChan <- AttemptData{
Error: err,
}
return
}
// Successfully sent bytes
dataChan <- AttemptData{
Error: nil,
ResponseBody: string(bodyBytes),
Response: res,
}
util.Debugln("attemptSend: data sent to New Relic succesfully")
return
}
// if error is http timeout, retry
if err, ok := err.(net.Error); ok && err.Timeout() {
timeout := baseSleepTime + time.Duration(math_rand.Intn(200))
util.Debugf("attemptSend: timeout error, retrying after %s: %v", timeout.String(), err)
time.Sleep(timeout)
// double wait time after 3 time out attempts
if (attempts+1)%3 == 0 {
baseSleepTime *= 2
}
if baseSleepTime > SendTimeoutMaxBackOff {
baseSleepTime = SendTimeoutMaxBackOff
}
} else {
// All other error types are fatal
dataChan <- AttemptData{
Error: err,
}
return
}
}
}
}
// SendFunctionLogs constructs log payloads and sends them to new relic
func (c *Client) SendFunctionLogs(ctx context.Context, invokedFunctionARN string, lines []logserver.LogLine) error {
start := time.Now()
if len(lines) == 0 {
util.Debugln("client.SendFunctionLogs invoked with 0 log lines. Returning without sending a payload to New Relic")
return nil
}
compressedPayloads, builder, err := c.buildLogPayloads(ctx, invokedFunctionARN, lines)
if err != nil {
return err
}
transmitStart := time.Now()
successCount, sentBytes := c.sendPayloads(compressedPayloads, builder)
totalTime := time.Since(start)
transmissionTime := time.Since(transmitStart)
util.Logf(
"Sent %d/%d New Relic function log batches successfully with certainty in %.3fms (%dms to transmit %.1fkB).\n",
successCount,
len(compressedPayloads),
float64(totalTime.Microseconds())/1000.0,
transmissionTime.Milliseconds(),
float64(sentBytes)/1024.0,
)
return nil
}
// getNewRelicTags adds tags to the logs if NR_TAGS has values
func getNewRelicTags(common map[string]interface{}) {
nrTagsStr := os.Getenv("NR_TAGS")
nrDelimiter := os.Getenv("NR_ENV_DELIMITER")
if nrDelimiter == "" {
nrDelimiter = ";"
}
if nrTagsStr != "" {
tags := strings.Split(nrTagsStr, nrDelimiter)
nrTags := make(map[string]string)
for _, tag := range tags {
keyValue := strings.Split(tag, ":")
if len(keyValue) == 2 {
nrTags[keyValue[0]] = keyValue[1]
}
}
for k, v := range nrTags {
common[k] = v
}
}
}
// buildLogPayloads is a helper function that improves readability of the SendFunctionLogs method
func (c *Client) buildLogPayloads(ctx context.Context, invokedFunctionARN string, lines []logserver.LogLine) ([]*bytes.Buffer, requestBuilder, error) {
common := map[string]interface{}{
"plugin": util.Id,
"faas.arn": invokedFunctionARN,
"faas.name": c.functionName,
}
getNewRelicTags(common)
logMessages := make([]FunctionLogMessage, 0, len(lines))
for _, l := range lines {
// Unix time in ms
ts := l.Time.UnixNano() / 1e6
var traceId string
if c.batch != nil && c.collectTraceID {
// There is a race condition here. Telemetry batch may be late, so the trace
// ID would be blank. This would require a lock to handle, which would delay
// logs being sent. Not sure if worth the performance hit yet.
traceId = c.batch.RetrieveTraceID(l.RequestID)
}
logMessages = append(logMessages, NewFunctionLogMessage(ts, l.RequestID, traceId, string(l.Content)))
}
// The Log API expects an array
logData := []DetailedFunctionLog{NewDetailedFunctionLog(common, logMessages)}
// Since the Log API won't send us more than 1MB, we shouldn't have any issues with payload size.
compressedPayload, err := CompressedJsonPayload(logData)
if err != nil {
return nil, nil, err
}
compressedPayloads := []*bytes.Buffer{compressedPayload}
var builder requestBuilder = func(buffer *bytes.Buffer) (*http.Request, error) {
req, err := BuildVortexRequest(ctx, c.logEndpoint, buffer, util.Name, c.licenseKey)
if err != nil {
return nil, err
}
req.Header.Add("X-Event-Source", "logs")
return req, err
}
return compressedPayloads, builder, nil
}