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

     1  package transformer
     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("host_metadata", func() operator.Builder { return NewHostMetadataConfig("") })
    14  }
    15  
    16  // NewHostMetadataConfig returns a HostMetadataConfig with default values
    17  func NewHostMetadataConfig(operatorID string) *HostMetadataConfig {
    18  	return &HostMetadataConfig{
    19  		TransformerConfig:    helper.NewTransformerConfig(operatorID, "host_decorator"),
    20  		HostIdentifierConfig: helper.NewHostIdentifierConfig(),
    21  	}
    22  }
    23  
    24  // HostMetadataConfig is the configuration of a host metadata operator
    25  type HostMetadataConfig struct {
    26  	helper.TransformerConfig    `yaml:",inline"`
    27  	helper.HostIdentifierConfig `yaml:",inline"`
    28  }
    29  
    30  // Build will build an operator from the supplied configuration
    31  func (c HostMetadataConfig) Build(context operator.BuildContext) (operator.Operator, error) {
    32  	transformerOperator, err := c.TransformerConfig.Build(context)
    33  	if err != nil {
    34  		return nil, errors.Wrap(err, "failed to build transformer")
    35  	}
    36  
    37  	hostIdentifier, err := c.HostIdentifierConfig.Build()
    38  	if err != nil {
    39  		return nil, errors.Wrap(err, "failed to build host labeler")
    40  	}
    41  
    42  	operator := &HostMetadata{
    43  		TransformerOperator: transformerOperator,
    44  		HostIdentifier:      hostIdentifier,
    45  	}
    46  
    47  	return operator, nil
    48  }
    49  
    50  // HostMetadata is an operator that can add host metadata to incoming entries
    51  type HostMetadata struct {
    52  	helper.TransformerOperator
    53  	helper.HostIdentifier
    54  }
    55  
    56  // Process will process an incoming entry using the metadata transform.
    57  func (h *HostMetadata) Process(ctx context.Context, entry *entry.Entry) error {
    58  	return h.ProcessWith(ctx, entry, h.Transform)
    59  }
    60  
    61  // Transform will transform an entry, adding the configured host metadata.
    62  func (h *HostMetadata) Transform(entry *entry.Entry) (*entry.Entry, error) {
    63  	h.HostIdentifier.Identify(entry)
    64  	return entry, nil
    65  }