Skip to content

Commit

Permalink
Rename Headers struct members to ProxyHeader
Browse files Browse the repository at this point in the history
The Headers slice is only ever used for an optional PROXY protocol
header, and was easy to confuse with HTTP headers.

Signed-off-by: Daniel Swarbrick <[email protected]>
  • Loading branch information
dswarbrick committed Sep 16, 2024
1 parent 4fa6677 commit ac01f1c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
23 changes: 12 additions & 11 deletions collector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type CacheObjectClient struct {
ch connectionHandler
basicAuthString string
headers []string
proxyHeader string
}

type connectionHandler interface {
Expand Down Expand Up @@ -48,11 +48,11 @@ func buildBasicAuthString(login string, password string) string {
}

type CacheObjectRequest struct {
Hostname string
Port int
Login string
Password string
Headers []string
Hostname string
Port int
Login string
Password string
ProxyHeader string
}

/*NewCacheObjectClient initializes a new cache client */
Expand All @@ -63,7 +63,7 @@ func NewCacheObjectClient(cor *CacheObjectRequest) *CacheObjectClient {
cor.Port,
},
buildBasicAuthString(cor.Login, cor.Password),
cor.Headers,
cor.ProxyHeader,
}
}

Expand All @@ -73,7 +73,7 @@ func (c *CacheObjectClient) readFromSquid(endpoint string) (*bufio.Reader, error
if err != nil {
return nil, err
}
r, err := get(conn, endpoint, c.basicAuthString, c.headers)
r, err := get(conn, endpoint, c.basicAuthString, c.proxyHeader)

if err != nil {
return nil, err
Expand Down Expand Up @@ -207,11 +207,12 @@ func (ch *connectionHandlerImpl) connect() (net.Conn, error) {
return net.Dial("tcp", fmt.Sprintf("%s:%d", ch.hostname, ch.port))
}

func get(conn net.Conn, path string, basicAuthString string, headers []string) (*http.Response, error) {
func get(conn net.Conn, path, basicAuthString, proxyHeader string) (*http.Response, error) {
var buf bytes.Buffer

for _, h := range headers {
fmt.Fprintf(&buf, "%s\r\n", h)
if proxyHeader != "" {
buf.WriteString(proxyHeader)
buf.WriteString("\r\n")
}

fmt.Fprintf(&buf, "GET cache_object://localhost/%s HTTP/1.0\r\n", path)
Expand Down
6 changes: 4 additions & 2 deletions collector/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ func TestReadFromSquid(t *testing.T) {
coc := &CacheObjectClient{
ch,
"",
[]string{},
"",
}
expected := "GET cache_object://localhost/test HTTP/1.0\r\nHost: localhost\r\nUser-Agent: squidclient/3.5.12\r\nAccept: */*\r\n\r\n"
// This test is overly brittle; the order of HTTP headers is not significant. Go sorts headers
// lexicographically when calling http.Header.Write().
expected := "GET cache_object://localhost/test HTTP/1.0\r\nAccept: */*\r\nHost: localhost\r\nUser-Agent: squidclient/3.5.12\r\n\r\n"
coc.readFromSquid("test")

assert.Equal(t, expected, string(ch.buffer))
Expand Down
14 changes: 7 additions & 7 deletions collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type Exporter struct {
}

type CollectorConfig struct {
Hostname string
Port int
Login string
Password string
Labels config.Labels
Headers []string
Hostname string
Port int
Login string
Password string
Labels config.Labels
ProxyHeader string
}

/*New initializes a new exporter */
Expand All @@ -57,7 +57,7 @@ func New(c *CollectorConfig) *Exporter {
c.Port,
c.Login,
c.Password,
c.Headers,
c.ProxyHeader,
}),

c.Hostname,
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ func main() {
}
collector.ExtractServiceTimes = cfg.ExtractServiceTimes

headers := []string{}
proxyHeader := ""

if cfg.UseProxyHeader {
headers = append(headers, createProxyHeader(cfg))
proxyHeader = createProxyHeader(cfg)
}

log.Println("Scraping metrics from", fmt.Sprintf("%s:%d", cfg.SquidHostname, cfg.SquidPort))
e := collector.New(&collector.CollectorConfig{
Hostname: cfg.SquidHostname,
Port: cfg.SquidPort,
Login: cfg.Login,
Password: cfg.Password,
Labels: cfg.Labels,
Headers: headers,
Hostname: cfg.SquidHostname,
Port: cfg.SquidPort,
Login: cfg.Login,
Password: cfg.Password,
Labels: cfg.Labels,
ProxyHeader: proxyHeader,
})
prometheus.MustRegister(e)

Expand Down

0 comments on commit ac01f1c

Please sign in to comment.