|
| 1 | +From 57f85577943734ab47924d1f2595de12a8613fbf Mon Sep 17 00:00:00 2001 |
| 2 | +From: Tonis Tiigi <tonistiigi@gmail.com> |
| 3 | +Date: Mon, 3 Feb 2025 22:14:55 -0800 |
| 4 | +Subject: [PATCH] otel: avoid tracing raw os arguments |
| 5 | + |
| 6 | +User might pass a value that they don't expect to |
| 7 | +be kept in trace storage. For example some cache backends |
| 8 | +allow passing authentication tokens with a flag. |
| 9 | + |
| 10 | +Instead use known primary config values as attributes |
| 11 | +of the root span. |
| 12 | + |
| 13 | +Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com> |
| 14 | +Modified patch to apply to AzureLinux |
| 15 | +Modified-by: Sandeep Karambelkar <skarambelkar@microsoft.com> |
| 16 | +Upstream Patch: https://github.com/docker/buildx/commit/18ccba072076ddbfb0aeedd6746d7719b0729b58 |
| 17 | +Upstream PR: https://github.com/docker/buildx/pull/3068 |
| 18 | +Only first commit is applicable from https://github.com/docker/buildx/pull/3068/commits |
| 19 | +--- |
| 20 | + commands/bake.go | 7 ++++++- |
| 21 | + commands/build.go | 6 +++++- |
| 22 | + util/tracing/trace.go | 7 +++---- |
| 23 | + 3 files changed, 14 insertions(+), 6 deletions(-) |
| 24 | + |
| 25 | +diff --git a/commands/bake.go b/commands/bake.go |
| 26 | +index cb23066..3fc0b74 100644 |
| 27 | +--- a/commands/bake.go |
| 28 | ++++ b/commands/bake.go |
| 29 | +@@ -26,6 +26,7 @@ import ( |
| 30 | + "github.com/moby/buildkit/util/progress/progressui" |
| 31 | + "github.com/pkg/errors" |
| 32 | + "github.com/spf13/cobra" |
| 33 | ++ "go.opentelemetry.io/otel/attribute" |
| 34 | + ) |
| 35 | + |
| 36 | + type bakeOptions struct { |
| 37 | +@@ -42,7 +43,11 @@ type bakeOptions struct { |
| 38 | + } |
| 39 | + |
| 40 | + func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) { |
| 41 | +- ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake") |
| 42 | ++ ctx, end, err := tracing.TraceCurrentCommand(ctx, append([]string{"bake"}, targets...), |
| 43 | ++ attribute.String("builder", in.builder), |
| 44 | ++ attribute.StringSlice("targets", targets), |
| 45 | ++ attribute.StringSlice("files", in.files), |
| 46 | ++ ) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | +diff --git a/commands/build.go b/commands/build.go |
| 51 | +index 6f151d4..e4d88c4 100644 |
| 52 | +--- a/commands/build.go |
| 53 | ++++ b/commands/build.go |
| 54 | +@@ -271,7 +271,11 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) |
| 55 | + mp := dockerCli.MeterProvider(ctx) |
| 56 | + defer metricutil.Shutdown(ctx, mp) |
| 57 | + |
| 58 | +- ctx, end, err := tracing.TraceCurrentCommand(ctx, "build") |
| 59 | ++ ctx, end, err := tracing.TraceCurrentCommand(ctx, []string{"build", options.contextPath}, |
| 60 | ++ attribute.String("builder", options.builder), |
| 61 | ++ attribute.String("context", options.contextPath), |
| 62 | ++ attribute.String("dockerfile", options.dockerfileName), |
| 63 | ++ ) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | +diff --git a/util/tracing/trace.go b/util/tracing/trace.go |
| 68 | +index c95ad5a..13ce349 100644 |
| 69 | +--- a/util/tracing/trace.go |
| 70 | ++++ b/util/tracing/trace.go |
| 71 | +@@ -2,7 +2,6 @@ package tracing |
| 72 | + |
| 73 | + import ( |
| 74 | + "context" |
| 75 | +- "os" |
| 76 | + "strings" |
| 77 | + |
| 78 | + "github.com/moby/buildkit/util/tracing/detect" |
| 79 | +@@ -10,13 +9,13 @@ import ( |
| 80 | + "go.opentelemetry.io/otel/trace" |
| 81 | + ) |
| 82 | + |
| 83 | +-func TraceCurrentCommand(ctx context.Context, name string) (context.Context, func(error), error) { |
| 84 | ++func TraceCurrentCommand(ctx context.Context, args []string, attrs ...attribute.KeyValue) (context.Context, func(error), error) { |
| 85 | + tp, err := detect.TracerProvider() |
| 86 | + if err != nil { |
| 87 | + return context.Background(), nil, err |
| 88 | + } |
| 89 | +- ctx, span := tp.Tracer("").Start(ctx, name, trace.WithAttributes( |
| 90 | +- attribute.String("command", strings.Join(os.Args, " ")), |
| 91 | ++ ctx, span := tp.Tracer("").Start(ctx, strings.Join(args, " "), trace.WithAttributes( |
| 92 | ++ attrs..., |
| 93 | + )) |
| 94 | + |
| 95 | + return ctx, func(err error) { |
| 96 | +-- |
| 97 | +2.45.3 |
| 98 | + |
0 commit comments