github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/implementation/logger/span.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package logger
    14  
    15  import (
    16  	"context"
    17  	"time"
    18  
    19  	"github.com/facebookincubator/go-belt"
    20  	"github.com/facebookincubator/go-belt/pkg/field"
    21  	"github.com/facebookincubator/go-belt/tool/experimental/tracer"
    22  )
    23  
    24  // Event is an unit of information added through method tracer.Span.Annotate.
    25  type Event struct {
    26  	Timestamp time.Time
    27  	Name      string
    28  }
    29  
    30  // SpanImpl is the implementation of tracer.Span.
    31  type SpanImpl struct {
    32  	Tracer       *TracerImpl
    33  	StartTSValue time.Time
    34  	Duration     time.Duration
    35  	ParentValue  tracer.Span
    36  	FieldsValue  *field.FieldsChain
    37  	NameValue    string
    38  	Events       []Event
    39  }
    40  
    41  var _ tracer.Span = (*SpanImpl)(nil)
    42  
    43  func (t *TracerImpl) newSpanCtx(
    44  	ctx context.Context,
    45  	name string,
    46  	parent tracer.Span,
    47  	options ...tracer.SpanOption,
    48  ) (tracer.Span, context.Context) {
    49  	span := &SpanImpl{
    50  		Tracer:       t,
    51  		StartTSValue: time.Now(),
    52  		ParentValue:  parent,
    53  		FieldsValue:  t.Fields,
    54  		NameValue:    name,
    55  	}
    56  	if !t.PreHooks.ProcessSpan(span) {
    57  		return tracer.NewNoopSpan(name, parent, time.Now()), ctx
    58  	}
    59  
    60  	var ctxDer context.Context
    61  	if ctx != nil {
    62  		ctxDer = tracer.CtxWithSpan(ctx, span)
    63  	}
    64  
    65  	return span, ctxDer
    66  }
    67  
    68  func (t *TracerImpl) newSpanBelt(
    69  	_belt *belt.Belt,
    70  	name string,
    71  	parent tracer.Span,
    72  	options ...tracer.SpanOption,
    73  ) (tracer.Span, *belt.Belt) {
    74  	span := &SpanImpl{
    75  		Tracer:       t,
    76  		StartTSValue: time.Now(),
    77  		ParentValue:  parent,
    78  		FieldsValue:  t.Fields,
    79  		NameValue:    name,
    80  	}
    81  	if !t.PreHooks.ProcessSpan(span) {
    82  		return tracer.NewNoopSpan(name, parent, time.Now()), _belt
    83  	}
    84  
    85  	var beltDer *belt.Belt
    86  	if _belt != nil {
    87  		beltDer = tracer.BeltWithSpan(_belt, span)
    88  	}
    89  
    90  	return span, beltDer
    91  }
    92  
    93  // ID implements tracer.Span
    94  func (span *SpanImpl) ID() any {
    95  	return span.TraceIDs()
    96  }
    97  
    98  // TraceIDs implements tracer.Span
    99  func (span *SpanImpl) TraceIDs() belt.TraceIDs {
   100  	return span.Tracer.TraceIDs
   101  }
   102  
   103  // Name implements tracer.Span
   104  func (span *SpanImpl) Name() string {
   105  	return span.NameValue
   106  }
   107  
   108  // StartTS implements tracer.Span
   109  func (span *SpanImpl) StartTS() time.Time {
   110  	return span.StartTSValue
   111  }
   112  
   113  // Fields implements tracer.Span
   114  func (span *SpanImpl) Fields() field.AbstractFields {
   115  	return span.FieldsValue
   116  }
   117  
   118  // Parent implements tracer.Span
   119  func (span *SpanImpl) Parent() tracer.Span {
   120  	return span.ParentValue
   121  }
   122  
   123  // SetName implements tracer.Span
   124  func (span *SpanImpl) SetName(name string) {
   125  	span.NameValue = name
   126  }
   127  
   128  // Annotate implements tracer.Span
   129  func (span *SpanImpl) Annotate(ts time.Time, event string) {
   130  	span.Events = append(span.Events, Event{
   131  		Timestamp: ts,
   132  		Name:      event,
   133  	})
   134  }
   135  
   136  // SetField implements tracer.Span
   137  func (span *SpanImpl) SetField(key field.Key, value field.Value) {
   138  	span.FieldsValue = span.FieldsValue.WithField(key, value)
   139  }
   140  
   141  // SetFields implements tracer.Span
   142  func (span *SpanImpl) SetFields(fields field.AbstractFields) {
   143  	span.FieldsValue = span.FieldsValue.WithFields(fields)
   144  }
   145  
   146  // Finish implements tracer.Span
   147  func (span *SpanImpl) Finish() {
   148  	span.Duration = time.Since(span.StartTSValue)
   149  	span.Tracer.send(span)
   150  }
   151  
   152  // FinishWithDuration implements tracer.Span
   153  func (span *SpanImpl) FinishWithDuration(duration time.Duration) {
   154  	span.Duration = duration
   155  	span.Tracer.send(span)
   156  }
   157  
   158  // Flush implements tracer.Span
   159  func (span *SpanImpl) Flush() {
   160  	span.Tracer.Flush()
   161  }