github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/parser/time.go (about)

     1  package parser
     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  	"github.com/observiq/carbon/operator/helper"
    10  )
    11  
    12  func init() {
    13  	operator.Register("time_parser", func() operator.Builder { return NewTimeParserConfig("") })
    14  }
    15  
    16  func NewTimeParserConfig(operatorID string) *TimeParserConfig {
    17  	return &TimeParserConfig{
    18  		TransformerConfig: helper.NewTransformerConfig(operatorID, "time_parser"),
    19  		TimeParser:        helper.NewTimeParser(),
    20  	}
    21  }
    22  
    23  // TimeParserConfig is the configuration of a time parser operator.
    24  type TimeParserConfig struct {
    25  	helper.TransformerConfig `yaml:",inline"`
    26  	helper.TimeParser        `yaml:",omitempty,inline"`
    27  }
    28  
    29  // Build will build a time parser operator.
    30  func (c TimeParserConfig) Build(context operator.BuildContext) (operator.Operator, error) {
    31  	transformerOperator, err := c.TransformerConfig.Build(context)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	if err := c.TimeParser.Validate(context); err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	timeParser := &TimeParserOperator{
    41  		TransformerOperator: transformerOperator,
    42  		TimeParser:          c.TimeParser,
    43  	}
    44  
    45  	return timeParser, nil
    46  }
    47  
    48  // TimeParserOperator is an operator that parses time from a field to an entry.
    49  type TimeParserOperator struct {
    50  	helper.TransformerOperator
    51  	helper.TimeParser
    52  }
    53  
    54  // CanOutput will always return true for a parser operator.
    55  func (t *TimeParserOperator) CanOutput() bool {
    56  	return true
    57  }
    58  
    59  // Process will parse time from an entry.
    60  func (t *TimeParserOperator) Process(ctx context.Context, entry *entry.Entry) error {
    61  	if err := t.Parse(ctx, entry); err != nil {
    62  		return errors.Wrap(err, "parse timestamp")
    63  	}
    64  	t.Write(ctx, entry)
    65  	return nil
    66  }