Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
tracing: Add tracing support to virtcontainers
Browse files Browse the repository at this point in the history
Add additional `context.Context` parameters and `struct` fields to allow
trace spans to be created by the `virtcontainers` internal functions,
objects and sub-packages.

Note that not every function is traced; we can add more traces as
desired.

Fixes #566.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Aug 22, 2018
1 parent 6ddc9b4 commit d0679a6
Show file tree
Hide file tree
Showing 40 changed files with 658 additions and 200 deletions.
4 changes: 2 additions & 2 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func create(ctx context.Context, containerID, bundlePath, console, pidFilePath s
},
}
kataLog.WithField("factory", factoryConfig).Info("load vm factory")
f, err := vf.NewFactory(factoryConfig, true)
f, err := vf.NewFactory(ctx, factoryConfig, true)
if err != nil {
kataLog.WithError(err).Warn("load vm factory failed, about to create new one")
f, err = vf.NewFactory(factoryConfig, false)
f, err = vf.NewFactory(ctx, factoryConfig, false)
if err != nil {
kataLog.WithError(err).Warn("create vm factory failed")
}
Expand Down
37 changes: 26 additions & 11 deletions cli/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ var factoryCLICommand = cli.Command{
var initFactoryCommand = cli.Command{
Name: "init",
Usage: "initialize a VM factory based on kata-runtime configuration",
Action: func(context *cli.Context) error {
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
Action: func(c *cli.Context) error {
ctx, err := cliContextToContext(c)
if err != nil {
return err
}

runtimeConfig, ok := c.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
if !ok {
return errors.New("invalid runtime config")
}
Expand All @@ -50,7 +55,7 @@ var initFactoryCommand = cli.Command{
},
}
kataLog.WithField("factory", factoryConfig).Info("create vm factory")
_, err := vf.NewFactory(factoryConfig, false)
_, err := vf.NewFactory(ctx, factoryConfig, false)
if err != nil {
kataLog.WithError(err).Error("create vm factory failed")
return err
Expand All @@ -68,8 +73,13 @@ var initFactoryCommand = cli.Command{
var destroyFactoryCommand = cli.Command{
Name: "destroy",
Usage: "destroy the VM factory",
Action: func(context *cli.Context) error {
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
Action: func(c *cli.Context) error {
ctx, err := cliContextToContext(c)
if err != nil {
return err
}

runtimeConfig, ok := c.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
if !ok {
return errors.New("invalid runtime config")
}
Expand All @@ -85,12 +95,12 @@ var destroyFactoryCommand = cli.Command{
},
}
kataLog.WithField("factory", factoryConfig).Info("load vm factory")
f, err := vf.NewFactory(factoryConfig, true)
f, err := vf.NewFactory(ctx, factoryConfig, true)
if err != nil {
kataLog.WithError(err).Error("load vm factory failed")
// ignore error
} else {
f.CloseFactory()
f.CloseFactory(ctx)
}
}
fmt.Fprintln(defaultOutputFile, "vm factory destroyed")
Expand All @@ -101,8 +111,13 @@ var destroyFactoryCommand = cli.Command{
var statusFactoryCommand = cli.Command{
Name: "status",
Usage: "query the status of VM factory",
Action: func(context *cli.Context) error {
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
Action: func(c *cli.Context) error {
ctx, err := cliContextToContext(c)
if err != nil {
return err
}

runtimeConfig, ok := c.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
if !ok {
return errors.New("invalid runtime config")
}
Expand All @@ -118,11 +133,11 @@ var statusFactoryCommand = cli.Command{
},
}
kataLog.WithField("factory", factoryConfig).Info("load vm factory")
f, err := vf.NewFactory(factoryConfig, true)
f, err := vf.NewFactory(ctx, factoryConfig, true)
if err != nil {
fmt.Fprintln(defaultOutputFile, "vm factory is off")
} else {
f.CloseFactory()
f.CloseFactory(ctx)
fmt.Fprintln(defaultOutputFile, "vm factory is on")
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package virtcontainers

import (
"context"
"fmt"
"syscall"

Expand Down Expand Up @@ -129,7 +130,7 @@ type agent interface {
// init().
// After init() is called, agent implementations should be initialized and ready
// to handle all other Agent interface methods.
init(sandbox *Sandbox, config interface{}) error
init(ctx context.Context, sandbox *Sandbox, config interface{}) error

// capabilities should return a structure that specifies the capabilities
// supported by the agent.
Expand Down
71 changes: 41 additions & 30 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func trace(parent context.Context, name string) (opentracing.Span, context.Conte
return span, ctx
}

func traceWithSubsys(ctx context.Context, subsys, name string) (opentracing.Span, context.Context) {
span, ctx := opentracing.StartSpanFromContext(ctx, name)

span.SetTag("subsystem", subsys)

return span, ctx
}

// SetLogger sets the logger for virtcontainers package.
func SetLogger(ctx context.Context, logger *logrus.Entry) {
fields := virtLog.Data
Expand All @@ -54,19 +62,22 @@ func CreateSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Fac
span, ctx := trace(ctx, "CreateSandbox")
defer span.Finish()

s, err := createSandboxFromConfig(sandboxConfig, factory)
s, err := createSandboxFromConfig(ctx, sandboxConfig, factory)
if err == nil {
s.releaseStatelessSandbox()
}

return s, err
}

func createSandboxFromConfig(sandboxConfig SandboxConfig, factory Factory) (*Sandbox, error) {
func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, factory Factory) (*Sandbox, error) {
span, ctx := trace(ctx, "createSandboxFromConfig")
defer span.Finish()

var err error

// Create the sandbox.
s, err := createSandbox(sandboxConfig, factory)
s, err := createSandbox(ctx, sandboxConfig, factory)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -139,7 +150,7 @@ func DeleteSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer unlockSandbox(lockFile)

// Fetch the sandbox from storage and create it.
s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +183,7 @@ func FetchSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer unlockSandbox(lockFile)

// Fetch the sandbox from storage and create it.
s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +220,7 @@ func StartSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer unlockSandbox(lockFile)

// Fetch the sandbox from storage and create it.
s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,7 +261,7 @@ func StopSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer unlockSandbox(lockFile)

// Fetch the sandbox from storage and create it.
s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -281,7 +292,7 @@ func RunSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor
span, ctx := trace(ctx, "RunSandbox")
defer span.Finish()

s, err := createSandboxFromConfig(sandboxConfig, factory)
s, err := createSandboxFromConfig(ctx, sandboxConfig, factory)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -345,7 +356,7 @@ func StatusSandbox(ctx context.Context, sandboxID string) (SandboxStatus, error)
return SandboxStatus{}, err
}

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
unlockSandbox(lockFile)
return SandboxStatus{}, err
Expand Down Expand Up @@ -401,7 +412,7 @@ func CreateContainer(ctx context.Context, sandboxID string, containerConfig Cont
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -433,7 +444,7 @@ func DeleteContainer(ctx context.Context, sandboxID, containerID string) (VCCont
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand All @@ -459,7 +470,7 @@ func StartContainer(ctx context.Context, sandboxID, containerID string) (VCConta
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -493,7 +504,7 @@ func StopContainer(ctx context.Context, sandboxID, containerID string) (VCContai
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -534,7 +545,7 @@ func EnterContainer(ctx context.Context, sandboxID, containerID string, cmd Cmd)
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -567,7 +578,7 @@ func StatusContainer(ctx context.Context, sandboxID, containerID string) (Contai
return ContainerStatus{}, err
}

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
unlockSandbox(lockFile)
return ContainerStatus{}, err
Expand Down Expand Up @@ -658,7 +669,7 @@ func KillContainer(ctx context.Context, sandboxID, containerID string, signal sy
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return err
}
Expand All @@ -685,7 +696,7 @@ func PauseSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
span, ctx := trace(ctx, "PauseSandbox")
defer span.Finish()

return togglePauseSandbox(sandboxID, true)
return togglePauseSandbox(ctx, sandboxID, true)
}

// ResumeSandbox is the virtcontainers resuming entry point which resumes
Expand All @@ -694,7 +705,7 @@ func ResumeSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
span, ctx := trace(ctx, "ResumeSandbox")
defer span.Finish()

return togglePauseSandbox(sandboxID, false)
return togglePauseSandbox(ctx, sandboxID, false)
}

// ProcessListContainer is the virtcontainers entry point to list
Expand All @@ -717,7 +728,7 @@ func ProcessListContainer(ctx context.Context, sandboxID, containerID string, op
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -752,7 +763,7 @@ func UpdateContainer(ctx context.Context, sandboxID, containerID string, resourc
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return err
}
Expand Down Expand Up @@ -781,7 +792,7 @@ func StatsContainer(ctx context.Context, sandboxID, containerID string) (Contain

defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return ContainerStats{}, err
}
Expand All @@ -790,7 +801,7 @@ func StatsContainer(ctx context.Context, sandboxID, containerID string) (Contain
return s.StatsContainer(containerID)
}

func togglePauseContainer(sandboxID, containerID string, pause bool) error {
func togglePauseContainer(ctx context.Context, sandboxID, containerID string, pause bool) error {
if sandboxID == "" {
return errNeedSandboxID
}
Expand All @@ -805,7 +816,7 @@ func togglePauseContainer(sandboxID, containerID string, pause bool) error {
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return err
}
Expand All @@ -829,15 +840,15 @@ func PauseContainer(ctx context.Context, sandboxID, containerID string) error {
span, ctx := trace(ctx, "PauseContainer")
defer span.Finish()

return togglePauseContainer(sandboxID, containerID, true)
return togglePauseContainer(ctx, sandboxID, containerID, true)
}

// ResumeContainer is the virtcontainers container resume entry point.
func ResumeContainer(ctx context.Context, sandboxID, containerID string) error {
span, ctx := trace(ctx, "ResumeContainer")
defer span.Finish()

return togglePauseContainer(sandboxID, containerID, false)
return togglePauseContainer(ctx, sandboxID, containerID, false)
}

// AddDevice will add a device to sandbox
Expand All @@ -852,7 +863,7 @@ func AddDevice(ctx context.Context, sandboxID string, info deviceConfig.DeviceIn
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand All @@ -872,7 +883,7 @@ func toggleInterface(ctx context.Context, sandboxID string, inf *grpc.Interface,
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -904,7 +915,7 @@ func ListInterfaces(ctx context.Context, sandboxID string) ([]*grpc.Interface, e
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand All @@ -924,7 +935,7 @@ func UpdateRoutes(ctx context.Context, sandboxID string, routes []*grpc.Route) (
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand All @@ -943,7 +954,7 @@ func ListRoutes(ctx context.Context, sandboxID string) ([]*grpc.Route, error) {
}
defer unlockSandbox(lockFile)

s, err := fetchSandbox(sandboxID)
s, err := fetchSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d0679a6

Please sign in to comment.