From b621948c1b08ba969aa3733be755a3dc78f5f62e Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Wed, 10 Jan 2024 13:30:20 -0500 Subject: [PATCH] feat(cli): report more useful User-Agent on engine API requests When using the Moby/Docker Engine API client, we do not have a useful user agent value being reported. Ideally, in the future, the Docker CLI will set this appropriately for plugins when it initializes the client. For now, manually set it, which is a bit hacky because it requires some casting & manually invoking an option function that's technically meant for initialization. In practice, this is pretty safe - the cast is checked defensively and we ignore any errors (which shouldn't be possible anyway). Signed-off-by: Milas Bowman --- cmd/main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 7686bb2132..d8c011e4d0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -24,6 +24,7 @@ import ( "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/docker/compose/v2/cmd/cmdtrace" + "github.com/docker/docker/client" "github.com/spf13/cobra" "github.com/docker/compose/v2/cmd/compatibility" @@ -42,6 +43,9 @@ func pluginMain() { if err := plugin.PersistentPreRunE(cmd, args); err != nil { return err } + // compose-specific initialization + dockerCliPostInitialize(dockerCli) + // TODO(milas): add an env var to enable logging from the // OTel components for debugging purposes _ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:]) @@ -67,6 +71,22 @@ func pluginMain() { }) } +// dockerCliPostInitialize performs Compose-specific configuration for the +// command.Cli instance provided by the plugin.Run() initialization. +// +// NOTE: This must be called AFTER plugin.PersistentPreRunE. +func dockerCliPostInitialize(dockerCli command.Cli) { + // HACK(milas): remove once docker/cli#4574 is merged; for now, + // set it in a rather roundabout way by grabbing the underlying + // concrete client and manually invoking an option on it + _ = dockerCli.Apply(func(cli *command.DockerCli) error { + if mobyClient, ok := cli.Client().(*client.Client); ok { + _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient) + } + return nil + }) +} + func main() { if plugin.RunningStandalone() { os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)