github.com/Jeffail/benthos/v3@v3.65.0/internal/tracing/span.go (about)

     1  package tracing
     2  
     3  import (
     4  	"github.com/opentracing/opentracing-go"
     5  )
     6  
     7  // Span abstracts the span type of our global tracing system in order to allow
     8  // it to be replaced in future.
     9  type Span struct {
    10  	w opentracing.Span
    11  }
    12  
    13  func openTracingSpan(s opentracing.Span) *Span {
    14  	if s == nil {
    15  		return nil
    16  	}
    17  	return &Span{w: s}
    18  }
    19  
    20  func (s *Span) unwrap() opentracing.Span {
    21  	if s == nil {
    22  		return nil
    23  	}
    24  	return s.w
    25  }
    26  
    27  // LogKV adds log key/value pairs to the span.
    28  func (s *Span) LogKV(kv ...string) {
    29  	alts := make([]interface{}, 0, len(kv))
    30  	for _, v := range kv {
    31  		alts = append(alts, v)
    32  	}
    33  	s.w.LogKV(alts...)
    34  }
    35  
    36  // SetTag sets a given tag to a value.
    37  func (s *Span) SetTag(key string, value interface{}) {
    38  	s.w.SetTag(key, value)
    39  }
    40  
    41  // Finish the span.
    42  func (s *Span) Finish() {
    43  	s.w.Finish()
    44  }
    45  
    46  // TextMap attempts to inject a span into a map object in text map format.
    47  func (s *Span) TextMap() (map[string]interface{}, error) {
    48  	spanMap := opentracing.TextMapCarrier{}
    49  
    50  	if err := opentracing.GlobalTracer().Inject(s.w.Context(), opentracing.TextMap, spanMap); err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	spanMapGeneric := make(map[string]interface{}, len(spanMap))
    55  	for k, v := range spanMap {
    56  		spanMapGeneric[k] = v
    57  	}
    58  
    59  	return spanMapGeneric, nil
    60  }