From a1f881a0d5b6154c47d1339017c6b787aa1d2fbc Mon Sep 17 00:00:00 2001 From: David Scott Date: Tue, 15 Oct 2024 10:50:33 +0100 Subject: [PATCH] desktop: allow this client to be identified via user-agent Previously the HTTP requests were sent with a generic Go-http-client user-agent which made it hard to determine where the requests are coming from. It's important that we can find clients so that they can be updated if APIs change in future. Signed-off-by: David Scott --- internal/desktop/client.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/desktop/client.go b/internal/desktop/client.go index 4f8c54770ed..6c49cc1160c 100644 --- a/internal/desktop/client.go +++ b/internal/desktop/client.go @@ -27,11 +27,15 @@ import ( "net/http" "strings" + "github.com/docker/compose/v2/internal" "github.com/docker/compose/v2/internal/memnet" "github.com/r3labs/sse" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) +// identify this client in the logs +var userAgent = "compose/" + internal.Version + // Client for integration with Docker Desktop features. type Client struct { apiEndpoint string @@ -76,6 +80,7 @@ func (c *Client) Ping(ctx context.Context) (*PingResponse, error) { if err != nil { return nil, err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err @@ -105,6 +110,7 @@ func (c *Client) FeatureFlags(ctx context.Context) (FeatureFlagResponse, error) if err != nil { return nil, err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err @@ -135,6 +141,7 @@ func (c *Client) GetFileSharesConfig(ctx context.Context) (*GetFileSharesConfigR if err != nil { return nil, err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err @@ -170,6 +177,7 @@ func (c *Client) CreateFileShare(ctx context.Context, r CreateFileShareRequest) return nil, err } req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err @@ -212,6 +220,7 @@ func (c *Client) ListFileShares(ctx context.Context) ([]FileShareSession, error) if err != nil { return nil, err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err @@ -236,6 +245,7 @@ func (c *Client) DeleteFileShare(ctx context.Context, id string) error { if err != nil { return err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return err @@ -268,6 +278,7 @@ func (c *Client) StreamFileShares(ctx context.Context) (<-chan EventMessage[[]Fi if err != nil { return nil, err } + req.Header.Set("User-Agent", userAgent) resp, err := c.client.Do(req) if err != nil { return nil, err