Skip to content

Commit 9dc175d

Browse files
committed
cli/command: un-export ResourceAttributesEnvvar, DockerCliAttributePrefix
These utility functions were added in 8890a1c, and are all related to OTEL. The ResourceAttributesEnvvar const defines the "OTEL_RESOURCE_ATTRIBUTES" environment-variable to use, which is part of the [OpenTelemetry specification], so should be considered a well-known env-var, and not up to us to define a const for. These code-changes were not yet included in a release, so we don't have to deprecate. This patch: - Moves the utility functions to the telemetry files, so that all code related to OpenTelemetry is together. - Un-exports the ResourceAttributesEnvvar to reduce our public API. - Un-exports the DockerCliAttributePrefix to reduce depdency on cli/command in CLI-plugins, but adds a TODO to move telemetry-related code to a common (internal) package. - Deprecates the cli-plugins/manager.ResourceAttributesEnvvar const. This const has no known consumers, so we could skip deprecation, but just in case some codebase uses this. [OpenTelemetry specification]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 43a2fcf commit 9dc175d

4 files changed

Lines changed: 64 additions & 51 deletions

File tree

cli-plugins/manager/cobra.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,17 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e
107107
}
108108

109109
const (
110-
dockerCliAttributePrefix = command.DockerCliAttributePrefix
111-
112-
cobraCommandPath = attribute.Key("cobra.command_path")
110+
// resourceAttributesEnvVar is the name of the envvar that includes additional
111+
// resource attributes for OTEL as defined in the [OpenTelemetry specification].
112+
//
113+
// [OpenTelemetry specification]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration
114+
resourceAttributesEnvVar = "OTEL_RESOURCE_ATTRIBUTES"
115+
116+
// dockerCLIAttributePrefix is the prefix for any docker cli OTEL attributes.
117+
//
118+
// It is a copy of the const defined in [command.dockerCLIAttributePrefix].
119+
dockerCLIAttributePrefix = "docker.cli."
120+
cobraCommandPath = attribute.Key("cobra.command_path")
113121
)
114122

