github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/helper/input.go (about)

     1  package helper
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/observiq/carbon/entry"
     7  	"github.com/observiq/carbon/errors"
     8  	"github.com/observiq/carbon/operator"
     9  	"go.uber.org/zap"
    10  )
    11  
    12  // NewInputConfig creates a new input config with default values.
    13  func NewInputConfig(operatorID, operatorType string) InputConfig {
    14  	return InputConfig{
    15  		LabelerConfig:    NewLabelerConfig(),
    16  		IdentifierConfig: NewIdentifierConfig(),
    17  		WriterConfig:     NewWriterConfig(operatorID, operatorType),
    18  		WriteTo:          entry.NewRecordField(),
    19  	}
    20  }
    21  
    22  // InputConfig provides a basic implementation of an input operator config.
    23  type InputConfig struct {
    24  	LabelerConfig    `yaml:",inline"`
    25  	IdentifierConfig `yaml:",inline"`
    26  	WriterConfig     `yaml:",inline"`
    27  	WriteTo          entry.Field `json:"write_to" yaml:"write_to"`
    28  }
    29  
    30  // Build will build a base producer.
    31  func (c InputConfig) Build(context operator.BuildContext) (InputOperator, error) {
    32  	writerOperator, err := c.WriterConfig.Build(context)
    33  	if err != nil {
    34  		return InputOperator{}, errors.WithDetails(err, "operator_id", c.ID())
    35  	}
    36  
    37  	labeler, err := c.LabelerConfig.Build()
    38  	if err != nil {
    39  		return InputOperator{}, errors.WithDetails(err, "operator_id", c.ID())
    40  	}
    41  
    42  	identifier, err := c.IdentifierConfig.Build()
    43  	if err != nil {
    44  		return InputOperator{}, errors.WithDetails(err, "operator_id", c.ID())
    45  	}
    46  
    47  	inputOperator := InputOperator{
    48  		Labeler:        labeler,
    49  		Identifier:     identifier,
    50  		WriterOperator: writerOperator,
    51  		WriteTo:        c.WriteTo,
    52  	}
    53  
    54  	return inputOperator, nil
    55  }
    56  
    57  // InputOperator provides a basic implementation of an input operator.
    58  type InputOperator struct {
    59  	Labeler
    60  	Identifier
    61  	WriterOperator
    62  	WriteTo entry.Field
    63  }
    64  
    65  // NewEntry will create a new entry using the `write_to`, `labels`, and `resource` configuration.
    66  func (i *InputOperator) NewEntry(value interface{}) (*entry.Entry, error) {
    67  	entry := entry.New()
    68  	entry.Set(i.WriteTo, value)
    69  
    70  	if err := i.Label(entry); err != nil {
    71  		return nil, errors.Wrap(err, "failed to add labels to entry")
    72  	}
    73  
    74  	if err := i.Identify(entry); err != nil {
    75  		return nil, errors.Wrap(err, "failed to add resource keys to entry")
    76  	}
    77  
    78  	return entry, nil
    79  }
    80  
    81  // CanProcess will always return false for an input operator.
    82  func (i *InputOperator) CanProcess() bool {
    83  	return false
    84  }
    85  
    86  // Process will always return an error if called.
    87  func (i *InputOperator) Process(ctx context.Context, entry *entry.Entry) error {
    88  	i.Errorw("Operator received an entry, but can not process", zap.Any("entry", entry))
    89  	return errors.NewError(
    90  		"Operator can not process logs.",
    91  		"Ensure that operator is not configured to receive logs from other operators",
    92  	)
    93  }