Skip to content

Commit

Permalink
[Heartbeat] Set HTTP user-agent to 'elastic_heartbeat'
Browse files Browse the repository at this point in the history
By default heartbeat uses the golang user agent, which is blacklisted
by a number of services, including elastic.co. URLs like
https://www.elastic.co/products/beats/heartbeat do not work with the
default go user agent due to such blacklists.

This changes the default user agent to be 'elastic_heartbeat'.

While the HTTP spec allows UAs to add version numbers to the end this PR
does not add the version number for two reasons:

1. Adding the version wouldn't practically be of use to anyone as one UA
version numbers for an uptime check aren't practically useful.
2. It would needlessly add complexity to this commit.

Resolves elastic#10170 (comment)
  • Loading branch information
andrewvc committed Oct 29, 2019
1 parent 6b6408f commit 6caa091
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Removed the `add_host_metadata` and `add_cloud_metadata` processors from the default config. These don't fit well with ECS for Heartbeat and were rarely used.
- Fixed/altered redirect behavior. `max_redirects` now defaults to 0 (no redirects). Following redirects now works across hosts, but some timing fields will not be reported. {pull}14125[14125]
- Removed `host.name` field that should never have been included. Heartbeat uses `observer.*` fields instead. {pull}14140[14140]
- Changed default user-agent to be `elastic_heartbeat` as the current default `Go-http-client/1.1` is often blacklisted. {pull}14291[14291]

*Journalbeat*

Expand Down
3 changes: 3 additions & 0 deletions heartbeat/monitors/active/http/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func buildRequest(addr string, config *Config, enc contentEncoder) (*http.Reques

request.Header.Add(k, v)
}
if ua := request.Header.Get("User-Agent"); ua == "" {
request.Header.Set("User-Agent", "elastic_heartbeat")
}

if enc != nil {
enc.AddHeaders(&request.Header)
Expand Down
28 changes: 28 additions & 0 deletions heartbeat/monitors/active/http/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -169,3 +171,29 @@ func TestRequestBuildingWithCustomHost(t *testing.T) {
assert.Equal(t, "custom-host", request.Header.Get("Host"))
}
}

func TestRequestBuildingWithNoUserAgent(t *testing.T) {
request, err := buildRequest("localhost", &Config{}, nilEncoder{})

require.Nil(t, err)
assert.Equal(t, "elastic_heartbeat", request.Header.Get("User-Agent"))
}

func TestRequestBuildingWithExplicitUserAgent(t *testing.T) {
expectedUserAgent := "some-user-agent"

var config = Config{
Check: checkConfig{
Request: requestParameters{
SendHeaders: map[string]string{
"User-Agent": expectedUserAgent,
},
},
},
}

request, err := buildRequest("localhost", &config, nilEncoder{})

require.Nil(t, err)
assert.Equal(t, expectedUserAgent, request.Header.Get("User-Agent"))
}

0 comments on commit 6caa091

Please sign in to comment.