Skip to content

Commit

Permalink
OpenTracing support added to start, stop, run, create, pull, and ps
Browse files Browse the repository at this point in the history
Drop context.Context field from cli.Context

Signed-off-by: Sebastian Jug <sejug@redhat.com>
  • Loading branch information
sjug committed Feb 18, 2019
1 parent bd6ca7c commit d9620f6
Show file tree
Hide file tree
Showing 115 changed files with 17,946 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/podman/cliconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MainFlags struct {
StorageDriver string
StorageOpts []string
Syslog bool
Trace bool

Config string
CpuProfile string
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func getAllOrLatestContainers(c *cliconfig.PodmanCommand, runtime *libpod.Runtim

// getContext returns a non-nil, empty context
func getContext() context.Context {
if Ctx != nil {
return Ctx
}
return context.TODO()
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/podman/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/docker/go-units"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -71,6 +72,11 @@ func init() {
}

func createCmd(c *cliconfig.CreateValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "createCmd")
defer span.Finish()
}

if err := createInit(&c.PodmanCommand); err != nil {
return err
}
Expand All @@ -95,6 +101,11 @@ func createCmd(c *cliconfig.CreateValues) error {
}

func createInit(c *cliconfig.PodmanCommand) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "createInit")
defer span.Finish()
}

// Docker-compatibility: the "-h" flag for run/create is reserved for
// the hostname (see https://github.com/containers/libpod/issues/1367).

Expand All @@ -106,6 +117,10 @@ func createInit(c *cliconfig.PodmanCommand) error {
}

func createContainer(c *cliconfig.PodmanCommand, runtime *libpod.Runtime) (*libpod.Container, *cc.CreateConfig, error) {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "createContainer")
defer span.Finish()
}

rtc := runtime.GetConfig()
ctx := getContext()
Expand Down
21 changes: 21 additions & 0 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"context"
"fmt"
"io"
"log/syslog"
"os"
"os/exec"
Expand All @@ -13,8 +15,10 @@ import (
"github.com/containers/libpod/libpod"
_ "github.com/containers/libpod/pkg/hooks/0.1.0"
"github.com/containers/libpod/pkg/rootless"
"github.com/containers/libpod/pkg/tracing"
"github.com/containers/libpod/version"
"github.com/containers/storage/pkg/reexec"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
lsyslog "github.com/sirupsen/logrus/hooks/syslog"
Expand All @@ -25,6 +29,9 @@ import (
// in the repository
var (
exitCode = 125
Ctx context.Context
span opentracing.Span
closer io.Closer
)

// Commands that the remote and local client have
Expand Down Expand Up @@ -112,6 +119,7 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&MainGlobalOpts.Syslog, "syslog", false, "Output logging information to syslog as well as the console")

rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.TmpDir, "tmpdir", "", "Path to the tmp directory")
rootCmd.PersistentFlags().BoolVar(&MainGlobalOpts.Trace, "trace", false, "enable opentracing output")
rootCmd.AddCommand(mainCommands...)
rootCmd.AddCommand(getMainCommands()...)

Expand Down Expand Up @@ -181,13 +189,26 @@ func before(cmd *cobra.Command, args []string) error {
}
pprof.StartCPUProfile(f)
}
if cmd.Flag("trace").Changed {
var tracer opentracing.Tracer
tracer, closer = tracing.Init("podman")
opentracing.SetGlobalTracer(tracer)

span = tracer.StartSpan("before-context")

Ctx = opentracing.ContextWithSpan(context.Background(), span)
}
return nil
}

func after(cmd *cobra.Command, args []string) error {
if cmd.Flag("cpu-profile").Changed {
pprof.StopCPUProfile()
}
if cmd.Flag("trace").Changed {
span.Finish()
closer.Close()
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/podman/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/containers/libpod/pkg/util"
"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-units"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -193,6 +194,11 @@ func init() {
}

func psCmd(c *cliconfig.PsValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "psCmd")
defer span.Finish()
}

var (
filterFuncs []libpod.ContainerFilter
outputContainers []*libpod.Container
Expand Down
7 changes: 7 additions & 0 deletions cmd/podman/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/containers/libpod/libpod/common"
image2 "github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/util"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,7 +58,13 @@ func init() {
// pullCmd gets the data from the command line and calls pullImage
// to copy an image from a registry to a local machine
func pullCmd(c *cliconfig.PullValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "pullCmd")
defer span.Finish()
}

runtime, err := adapter.GetRuntime(&c.PodmanCommand)

if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/podman/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,6 +45,11 @@ func init() {
}

func runCmd(c *cliconfig.RunValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "runCmd")
defer span.Finish()
}

if err := createInit(&c.PodmanCommand); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/podman/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,6 +45,11 @@ func init() {
}

func startCmd(c *cliconfig.StartValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "startCmd")
defer span.Finish()
}

args := c.InputArgs
if len(args) < 1 && !c.Latest {
return errors.Errorf("you must provide at least one container name or id")
Expand Down
5 changes: 5 additions & 0 deletions cmd/podman/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,6 +47,10 @@ func init() {
}

func stopCmd(c *cliconfig.StopValues) error {
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "stopCmd")
defer span.Finish()
}

if err := checkAllAndLatest(&c.PodmanCommand); err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containers/libpod/pkg/lookup"
"github.com/containers/storage/pkg/stringid"
"github.com/docker/docker/daemon/caps"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -22,6 +23,10 @@ import (

// Init creates a container in the OCI runtime
func (c *Container) Init(ctx context.Context) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "containerInit")
span.SetTag("struct", "container")
defer span.Finish()

