From 8bedb69f2c48ff15a9d9efbb5553b52efd1518b4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 3 Mar 2025 14:24:00 +0100 Subject: [PATCH] cli-plugins/manager: move OTEL-related code to separate file Signed-off-by: Sebastiaan van Stijn --- cli-plugins/manager/cobra.go | 76 ----------------------------- cli-plugins/manager/telemetry.go | 84 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 76 deletions(-) create mode 100644 cli-plugins/manager/telemetry.go diff --git a/cli-plugins/manager/cobra.go b/cli-plugins/manager/cobra.go index ba3fedcd9a..73dcc9cce1 100644 --- a/cli-plugins/manager/cobra.go +++ b/cli-plugins/manager/cobra.go @@ -3,14 +3,10 @@ package manager import ( "fmt" "os" - "strings" "sync" "github.com/docker/cli/cli/command" "github.com/spf13/cobra" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/baggage" ) const ( @@ -105,75 +101,3 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e }) return err } - -const ( - // resourceAttributesEnvVar is the name of the envvar that includes additional - // resource attributes for OTEL as defined in the [OpenTelemetry specification]. - // - // [OpenTelemetry specification]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration - resourceAttributesEnvVar = "OTEL_RESOURCE_ATTRIBUTES" - - // dockerCLIAttributePrefix is the prefix for any docker cli OTEL attributes. - // - // It is a copy of the const defined in [command.dockerCLIAttributePrefix]. - dockerCLIAttributePrefix = "docker.cli." - cobraCommandPath = attribute.Key("cobra.command_path") -) - -func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Set { - commandPath := cmd.Annotations[CommandAnnotationPluginCommandPath] - if commandPath == "" { - commandPath = fmt.Sprintf("%s %s", cmd.CommandPath(), plugin.Name) - } - - attrSet := attribute.NewSet( - cobraCommandPath.String(commandPath), - ) - - kvs := make([]attribute.KeyValue, 0, attrSet.Len()) - for iter := attrSet.Iter(); iter.Next(); { - attr := iter.Attribute() - kvs = append(kvs, attribute.KeyValue{ - Key: dockerCLIAttributePrefix + attr.Key, - Value: attr.Value, - }) - } - return attribute.NewSet(kvs...) -} - -func appendPluginResourceAttributesEnvvar(env []string, cmd *cobra.Command, plugin Plugin) []string { - if attrs := getPluginResourceAttributes(cmd, plugin); attrs.Len() > 0 { - // Construct baggage members for each of the attributes. - // Ignore any failures as these aren't significant and - // represent an internal issue. - members := make([]baggage.Member, 0, attrs.Len()) - for iter := attrs.Iter(); iter.Next(); { - attr := iter.Attribute() - m, err := baggage.NewMemberRaw(string(attr.Key), attr.Value.AsString()) - if err != nil { - otel.Handle(err) - continue - } - members = append(members, m) - } - - // Combine plugin added resource attributes with ones found in the environment - // variable. Our own attributes should be namespaced so there shouldn't be a - // conflict. We do not parse the environment variable because we do not want - // to handle errors in user configuration. - attrsSlice := make([]string, 0, 2) - if v := strings.TrimSpace(os.Getenv(resourceAttributesEnvVar)); v != "" { - attrsSlice = append(attrsSlice, v) - } - if b, err := baggage.New(members...); err != nil { - otel.Handle(err) - } else if b.Len() > 0 { - attrsSlice = append(attrsSlice, b.String()) - } - - if len(attrsSlice) > 0 { - env = append(env, resourceAttributesEnvVar+"="+strings.Join(attrsSlice, ",")) - } - } - return env -} diff --git a/cli-plugins/manager/telemetry.go b/cli-plugins/manager/telemetry.go new file mode 100644 index 0000000000..3382d4d8d2 --- /dev/null +++ b/cli-plugins/manager/telemetry.go @@ -0,0 +1,84 @@ +package manager + +import ( + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/baggage" +) + +const ( + // resourceAttributesEnvVar is the name of the envvar that includes additional + // resource attributes for OTEL as defined in the [OpenTelemetry specification]. + // + // [OpenTelemetry specification]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration + resourceAttributesEnvVar = "OTEL_RESOURCE_ATTRIBUTES" + + // dockerCLIAttributePrefix is the prefix for any docker cli OTEL attributes. + // + // It is a copy of the const defined in [command.dockerCLIAttributePrefix]. + dockerCLIAttributePrefix = "docker.cli." + cobraCommandPath = attribute.Key("cobra.command_path") +) + +func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Set { + commandPath := cmd.Annotations[CommandAnnotationPluginCommandPath] + if commandPath == "" { + commandPath = fmt.Sprintf("%s %s", cmd.CommandPath(), plugin.Name) + } + + attrSet := attribute.NewSet( + cobraCommandPath.String(commandPath), + ) + + kvs := make([]attribute.KeyValue, 0, attrSet.Len()) + for iter := attrSet.Iter(); iter.Next(); { + attr := iter.Attribute() + kvs = append(kvs, attribute.KeyValue{ + Key: dockerCLIAttributePrefix + attr.Key, + Value: attr.Value, + }) + } + return attribute.NewSet(kvs...) +} + +func appendPluginResourceAttributesEnvvar(env []string, cmd *cobra.Command, plugin Plugin) []string { + if attrs := getPluginResourceAttributes(cmd, plugin); attrs.Len() > 0 { + // Construct baggage members for each of the attributes. + // Ignore any failures as these aren't significant and + // represent an internal issue. + members := make([]baggage.Member, 0, attrs.Len()) + for iter := attrs.Iter(); iter.Next(); { + attr := iter.Attribute() + m, err := baggage.NewMemberRaw(string(attr.Key), attr.Value.AsString()) + if err != nil { + otel.Handle(err) + continue + } + members = append(members, m) + } + + // Combine plugin added resource attributes with ones found in the environment + // variable. Our own attributes should be namespaced so there shouldn't be a + // conflict. We do not parse the environment variable because we do not want + // to handle errors in user configuration. + attrsSlice := make([]string, 0, 2) + if v := strings.TrimSpace(os.Getenv(resourceAttributesEnvVar)); v != "" { + attrsSlice = append(attrsSlice, v) + } + if b, err := baggage.New(members...); err != nil { + otel.Handle(err) + } else if b.Len() > 0 { + attrsSlice = append(attrsSlice, b.String()) + } + + if len(attrsSlice) > 0 { + env = append(env, resourceAttributesEnvVar+"="+strings.Join(attrsSlice, ",")) + } + } + return env +}