Skip to content

Commit

Permalink
feat: log a warning when authentication with APM server fails (#228)
Browse files Browse the repository at this point in the history
* feat: log a warning if the APM agent data request fails

The intake route of the APM server sends a 202 status accepted
if the request succeeds.

* test: update test server to reflect APM intake route behaviour

* fix: update code to only handle auth errors (401)

* fix: add test to assert the transport status after auth error
  • Loading branch information
kruskall authored Jul 18, 2022
1 parent 5f3bada commit 2057119
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apm-lambda-extension/extension/apm_server_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ func (transport *ApmServerTransport) PostToApmServer(ctx context.Context, agentD
return fmt.Errorf("failed to read the response body after posting to the APM server")
}

if resp.StatusCode == http.StatusUnauthorized {
Log.Warnf("Authentication with the APM server failed: response status code: %d", resp.StatusCode)
Log.Debugf("APM server response body: %v", string(body))
return nil
}

transport.SetApmServerTransportState(ctx, Healthy)
Log.Debug("Transport status set to healthy")
Log.Debugf("APM server response body: %v", string(body))
Expand Down
49 changes: 49 additions & 0 deletions apm-lambda-extension/extension/apm_server_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestPostToApmServerDataCompressed(t *testing.T) {
bytes, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, string(data), string(bytes))
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(`{"foo": "bar"}`)); err != nil {
t.Fail()
return
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestPostToApmServerDataNotCompressed(t *testing.T) {
compressedBytes, _ := ioutil.ReadAll(pr)
assert.Equal(t, string(compressedBytes), string(requestBytes))
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(`{"foo": "bar"}`)); err != nil {
t.Fail()
return
Expand Down Expand Up @@ -334,6 +336,7 @@ func TestAPMServerRecovery(t *testing.T) {
bytes, _ := ioutil.ReadAll(r.Body)
assert.Equal(t, string(data), string(bytes))
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(`{"foo": "bar"}`)); err != nil {
return
}
Expand All @@ -359,6 +362,52 @@ func TestAPMServerRecovery(t *testing.T) {
assert.Equal(t, transport.reconnectionCount, -1)
}

func TestAPMServerAuthFails(t *testing.T) {
// Compress the data
pr, pw := io.Pipe()
gw, _ := gzip.NewWriterLevel(pw, gzip.BestSpeed)
go func() {
if _, err := gw.Write([]byte("")); err != nil {
t.Fail()
return
}
if err := gw.Close(); err != nil {
t.Fail()
return
}
if err := pw.Close(); err != nil {
t.Fail()
return
}
}()

// Create AgentData struct with compressed data
data, _ := io.ReadAll(pr)
agentData := AgentData{Data: data, ContentEncoding: "gzip"}

// Create apm server and handler
apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
}))
defer apmServer.Close()

config := extensionConfig{
apmServerUrl: apmServer.URL + "/",
}

transport := InitApmServerTransport(&config)
transport.SetApmServerTransportState(context.Background(), Healthy)
transport.SetApmServerTransportState(context.Background(), Failing)
for {
if transport.status != Failing {
break
}
}
assert.Equal(t, transport.status, Pending)
assert.NoError(t, transport.PostToApmServer(context.Background(), agentData))
assert.NotEqual(t, transport.status, Healthy)
}

func TestContinuedAPMServerFailure(t *testing.T) {
// Compress the data
pr, pw := io.Pipe()
Expand Down

0 comments on commit 2057119

Please sign in to comment.