Skip to content

Commit

Permalink
Set user agent on outgoing http requests
Browse files Browse the repository at this point in the history
Signed-off-by: Calle Pettersson <calle@cape.nu>
  • Loading branch information
carlpett committed Jun 19, 2020
1 parent 1729592 commit 5f39e50
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/canary/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/loki/pkg/build"
loghttp "github.com/grafana/loki/pkg/loghttp/legacy"
"github.com/grafana/loki/pkg/logproto"
)
Expand All @@ -28,6 +29,7 @@ var (
Name: "ws_reconnects",
Help: "counts every time the websocket connection has to reconnect",
})
userAgent = fmt.Sprintf("loki-canary/%s", build.Version)
)

type LokiReader interface {
Expand Down Expand Up @@ -115,6 +117,7 @@ func (r *Reader) Query(start time.Time, end time.Time) ([]time.Time, error) {
}

req.SetBasicAuth(r.user, r.pass)
req.Header.Set("User-Agent", userAgent)

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/logcli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
json "github.com/json-iterator/go"
"github.com/prometheus/common/config"

"github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/loghttp"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/util"
Expand All @@ -29,6 +30,10 @@ const (
tailPath = "/loki/api/v1/tail"
)

var (
userAgent = fmt.Sprintf("loki-logcli/%s", build.Version)
)

// Client contains fields necessary to query a Loki instance
type Client struct {
TLSConfig config.TLSConfig
Expand Down Expand Up @@ -141,6 +146,7 @@ func (c *Client) doRequest(path, query string, quiet bool, out interface{}) erro
}

req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("User-Agent", userAgent)

if c.OrgID != "" {
req.Header.Set("X-Scope-OrgID", c.OrgID)
Expand Down
4 changes: 4 additions & 0 deletions pkg/promtail/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"

"github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/helpers"
"github.com/grafana/loki/pkg/logproto"
)
Expand Down Expand Up @@ -70,6 +71,8 @@ var (
countersWithHost = []*prometheus.CounterVec{
encodedBytes, sentBytes, droppedBytes, sentEntries, droppedEntries,
}

userAgent = fmt.Sprintf("promtail/%s", build.Version)
)

func init() {
Expand Down Expand Up @@ -259,6 +262,7 @@ func (c *client) send(ctx context.Context, tenantID string, buf []byte) (int, er
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", contentType)
req.Header.Set("User-Agent", userAgent)

// If the tenant ID is not empty promtail is running in multi-tenant mode, so
// we should send it to Loki
Expand Down

0 comments on commit 5f39e50

Please sign in to comment.