Skip to content

Commit e1aa4f7

Browse files
authored
otel: add args & flags to cli traces (docker#10974)
Signed-off-by: rvigus <[email protected]>
1 parent d7b0b2b commit e1aa4f7

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

cmd/cmdtrace/cmd_span.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
commands "github.com/docker/compose/v2/cmd/compose"
3030
"github.com/docker/compose/v2/internal/tracing"
3131
"github.com/spf13/cobra"
32+
flag "github.com/spf13/pflag"
3233
"go.opentelemetry.io/otel/attribute"
3334
"go.opentelemetry.io/otel/codes"
3435
"go.opentelemetry.io/otel/trace"
@@ -42,7 +43,7 @@ import (
4243
// vars, creates a root span for the command, and wraps the actual
4344
// command invocation to ensure the span is properly finalized and
4445
// exported before exit.
45-
func Setup(cmd *cobra.Command, dockerCli command.Cli) error {
46+
func Setup(cmd *cobra.Command, dockerCli command.Cli, args []string) error {
4647
tracingShutdown, err := tracing.InitTracing(dockerCli)
4748
if err != nil {
4849
return fmt.Errorf("initializing tracing: %w", err)
@@ -53,6 +54,9 @@ func Setup(cmd *cobra.Command, dockerCli command.Cli) error {
5354
ctx,
5455
"cli/"+strings.Join(commandName(cmd), "-"),
5556
)
57+
cmdSpan.SetAttributes(attribute.StringSlice("cli.args", args))
58+
cmdSpan.SetAttributes(attribute.StringSlice("cli.flags", getFlags(cmd.Flags())))
59+
5660
cmd.SetContext(ctx)
5761
wrapRunE(cmd, cmdSpan, tracingShutdown)
5862
return nil
@@ -129,3 +133,11 @@ func commandName(cmd *cobra.Command) []string {
129133
sort.Sort(sort.Reverse(sort.StringSlice(name)))
130134
return name
131135
}
136+
137+
func getFlags(fs *flag.FlagSet) []string {
138+
var result []string
139+
fs.Visit(func(flag *flag.Flag) {
140+
result = append(result, flag.Name)
141+
})
142+
return result
143+
}

cmd/cmdtrace/cmd_span_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmdtrace
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
flag "github.com/spf13/pflag"
24+
)
25+
26+
func TestGetFlags(t *testing.T) {
27+
// Initialize flagSet with flags
28+
fs := flag.NewFlagSet("up", flag.ContinueOnError)
29+
var (
30+
detach string
31+
timeout string
32+
)
33+
fs.StringVar(&detach, "detach", "d", "")
34+
fs.StringVar(&timeout, "timeout", "t", "")
35+
_ = fs.Set("detach", "detach")
36+
_ = fs.Set("timeout", "timeout")
37+
38+
tests := []struct {
39+
name string
40+
input *flag.FlagSet
41+
expected []string
42+
}{
43+
{
44+
name: "NoFlags",
45+
input: flag.NewFlagSet("NoFlags", flag.ContinueOnError),
46+
expected: nil,
47+
},
48+
{
49+
name: "Flags",
50+
input: fs,
51+
expected: []string{"detach", "timeout"},
52+
},
53+
}
54+
55+
for _, test := range tests {
56+
t.Run(test.name, func(t *testing.T) {
57+
result := getFlags(test.input)
58+
if !reflect.DeepEqual(result, test.expected) {
59+
t.Errorf("Expected %v, but got %v", test.expected, result)
60+
}
61+
})
62+
}
63+
64+
}

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func pluginMain() {
4545
}
4646
// TODO(milas): add an env var to enable logging from the
4747
// OTel components for debugging purposes
48-
_ = cmdtrace.Setup(cmd, dockerCli)
48+
_ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:])
4949

5050
if originalPreRun != nil {
5151
return originalPreRun(cmd, args)

0 commit comments

Comments
 (0)