Skip to content

Commit a07548d

Browse files
committed
connect client side opentelemetry
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 34c05e2 commit a07548d

File tree

10 files changed

+192
-3
lines changed

10 files changed

+192
-3
lines changed

cmd/buildx/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
cliflags "github.com/docker/cli/cli/flags"
1616
"github.com/moby/buildkit/solver/errdefs"
1717
"github.com/moby/buildkit/util/stack"
18+
"go.opentelemetry.io/otel"
19+
20+
_ "github.com/moby/buildkit/util/tracing/detect/delegated"
21+
_ "github.com/moby/buildkit/util/tracing/env"
1822

1923
// FIXME: "k8s.io/client-go/plugin/pkg/client/auth/azure" is excluded because of compilation error
2024
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
@@ -31,6 +35,9 @@ var experimental string
3135
func init() {
3236
seed.WithTimeAndRand()
3337
stack.SetVersionInfo(version.Version, version.Revision)
38+
39+
// do not log tracing errors to stdio
40+
otel.SetErrorHandler(skipErrors{})
3441
}
3542

3643
func main() {
@@ -90,3 +97,7 @@ func main() {
9097
os.Exit(1)
9198
}
9299
}
100+
101+
type skipErrors struct{}
102+
103+
func (skipErrors) Handle(err error) {}

commands/bake.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/buildx/bake"
1010
"github.com/docker/buildx/build"
1111
"github.com/docker/buildx/util/progress"
12+
"github.com/docker/buildx/util/tracing"
1213
"github.com/docker/cli/cli/command"
1314
"github.com/moby/buildkit/util/appcontext"
1415
"github.com/pkg/errors"
@@ -25,6 +26,14 @@ type bakeOptions struct {
2526
func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error) {
2627
ctx := appcontext.Context()
2728

29+
ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
30+
if err != nil {
31+
return err
32+
}
33+
defer func() {
34+
end(err)
35+
}()
36+
2837
var url string
2938

3039
if len(targets) > 0 {

commands/build.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/docker/buildx/util/buildflags"
1111
"github.com/docker/buildx/util/platformutil"
1212
"github.com/docker/buildx/util/progress"
13+
"github.com/docker/buildx/util/tracing"
1314
"github.com/docker/cli/cli"
1415
"github.com/docker/cli/cli/command"
1516
"github.com/moby/buildkit/client"
@@ -75,7 +76,7 @@ type commonOptions struct {
7576
exportLoad bool
7677
}
7778

78-
func runBuild(dockerCli command.Cli, in buildOptions) error {
79+
func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
7980
if in.squash {
8081
return errors.Errorf("squash currently not implemented")
8182
}
@@ -85,6 +86,14 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
8586

8687
ctx := appcontext.Context()
8788

89+
ctx, end, err := tracing.TraceCurrentCommand(ctx, "build")
90+
if err != nil {
91+
return err
92+
}
93+
defer func() {
94+
end(err)
95+
}()
96+
8897
noCache := false
8998
if in.noCache != nil {
9099
noCache = *in.noCache

driver/docker-container/driver.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
dockerclient "github.com/docker/docker/client"
2222
"github.com/docker/docker/pkg/stdcopy"
2323
"github.com/moby/buildkit/client"
24+
"github.com/moby/buildkit/util/tracing/detect"
2425
"github.com/pkg/errors"
2526
)
2627

@@ -279,9 +280,11 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
279280

280281
conn = demuxConn(conn)
281282

283+
tracer, _ := detect.Tracer()
284+
282285
return client.New(ctx, "", client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
283286
return conn, nil
284-
}))
287+
}), client.WithTracer(tracer))
285288
}
286289

287290
func (d *Driver) Factory() driver.Factory {

driver/kubernetes/driver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/buildx/util/platformutil"
1616
"github.com/docker/buildx/util/progress"
1717
"github.com/moby/buildkit/client"
18+
"github.com/moby/buildkit/util/tracing/detect"
1819
"github.com/pkg/errors"
1920
appsv1 "k8s.io/api/apps/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -169,9 +170,10 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
169170
if err != nil {
170171
return nil, err
171172
}
173+
tracer, _ := detect.Tracer()
172174
return client.New(ctx, "", client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
173175
return conn, nil
174-
}))
176+
}), client.WithTracer(tracer))
175177
}
176178

177179
func (d *Driver) Factory() driver.Factory {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ require (
5050
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea
5151
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
5252
github.com/zclconf/go-cty v1.7.1
53+
go.opentelemetry.io/otel v0.20.0
54+
go.opentelemetry.io/otel/trace v0.20.0
5355
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
5456
gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect
5557
gopkg.in/fatih/pool.v2 v2.0.0 // indirect

util/tracing/trace.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tracing
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"github.com/moby/buildkit/util/tracing/detect"
8+
"go.opentelemetry.io/otel/attribute"
9+
"go.opentelemetry.io/otel/trace"
10+
)
11+
12+
func TraceCurrentCommand(ctx context.Context, name string) (context.Context, func(error), error) {
13+
tracer, err := detect.Tracer()
14+
if err != nil {
15+
return context.Background(), nil, err
16+
}
17+
ctx, span := tracer.Start(ctx, name, trace.WithAttributes(
18+
attribute.Array("command", attribute.ArrayValue(os.Args)),
19+
))
20+
21+
return ctx, func(err error) {
22+
if err != nil {
23+
span.RecordError(err)
24+
}
25+
span.End()
26+
27+
detect.Shutdown(context.TODO())
28+
}, nil
29+
}

vendor/github.com/moby/buildkit/util/tracing/detect/delegated/delegated.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/buildkit/util/tracing/env/traceenv.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ github.com/moby/buildkit/util/sshutil
302302
github.com/moby/buildkit/util/stack
303303
github.com/moby/buildkit/util/system
304304
github.com/moby/buildkit/util/tracing/detect
305+
github.com/moby/buildkit/util/tracing/detect/delegated
306+
github.com/moby/buildkit/util/tracing/env
305307
github.com/moby/buildkit/util/tracing/otlpgrpc
306308
github.com/moby/buildkit/util/tracing/otlptransform
307309
# github.com/moby/locker v1.0.1

0 commit comments

Comments
 (0)