File: cmd_span.go

package info (click to toggle)
docker-compose 2.32.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,300 kB
  • sloc: makefile: 113; sh: 2
file content (144 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
   Copyright 2023 Docker Compose CLI authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package cmdtrace

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"
	"time"

	dockercli "github.com/docker/cli/cli"
	"github.com/docker/cli/cli/command"
	commands "github.com/docker/compose/v2/cmd/compose"
	"github.com/docker/compose/v2/internal/tracing"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/trace"
)

// Setup should be called as part of the command's PersistentPreRunE
// as soon as possible after initializing the dockerCli.
//
// It initializes the tracer for the CLI using both auto-detection
// from the Docker context metadata as well as standard OTEL_ env
// vars, creates a root span for the command, and wraps the actual
// command invocation to ensure the span is properly finalized and
// exported before exit.
func Setup(cmd *cobra.Command, dockerCli command.Cli, args []string) error {
	tracingShutdown, err := tracing.InitTracing(dockerCli)
	if err != nil {
		return fmt.Errorf("initializing tracing: %w", err)
	}

	ctx := cmd.Context()
	ctx, cmdSpan := otel.Tracer("").Start(
		ctx,
		"cli/"+strings.Join(commandName(cmd), "-"),
	)
	cmdSpan.SetAttributes(attribute.StringSlice("cli.args", args))
	cmdSpan.SetAttributes(attribute.StringSlice("cli.flags", getFlags(cmd.Flags())))

	cmd.SetContext(ctx)
	wrapRunE(cmd, cmdSpan, tracingShutdown)
	return nil
}

// wrapRunE injects a wrapper function around the command's actual RunE (or Run)
// method. This is necessary to capture the command result for reporting as well
// as flushing any spans before exit.
//
// Unfortunately, PersistentPostRun(E) can't be used for this purpose because it
// only runs if RunE does _not_ return an error, but this should run unconditionally.
func wrapRunE(c *cobra.Command, cmdSpan trace.Span, tracingShutdown tracing.ShutdownFunc) {
	origRunE := c.RunE
	if origRunE == nil {
		origRun := c.Run
		//nolint:unparam // wrapper function for RunE, always returns nil by design
		origRunE = func(cmd *cobra.Command, args []string) error {
			origRun(cmd, args)
			return nil
		}
		c.Run = nil
	}

	c.RunE = func(cmd *cobra.Command, args []string) error {
		cmdErr := origRunE(cmd, args)
		if cmdSpan != nil {
			if cmdErr != nil && !errors.Is(cmdErr, context.Canceled) {
				// default exit code is 1 if a more descriptive error
				// wasn't returned
				exitCode := 1
				var statusErr dockercli.StatusError
				if errors.As(cmdErr, &statusErr) {
					exitCode = statusErr.StatusCode
				}
				cmdSpan.SetStatus(codes.Error, "CLI command returned error")
				cmdSpan.RecordError(cmdErr, trace.WithAttributes(
					attribute.Int("exit_code", exitCode),
				))

			} else {
				cmdSpan.SetStatus(codes.Ok, "")
			}
			cmdSpan.End()
		}
		if tracingShutdown != nil {
			// use background for root context because the cmd's context might have
			// been canceled already
			ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
			defer cancel()
			// TODO(milas): add an env var to enable logging from the
			// OTel components for debugging purposes
			_ = tracingShutdown(ctx)
		}
		return cmdErr
	}
}

// commandName returns the path components for a given command.
//
// The root Compose command and anything before (i.e. "docker")
// are not included.
//
// For example:
//   - docker compose alpha watch -> [alpha, watch]
//   - docker-compose up -> [up]
func commandName(cmd *cobra.Command) []string {
	var name []string
	for c := cmd; c != nil; c = c.Parent() {
		if c.Name() == commands.PluginName {
			break
		}
		name = append(name, c.Name())
	}
	sort.Sort(sort.Reverse(sort.StringSlice(name)))
	return name
}

func getFlags(fs *flag.FlagSet) []string {
	var result []string
	fs.Visit(func(flag *flag.Flag) {
		result = append(result, flag.Name)
	})
	return result
}