Skip to content

Commit 9b9cccc

Browse files
Refactor: Improve tracing error handling for OpenTelemetry
Co-authored-by: jacob <jacob@depot.dev>
1 parent 33162cb commit 9b9cccc

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

pkg/buildx/commands/bake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator, pri
4949

5050
ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
5151
if err != nil {
52-
return nil, nil, err
52+
return nil, nil, wrapTracingError(err)
5353
}
5454
defer func() {
5555
end(err)

pkg/buildx/commands/build.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func runBuild(dockerCli command.Cli, validatedOpts map[string]build.Options, in
140140

141141
ctx, end, err := tracing.TraceCurrentCommand(ctx, "build")
142142
if err != nil {
143-
return err
143+
return wrapTracingError(err)
144144
}
145145
defer func() {
146146
end(err)
@@ -1100,6 +1100,18 @@ func wrapBuildError(err error, bake bool) error {
11001100
return err
11011101
}
11021102

1103+
func wrapTracingError(err error) error {
1104+
if err == nil {
1105+
return nil
1106+
}
1107+
errMsg := err.Error()
1108+
if strings.Contains(errMsg, "conflicting Schema URL") || strings.Contains(errMsg, "cannot merge resource") {
1109+
msg := fmt.Sprintf("%s\n\nThis error is usually caused by conflicting OpenTelemetry environment variables.\nTo resolve this issue, try setting DEPOT_DISABLE_OTEL=1 in your environment.", errMsg)
1110+
return &wrapped{err, msg}
1111+
}
1112+
return err
1113+
}
1114+
11031115
type wrapped struct {
11041116
err error
11051117
msg string

pkg/buildx/commands/build_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package commands
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestWrapTracingError(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
err error
13+
shouldWrap bool
14+
expectedMsg string
15+
expectedInMsg string
16+
}{
17+
{
18+
name: "nil error",
19+
err: nil,
20+
shouldWrap: false,
21+
},
22+
{
23+
name: "conflicting Schema URL error",
24+
err: errors.New("Error: cannot merge resource due to conflicting Schema URL"),
25+
shouldWrap: true,
26+
expectedInMsg: "DEPOT_DISABLE_OTEL=1",
27+
},
28+
{
29+
name: "cannot merge resource error",
30+
err: errors.New("cannot merge resource with different schema"),
31+
shouldWrap: true,
32+
expectedInMsg: "OpenTelemetry environment variables",
33+
},
34+
{
35+
name: "unrelated error",
36+
err: errors.New("some other error"),
37+
shouldWrap: false,
38+
},
39+
}
40+
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
result := wrapTracingError(tt.err)
44+
45+
if tt.err == nil {
46+
if result != nil {
47+
t.Errorf("expected nil error, got %v", result)
48+
}
49+
return
50+
}
51+
52+
if tt.shouldWrap {
53+
wrapped, ok := result.(*wrapped)
54+
if !ok {
55+
t.Errorf("expected wrapped error, got %T", result)
56+
return
57+
}
58+
59+
if !strings.Contains(wrapped.Error(), tt.expectedInMsg) {
60+
t.Errorf("expected error message to contain %q, got %q", tt.expectedInMsg, wrapped.Error())
61+
}
62+
63+
// Verify we can unwrap to get the original error
64+
if errors.Unwrap(result) != tt.err {
65+
t.Errorf("expected unwrapped error to be original error")
66+
}
67+
} else {
68+
// Should return the original error unchanged
69+
if result != tt.err {
70+
t.Errorf("expected error to be unchanged, got %v", result)
71+
}
72+
}
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)