Skip to content

Commit

Permalink
api: add User-Agent header (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
LawnGnome authored Nov 2, 2021
1 parent 240d3b6 commit a54e098
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.

### Added

- Requests to Sourcegraph will now include the operating system and architecture `src` is running on by default. To disable this, set the `SRC_DISABLE_USER_AGENT_TELEMETRY` environment variable to any non-empty string, or provide the `-user-agent-telemetry=false` flag on the command line. [#15769](https://github.com/sourcegraph/sourcegraph/issues/15769)

### Changed

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ mv /usr/local/bin/src /usr/local/bin/src-cli

You can then invoke it via `src-cli`.

## Telemetry

`src` includes the operating system and architecture in the `User-Agent` header sent to Sourcegraph. For example, running `src` version 3.21.10 on an x86-64 Linux host will result in this header:

```
src-cli/3.21.10 linux amd64
```

To disable this and _only_ send the version, you can set `-user-agent-telemetry=false` for a single command, or set the `SRC_DISABLE_USER_AGENT_TELEMETRY` environment variable to any non-blank string.

As with [other Sourcegraph telemetry](https://docs.sourcegraph.com/dev/background-information/telemetry), any collected data is only sent to Sourcegraph.com in aggregate form.

## Development

Some useful notes on developing `src` can be found in [DEVELOPMENT.md](DEVELOPMENT.md).
7 changes: 7 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"io"
"net/http"
"os"
"runtime"
"strings"

ioaux "github.com/jig/teereadcloser"
"github.com/kballard/go-shellquote"
"github.com/mattn/go-isatty"
"github.com/sourcegraph/src-cli/internal/version"
)

// Client instances provide methods to create API requests.
Expand Down Expand Up @@ -161,6 +163,11 @@ func (c *client) createHTTPRequest(ctx context.Context, method, p string, body i
if err != nil {
return nil, err
}
if c.opts.Flags.UserAgentTelemetry() {
req.Header.Set("User-Agent", fmt.Sprintf("src-cli/%s %s %s", version.BuildTag, runtime.GOOS, runtime.GOARCH))
} else {
req.Header.Set("User-Agent", "src-cli/"+version.BuildTag)
}
if c.opts.AccessToken != "" {
req.Header.Set("Authorization", "token "+c.opts.AccessToken)
}
Expand Down
20 changes: 19 additions & 1 deletion internal/api/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import "flag"
import (
"flag"
"os"
)

// Flags encapsulates the standard flags that should be added to all commands
// that issue API requests.
Expand All @@ -9,6 +12,7 @@ type Flags struct {
getCurl *bool
trace *bool
insecureSkipVerify *bool
userAgentTelemetry *bool
}

func (f *Flags) Trace() bool {
Expand All @@ -18,6 +22,13 @@ func (f *Flags) Trace() bool {
return *(f.trace)
}

func (f *Flags) UserAgentTelemetry() bool {
if f.userAgentTelemetry == nil {
return defaultUserAgentTelemetry()
}
return *(f.userAgentTelemetry)
}

// NewFlags instantiates a new Flags structure and attaches flags to the given
// flag set.
func NewFlags(flagSet *flag.FlagSet) *Flags {
Expand All @@ -26,15 +37,22 @@ func NewFlags(flagSet *flag.FlagSet) *Flags {
getCurl: flagSet.Bool("get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"),
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validation of TLS certificates against trusted chains"),
userAgentTelemetry: flagSet.Bool("user-agent-telemetry", defaultUserAgentTelemetry(), "Include the operating system and architecture in the User-Agent sent with requests to Sourcegraph"),
}
}

func defaultFlags() *Flags {
telemetry := defaultUserAgentTelemetry()
d := false
return &Flags{
dump: &d,
getCurl: &d,
trace: &d,
insecureSkipVerify: &d,
userAgentTelemetry: &telemetry,
}
}

func defaultUserAgentTelemetry() bool {
return os.Getenv("SRC_DISABLE_USER_AGENT_TELEMETRY") == ""
}

0 comments on commit a54e098

Please sign in to comment.