Skip to content

Commit

Permalink
correctly validate span from context
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Jun 16, 2021
1 parent c676959 commit b10f259
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion client/solve.go
Expand Up @@ -93,7 +93,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
statusContext, cancelStatus := context.WithCancel(context.Background())
defer cancelStatus()

if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
statusContext = trace.ContextWithSpan(statusContext, span)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/buildctl/common/common.go
Expand Up @@ -64,7 +64,7 @@ func ResolveClient(c *cli.Context) (*client.Client, error) {

ctx := CommandContext(c)

if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
opts = append(opts, client.WithTracer(span.Tracer()))
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/buildctl/common/trace.go
Expand Up @@ -32,8 +32,9 @@ func AttachAppContext(app *cli.App) error {
}
}

ctx, span = tracer.Start(ctx, name)
span.SetAttributes(attribute.KeyValue{Key: "command", Value: attribute.ArrayValue(os.Args)})
ctx, span = tracer.Start(ctx, name, trace.WithAttributes(
attribute.Array("command", attribute.ArrayValue(os.Args)),
))

clicontext.App.Metadata["context"] = ctx
return nil
Expand Down
2 changes: 1 addition & 1 deletion session/grpc.go
Expand Up @@ -43,7 +43,7 @@ func grpcClientConn(ctx context.Context, conn net.Conn) (context.Context, *grpc.
grpc.WithInsecure(),
}

if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
tracer := span.Tracer()
unary = append(unary, filterClient(otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators))))
stream = append(stream, otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators)))
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Expand Up @@ -55,7 +55,7 @@ func NewSession(ctx context.Context, name, sharedKey string) (*Session, error) {

serverOpts := []grpc.ServerOption{}

if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
tracer := span.Tracer()
unary = append(unary, filterServer(otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators))))
stream = append(stream, otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(constTracerProvider{tracer: tracer}), otelgrpc.WithPropagators(propagators)))
Expand Down
2 changes: 1 addition & 1 deletion solver/jobs.go
Expand Up @@ -493,7 +493,7 @@ func (jl *Solver) deleteIfUnreferenced(k digest.Digest, st *state) {
}

func (j *Job) Build(ctx context.Context, e Edge) (CachedResult, error) {
if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
j.span = span
}

Expand Down
2 changes: 1 addition & 1 deletion util/tracing/tracing.go
Expand Up @@ -44,7 +44,7 @@ func FinishWithError(span trace.Span, err error) {
// context.WithoutCancel() that would copy the context but reset ctx.Done
func ContextWithSpanFromContext(ctx, ctx2 context.Context) context.Context {
// if already is a span then noop
if span := trace.SpanFromContext(ctx); span != nil {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
return ctx
}
if span := trace.SpanFromContext(ctx2); span != nil {
Expand Down

0 comments on commit b10f259

Please sign in to comment.