Skip to content

Commit

Permalink
[pkg/stanza] Add 'strip_ansi_escape_codes' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Jan 23, 2025
1 parent 0e179f0 commit b32fac5
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 99 deletions.
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: []

# (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: []
2 changes: 1 addition & 1 deletion pkg/stanza/adapter/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +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/stripansicolors"
_ "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>

This file was deleted.

43 changes: 0 additions & 43 deletions pkg/stanza/operator/transformer/stripansicolors/transformer.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package stripansicolors // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/stripansicolors"
package stripansiescapecodes // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/stripansiescapecodes"

import (
"go.opentelemetry.io/collector/component"
Expand All @@ -11,31 +11,31 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const operatorType = "strip_ansi_colors"
const operatorType = "strip_ansi_escape_codes"

func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}

// NewConfig creates a new unquote config with default values
// NewConfig creates a new strip_ansi_escape_codes config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewConfigWithID creates a new unquote config with default values
// 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 unquote operator.
// 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_colors operator.
// 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package stripansicolors
package stripansiescapecodes

import (
"path/filepath"
Expand All @@ -17,47 +17,47 @@ func TestUnmarshal(t *testing.T) {
TestsFile: filepath.Join(".", "testdata", "config.yaml"),
Tests: []operatortest.ConfigUnmarshalTest{
{
Name: "unquote_body",
Name: "strip_ansi_escape_codes_body",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField("nested")
return cfg
}(),
},
{
Name: "unquote_single_attribute",
Name: "strip_ansi_escape_codes_single_attribute",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewAttributeField("key")
return cfg
}(),
},
{
Name: "unquote_single_resource",
Name: "strip_ansi_escape_codes_single_resource",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewResourceField("key")
return cfg
}(),
},
{
Name: "unquote_nested_body",
Name: "strip_ansi_escape_codes_nested_body",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField("one", "two")
return cfg
}(),
},
{
Name: "unquote_nested_attribute",
Name: "strip_ansi_escape_codes_nested_attribute",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewAttributeField("one", "two")
return cfg
}(),
},
{
Name: "unquote_nested_resource",
Name: "strip_ansi_escape_codes_nested_resource",
Expect: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewResourceField("one", "two")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package stripansicolors
package stripansiescapecodes

import (
"testing"
Expand Down
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

0 comments on commit b32fac5

Please sign in to comment.