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

     1  package tracing
     2  
     3  import (
     4  	"sync/atomic"
     5  	"time"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/processor"
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  )
    10  
    11  type tracedProcessor struct {
    12  	e       *events
    13  	errCtr  *uint64
    14  	wrapped types.Processor
    15  }
    16  
    17  func traceProcessor(e *events, errCtr *uint64, p types.Processor) types.Processor {
    18  	t := &tracedProcessor{
    19  		e:       e,
    20  		errCtr:  errCtr,
    21  		wrapped: p,
    22  	}
    23  	return t
    24  }
    25  
    26  func (t *tracedProcessor) ProcessMessage(m types.Message) ([]types.Message, types.Response) {
    27  	prevErrs := make([]string, m.Len())
    28  	_ = m.Iter(func(i int, part types.Part) error {
    29  		t.e.Add(EventConsume, string(part.Get()))
    30  		prevErrs[i] = processor.GetFail(part)
    31  		return nil
    32  	})
    33  
    34  	outMsgs, res := t.wrapped.ProcessMessage(m)
    35  	for _, outMsg := range outMsgs {
    36  		_ = outMsg.Iter(func(i int, part types.Part) error {
    37  			t.e.Add(EventProduce, string(part.Get()))
    38  			failStr := processor.GetFail(part)
    39  			if failStr == "" {
    40  				return nil
    41  			}
    42  			if len(prevErrs) <= i || prevErrs[i] == failStr {
    43  				return nil
    44  			}
    45  			_ = atomic.AddUint64(t.errCtr, 1)
    46  			t.e.Add(EventError, failStr)
    47  			return nil
    48  		})
    49  	}
    50  	if len(outMsgs) == 0 {
    51  		t.e.Add(EventDelete, "")
    52  	}
    53  
    54  	return outMsgs, res
    55  }
    56  
    57  func (t *tracedProcessor) CloseAsync() {
    58  	t.wrapped.CloseAsync()
    59  }
    60  
    61  func (t *tracedProcessor) WaitForClose(timeout time.Duration) error {
    62  	return t.wrapped.WaitForClose(timeout)
    63  }