115123
func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Set {
@@ -126,7 +134,7 @@ func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Se
126134
for iter := attrSet.Iter(); iter.Next(); {
127135
attr := iter.Attribute()
128136
kvs = append(kvs, attribute.KeyValue{
129-
Key: dockerCliAttributePrefix + attr.Key,
137+
Key: dockerCLIAttributePrefix + attr.Key,
130138
Value: attr.Value,
131139
})
132140
}
@@ -154,7 +162,7 @@ func appendPluginResourceAttributesEnvvar(env []string, cmd *cobra.Command, plug
154162
// conflict. We do not parse the environment variable because we do not want
155163
// to handle errors in user configuration.
156164
attrsSlice := make([]string, 0, 2)
157-
if v := strings.TrimSpace(os.Getenv(ResourceAttributesEnvvar)); v != "" {
165+
if v := strings.TrimSpace(os.Getenv(resourceAttributesEnvVar)); v != "" {
158166
attrsSlice = append(attrsSlice, v)
159167
}
160168
if b, err := baggage.New(members...); err != nil {
@@ -164,7 +172,7 @@ func appendPluginResourceAttributesEnvvar(env []string, cmd *cobra.Command, plug
164172
}
165173

166174
if len(attrsSlice) > 0 {
167-
env = append(env, ResourceAttributesEnvvar+"="+strings.Join(attrsSlice, ","))
175+
env = append(env, resourceAttributesEnvVar+"="+strings.Join(attrsSlice, ","))
168176
}
169177
}
170178
return env

cli-plugins/manager/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const (
2626

2727
// ResourceAttributesEnvvar is the name of the envvar that includes additional
2828
// resource attributes for OTEL.
29-
ResourceAttributesEnvvar = command.ResourceAttributesEnvvar
29+
//
30+
// Deprecated: The "OTEL_RESOURCE_ATTRIBUTES" env-var is part of the OpenTelemetry specification; users should define their own const for this. This const will be removed in the next release.
31+
ResourceAttributesEnvvar = "OTEL_RESOURCE_ATTRIBUTES"
3032
)
3133

3234
// errPluginNotFound is the error returned when a plugin could not be found.

cli/command/cli.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"path/filepath"
1212
"runtime"
1313
"strconv"
14-
"strings"
1514
"sync"
1615
"time"
1716

@@ -593,46 +592,3 @@ func DefaultContextStoreConfig() store.Config {
593592
defaultStoreEndpoints...,
594593
)
595594
}
596-
597-
const (
598-
// ResourceAttributesEnvvar is the name of the envvar that includes additional
599-
// resource attributes for OTEL.
600-
ResourceAttributesEnvvar = "OTEL_RESOURCE_ATTRIBUTES"
601-
602-
// DockerCliAttributePrefix is the prefix for any docker cli OTEL attributes.
603-
DockerCliAttributePrefix = "docker.cli."
604-
)
605-
606-
func filterResourceAttributesEnvvar() {
607-
if v := os.Getenv(ResourceAttributesEnvvar); v != "" {
608-
if filtered := filterResourceAttributes(v); filtered != "" {
609-
os.Setenv(ResourceAttributesEnvvar, filtered)
610-
} else {
611-
os.Unsetenv(ResourceAttributesEnvvar)
612-
}
613-
}
614-
}
615-
616-
func filterResourceAttributes(s string) string {
617-
if trimmed := strings.TrimSpace(s); trimmed == "" {
618-
return trimmed
619-
}
620-
621-
pairs := strings.Split(s, ",")
622-
elems := make([]string, 0, len(pairs))
623-
for _, p := range pairs {
624-
k, _, found := strings.Cut(p, "=")
625-
if !found {
626-
// Do not interact with invalid otel resources.
627-
elems = append(elems, p)
628-
continue
629-
}
630-
631-
// Skip attributes that have our docker.cli prefix.
632-
if strings.HasPrefix(k, DockerCliAttributePrefix) {
633-
continue
634-
}
635-
elems = append(elems, p)
636-
}
637-
return strings.Join(elems, ",")
638-
}

cli/command/telemetry.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"path/filepath"
7+
"strings"
78
"sync"
89
"time"
910

@@ -216,3 +217,49 @@ func (r *cliReader) ForceFlush(ctx context.Context) error {
216217
func deltaTemporality(_ sdkmetric.InstrumentKind) metricdata.Temporality {
217218
return metricdata.DeltaTemporality
218219
}
220+
221+
// resourceAttributesEnvVar is the name of the envvar that includes additional
222+
// resource attributes for OTEL as defined in the [OpenTelemetry specification].
223+
//
224+
// [OpenTelemetry specification]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration
225+
const resourceAttributesEnvVar = "OTEL_RESOURCE_ATTRIBUTES"
226+
227+
func filterResourceAttributesEnvvar() {
228+
if v := os.Getenv(resourceAttributesEnvVar); v != "" {
229+
if filtered := filterResourceAttributes(v); filtered != "" {
230+
_ = os.Setenv(resourceAttributesEnvVar, filtered)
231+
} else {
232+
_ = os.Unsetenv(resourceAttributesEnvVar)
233+
}
234+
}
235+
}
236+
237+
// dockerCLIAttributePrefix is the prefix for any docker cli OTEL attributes.
238+
// When updating, make sure to also update the copy in cli-plugins/manager.
239+
//
240+
// TODO(thaJeztah): move telemetry-related code to an (internal) package to reduce dependency on cli/command in cli-plugins, which has too many imports.
241+
const dockerCLIAttributePrefix = "docker.cli."
242+
243+
func filterResourceAttributes(s string) string {
244+
if trimmed := strings.TrimSpace(s); trimmed == "" {
245+
return trimmed
246+
}
247+
248+
pairs := strings.Split(s, ",")
249+
elems := make([]string, 0, len(pairs))
250+
for _, p := range pairs {
251+
k, _, found := strings.Cut(p, "=")
252+
if !found {
253+
// Do not interact with invalid otel resources.
254+
elems = append(elems, p)
255+
continue
256+
}
257+
258+
// Skip attributes that have our docker.cli prefix.
259+
if strings.HasPrefix(k, dockerCLIAttributePrefix) {
260+
continue
261+
}
262+
elems = append(elems, p)
263+
}
264+
return strings.Join(elems, ",")
265+
}

0 commit comments

Comments
 (0)