github.com/blend/go-sdk@v1.20220411.3/tracing/crontrace/tracer.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package crontrace
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	opentracing "github.com/opentracing/opentracing-go"
    15  
    16  	"github.com/blend/go-sdk/cron"
    17  	"github.com/blend/go-sdk/tracing"
    18  )
    19  
    20  // Tracer returns a opentracing cron tracer.
    21  func Tracer(t opentracing.Tracer) cron.Tracer {
    22  	return &tracer{tracer: t}
    23  }
    24  
    25  type tracer struct {
    26  	tracer opentracing.Tracer
    27  }
    28  
    29  func (t tracer) Start(ctx context.Context, jobName string) (context.Context, cron.TraceFinisher) {
    30  	startOptions := []opentracing.StartSpanOption{
    31  		opentracing.Tag{Key: tracing.TagKeyResourceName, Value: jobName},
    32  		opentracing.Tag{Key: tracing.TagKeySpanType, Value: tracing.SpanTypeJob},
    33  		tracing.TagMeasured(),
    34  		opentracing.StartTime(time.Now().UTC()),
    35  	}
    36  	span, spanCtx := tracing.StartSpanFromContext(ctx, t.tracer, tracing.OperationJob, startOptions...)
    37  	return spanCtx, &traceFinisher{span: span}
    38  }
    39  
    40  type traceFinisher struct {
    41  	span opentracing.Span
    42  }
    43  
    44  func (tf traceFinisher) Finish(ctx context.Context, err error) {
    45  	if tf.span == nil {
    46  		return
    47  	}
    48  	tracing.SpanError(tf.span, err)
    49  	tf.span.Finish()
    50  }