Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listen on default port 8200 if unspecified #886

Merged
merged 1 commit into from
Apr 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ https://github.com/elastic/apm-server/compare/9e0a1e281e56044745f1a4f18dcbf00a7f

==== Added

- Listen on default port 8200 if unspecified {pull}[886]886.

==== Deprecated

==== Known Issues
Expand Down
7 changes: 7 additions & 0 deletions beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func parseListener(host string) (string, string) {
// This should only be called once, from Run.
func (bt *beater) listen() (net.Listener, error) {
network, path := parseListener(bt.config.Host)
if network == "tcp" {
if _, _, err := net.SplitHostPort(path); err != nil {
// tack on a port if SplitHostPort fails on what should be a tcp network address
// if there were already too many colons, one more won't hurt
path = net.JoinHostPort(path, defaultPort)
}
}
lis, err := net.Listen(network, path)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion beater/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package beater

import (
"net"
"regexp"
"time"

"github.com/elastic/apm-server/sourcemap"
"github.com/elastic/beats/libbeat/common"
)

const defaultPort = "8200"

type Config struct {
Host string `config:"host"`
MaxUnzippedSize int64 `config:"max_unzipped_size"`
Expand Down Expand Up @@ -109,7 +112,7 @@ func replaceVersion(pattern, version string) string {

func defaultConfig(beatVersion string) *Config {
return &Config{
Host: "localhost:8200",
Host: net.JoinHostPort("localhost", defaultPort),
MaxUnzippedSize: 30 * 1024 * 1024, // 30mb
MaxHeaderSize: 1 * 1024 * 1024, // 1mb
ConcurrentRequests: 5,
Expand Down
27 changes: 27 additions & 0 deletions beater/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ func TestServerOk(t *testing.T) {
assert.Equal(t, http.StatusAccepted, res.StatusCode, body(t, res))
}

func TestServerTcpNoPort(t *testing.T) {
// possibly flakey but worth it
// try to connect to localhost:defaultPort
// if connection succeeds, port is in use and skip test
// if it fails, make sure it is because connection refused
if conn, err := net.DialTimeout("tcp", net.JoinHostPort("localhost", defaultPort), 2*time.Second); err == nil {
conn.Close()
t.Skipf("default port is in use")
} else {
if e, ok := err.(*net.OpError); !ok || e.Op != "dial" {
// failed for some other reason, not connection refused
t.Error(err)
}
}
ucfg, err := common.NewConfigFrom(map[string]interface{}{
"host": "localhost",
})
assert.NoError(t, err)
btr, teardown := setupServer(t, ucfg)
defer teardown()

baseUrl, client := btr.client(false)
rsp, err := client.Get(baseUrl + HealthCheckURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rsp.StatusCode)
}

func tmpTestUnix(t *testing.T) string {
f, err := ioutil.TempFile("", "test-apm-server")
assert.NoError(t, err)
Expand Down