Skip to content

Commit f030327

Browse files
committed
cmd/scollector: Set custom UserAgent and use facebookgo/httpcontrol for 60s RequestTimeout
1 parent a172338 commit f030327

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

cmd/scollector/conf/conf.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type Conf struct {
4949
// UseNtlm specifies if HTTP requests should authenticate with NTLM.
5050
UseNtlm bool
5151

52+
// UserAgentMessage is an optional message that is appended to the User Agent
53+
UserAgentMessage string
54+
5255
HAProxy []HAProxy
5356
SNMP []SNMP
5457
MIBS map[string]MIB

cmd/scollector/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ Default is 500.
103103
MaxQueueLen (integer): is the number of metrics keept internally.
104104
Default is 200000.
105105
106+
UserAgentMessage (string): is an optional message that will be appended to the
107+
User Agent when making HTTP requests. This can be used to add contact details
108+
so external services are aware of who is making the requests.
109+
Example: Scollector/0.6.0 (UserAgentMessage added here)
110+
106111
Filter (array of string): Only include collectors matching these terms. Prefix
107112
with - to invert match and exclude collectors matching those terms. Use
108113
*,-term,-anotherterm to include all collectors except excluded terms.

cmd/scollector/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"bosun.org/slog"
2828
"bosun.org/util"
2929
"github.com/BurntSushi/toml"
30+
"github.com/facebookgo/httpcontrol"
3031
)
3132

3233
var (
@@ -46,6 +47,18 @@ var (
4647
mains []func()
4748
)
4849

50+
type scollectorHTTPTransport struct {
51+
UserAgent string
52+
http.RoundTripper
53+
}
54+
55+
func (t *scollectorHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
56+
if req.Header.Get("User-Agent") == "" {
57+
req.Header.Add("User-Agent", t.UserAgent)
58+
}
59+
return t.RoundTripper.RoundTrip(req)
60+
}
61+
4962
func main() {
5063
flag.Parse()
5164
if *flagToToml != "" {
@@ -64,6 +77,20 @@ func main() {
6477
m()
6578
}
6679
conf := readConf()
80+
ua := "Scollector/" + version.ShortVersion()
81+
if conf.UserAgentMessage != "" {
82+
ua += fmt.Sprintf(" (%s)", conf.UserAgentMessage)
83+
}
84+
client := &http.Client{
85+
Transport: &scollectorHTTPTransport{
86+
ua,
87+
&httpcontrol.Transport{
88+
RequestTimeout: time.Minute,
89+
},
90+
},
91+
}
92+
http.DefaultClient = client
93+
collect.DefaultClient = client
6794
if *flagHost != "" {
6895
conf.Host = *flagHost
6996
}

0 commit comments

Comments
 (0)