github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/filter.go (about)

     1  package processor
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/Jeffail/benthos/v3/internal/docs"
     8  	"github.com/Jeffail/benthos/v3/internal/tracing"
     9  	"github.com/Jeffail/benthos/v3/lib/condition"
    10  	"github.com/Jeffail/benthos/v3/lib/log"
    11  	"github.com/Jeffail/benthos/v3/lib/metrics"
    12  	"github.com/Jeffail/benthos/v3/lib/response"
    13  	"github.com/Jeffail/benthos/v3/lib/types"
    14  )
    15  
    16  //------------------------------------------------------------------------------
    17  
    18  func init() {
    19  	Constructors[TypeFilter] = TypeSpec{
    20  		constructor: NewFilter,
    21  		Status:      docs.StatusDeprecated,
    22  		Footnotes: `
    23  ## Alternatives
    24  
    25  All functionality of this processor has been superseded by the
    26  [bloblang](/docs/components/processors/bloblang) processor.`,
    27  		config: docs.FieldComponent().HasType(docs.FieldTypeCondition),
    28  	}
    29  }
    30  
    31  //------------------------------------------------------------------------------
    32  
    33  // FilterConfig contains configuration fields for the Filter processor.
    34  type FilterConfig struct {
    35  	condition.Config `json:",inline" yaml:",inline"`
    36  }
    37  
    38  // NewFilterConfig returns a FilterConfig with default values.
    39  func NewFilterConfig() FilterConfig {
    40  	return FilterConfig{
    41  		Config: condition.NewConfig(),
    42  	}
    43  }
    44  
    45  //------------------------------------------------------------------------------
    46  
    47  // MarshalYAML prints the child condition instead of {}.
    48  func (f FilterConfig) MarshalYAML() (interface{}, error) {
    49  	return f.Config, nil
    50  }
    51  
    52  //------------------------------------------------------------------------------
    53  
    54  // Filter is a processor that checks each message against a condition and
    55  // rejects the message if a condition returns false.
    56  type Filter struct {
    57  	log   log.Modular
    58  	stats metrics.Type
    59  
    60  	condition condition.Type
    61  
    62  	mCount     metrics.StatCounter
    63  	mDropped   metrics.StatCounter
    64  	mSent      metrics.StatCounter
    65  	mBatchSent metrics.StatCounter
    66  }
    67  
    68  // NewFilter returns a Filter processor.
    69  func NewFilter(
    70  	conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
    71  ) (Type, error) {
    72  	cond, err := condition.New(conf.Filter.Config, mgr, log, stats)
    73  	if err != nil {
    74  		return nil, fmt.Errorf(
    75  			"failed to construct condition '%v': %v",
    76  			conf.Filter.Config.Type, err,
    77  		)
    78  	}
    79  	return &Filter{
    80  		log:       log,
    81  		stats:     stats,
    82  		condition: cond,
    83  
    84  		mCount:     stats.GetCounter("count"),
    85  		mDropped:   stats.GetCounter("dropped"),
    86  		mSent:      stats.GetCounter("sent"),
    87  		mBatchSent: stats.GetCounter("batch.sent"),
    88  	}, nil
    89  }
    90  
    91  //------------------------------------------------------------------------------
    92  
    93  // ProcessMessage applies the processor to a message, either creating >0
    94  // resulting messages or a response to be sent back to the message source.
    95  func (c *Filter) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
    96  	c.mCount.Incr(1)
    97  
    98  	spans := tracing.CreateChildSpans(TypeFilter, msg)
    99  
   100  	filterRes := c.condition.Check(msg)
   101  	for _, s := range spans {
   102  		if !filterRes {
   103  			s.LogKV(
   104  				"event", "dropped",
   105  				"type", "filtered",
   106  			)
   107  		}
   108  		s.SetTag("result", filterRes)
   109  		s.Finish()
   110  	}
   111  	if !filterRes {
   112  		c.mDropped.Incr(int64(msg.Len()))
   113  		return nil, response.NewAck()
   114  	}
   115  
   116  	c.mBatchSent.Incr(1)
   117  	c.mSent.Incr(int64(msg.Len()))
   118  	msgs := [1]types.Message{msg}
   119  	return msgs[:], nil
   120  }
   121  
   122  // CloseAsync shuts down the processor and stops processing requests.
   123  func (c *Filter) CloseAsync() {
   124  }
   125  
   126  // WaitForClose blocks until the processor has closed down.
   127  func (c *Filter) WaitForClose(timeout time.Duration) error {
   128  	return nil
   129  }
   130  
   131  //------------------------------------------------------------------------------