mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
otel: add args & flags to cli traces (#10974)
Signed-off-by: rvigus <roryvigus@gmail.com>
This commit is contained in:
parent
d7b0b2bd7d
commit
e1aa4f779b
@ -29,6 +29,7 @@ import (
|
|||||||
commands "github.com/docker/compose/v2/cmd/compose"
|
commands "github.com/docker/compose/v2/cmd/compose"
|
||||||
"github.com/docker/compose/v2/internal/tracing"
|
"github.com/docker/compose/v2/internal/tracing"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/codes"
|
"go.opentelemetry.io/otel/codes"
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
@ -42,7 +43,7 @@ import (
|
|||||||
// vars, creates a root span for the command, and wraps the actual
|
// vars, creates a root span for the command, and wraps the actual
|
||||||
// command invocation to ensure the span is properly finalized and
|
// command invocation to ensure the span is properly finalized and
|
||||||
// exported before exit.
|
// exported before exit.
|
||||||
func Setup(cmd *cobra.Command, dockerCli command.Cli) error {
|
func Setup(cmd *cobra.Command, dockerCli command.Cli, args []string) error {
|
||||||
tracingShutdown, err := tracing.InitTracing(dockerCli)
|
tracingShutdown, err := tracing.InitTracing(dockerCli)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("initializing tracing: %w", err)
|
return fmt.Errorf("initializing tracing: %w", err)
|
||||||
@ -53,6 +54,9 @@ func Setup(cmd *cobra.Command, dockerCli command.Cli) error {
|
|||||||
ctx,
|
ctx,
|
||||||
"cli/"+strings.Join(commandName(cmd), "-"),
|
"cli/"+strings.Join(commandName(cmd), "-"),
|
||||||
)
|
)
|
||||||
|
cmdSpan.SetAttributes(attribute.StringSlice("cli.args", args))
|
||||||
|
cmdSpan.SetAttributes(attribute.StringSlice("cli.flags", getFlags(cmd.Flags())))
|
||||||
|
|
||||||
cmd.SetContext(ctx)
|
cmd.SetContext(ctx)
|
||||||
wrapRunE(cmd, cmdSpan, tracingShutdown)
|
wrapRunE(cmd, cmdSpan, tracingShutdown)
|
||||||
return nil
|
return nil
|
||||||
@ -129,3 +133,11 @@ func commandName(cmd *cobra.Command) []string {
|
|||||||
sort.Sort(sort.Reverse(sort.StringSlice(name)))
|
sort.Sort(sort.Reverse(sort.StringSlice(name)))
|
||||||
return 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
|
||||||
|
}
|
||||||
|
64
cmd/cmdtrace/cmd_span_test.go
Normal file
64
cmd/cmdtrace/cmd_span_test.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 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 (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetFlags(t *testing.T) {
|
||||||
|
// Initialize flagSet with flags
|
||||||
|
fs := flag.NewFlagSet("up", flag.ContinueOnError)
|
||||||
|
var (
|
||||||
|
detach string
|
||||||
|
timeout string
|
||||||
|
)
|
||||||
|
fs.StringVar(&detach, "detach", "d", "")
|
||||||
|
fs.StringVar(&timeout, "timeout", "t", "")
|
||||||
|
_ = fs.Set("detach", "detach")
|
||||||
|
_ = fs.Set("timeout", "timeout")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *flag.FlagSet
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "NoFlags",
|
||||||
|
input: flag.NewFlagSet("NoFlags", flag.ContinueOnError),
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Flags",
|
||||||
|
input: fs,
|
||||||
|
expected: []string{"detach", "timeout"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
result := getFlags(test.input)
|
||||||
|
if !reflect.DeepEqual(result, test.expected) {
|
||||||
|
t.Errorf("Expected %v, but got %v", test.expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -45,7 +45,7 @@ func pluginMain() {
|
|||||||
}
|
}
|
||||||
// TODO(milas): add an env var to enable logging from the
|
// TODO(milas): add an env var to enable logging from the
|
||||||
// OTel components for debugging purposes
|
// OTel components for debugging purposes
|
||||||
_ = cmdtrace.Setup(cmd, dockerCli)
|
_ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:])
|
||||||
|
|
||||||
if originalPreRun != nil {
|
if originalPreRun != nil {
|
||||||
return originalPreRun(cmd, args)
|
return originalPreRun(cmd, args)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user