forked from elastic/apm-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapmserver.go
315 lines (288 loc) · 9.98 KB
/
apmserver.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apmproxy
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"time"
)
type jsonResult struct {
Errors []jsonError `json:"errors,omitempty"`
}
type jsonError struct {
Message string `json:"message"`
Document string `json:"document,omitempty"`
}
// ForwardApmData receives agent data as it comes in and posts it to the APM server.
// Stop checking for, and sending agent data when the function invocation
// has completed, signaled via a channel.
func (c *Client) ForwardApmData(ctx context.Context, metadataContainer *MetadataContainer) error {
if c.IsUnhealthy() {
return nil
}
for {
select {
case <-ctx.Done():
c.logger.Debug("Invocation context cancelled, not processing any more agent data")
return nil
case agentData := <-c.DataChannel:
if metadataContainer.Metadata == nil {
metadata, err := ProcessMetadata(agentData)
if err != nil {
return fmt.Errorf("failed to extract metadata from agent payload %w", err)
}
metadataContainer.Metadata = metadata
}
if err := c.PostToApmServer(ctx, agentData); err != nil {
return fmt.Errorf("error sending to APM server, skipping: %v", err)
}
}
}
}
// FlushAPMData reads all the apm data in the apm data channel and sends it to the APM server.
func (c *Client) FlushAPMData(ctx context.Context) {
if c.IsUnhealthy() {
c.logger.Debug("Flush skipped - Transport failing")
return
}
c.logger.Debug("Flush started - Checking for agent data")
for {
select {
case agentData := <-c.DataChannel:
c.logger.Debug("Flush in progress - Processing agent data")
if err := c.PostToApmServer(ctx, agentData); err != nil {
c.logger.Errorf("Error sending to APM server, skipping: %v", err)
}
default:
c.logger.Debug("Flush ended - No agent data on buffer")
return
}
}
}
// PostToApmServer takes a chunk of APM agent data and posts it to the APM server.
//
// The function compresses the APM agent data, if it's not already compressed.
// It sets the APM transport status to failing upon errors, as part of the backoff
// strategy.
func (c *Client) PostToApmServer(ctx context.Context, agentData AgentData) error {
// todo: can this be a streaming or streaming style call that keeps the
// connection open across invocations?
if c.IsUnhealthy() {
return errors.New("transport status is unhealthy")
}
endpointURI := "intake/v2/events"
encoding := agentData.ContentEncoding
var r io.Reader
if agentData.ContentEncoding != "" {
r = bytes.NewReader(agentData.Data)
} else {
encoding = "gzip"
buf := c.bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
c.bufferPool.Put(buf)
}()
gw, err := gzip.NewWriterLevel(buf, gzip.BestSpeed)
if err != nil {
return err
}
if _, err := gw.Write(agentData.Data); err != nil {
return fmt.Errorf("failed to compress data: %w", err)
}
if err := gw.Close(); err != nil {
return fmt.Errorf("failed to write compressed data to buffer: %w", err)
}
r = buf
}
req, err := http.NewRequest(http.MethodPost, c.serverURL+endpointURI, r)
if err != nil {
return fmt.Errorf("failed to create a new request when posting to APM server: %v", err)
}
req.Header.Add("Content-Encoding", encoding)
req.Header.Add("Content-Type", "application/x-ndjson")
if c.ServerAPIKey != "" {
req.Header.Add("Authorization", "ApiKey "+c.ServerAPIKey)
} else if c.ServerSecretToken != "" {
req.Header.Add("Authorization", "Bearer "+c.ServerSecretToken)
}
c.logger.Debug("Sending data chunk to APM server")
resp, err := c.client.Do(req)
if err != nil {
c.UpdateStatus(ctx, Failing)
return fmt.Errorf("failed to post to APM server: %v", err)
}
defer resp.Body.Close()
// On success, the server will respond with a 202 Accepted status code and no body.
if resp.StatusCode == http.StatusAccepted {
c.UpdateStatus(ctx, Healthy)
return nil
}
// RateLimited
if resp.StatusCode == http.StatusTooManyRequests {
c.logger.Warnf("Transport has been rate limited: response status code: %d", resp.StatusCode)
c.UpdateStatus(ctx, RateLimited)
return nil
}
jErr := jsonResult{}
if err := json.NewDecoder(resp.Body).Decode(&jErr); err != nil {
// non critical error.
// Log a warning and continue.
c.logger.Warnf("failed to decode response body: %v", err)
}
// Auth errors
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
c.logger.Warnf("Authentication with the APM server failed: response status code: %d", resp.StatusCode)
for _, err := range jErr.Errors {
c.logger.Warnf("failed to authenticate: document %s: message: %s", err.Document, err.Message)
}
c.UpdateStatus(ctx, Failing)
return nil
}
// ClientErrors
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
c.logger.Warnf("client error: response status code: %d", resp.StatusCode)
for _, err := range jErr.Errors {
c.logger.Warnf("client error: document %s: message: %s", err.Document, err.Message)
}
c.UpdateStatus(ctx, ClientFailing)
return nil
}
// critical errors
if resp.StatusCode == http.StatusInternalServerError || resp.StatusCode == http.StatusServiceUnavailable {
c.logger.Warnf("failed to post data to APM server: response status code: %d", resp.StatusCode)
for _, err := range jErr.Errors {
c.logger.Warnf("critical error: document %s: message: %s", err.Document, err.Message)
}
c.UpdateStatus(ctx, Failing)
return nil
}
c.logger.Warnf("unhandled status code: %d", resp.StatusCode)
return nil
}
// IsUnhealthy returns true if the apmproxy is not healthy.
func (c *Client) IsUnhealthy() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.Status == Failing
}
// UpdateStatus takes a state of the APM server transport and updates
// the current state of the transport. For a change to a failing state, the grace period
// is calculated and a go routine is started that waits for that period to complete
// before changing the status to "pending". This would allow a subsequent send attempt
// to the APM server.
//
// This function is public for use in tests.
func (c *Client) UpdateStatus(ctx context.Context, status Status) {
// Reduce lock contention as UpdateStatus is called on every
// successful request
c.mu.RLock()
if status == c.Status {
c.mu.RUnlock()
return
}
c.mu.RUnlock()
switch status {
case Healthy:
c.mu.Lock()
if c.Status == status {
return
}
c.Status = status
c.logger.Debugf("APM server Transport status set to %s", c.Status)
c.ReconnectionCount = -1
c.mu.Unlock()
case RateLimited, ClientFailing:
// No need to start backoff, this is a temporary status. It usually
// means we went over the limit of events/s.
c.mu.Lock()
c.Status = status
c.logger.Debugf("APM server Transport status set to %s", c.Status)
c.mu.Unlock()
case Failing:
c.mu.Lock()
c.Status = status
c.logger.Debugf("APM server Transport status set to %s", c.Status)
c.ReconnectionCount++
gracePeriodTimer := time.NewTimer(c.ComputeGracePeriod())
c.logger.Debugf("Grace period entered, reconnection count : %d", c.ReconnectionCount)
c.mu.Unlock()
go func() {
select {
case <-gracePeriodTimer.C:
c.logger.Debug("Grace period over - timer timed out")
case <-ctx.Done():
c.logger.Debug("Grace period over - context done")
}
c.mu.Lock()
c.Status = Started
c.logger.Debugf("APM server Transport status set to %s", c.Status)
c.mu.Unlock()
}()
default:
c.logger.Errorf("Cannot set APM server Transport status to %s", status)
}
}
// ComputeGracePeriod https://github.com/elastic/apm/blob/main/specs/agents/transport.md#transport-errors
func (c *Client) ComputeGracePeriod() time.Duration {
// If reconnectionCount is 0, returns a random number in an interval.
// The grace period for the first reconnection count was 0 but that
// leads to collisions with multiple environments.
if c.ReconnectionCount == 0 {
gracePeriod := rand.Float64() * 5
return time.Duration(gracePeriod * float64(time.Second))
}
gracePeriodWithoutJitter := math.Pow(math.Min(float64(c.ReconnectionCount), 6), 2)
jitter := rand.Float64()/5 - 0.1
return time.Duration((gracePeriodWithoutJitter + jitter*gracePeriodWithoutJitter) * float64(time.Second))
}
// EnqueueAPMData adds a AgentData struct to the agent data channel, effectively queueing for a send
// to the APM server.
func (c *Client) EnqueueAPMData(agentData AgentData) {
select {
case c.DataChannel <- agentData:
c.logger.Debug("Adding agent data to buffer to be sent to apm server")
default:
c.logger.Warn("Channel full: dropping a subset of agent data")
}
}
// ShouldFlush returns true if the client should flush APM data after processing the event.
func (c *Client) ShouldFlush() bool {
return c.sendStrategy == SyncFlush
}
// ResetFlush resets the client's "agent flushed" state, such that
// subsequent calls to WaitForFlush will block until another request
// is received from the agent indicating it has flushed.
func (c *Client) ResetFlush() {
c.flushMutex.Lock()
defer c.flushMutex.Unlock()
c.flushCh = make(chan struct{})
}
// WaitForFlush returns a channel that is closed when the agent has signalled that
// the Lambda invocation has completed, and there is no more APM data coming.
func (c *Client) WaitForFlush() <-chan struct{} {
c.flushMutex.Lock()
defer c.flushMutex.Unlock()
return c.flushCh
}