Skip to content

Commit

Permalink
connect client side opentelemetry
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jun 16, 2021
1 parent 34c05e2 commit a07548d
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cmd/buildx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
cliflags "github.com/docker/cli/cli/flags"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/util/stack"
"go.opentelemetry.io/otel"

_ "github.com/moby/buildkit/util/tracing/detect/delegated"
_ "github.com/moby/buildkit/util/tracing/env"

// FIXME: "k8s.io/client-go/plugin/pkg/client/auth/azure" is excluded because of compilation error
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand All @@ -31,6 +35,9 @@ var experimental string
func init() {
seed.WithTimeAndRand()
stack.SetVersionInfo(version.Version, version.Revision)

// do not log tracing errors to stdio
otel.SetErrorHandler(skipErrors{})
}

func main() {
Expand Down Expand Up @@ -90,3 +97,7 @@ func main() {
os.Exit(1)
}
}

type skipErrors struct{}

func (skipErrors) Handle(err error) {}
9 changes: 9 additions & 0 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
"github.com/docker/buildx/util/progress"
"github.com/docker/buildx/util/tracing"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/appcontext"
"github.com/pkg/errors"
Expand All @@ -25,6 +26,14 @@ type bakeOptions struct {
func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error) {
ctx := appcontext.Context()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
if err != nil {
return err
}
defer func() {
end(err)
}()

var url string

if len(targets) > 0 {
Expand Down
11 changes: 10 additions & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/buildx/util/tracing"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -75,7 +76,7 @@ type commonOptions struct {
exportLoad bool
}

func runBuild(dockerCli command.Cli, in buildOptions) error {
func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
if in.squash {
return errors.Errorf("squash currently not implemented")
}
Expand All @@ -85,6 +86,14 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {

ctx := appcontext.Context()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "build")
if err != nil {
return err
}
defer func() {
end(err)
}()

noCache := false
if in.noCache != nil {
noCache = *in.noCache
Expand Down
5 changes: 4 additions & 1 deletion driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/tracing/detect"
"github.com/pkg/errors"
)

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

conn = demuxConn(conn)

tracer, _ := detect.Tracer()

return client.New(ctx, "", client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return conn, nil
}))
}), client.WithTracer(tracer))
}

func (d *Driver) Factory() driver.Factory {
Expand Down
4 changes: 3 additions & 1 deletion driver/kubernetes/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/tracing/detect"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -169,9 +170,10 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
if err != nil {
return nil, err
}
tracer, _ := detect.Tracer()
return client.New(ctx, "", client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return conn, nil
}))
}), client.WithTracer(tracer))
}

func (d *Driver) Factory() driver.Factory {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ require (
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/zclconf/go-cty v1.7.1
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/trace v0.20.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect
gopkg.in/fatih/pool.v2 v2.0.0 // indirect
Expand Down
29 changes: 29 additions & 0 deletions util/tracing/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tracing

import (
"context"
"os"

"github.com/moby/buildkit/util/tracing/detect"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

func TraceCurrentCommand(ctx context.Context, name string) (context.Context, func(error), error) {
tracer, err := detect.Tracer()
if err != nil {
return context.Background(), nil, err
}
ctx, span := tracer.Start(ctx, name, trace.WithAttributes(
attribute.Array("command", attribute.ArrayValue(os.Args)),
))

return ctx, func(err error) {
if err != nil {
span.RecordError(err)
}
span.End()

detect.Shutdown(context.TODO())
}, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ github.com/moby/buildkit/util/sshutil
github.com/moby/buildkit/util/stack
github.com/moby/buildkit/util/system
github.com/moby/buildkit/util/tracing/detect
github.com/moby/buildkit/util/tracing/detect/delegated
github.com/moby/buildkit/util/tracing/env
github.com/moby/buildkit/util/tracing/otlpgrpc
github.com/moby/buildkit/util/tracing/otlptransform
# github.com/moby/locker v1.0.1
Expand Down

0 comments on commit a07548d

Please sign in to comment.