cmd_span.go 4.3 KB

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