From 7141f972700ed454438d8539dd0bec79c0b61cf4 Mon Sep 17 00:00:00 2001
From: Sebastian Jug <sejug@redhat.com>
Date: Tue, 16 Oct 2018 16:30:53 -0400
Subject: OpenTracing support added to start, stop, run, create, pull, and ps

Drop context.Context field from cli.Context

Signed-off-by: Sebastian Jug <sejug@redhat.com>
---
 cmd/podman/cliconfig/config.go |  1 +
 cmd/podman/common.go           |  3 +++
 cmd/podman/create.go           | 15 +++++++++++++++
 cmd/podman/main.go             | 21 +++++++++++++++++++++
 cmd/podman/ps.go               |  6 ++++++
 cmd/podman/pull.go             |  7 +++++++
 cmd/podman/run.go              |  6 ++++++
 cmd/podman/start.go            |  6 ++++++
 cmd/podman/stop.go             |  5 +++++
 9 files changed, 70 insertions(+)

(limited to 'cmd')

diff --git a/cmd/podman/cliconfig/config.go b/cmd/podman/cliconfig/config.go
index 9c9be3618..ca788529c 100644
--- a/cmd/podman/cliconfig/config.go
+++ b/cmd/podman/cliconfig/config.go
@@ -24,6 +24,7 @@ type MainFlags struct {
 	StorageDriver     string
 	StorageOpts       []string
 	Syslog            bool
+	Trace             bool
 
 	Config     string
 	CpuProfile string
diff --git a/cmd/podman/common.go b/cmd/podman/common.go
index ec755c4a8..fed07de7c 100644
--- a/cmd/podman/common.go
+++ b/cmd/podman/common.go
@@ -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()
 }
 
diff --git a/cmd/podman/create.go b/cmd/podman/create.go
index e7efe7502..216f171a8 100644
--- a/cmd/podman/create.go
+++ b/cmd/podman/create.go
@@ -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"
@@ -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
 	}
@@ -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).
 
@@ -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()
diff --git a/cmd/podman/main.go b/cmd/podman/main.go
index f9820c075..ecb72f58b 100644
--- a/cmd/podman/main.go
+++ b/cmd/podman/main.go
@@ -1,7 +1,9 @@
 package main
 
 import (
+	"context"
 	"fmt"
+	"io"
 	"log/syslog"
 	"os"
 	"os/exec"
@@ -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"
@@ -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
@@ -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()...)
 
@@ -181,6 +189,15 @@ 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
 }
 
@@ -188,6 +205,10 @@ 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
 }
 
diff --git a/cmd/podman/ps.go b/cmd/podman/ps.go
index 482e423b7..d7f0d9da0 100644
--- a/cmd/podman/ps.go
+++ b/cmd/podman/ps.go
@@ -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"
@@ -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
diff --git a/cmd/podman/pull.go b/cmd/podman/pull.go
index 859228cbd..0065e975a 100644
--- a/cmd/podman/pull.go
+++ b/cmd/podman/pull.go
@@ -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"
@@ -59,7 +60,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")
 	}
diff --git a/cmd/podman/run.go b/cmd/podman/run.go
index ff4c3b418..bea9b1743 100644
--- a/cmd/podman/run.go
+++ b/cmd/podman/run.go
@@ -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"
@@ -46,6 +47,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
 	}
diff --git a/cmd/podman/start.go b/cmd/podman/start.go
index f3639cb23..db8abae83 100644
--- a/cmd/podman/start.go
+++ b/cmd/podman/start.go
@@ -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"
@@ -46,6 +47,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")
diff --git a/cmd/podman/stop.go b/cmd/podman/stop.go
index 15c3984f4..94fdf321e 100644
--- a/cmd/podman/stop.go
+++ b/cmd/podman/stop.go
@@ -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"
@@ -48,6 +49,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
-- 
cgit v1.2.3-54-g00ecf