mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-22 20:39:41 +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>
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
// Copyright (c) 2014,2015,2016 Docker, Inc.
|
|
// Copyright (c) 2017 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var stateCLICommand = cli.Command{
|
|
Name: "state",
|
|
Usage: "output the state of a container",
|
|
ArgsUsage: `<container-id>
|
|
|
|
<container-id> is your name for the instance of the container`,
|
|
Description: `The state command outputs current state information for the
|
|
instance of a container.`,
|
|
Action: func(context *cli.Context) error {
|
|
ctx, err := cliContextToContext(context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
args := context.Args()
|
|
if len(args) != 1 {
|
|
return fmt.Errorf("Expecting only one container ID, got %d: %v", len(args), []string(args))
|
|
}
|
|
|
|
return state(ctx, args.First())
|
|
},
|
|
}
|
|
|
|
func state(ctx context.Context, containerID string) error {
|
|
span, _ := opentracing.StartSpanFromContext(ctx, "state")
|
|
defer span.Finish()
|
|
|
|
kataLog = kataLog.WithField("container", containerID)
|
|
span.SetTag("container", containerID)
|
|
|
|
setExternalLoggers(kataLog)
|
|
|
|
// Checks the MUST and MUST NOT from OCI runtime specification
|
|
status, _, err := getExistingContainerInfo(containerID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Convert the status to the expected State structure
|
|
state := oci.StatusToOCIState(status)
|
|
|
|
stateJSON, err := json.MarshalIndent(state, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Print stateJSON to stdout
|
|
fmt.Fprintf(os.Stdout, "%s", stateJSON)
|
|
|
|
return nil
|
|
}
|