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

     1  package tracing
     2  
     3  import "sync"
     4  
     5  // EventType describes the type of event a component might experience during
     6  // a config run.
     7  type EventType string
     8  
     9  // Various event types.
    10  var (
    11  	EventProduce EventType = "PRODUCE"
    12  	EventConsume EventType = "CONSUME"
    13  	EventDelete  EventType = "DELETE"
    14  	EventError   EventType = "ERROR"
    15  )
    16  
    17  // NodeEvent represents a single event that occured within the stream.
    18  type NodeEvent struct {
    19  	Type    EventType
    20  	Content string
    21  }
    22  
    23  // Summary is a high level description of all traced events.
    24  type Summary struct {
    25  	Input           uint64
    26  	Output          uint64
    27  	ProcessorErrors uint64
    28  
    29  	inputEvents     sync.Map
    30  	processorEvents sync.Map
    31  	outputEvents    sync.Map
    32  }
    33  
    34  // NewSummary creates a new tracing summary that can be passed to component
    35  // constructors for adding traces.
    36  func NewSummary() *Summary {
    37  	return &Summary{}
    38  }
    39  
    40  // InputEvents returns a map of input labels to events traced during the
    41  // execution of a stream pipeline.
    42  func (s *Summary) InputEvents() map[string][]NodeEvent {
    43  	m := map[string][]NodeEvent{}
    44  	s.inputEvents.Range(func(key, value interface{}) bool {
    45  		m[key.(string)] = value.(*events).Extract()
    46  		return true
    47  	})
    48  	return m
    49  }
    50  
    51  // ProcessorEvents returns a map of processor labels to events traced during the
    52  // execution of a stream pipeline.
    53  func (s *Summary) ProcessorEvents() map[string][]NodeEvent {
    54  	m := map[string][]NodeEvent{}
    55  	s.processorEvents.Range(func(key, value interface{}) bool {
    56  		m[key.(string)] = value.(*events).Extract()
    57  		return true
    58  	})
    59  	return m
    60  }
    61  
    62  // OutputEvents returns a map of output labels to events traced during the
    63  // execution of a stream pipeline.
    64  func (s *Summary) OutputEvents() map[string][]NodeEvent {
    65  	m := map[string][]NodeEvent{}
    66  	s.outputEvents.Range(func(key, value interface{}) bool {
    67  		m[key.(string)] = value.(*events).Extract()
    68  		return true
    69  	})
    70  	return m
    71  }
    72  
    73  //------------------------------------------------------------------------------
    74  
    75  func (s *Summary) wInputEvents(label string) (e *events, counter *uint64) {
    76  	i, _ := s.inputEvents.LoadOrStore(label, &events{})
    77  	return i.(*events), &s.Input
    78  }
    79  
    80  func (s *Summary) wOutputEvents(label string) (e *events, counter *uint64) {
    81  	i, _ := s.outputEvents.LoadOrStore(label, &events{})
    82  	return i.(*events), &s.Output
    83  }
    84  
    85  func (s *Summary) wProcessorEvents(label string) (e *events, errCounter *uint64) {
    86  	i, _ := s.processorEvents.LoadOrStore(label, &events{})
    87  	return i.(*events), &s.ProcessorErrors
    88  }
    89  
    90  type events struct {
    91  	mut sync.Mutex
    92  	m   []NodeEvent
    93  }
    94  
    95  func (e *events) Add(t EventType, content string) {
    96  	e.mut.Lock()
    97  	defer e.mut.Unlock()
    98  
    99  	e.m = append(e.m, NodeEvent{
   100  		Type:    t,
   101  		Content: content,
   102  	})
   103  }
   104  
   105  func (e *events) Extract() []NodeEvent {
   106  	e.mut.Lock()
   107  	defer e.mut.Unlock()
   108  
   109  	eventsCopy := make([]NodeEvent, len(e.m))
   110  	for i, e := range e.m {
   111  		eventsCopy[i] = e
   112  	}
   113  
   114  	return eventsCopy
   115  }