mirror of
https://github.com/docker/compose.git
synced 2026-11-17 13:45:39 +00:00
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
OTel SDK/exporter errors already go through docker/cli's own otel.SetErrorHandler(debug.OTELErrorHandler), installed by the plugin framework at startup. Our own handler, installed at package init time, was always clobbered before any command ran (plugin.Run() sets its handler after all inits execute), so it never had any effect - verified by tracing the exact call order and reproducing the flag behavior against a built binary. Drop that dead handler along with the COMPOSE_OTEL_DEBUG env var it introduced. The one genuinely-discarded error (the tracer shutdown/flush error in wrapRunE) now goes through logrus like everything else in the codebase: it's already gated by --debug/-D via logrus's own level, so no explicit check is needed. Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
149 lines
4.4 KiB
Go
149 lines
4.4 KiB
Go
/*
|
|
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"
|
|
"github.com/sirupsen/logrus"
|
|
"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"
|
|
|
|
commands "github.com/docker/compose/v5/cmd/compose"
|
|
"github.com/docker/compose/v5/internal/tracing"
|
|
)
|
|
|
|
// 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.flags", getFlags(cmd.Flags())),
|
|
attribute.Bool("cli.isatty", dockerCli.In().IsTerminal()),
|
|
)
|
|
|
|
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()
|
|
if err := tracingShutdown(ctx); err != nil {
|
|
logrus.WithError(err).Debug("otel: shutdown error")
|
|
}
|
|
}
|
|
return cmdErr
|
|
}
|
|
}
|
|
|
|
// commandName returns the path components for a given command,
|
|
// in reverse alphabetical order for consistent usage metrics.
|
|
//
|
|
// The root Compose command and anything before (i.e. "docker")
|
|
// are not included.
|
|
//
|
|
// For example:
|
|
// - docker compose alpha watch -> [watch, alpha]
|
|
// - 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
|
|
}
|