Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] Add 'strip_ansi_escape_codes' operator #37443

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/strip-ansi-escape-codes.yaml
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: []
1 change: 1 addition & 0 deletions pkg/stanza/adapter/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ import (
_ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/remove"
_ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/retain"
_ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/router"
_ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/stripansiescapecodes"
_ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/unquote"
)
1 change: 1 addition & 0 deletions pkg/stanza/docs/operators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ General purpose:
- [remove](./remove.md)
- [retain](./retain.md)
- [router](./router.md)
- [strip_ansi_escape_codes](./strip_ansi_escape_codes.md)
- [unquote](./unquote.md)
- [assign_keys](./assign_keys.md)
53 changes: 53 additions & 0 deletions pkg/stanza/docs/operators/strip_ansi_escape_codes.md
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 pkg/stanza/operator/transformer/stripansiescapecodes/config.go
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
}
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)
}
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)
}
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
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)
}
}
Loading
Loading