if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down Expand Up @@ -66,6 +71,10 @@ func (c *Container) Init(ctx context.Context) (err error) {
// Init().
// If recursive is set, Start will also start all containers this container depends on.
func (c *Container) Start(ctx context.Context, recursive bool) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "containerStart")
span.SetTag("struct", "container")
defer span.Finish()

if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down
29 changes: 29 additions & 0 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/containers/storage/pkg/mount"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/text/language"
Expand Down Expand Up @@ -247,6 +248,10 @@ func (c *Container) syncContainer() error {

// Create container root filesystem for use
func (c *Container) setupStorage(ctx context.Context) error {
span, _ := opentracing.StartSpanFromContext(ctx, "setupStorage")
span.SetTag("type", "container")
defer span.Finish()

if !c.valid {
return errors.Wrapf(ErrCtrRemoved, "container %s is not valid", c.ID())
}
Expand Down Expand Up @@ -779,6 +784,10 @@ func (c *Container) completeNetworkSetup() error {

// Initialize a container, creating it in the runtime
func (c *Container) init(ctx context.Context) error {
span, _ := opentracing.StartSpanFromContext(ctx, "init")
span.SetTag("struct", "container")
defer span.Finish()

// Generate the OCI spec
spec, err := c.generateSpec(ctx)
if err != nil {
Expand Down Expand Up @@ -812,6 +821,10 @@ func (c *Container) init(ctx context.Context) error {
// Deletes the container in the runtime, and resets its state to Exited.
// The container can be restarted cleanly after this.
func (c *Container) cleanupRuntime(ctx context.Context) error {
span, _ := opentracing.StartSpanFromContext(ctx, "cleanupRuntime")
span.SetTag("struct", "container")
defer span.Finish()

// If the container is not ContainerStateStopped, do nothing
if c.state.State != ContainerStateStopped {
return nil
Expand Down Expand Up @@ -847,6 +860,10 @@ func (c *Container) cleanupRuntime(ctx context.Context) error {
// Not necessary for ContainerStateExited - the container has already been
// removed from the runtime, so init() can proceed freely.
func (c *Container) reinit(ctx context.Context) error {
span, _ := opentracing.StartSpanFromContext(ctx, "reinit")
span.SetTag("struct", "container")
defer span.Finish()

logrus.Debugf("Recreating container %s in OCI runtime", c.ID())

if err := c.cleanupRuntime(ctx); err != nil {
Expand Down Expand Up @@ -1079,6 +1096,10 @@ func (c *Container) cleanupStorage() error {
func (c *Container) cleanup(ctx context.Context) error {
var lastError error

span, _ := opentracing.StartSpanFromContext(ctx, "cleanup")
span.SetTag("struct", "container")
defer span.Finish()

logrus.Debugf("Cleaning up container %s", c.ID())

// Clean up network namespace, if present
Expand Down Expand Up @@ -1110,6 +1131,10 @@ func (c *Container) cleanup(ctx context.Context) error {
// delete deletes the container and runs any configured poststop
// hooks.
func (c *Container) delete(ctx context.Context) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "delete")
span.SetTag("struct", "container")
defer span.Finish()

if err := c.runtime.ociRuntime.deleteContainer(c); err != nil {
return errors.Wrapf(err, "error removing container %s from runtime", c.ID())
}
Expand All @@ -1125,6 +1150,10 @@ func (c *Container) delete(ctx context.Context) (err error) {
// the OCI Runtime Specification (which requires them to run
// post-delete, despite the stage name).
func (c *Container) postDeleteHooks(ctx context.Context) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "postDeleteHooks")
span.SetTag("struct", "container")
defer span.Finish()

if c.state.ExtensionStageHooks != nil {
extensionHooks, ok := c.state.ExtensionStageHooks["poststop"]
if ok {
Expand Down
6 changes: 6 additions & 0 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -170,10 +171,15 @@ func (c *Container) cleanupNetwork() error {
// Generate spec for a container
// Accepts a map of the container's dependencies
func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "generateSpec")
span.SetTag("type", "container")
defer span.Finish()

execUser, err := lookup.GetUserGroupInfo(c.state.Mountpoint, c.config.User, nil)
if err != nil {
return nil, err
}

g := generate.NewFromSpec(c.config.Spec)

// If network namespace was requested, add it now
Expand Down
9 changes: 9 additions & 0 deletions libpod/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/containers/storage/pkg/reexec"
digest "github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -126,6 +127,10 @@ func (ir *Runtime) NewFromLocal(name string) (*Image, error) {
// New creates a new image object where the image could be local
// or remote
func (ir *Runtime) New(ctx context.Context, name, signaturePolicyPath, authfile string, writer io.Writer, dockeroptions *DockerRegistryOptions, signingoptions SigningOptions, forcePull bool, label *string) (*Image, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "newImage")
span.SetTag("type", "runtime")
defer span.Finish()

// We don't know if the image is local or not ... check local first
newImage := Image{
InputName: name,
Expand Down Expand Up @@ -805,6 +810,10 @@ func (i *Image) imageInspectInfo(ctx context.Context) (*types.ImageInspectInfo,

// Inspect returns an image's inspect data
func (i *Image) Inspect(ctx context.Context) (*inspect.ImageData, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "imageInspect")
span.SetTag("type", "image")
defer span.Finish()

ociv1Img, err := i.ociv1Image(ctx)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit d9620f6

Please sign in to comment.