Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apmproxy/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (c *Client) handleInfoRequest() (func(w http.ResponseWriter, r *http.Reques
// Don't update the status of the transport as it is possible that the extension
// is frozen while processing the request and context is canceled due to timeout.
c.logger.Errorf("Error querying version from the APM server: %v", err)


// Server is unreachable, return StatusBadGateway (default behaviour) to avoid
// returning a Status OK.
w.WriteHeader(http.StatusBadGateway)
}

return func(w http.ResponseWriter, r *http.Request) {
Expand Down
38 changes: 38 additions & 0 deletions apmproxy/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ func TestInfoProxyErrorStatusCode(t *testing.T) {
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
}

func TestInfoProxyUnreachable(t *testing.T) {
// Create apm server and handler
apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
// Shutdown
apmServer.Close()

// Create extension config and start the server
apmClient, err := apmproxy.NewClient(
apmproxy.WithURL(apmServer.URL),
apmproxy.WithSecretToken("foo"),
apmproxy.WithAPIKey("bar"),
apmproxy.WithReceiverAddress(":1234"),
apmproxy.WithReceiverTimeout(15*time.Second),
apmproxy.WithLogger(zaptest.NewLogger(t).Sugar()),
)
require.NoError(t, err)

require.NoError(t, apmClient.StartReceiver())
defer func() {
require.NoError(t, apmClient.Shutdown())
}()

hosts, _ := net.LookupHost("localhost")
url := "http://" + hosts[0] + ":1234"

// Create a request to send to the extension
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)

// Send the request to the extension
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)

// Make sure we don't get a 200 OK
assert.Equal(t, http.StatusBadGateway, resp.StatusCode)
}

func Test_handleInfoRequest(t *testing.T) {
headers := map[string]string{"Authorization": "test-value"}
// Copied from https://github.com/elastic/apm-server/blob/master/testdata/intake-v2/transactions.ndjson.
Expand Down