mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-22 12:29:49 +00:00
Add initial support for opentracing by using the `jaeger` package. Since opentracing uses the `context` package, add a `context.Context` as the first parameter to all the functions that we might want to trace. Trace "spans" (trace points) are then added by extracting the trace details from the specified context parameter. Notes: - Although the tracer is created in `main()`, the "root span" (aka the first trace point) is not added until `beforeSubcommands()`. This is by design and is a compromise: by delaying the creation of the root span, the spans become much more readable since using the web-based JaegerUI, you will see traces like this: ``` kata-runtime: kata-runtime create ------------ ------------------- ^ ^ | | Trace name First span name (which clearly shows the CLI command that was run) ``` Creating the span earlier means it is necessary to expand 'n' spans in the UI before you get to see the name of the CLI command that was run. In adding support, this became very tedious, hence my design decision to defer the creation of the root span until after signal handling has been setup and after CLI options have been parsed, but still very early in the code path. - At this stage, the tracing stops at the `virtcontainers` call boundary. - Tracing is "always on" as there doesn't appear to be a way to toggle it. However, its resolves to a "nop" unless the tracer can talk to a jaeger agent. Note that this commit required a bit of rework to `beforeSubcommands()` to reduce the cyclomatic complexity. Fixes #557. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
// Copyright (c) 2014,2015,2016 Docker, Inc.
|
|
// Copyright (c) 2017 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var psCLICommand = cli.Command{
|
|
Name: "ps",
|
|
Usage: "ps displays the processes running inside a container",
|
|
ArgsUsage: `<container-id> [ps options]`,
|
|
Flags: []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "format, f",
|
|
Value: "table",
|
|
Usage: `select one of: ` + formatOptions,
|
|
},
|
|
},
|
|
Action: func(context *cli.Context) error {
|
|
ctx, err := cliContextToContext(context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if context.Args().Present() == false {
|
|
return fmt.Errorf("Missing container ID, should at least provide one")
|
|
}
|
|
|
|
var args []string
|
|
if len(context.Args()) > 1 {
|
|
// [1:] is to remove container_id:
|
|
// context.Args(): [container_id ps_arg1 ps_arg2 ...]
|
|
// args: [ps_arg1 ps_arg2 ...]
|
|
args = context.Args()[1:]
|
|
}
|
|
|
|
return ps(ctx, context.Args().First(), context.String("format"), args)
|
|
},
|
|
SkipArgReorder: true,
|
|
}
|
|
|
|
func ps(ctx context.Context, containerID, format string, args []string) error {
|
|
span, _ := opentracing.StartSpanFromContext(ctx, "ps")
|
|
defer span.Finish()
|
|
|
|
if containerID == "" {
|
|
return fmt.Errorf("Missing container ID")
|
|
}
|
|
|
|
kataLog = kataLog.WithField("container", containerID)
|
|
setExternalLoggers(kataLog)
|
|
span.SetTag("container", containerID)
|
|
|
|
// Checks the MUST and MUST NOT from OCI runtime specification
|
|
status, sandboxID, err := getExistingContainerInfo(containerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
containerID = status.ID
|
|
|
|
kataLog = kataLog.WithFields(logrus.Fields{
|
|
"container": containerID,
|
|
"sandbox": sandboxID,
|
|
})
|
|
|
|
setExternalLoggers(kataLog)
|
|
span.SetTag("container", containerID)
|
|
span.SetTag("sandbox", sandboxID)
|
|
|
|
// container MUST be running
|
|
if status.State.State != vc.StateRunning {
|
|
return fmt.Errorf("Container %s is not running", containerID)
|
|
}
|
|
|
|
var options vc.ProcessListOptions
|
|
|
|
options.Args = args
|
|
if len(options.Args) == 0 {
|
|
options.Args = []string{"-ef"}
|
|
}
|
|
|
|
options.Format = format
|
|
|
|
msg, err := vci.ProcessListContainer(containerID, sandboxID, options)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Print(string(msg))
|
|
|
|
return nil
|
|
}
|