Skip to content

Commit

Permalink
Merge pull request #95 from vansante/req-tracking
Browse files Browse the repository at this point in the history
Make tracking requests a bit easier by logging
  • Loading branch information
vansante authored Jun 27, 2024
2 parents b2d74d7 + 24bb369 commit 7f693d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log/slog"
"net/http"
"os"
"strconv"
"strings"
"time"
Expand All @@ -22,6 +23,8 @@ var (
ErrResumeNotPossible = errors.New("resume not possible")
)

const clientUserAgent = "go-zfsutils@%s"

// Client is the struct used to send requests to a zfs http server
type Client struct {
server string
Expand All @@ -32,12 +35,19 @@ type Client struct {

// NewClient creates a new client for a zfs http server
func NewClient(server string, logger *slog.Logger) *Client {
return &Client{
client := &Client{
server: server,
headers: make(map[string]string, 8),
logger: logger,
client: http.DefaultClient,
}
host, _ := os.Hostname()
client.headers["User-Agent"] = fmt.Sprintf(
clientUserAgent,
host,
)

return client
}

// SetClient configures a custom http client for doing requests
Expand Down
2 changes: 2 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (h *HTTP) middleware(handle handle) http.HandlerFunc {
logger := h.logger.With(slog.Group("req",
"URL", req.URL.String(),
"method", req.Method),
"remoteAddr", req.RemoteAddr,
"userAgent", req.UserAgent(),
)
logger.Info("zfs.http.middleware: Handling")

Expand Down

0 comments on commit 7f693d8

Please sign in to comment.