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>
100 lines
1.6 KiB
Go
100 lines
1.6 KiB
Go
// Copyright 2018 Intel Corporation.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os/signal"
|
|
"runtime/pprof"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// List of handled signals.
|
|
//
|
|
// The value is true if receiving the signal should be fatal.
|
|
var handledSignalsMap = map[syscall.Signal]bool{
|
|
syscall.SIGABRT: true,
|
|
syscall.SIGBUS: true,
|
|
syscall.SIGILL: true,
|
|
syscall.SIGQUIT: true,
|
|
syscall.SIGSEGV: true,
|
|
syscall.SIGSTKFLT: true,
|
|
syscall.SIGSYS: true,
|
|
syscall.SIGTRAP: true,
|
|
syscall.SIGUSR1: false,
|
|
}
|
|
|
|
func handlePanic(ctx context.Context) {
|
|
r := recover()
|
|
|
|
if r != nil {
|
|
msg := fmt.Sprintf("%s", r)
|
|
kataLog.WithField("panic", msg).Error("fatal error")
|
|
|
|
die(ctx)
|
|
}
|
|
}
|
|
|
|
func backtrace() {
|
|
profiles := pprof.Profiles()
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
for _, p := range profiles {
|
|
// The magic number requests a full stacktrace. See
|
|
// https://golang.org/pkg/runtime/pprof/#Profile.WriteTo.
|
|
pprof.Lookup(p.Name()).WriteTo(buf, 2)
|
|
}
|
|
|
|
for _, line := range strings.Split(buf.String(), "\n") {
|
|
kataLog.Error(line)
|
|
}
|
|
}
|
|
|
|
func fatalSignal(sig syscall.Signal) bool {
|
|
s, exists := handledSignalsMap[sig]
|
|
if !exists {
|
|
return false
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func nonFatalSignal(sig syscall.Signal) bool {
|
|
s, exists := handledSignalsMap[sig]
|
|
if !exists {
|
|
return false
|
|
}
|
|
|
|
return !s
|
|
}
|
|
|
|
func handledSignals() []syscall.Signal {
|
|
var signals []syscall.Signal
|
|
|
|
for sig := range handledSignalsMap {
|
|
signals = append(signals, sig)
|
|
}
|
|
|
|
return signals
|
|
}
|
|
|
|
func die(ctx context.Context) {
|
|
stopTracing(ctx)
|
|
|
|
backtrace()
|
|
|
|
if crashOnError {
|
|
signal.Reset(syscall.SIGABRT)
|
|
syscall.Kill(0, syscall.SIGABRT)
|
|
}
|
|
|
|
exit(1)
|
|
}
|