cmd_span.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(attribute.StringSlice("cli.args", args))
  51. cmdSpan.SetAttributes(attribute.StringSlice("cli.flags", getFlags(cmd.Flags())))
  52. cmd.SetContext(ctx)
  53. wrapRunE(cmd, cmdSpan, tracingShutdown)
  54. return nil
  55. }
  56. // wrapRunE injects a wrapper function around the command's actual RunE (or Run)
  57. // method. This is necessary to capture the command result for reporting as well
  58. // as flushing any spans before exit.
  59. //
  60. // Unfortunately, PersistentPostRun(E) can't be used for this purpose because it
  61. // only runs if RunE does _not_ return an error, but this should run unconditionally.
  62. func wrapRunE(c *cobra.Command, cmdSpan trace.Span, tracingShutdown tracing.ShutdownFunc) {
  63. origRunE := c.RunE
  64. if origRunE == nil {
  65. origRun := c.Run
  66. //nolint:unparam // wrapper function for RunE, always returns nil by design
  67. origRunE = func(cmd *cobra.Command, args []string) error {
  68. origRun(cmd, args)
  69. return nil
  70. }
  71. c.Run = nil
  72. }
  73. c.RunE = func(cmd *cobra.Command, args []string) error {
  74. cmdErr := origRunE(cmd, args)
  75. if cmdSpan != nil {
  76. if cmdErr != nil && !errors.Is(cmdErr, context.Canceled) {
  77. // default exit code is 1 if a more descriptive error
  78. // wasn't returned
  79. exitCode := 1
  80. var statusErr dockercli.StatusError
  81. if errors.As(cmdErr, &statusErr) {
  82. exitCode = statusErr.StatusCode
  83. }
  84. cmdSpan.SetStatus(codes.Error, "CLI command returned error")
  85. cmdSpan.RecordError(cmdErr, trace.WithAttributes(
  86. attribute.Int("exit_code", exitCode),
  87. ))
  88. } else {
  89. cmdSpan.SetStatus(codes.Ok, "")
  90. }
  91. cmdSpan.End()
  92. }
  93. if tracingShutdown != nil {
  94. // use background for root context because the cmd's context might have
  95. // been canceled already
  96. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
  97. defer cancel()
  98. // TODO(milas): add an env var to enable logging from the
  99. // OTel components for debugging purposes
  100. _ = tracingShutdown(ctx)
  101. }
  102. return cmdErr
  103. }
  104. }
  105. // commandName returns the path components for a given command.
  106. //
  107. // The root Compose command and anything before (i.e. "docker")
  108. // are not included.
  109. //
  110. // For example:
  111. // - docker compose alpha watch -> [alpha, watch]
  112. // - docker-compose up -> [up]
  113. func commandName(cmd *cobra.Command) []string {
  114. var name []string
  115. for c := cmd; c != nil; c = c.Parent() {
  116. if c.Name() == commands.PluginName {
  117. break
  118. }
  119. name = append(name, c.Name())
  120. }
  121. sort.Sort(sort.Reverse(sort.StringSlice(name)))
  122. return name
  123. }
  124. func getFlags(fs *flag.FlagSet) []string {
  125. var result []string
  126. fs.Visit(func(flag *flag.Flag) {
  127. result = append(result, flag.Name)
  128. })
  129. return result
  130. }