-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/stanza] Add 'strip_ansi_escape_codes' operator
- Loading branch information
Showing
10 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/stanza | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add 'strip_ansi_escape_codes' operator | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [37443] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## `strip_ansi_escape_codes` operator | ||
|
||
The `strip_ansi_escape_codes` operator removes ANSI escape sequences (such as color codes) from a string. | ||
|
||
### Configuration Fields | ||
|
||
| Field | Default | Description | | ||
| --- | --- | --- | | ||
| `id` | `strip_ansi_escape_codes` | A unique identifier for the operator. | | ||
| `output` | Next in pipeline | The connected operator(s) that will receive all outbound entries. | | ||
| `field` | required | The [field](../types/field.md) to strip. Must be a string. | | ||
| `on_error` | `send` | The behavior of the operator if it encounters an error. See [on_error](../types/on_error.md). | | ||
| `if` | | An [expression](../types/expression.md) that, when set, will be evaluated to determine whether this operator should be used for the given entry. This allows you to do easy conditional parsing without branching logic with routers. | | ||
|
||
Right now the operator only removes the ANSI "Control Sequence Introducer (CSI)" escape codes starting with `ESC [`. | ||
|
||
### Example Configurations: | ||
|
||
<hr> | ||
|
||
Remove all color escape codes from the body | ||
```yaml | ||
- type: strip_ansi_escape_codes | ||
field: body | ||
``` | ||
<table> | ||
<tr><td> Input Entry </td> <td> Output Entry </td></tr> | ||
<tr> | ||
<td> | ||
```json | ||
{ | ||
"resource": { }, | ||
"attributes": { }, | ||
"body": "\x1b[31mred\x1b[0m" | ||
} | ||
``` | ||
|
||
</td> | ||
<td> | ||
|
||
```json | ||
{ | ||
"resource": { }, | ||
"attributes": { }, | ||
"body": "red" | ||
} | ||
``` | ||
|
||
</td> | ||
</tr> | ||
</table> |
49 changes: 49 additions & 0 deletions
49
pkg/stanza/operator/transformer/stripansiescapecodes/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stripansiescapecodes // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/stripansiescapecodes" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/component" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
const operatorType = "strip_ansi_escape_codes" | ||
|
||
func init() { | ||
operator.Register(operatorType, func() operator.Builder { return NewConfig() }) | ||
} | ||
|
||
// NewConfig creates a new strip_ansi_escape_codes config with default values | ||
func NewConfig() *Config { | ||
return NewConfigWithID(operatorType) | ||
} | ||
|
||
// NewConfigWithID creates a new strip_ansi_escape_codes config with default values | ||
func NewConfigWithID(operatorID string) *Config { | ||
return &Config{ | ||
TransformerConfig: helper.NewTransformerConfig(operatorID, operatorType), | ||
} | ||
} | ||
|
||
// Config is the configuration of an strip_ansi_escape_codes operator. | ||
type Config struct { | ||
helper.TransformerConfig `mapstructure:",squash"` | ||
Field entry.Field `mapstructure:"field"` | ||
} | ||
|
||
// Build will build an strip_ansi_escape_codes operator. | ||
func (c Config) Build(set component.TelemetrySettings) (operator.Operator, error) { | ||
transformerOperator, err := c.TransformerConfig.Build(set) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Transformer{ | ||
TransformerOperator: transformerOperator, | ||
field: c.Field, | ||
}, nil | ||
} |
69 changes: 69 additions & 0 deletions
69
pkg/stanza/operator/transformer/stripansiescapecodes/config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package stripansiescapecodes | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest" | ||
) | ||
|
||
// test unmarshalling of values into config struct | ||
func TestUnmarshal(t *testing.T) { | ||
operatortest.ConfigUnmarshalTests{ | ||
DefaultConfig: NewConfig(), | ||
TestsFile: filepath.Join(".", "testdata", "config.yaml"), | ||
Tests: []operatortest.ConfigUnmarshalTest{ | ||
{ | ||
Name: "strip_ansi_escape_codes_body", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewBodyField("nested") | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
Name: "strip_ansi_escape_codes_single_attribute", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewAttributeField("key") | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
Name: "strip_ansi_escape_codes_single_resource", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewResourceField("key") | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
Name: "strip_ansi_escape_codes_nested_body", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewBodyField("one", "two") | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
Name: "strip_ansi_escape_codes_nested_attribute", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewAttributeField("one", "two") | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
Name: "strip_ansi_escape_codes_nested_resource", | ||
Expect: func() *Config { | ||
cfg := NewConfig() | ||
cfg.Field = entry.NewResourceField("one", "two") | ||
return cfg | ||
}(), | ||
}, | ||
}, | ||
}.Run(t) | ||
} |
14 changes: 14 additions & 0 deletions
14
pkg/stanza/operator/transformer/stripansiescapecodes/package_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stripansiescapecodes | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} |
18 changes: 18 additions & 0 deletions
18
pkg/stanza/operator/transformer/stripansiescapecodes/testdata/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
strip_ansi_escape_codes_body: | ||
type: strip_ansi_escape_codes | ||
field: body.nested | ||
strip_ansi_escape_codes_nested_attribute: | ||
type: strip_ansi_escape_codes | ||
field: attributes.one.two | ||
strip_ansi_escape_codes_nested_body: | ||
type: strip_ansi_escape_codes | ||
field: body.one.two | ||
strip_ansi_escape_codes_nested_resource: | ||
type: strip_ansi_escape_codes | ||
field: resource.one.two | ||
strip_ansi_escape_codes_single_attribute: | ||
type: strip_ansi_escape_codes | ||
field: attributes.key | ||
strip_ansi_escape_codes_single_resource: | ||
type: strip_ansi_escape_codes | ||
field: resource.key |
42 changes: 42 additions & 0 deletions
42
pkg/stanza/operator/transformer/stripansiescapecodes/transformer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stripansiescapecodes // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/stripansiescapecodes" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
var ansiCsiEscapeRegex = regexp.MustCompile(`\x1B\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]`) | ||
|
||
// Transformer is an operator that will remove ANSI Escape Codes from a string. | ||
type Transformer struct { | ||
helper.TransformerOperator | ||
field entry.Field | ||
} | ||
|
||
// Process will remove ANSI Escape Codes from a string | ||
func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error { | ||
return t.ProcessWith(ctx, entry, t.strip) | ||
} | ||
|
||
// strip will remove ANSI Escape Codes from a string | ||
func (t *Transformer) strip(e *entry.Entry) error { | ||
value, ok := t.field.Get(e) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
switch v := value.(type) { | ||
case string: | ||
s := ansiCsiEscapeRegex.ReplaceAllString(v, "") | ||
return t.field.Set(e, s) | ||
default: | ||
return fmt.Errorf("type %T cannot be stripped", value) | ||
} | ||
} |
Oops, something went wrong.