Skip to content

Commit

Permalink
Add otel tracing support
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Sep 10, 2023
1 parent 3bf249d commit 2f9b0db
Show file tree
Hide file tree
Showing 127 changed files with 20,289 additions and 3 deletions.
48 changes: 47 additions & 1 deletion cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import (
"github.com/docker/cli/cli/version"
"github.com/docker/docker/api/types/versions"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/tracing/detect"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
Expand All @@ -40,7 +45,48 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", args[0])
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return isSupported(cmd, dockerCli)
dockerCli.WithContext(cmd.Context())
if err := isSupported(cmd, dockerCli); err != nil {
return err
}

// Buildkit's detect package currently follows the old otel spec which defaulted to gRPC.
// Since the spec changed to default to http/protobuf.
// If these env vars are not set then we set them to the new default so detect will give us the expected protocol.
// This is the same as on the dockerd side.
// This can be removed after buildkit's detect package is updated.
if os.Getenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL") == "" && os.Getenv("OTEL_EXPORTER_OTLP_PROTOCOL") == "" {
os.Setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "http/protobuf")
}
detect.Recorder = detect.NewTraceRecorder()

if v := os.Getenv("OTEL_SERVICE_NAME"); v == "" {
os.Setenv("OTEL_SERVICE_NAME", "docker-cli")
}

tp, err := detect.TracerProvider()
if err != nil {
return fmt.Errorf("error setting up tracing: %w", err)
}

otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
if tp != nil {
otel.SetTracerProvider(tp)
ctx, _ := tp.Tracer("").Start(cmd.Context(), cmd.Name())
cmd.SetContext(ctx)
dockerCli.WithContext(ctx)
}

return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// TODO: There doesn't seem to be a way to determine if the command returned an an error
// so we can set the span status here.
trace.SpanFromContext(cmd.Context()).End()
tracer, ok := otel.GetTracerProvider().(*tracesdk.TracerProvider)
if ok {
tracer.Shutdown(cmd.Context())
}
},
Version: fmt.Sprintf("%s, build %s", version.Version, version.GitCommit),
DisableFlagsInUseLine: true,
Expand Down
15 changes: 13 additions & 2 deletions vendor.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ require (
gotest.tools/v3 v3.5.0
)

require (
go.opentelemetry.io/otel v1.4.1
go.opentelemetry.io/otel/sdk v1.4.1
go.opentelemetry.io/otel/trace v1.4.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
Expand All @@ -60,6 +67,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand All @@ -75,10 +83,13 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.etcd.io/etcd/raft/v3 v3.5.6 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.29.0 // indirect
go.opentelemetry.io/otel v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.4.1 // indirect
go.opentelemetry.io/otel/internal/metric v0.27.0 // indirect
go.opentelemetry.io/otel/metric v0.27.0 // indirect
go.opentelemetry.io/otel/trace v1.4.1 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand Down
Loading

0 comments on commit 2f9b0db

Please sign in to comment.