|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package regexreplace // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/regexreplace" |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/component" |
| 11 | + |
| 12 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" |
| 15 | +) |
| 16 | + |
| 17 | +const operatorType = "regex_replace" |
| 18 | + |
| 19 | +// derived from https://en.wikipedia.org/wiki/ANSI_escape_code#CSIsection |
| 20 | +var ansiCsiEscapeRegex = regexp.MustCompile(`\x1B\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]`) |
| 21 | + |
| 22 | +func init() { |
| 23 | + operator.Register(operatorType, func() operator.Builder { return NewConfig() }) |
| 24 | +} |
| 25 | + |
| 26 | +// NewConfig creates a new ansi_control_sequences config with default values |
| 27 | +func NewConfig() *Config { |
| 28 | + return NewConfigWithID(operatorType) |
| 29 | +} |
| 30 | + |
| 31 | +// NewConfigWithID creates a new ansi_control_sequences config with default values |
| 32 | +func NewConfigWithID(operatorID string) *Config { |
| 33 | + return &Config{ |
| 34 | + TransformerConfig: helper.NewTransformerConfig(operatorID, operatorType), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Config is the configuration of an ansi_control_sequences operator. |
| 39 | +type Config struct { |
| 40 | + helper.TransformerConfig `mapstructure:",squash"` |
| 41 | + RegexName string `mapstructure:"regex_name"` |
| 42 | + Regex string `mapstructure:"regex"` |
| 43 | + ReplaceWith string `mapstructure:"replace_with"` |
| 44 | + Field entry.Field `mapstructure:"field"` |
| 45 | +} |
| 46 | + |
| 47 | +func (c *Config) getRegexp() (*regexp.Regexp, error) { |
| 48 | + if (c.RegexName == "") == (c.Regex == "") { |
| 49 | + return nil, fmt.Errorf("either regex or regex_name must be set") |
| 50 | + } |
| 51 | + |
| 52 | + switch c.RegexName { |
| 53 | + case "ansi_control_sequences": |
| 54 | + return ansiCsiEscapeRegex, nil |
| 55 | + case "": |
| 56 | + return regexp.Compile(c.Regex) |
| 57 | + default: |
| 58 | + return nil, fmt.Errorf("regex_name %s is unknown", c.RegexName) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Build will build an ansi_control_sequences operator. |
| 63 | +func (c Config) Build(set component.TelemetrySettings) (operator.Operator, error) { |
| 64 | + transformerOperator, err := c.TransformerConfig.Build(set) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + regexp, err := c.getRegexp() |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + return &Transformer{ |
| 75 | + TransformerOperator: transformerOperator, |
| 76 | + field: c.Field, |
| 77 | + regexp: regexp, |
| 78 | + replaceWith: c.ReplaceWith, |
| 79 | + }, nil |
| 80 | +} |
0 commit comments