浏览代码

otel: add args & flags to cli traces (#10974)

Signed-off-by: rvigus <[email protected]>
Rory 2 年之前
父节点
当前提交
e1aa4f779b
共有 3 个文件被更改,包括 78 次插入2 次删除
  1. 13 1
      cmd/cmdtrace/cmd_span.go
  2. 64 0
      cmd/cmdtrace/cmd_span_test.go
  3. 1 1
      cmd/main.go

+ 13 - 1
cmd/cmdtrace/cmd_span.go

@@ -29,6 +29,7 @@ import (
 	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/attribute"
 	"go.opentelemetry.io/otel/codes"
 	"go.opentelemetry.io/otel/trace"
@@ -42,7 +43,7 @@ import (
 // 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) error {
+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)
@@ -53,6 +54,9 @@ func Setup(cmd *cobra.Command, dockerCli command.Cli) error {
 		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
@@ -129,3 +133,11 @@ func commandName(cmd *cobra.Command) []string {
 	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
+}

+ 64 - 0
cmd/cmdtrace/cmd_span_test.go

@@ -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)
+			}
+		})
+	}
+
+}

+ 1 - 1
cmd/main.go

@@ -45,7 +45,7 @@ func pluginMain() {
 			}
 			// TODO(milas): add an env var to enable logging from the
 			// OTel components for debugging purposes
-			_ = cmdtrace.Setup(cmd, dockerCli)
+			_ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:])
 
 			if originalPreRun != nil {
 				return originalPreRun(cmd, args)