Skip to content

Commit

Permalink
Merge pull request moby#47906 from akerouanton/libnet-add-otel-spans-v3
Browse files Browse the repository at this point in the history
api, daemon, libnet: Create OTel spans at various places
  • Loading branch information
akerouanton authored Jun 14, 2024
2 parents 076c976 + 57c6a5e commit 1882da8
Show file tree
Hide file tree
Showing 77 changed files with 914 additions and 542 deletions.
10 changes: 10 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ issues:
linters:
- staticcheck

- text: "ineffectual assignment to ctx"
source: "ctx[, ].*=.*\\(ctx[,)]"
linters:
- ineffassign

- text: "SA4006: this value of `ctx` is never used"
source: "ctx[, ].*=.*\\(ctx[,)]"
linters:
- staticcheck

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0

Expand Down
4 changes: 4 additions & 0 deletions api/server/router/container/container_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/docker/docker/runconfig"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -188,6 +189,9 @@ func (s *containerRouter) getContainersExport(ctx context.Context, w http.Respon
}

func (s *containerRouter) postContainersStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
ctx, span := otel.Tracer("").Start(ctx, "containerRouter.postContainersStart")
defer span.End()

// If contentLength is -1, we can assumed chunked encoding
// or more technically that the length is unknown
// https://golang.org/src/pkg/net/http/request.go#L139
Expand Down
2 changes: 1 addition & 1 deletion api/server/router/network/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Backend interface {
GetNetworks(filters.Args, backend.NetworkListConfig) ([]network.Inspect, error)
CreateNetwork(nc network.CreateRequest) (*network.CreateResponse, error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
ConnectContainerToNetwork(ctx context.Context, containerName, networkName string, endpointConfig *network.EndpointSettings) error
DisconnectContainerFromNetwork(containerName string, networkName string, force bool) error
DeleteNetwork(networkID string) error
NetworksPrune(ctx context.Context, pruneFilters filters.Args) (*network.PruneReport, error)
Expand Down
2 changes: 1 addition & 1 deletion api/server/router/network/network_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW
// The reason is that, In case of attachable network in swarm scope, the actual local network
// may not be available at the time. At the same time, inside daemon `ConnectContainerToNetwork`
// does the ambiguity check anyway. Therefore, passing the name to daemon would be enough.
return n.backend.ConnectContainerToNetwork(connect.Container, vars["id"], connect.EndpointConfig)
return n.backend.ConnectContainerToNetwork(ctx, connect.Container, vars["id"], connect.EndpointConfig)
}

func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
Expand Down
8 changes: 4 additions & 4 deletions builder/builder-next/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ func (iface *lnInterface) init(c *libnetwork.Controller, n *libnetwork.Network)
defer close(iface.ready)
id := identity.NewID()

ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
ep, err := n.CreateEndpoint(context.TODO(), id, libnetwork.CreateOptionDisableResolution())
if err != nil {
iface.err = err
return
}

sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
sbx, err := c.NewSandbox(context.TODO(), id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
if err != nil {
iface.err = err
return
}

if err := ep.Join(sbx); err != nil {
if err := ep.Join(context.TODO(), sbx); err != nil {
iface.err = err
return
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (iface *lnInterface) Close() error {
<-iface.ready
if iface.sbx != nil {
go func() {
if err := iface.sbx.Delete(); err != nil {
if err := iface.sbx.Delete(context.TODO()); err != nil {
log.G(context.TODO()).WithError(err).Errorf("failed to delete builder network sandbox")
}
if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
Expand Down
10 changes: 9 additions & 1 deletion container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import (
"github.com/moby/sys/symlink"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

const (
Expand Down Expand Up @@ -200,7 +203,12 @@ func (container *Container) toDisk() (*Container, error) {

// CheckpointTo makes the Container's current state visible to queries, and persists state.
// Callers must hold a Container lock.
func (container *Container) CheckpointTo(store *ViewDB) error {
func (container *Container) CheckpointTo(ctx context.Context, store *ViewDB) error {
ctx, span := otel.Tracer("").Start(ctx, "container.CheckpointTo", trace.WithAttributes(
attribute.String("container.ID", container.ID),
attribute.String("container.Name", container.Name)))
defer span.End()

deepCopy, err := container.toDisk()
if err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions container/view_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container // import "github.com/docker/docker/container"

import (
"context"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestViewSaveDelete(t *testing.T) {
t.Fatal(err)
}
c := newContainer(t)
if err := c.CheckpointTo(db); err != nil {
if err := c.CheckpointTo(context.Background(), db); err != nil {
t.Fatal(err)
}
if err := db.Delete(c); err != nil {
Expand All @@ -61,11 +62,11 @@ func TestViewAll(t *testing.T) {
two = newContainer(t)
)
one.Pid = 10
if err := one.CheckpointTo(db); err != nil {
if err := one.CheckpointTo(context.Background(), db); err != nil {
t.Fatal(err)
}
two.Pid = 20
if err := two.CheckpointTo(db); err != nil {
if err := two.CheckpointTo(context.Background(), db); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -94,7 +95,7 @@ func TestViewGet(t *testing.T) {
one = newContainer(t)
)
one.ImageID = "some-image-123"
if err := one.CheckpointTo(db); err != nil {
if err := one.CheckpointTo(context.Background(), db); err != nil {
t.Fatal(err)
}
s, err := db.Snapshot().Get(one.ID)
Expand Down Expand Up @@ -174,7 +175,7 @@ func TestViewWithHealthCheck(t *testing.T) {
Status: "starting",
},
}
if err := one.CheckpointTo(db); err != nil {
if err := one.CheckpointTo(context.Background(), db); err != nil {
t.Fatal(err)
}
s, err := db.Snapshot().Get(one.ID)
Expand Down
2 changes: 1 addition & 1 deletion daemon/cluster/executor/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Backend interface {
ContainerStart(ctx context.Context, name string, checkpoint string, checkpointDir string) error
ContainerStop(ctx context.Context, name string, config container.StopOptions) error
ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
ConnectContainerToNetwork(ctx context.Context, containerName, networkName string, endpointConfig *network.EndpointSettings) error
ActivateContainerServiceBinding(containerName string) error
DeactivateContainerServiceBinding(containerName string) error
UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error
Expand Down
2 changes: 1 addition & 1 deletion daemon/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (daemon *Daemon) Register(c *container.Container) error {
defer c.Unlock()

daemon.containers.Add(c.ID, c)
return c.CheckpointTo(daemon.containersReplica)
return c.CheckpointTo(context.TODO(), daemon.containersReplica)
}

func (daemon *Daemon) newContainer(name string, operatingSystem string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
Expand Down
Loading

0 comments on commit 1882da8

Please sign in to comment.