cmd_span.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright 2023 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 cmdtrace
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "sort"
  19. "strings"
  20. "time"
  21. dockercli "github.com/docker/cli/cli"
  22. "github.com/docker/cli/cli/command"
  23. commands "github.com/docker/compose/v2/cmd/compose"
  24. "github.com/docker/compose/v2/internal/tracing"
  25. "github.com/spf13/cobra"
  26. flag "github.com/spf13/pflag"
  27. "go.opentelemetry.io/otel"
  28. "go.opentelemetry.io/otel/attribute"
  29. "go.opentelemetry.io/otel/codes"
  30. "go.opentelemetry.io/otel/trace"
  31. )
  32. // Setup should be called as part of the command's PersistentPreRunE
  33. // as soon as possible after initializing the dockerCli.
  34. //
  35. // It initializes the tracer for the CLI using both auto-detection
  36. // from the Docker context metadata as well as standard OTEL_ env
  37. // vars, creates a root span for the command, and wraps the actual
  38. // command invocation to ensure the span is properly finalized and
  39. // exported before exit.
  40. func Setup(cmd *cobra.Command, dockerCli command.Cli, args []string) error {
  41. tracingShutdown, err := tracing.InitTracing(dockerCli)
  42. if err != nil {
  43. return fmt.Errorf("initializing tracing: %w", err)
  44. }
  45. ctx := cmd.Context()
  46. ctx, cmdSpan := otel.Tracer("").Start(
  47. ctx,
  48. "cli/"+strings.Join(commandName(cmd), "-"),
  49. )
  50. cmdSpan.SetAttributes(
  51. attribute.StringSlice("cli.flags", getFlags(cmd.Flags())),
  52. attribute.Bool("cli.isatty", dockerCli.In().IsTerminal()),
  53. )
  54. cmd.SetContext(ctx)
  55. wrapRunE(cmd, cmdSpan, tracingShutdown)
  56. return nil
  57. }
  58. // wrapRunE injects a wrapper function around the command's actual RunE (or Run)
  59. // method. This is necessary to capture the command result for reporting as well
  60. // as flushing any spans before exit.
  61. //
  62. // Unfortunately, PersistentPostRun(E) can't be used for this purpose because it
  63. // only runs if RunE does _not_ return an error, but this should run unconditionally.
  64. func wrapRunE(c *cobra.Command, cmdSpan trace.Span, tracingShutdown tracing.ShutdownFunc) {
  65. origRunE := c.RunE
  66. if origRunE == nil {
  67. origRun := c.Run
  68. //nolint:unparam // wrapper function for RunE, always returns nil by design
  69. origRunE = func(cmd *cobra.Command, args []string) error {
  70. origRun(cmd, args)
  71. return nil
  72. }
  73. c.Run = nil
  74. }
  75. c.RunE = func(cmd *cobra.Command, args []string) error {
  76. cmdErr := origRunE(cmd, args)
  77. if cmdSpan != nil {
  78. if cmdErr != nil && !errors.Is(cmdErr, context.Canceled) {
  79. // default exit code is 1 if a more descriptive error
  80. // wasn't returned
  81. exitCode := 1
  82. var statusErr dockercli.StatusError
  83. if errors.As(cmdErr, &statusErr) {
  84. exitCode = statusErr.StatusCode
  85. }
  86. cmdSpan.SetStatus(codes.Error, "CLI command returned error")
  87. cmdSpan.RecordError(cmdErr, trace.WithAttributes(
  88. attribute.Int("exit_code", exitCode),
  89. ))
  90. } else {
  91. cmdSpan.SetStatus(codes.Ok, "")
  92. }
  93. cmdSpan.End()
  94. }
  95. if tracingShutdown != nil {
  96. // use background for root context because the cmd's context might have
  97. // been canceled already
  98. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
  99. defer cancel()
  100. // TODO(milas): add an env var to enable logging from the
  101. // OTel components for debugging purposes
  102. _ = tracingShutdown(ctx)
  103. }
  104. return cmdErr
  105. }
  106. }
  107. // commandName returns the path components for a given command,
  108. // in reverse alphabetical order for consistent usage metrics.
  109. //
  110. // The root Compose command and anything before (i.e. "docker")
  111. // are not included.
  112. //
  113. // For example:
  114. // - docker compose alpha watch -> [watch, alpha]
  115. // - docker-compose up -> [up]
  116. func commandName(cmd *cobra.Command) []string {
  117. var name []string
  118. for c := cmd; c != nil; c = c.Parent() {
  119. if c.Name() == commands.PluginName {
  120. break
  121. }
  122. name = append(name, c.Name())
  123. }
  124. sort.Sort(sort.Reverse(sort.StringSlice(name)))
  125. return name
  126. }
  127. func getFlags(fs *flag.FlagSet) []string {
  128. var result []string
  129. fs.Visit(func(flag *flag.Flag) {
  130. result = append(result, flag.Name)
  131. })
  132. return result
  133. }