diff --git a/cmd/influx/main.go b/cmd/influx/main.go index d0f0fa5daef..4153c1adebc 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "strings" "sync" "time" @@ -51,7 +52,17 @@ func newHTTPClient() (*httpc.Client, error) { return httpClient, nil } - c, err := http.NewHTTPClient(flags.Host, flags.Token, flags.skipVerify) + userAgent := fmt.Sprintf( + "influx/%s (%s) Sha/%s Date/%s", + version, runtime.GOOS, commit, date, + ) + + c, err := http.NewHTTPClient( + flags.Host, + flags.Token, + flags.skipVerify, + httpc.WithUserAgentHeader(userAgent), + ) if err != nil { return nil, err } diff --git a/pkg/httpc/options.go b/pkg/httpc/options.go index bfc142a15c7..fe4544706bc 100644 --- a/pkg/httpc/options.go +++ b/pkg/httpc/options.go @@ -42,34 +42,26 @@ func WithAuth(fn func(r *http.Request)) ClientOptFn { // WithAuthToken provides token auth for requests. func WithAuthToken(token string) ClientOptFn { - return func(opts *clientOpt) error { - fn := func(r *http.Request) { - r.Header.Set("Authorization", "Token "+token) - } - return WithAuth(fn)(opts) - } + return WithAuth(func(r *http.Request) { + r.Header.Set("Authorization", "Token "+token) + }) } // WithSessionCookie provides cookie auth for requests to mimic the browser. // Typically, session is influxdb.Session.Key. func WithSessionCookie(session string) ClientOptFn { - return func(opts *clientOpt) error { - fn := func(r *http.Request) { - r.AddCookie(&http.Cookie{ - Name: "session", - Value: session, - }) - } - return WithAuth(fn)(opts) - } + return WithAuth(func(r *http.Request) { + r.AddCookie(&http.Cookie{ + Name: "session", + Value: session, + }) + }) } // WithContentType sets the content type that will be applied to the requests created // by the Client. func WithContentType(ct string) ClientOptFn { - return func(opt *clientOpt) error { - return WithHeader(headerContentType, ct)(opt) - } + return WithHeader(headerContentType, ct) } func withDoer(d doer) ClientOptFn { @@ -91,6 +83,11 @@ func WithHeader(header, val string) ClientOptFn { } } +// WithUserAgentHeader sets the user agent for the http client requests. +func WithUserAgentHeader(userAgent string) ClientOptFn { + return WithHeader("User-Agent", userAgent) +} + // WithHTTPClient sets the raw http client on the httpc Client. func WithHTTPClient(c *http.Client) ClientOptFn { return func(opt *clientOpt) error { @@ -136,12 +133,9 @@ func WithWriterFn(fn WriteCloserFn) ClientOptFn { // WithWriterGZIP gzips the request body generated from this client. func WithWriterGZIP() ClientOptFn { - return func(opt *clientOpt) error { - fn := func(w io.WriteCloser) (string, string, io.WriteCloser) { - return headerContentEncoding, "gzip", gzip.NewWriter(w) - } - return WithWriterFn(fn)(opt) - } + return WithWriterFn(func(w io.WriteCloser) (string, string, io.WriteCloser) { + return headerContentEncoding, "gzip", gzip.NewWriter(w) + }) } func defaultHTTPClient(scheme string, insecure bool) *http.Client {