Merge pull request #2695 from hinshun/exec-telemetry

Add events for exec op for container start / exit
master
Tõnis Tiigi 2022-03-02 16:37:07 -08:00 committed by GitHub
commit e58afa464c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -11,6 +11,8 @@ import (
"time"
"github.com/moby/buildkit/util/bklog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
@ -207,8 +209,10 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M
}
}()
trace.SpanFromContext(ctx).AddEvent("Container created")
err = w.runProcess(ctx, task, process.Resize, process.Signal, func() {
startedOnce.Do(func() {
trace.SpanFromContext(ctx).AddEvent("Container started")
if started != nil {
close(started)
}
@ -396,6 +400,12 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
if cancel != nil {
cancel()
}
trace.SpanFromContext(ctx).AddEvent(
"Container exited",
trace.WithAttributes(
attribute.Int("exit.code", int(status.ExitCode())),
),
)
if status.ExitCode() != 0 {
exitErr := &gatewayapi.ExitError{
ExitCode: status.ExitCode(),

View File

@ -12,6 +12,8 @@ import (
"time"
"github.com/moby/buildkit/util/bklog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/containerd/containerd/mount"
containerdoci "github.com/containerd/containerd/oci"
@ -316,8 +318,10 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
bklog.G(ctx).Debugf("> creating %s %v", id, meta.Args)
trace.SpanFromContext(ctx).AddEvent("Container created")
err = w.run(runCtx, id, bundle, process, func() {
startedOnce.Do(func() {
trace.SpanFromContext(ctx).AddEvent("Container started")
if started != nil {
close(started)
}
@ -339,6 +343,12 @@ func exitError(ctx context.Context, err error) error {
ExitCode: uint32(runcExitError.Status),
}
}
trace.SpanFromContext(ctx).AddEvent(
"Container exited",
trace.WithAttributes(
attribute.Int("exit.code", int(exitErr.ExitCode)),
),
)
select {
case <-ctx.Done():
exitErr.Err = errors.Wrapf(ctx.Err(), exitErr.Error())
@ -348,6 +358,10 @@ func exitError(ctx context.Context, err error) error {
}
}
trace.SpanFromContext(ctx).AddEvent(
"Container exited",
trace.WithAttributes(attribute.Int("exit.code", 0)),
)
return nil
}

View File

@ -27,6 +27,7 @@ import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/semaphore"
)
@ -234,6 +235,8 @@ func addDefaultEnvvar(env []string, k, v string) []string {
}
func (e *execOp) Exec(ctx context.Context, g session.Group, inputs []solver.Result) (results []solver.Result, err error) {
trace.SpanFromContext(ctx).AddEvent("ExecOp started")
refs := make([]*worker.WorkerRef, len(inputs))
for i, inp := range inputs {
var ok bool