github.com/blend/go-sdk@v1.20220411.3/tracing/autoflushtrace/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 autoflushtrace
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/opentracing/opentracing-go"
    15  
    16  	"github.com/blend/go-sdk/autoflush"
    17  	"github.com/blend/go-sdk/tracing"
    18  )
    19  
    20  var (
    21  	_ autoflush.Tracer        = (*autoflushTracer)(nil)
    22  	_ autoflush.TraceFinisher = (*autoflushTraceFinisher)(nil)
    23  )
    24  
    25  // Tracer returns a new tracer.
    26  func Tracer(tracer opentracing.Tracer) autoflush.Tracer {
    27  	return &autoflushTracer{tracer: tracer}
    28  }
    29  
    30  type autoflushTracer struct {
    31  	tracer opentracing.Tracer
    32  }
    33  
    34  func (aft autoflushTracer) StartAdd(ctx context.Context) autoflush.TraceFinisher {
    35  	startOptions := []opentracing.StartSpanOption{
    36  		opentracing.Tag{Key: tracing.TagKeySpanType, Value: "queue"},
    37  		tracing.TagMeasured(),
    38  		opentracing.StartTime(time.Now().UTC()),
    39  	}
    40  	span, _ := tracing.StartSpanFromContext(ctx, aft.tracer, "autoflush.add", startOptions...)
    41  	return autoflushTraceFinisher{span: span}
    42  }
    43  
    44  func (aft autoflushTracer) StartAddMany(ctx context.Context) autoflush.TraceFinisher {
    45  	startOptions := []opentracing.StartSpanOption{
    46  		opentracing.Tag{Key: tracing.TagKeySpanType, Value: "queue"},
    47  		tracing.TagMeasured(),
    48  		opentracing.StartTime(time.Now().UTC()),
    49  	}
    50  	span, _ := tracing.StartSpanFromContext(ctx, aft.tracer, "autoflush.add_many", startOptions...)
    51  	return autoflushTraceFinisher{span: span}
    52  }
    53  
    54  func (aft autoflushTracer) StartQueueFlush(ctx context.Context) autoflush.TraceFinisher {
    55  	startOptions := []opentracing.StartSpanOption{
    56  		opentracing.Tag{Key: tracing.TagKeySpanType, Value: "queue"},
    57  		tracing.TagMeasured(),
    58  		opentracing.StartTime(time.Now().UTC()),
    59  	}
    60  	span, _ := tracing.StartSpanFromContext(ctx, aft.tracer, "autoflush.queue_flush", startOptions...)
    61  	return autoflushTraceFinisher{span: span}
    62  }
    63  
    64  func (aft autoflushTracer) StartFlush(ctx context.Context) (context.Context, autoflush.TraceFinisher) {
    65  	startOptions := []opentracing.StartSpanOption{
    66  		opentracing.Tag{Key: tracing.TagKeySpanType, Value: "queue"},
    67  		tracing.TagMeasured(),
    68  		opentracing.StartTime(time.Now().UTC()),
    69  	}
    70  	span, ctx := tracing.StartSpanFromContext(ctx, aft.tracer, "autoflush.flush", startOptions...)
    71  	return ctx, autoflushTraceFinisher{span: span}
    72  }
    73  
    74  type autoflushTraceFinisher struct {
    75  	span opentracing.Span
    76  }
    77  
    78  func (tf autoflushTraceFinisher) Finish(err error) {
    79  	if err != nil {
    80  		tracing.SpanError(tf.span, err)
    81  	}
    82  	tf.span.Finish()
    83  }