main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "context"
  16. "os"
  17. "time"
  18. dockercli "github.com/docker/cli/cli"
  19. "github.com/docker/cli/cli-plugins/manager"
  20. "github.com/docker/cli/cli-plugins/plugin"
  21. "github.com/docker/cli/cli/command"
  22. "github.com/pkg/errors"
  23. "github.com/spf13/cobra"
  24. "go.opentelemetry.io/otel/attribute"
  25. "go.opentelemetry.io/otel/codes"
  26. "go.opentelemetry.io/otel/trace"
  27. "github.com/docker/compose/v2/cmd/compatibility"
  28. commands "github.com/docker/compose/v2/cmd/compose"
  29. "github.com/docker/compose/v2/internal"
  30. "github.com/docker/compose/v2/internal/tracing"
  31. "github.com/docker/compose/v2/pkg/api"
  32. "github.com/docker/compose/v2/pkg/compose"
  33. )
  34. func pluginMain() {
  35. plugin.Run(func(dockerCli command.Cli) *cobra.Command {
  36. var tracingShutdown tracing.ShutdownFunc
  37. var cmdSpan trace.Span
  38. serviceProxy := api.NewServiceProxy().WithService(compose.NewComposeService(dockerCli))
  39. cmd := commands.RootCommand(dockerCli, serviceProxy)
  40. originalPreRun := cmd.PersistentPreRunE
  41. cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
  42. if err := plugin.PersistentPreRunE(cmd, args); err != nil {
  43. return err
  44. }
  45. // the call to plugin.PersistentPreRunE is what actually
  46. // initializes the command.Cli instance, so this is the earliest
  47. // that tracing can be practically initialized (in the future,
  48. // this could ideally happen in coordination with docker/cli)
  49. tracingShutdown, _ = tracing.InitTracing(dockerCli)
  50. ctx := cmd.Context()
  51. ctx, cmdSpan = tracing.Tracer.Start(
  52. ctx, "cli/"+cmd.Name(),
  53. trace.WithAttributes(
  54. attribute.String("compose.version", internal.Version),
  55. attribute.String("docker.context", dockerCli.CurrentContext()),
  56. ),
  57. )
  58. cmd.SetContext(ctx)
  59. if originalPreRun != nil {
  60. return originalPreRun(cmd, args)
  61. }
  62. return nil
  63. }
  64. // manually wrap RunE instead of using PersistentPostRunE because the
  65. // latter only runs when RunE does _not_ return an error, but the
  66. // tracing clean-up logic should always be invoked
  67. originalPersistentPostRunE := cmd.PersistentPostRunE
  68. cmd.PersistentPostRunE = func(cmd *cobra.Command, args []string) (err error) {
  69. defer func() {
  70. if cmdSpan != nil {
  71. if err != nil && !errors.Is(err, context.Canceled) {
  72. cmdSpan.SetStatus(codes.Error, "CLI command returned error")
  73. cmdSpan.RecordError(err)
  74. }
  75. cmdSpan.End()
  76. }
  77. if tracingShutdown != nil {
  78. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  79. defer cancel()
  80. _ = tracingShutdown(ctx)
  81. }
  82. }()
  83. if originalPersistentPostRunE != nil {
  84. return originalPersistentPostRunE(cmd, args)
  85. }
  86. return nil
  87. }
  88. cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
  89. return dockercli.StatusError{
  90. StatusCode: compose.CommandSyntaxFailure.ExitCode,
  91. Status: err.Error(),
  92. }
  93. })
  94. return cmd
  95. },
  96. manager.Metadata{
  97. SchemaVersion: "0.1.0",
  98. Vendor: "Docker Inc.",
  99. Version: internal.Version,
  100. })
  101. }
  102. func main() {
  103. if plugin.RunningStandalone() {
  104. os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
  105. }
  106. pluginMain()
  107. }