cmd_span.go 4.0 KB

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