Skip to content

Commit

Permalink
Refactor. Improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
george pogosyan committed Jul 3, 2024
1 parent 234f8d3 commit c083993
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
38 changes: 37 additions & 1 deletion plugin/action/remove_fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,43 @@ Nested fields supported: list subfield names separated with dot.
Example:
```
fields: ["a.b.c"]
{"a":{"b":{"c": 100}}} -> {"a":{"b":{}}}
# event before processing
{
"a": {
"b": {
"c": 100,
"d": "some"
}
}
}
# event after processing
{
"a": {
"b": {
"d": "some" # "c" removed
}
}
}
```

If field name contains dots use backslash for escaping.
Example:
```
fields:
- exception\.type
# event before processing
{
"message": "Exception occurred",
"exception.type": "SomeType"
}
# event after processing
{
"message": "Exception occurred" # "exception.type" removed
}
```

<br>
Expand Down
56 changes: 40 additions & 16 deletions plugin/action/remove_fields/remove_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,43 @@ type Config struct {
// > Example:
// > ```
// > fields: ["a.b.c"]
// > {"a":{"b":{"c": 100}}} -> {"a":{"b":{}}}
// >
// > # event before processing
// > {
// > "a": {
// > "b": {
// > "c": 100,
// > "d": "some"
// > }
// > }
// > }
// >
// > # event after processing
// > {
// > "a": {
// > "b": {
// > "d": "some" # "c" removed
// > }
// > }
// > }
// > ```
// >
// > If field name contains dots use backslash for escaping.
// > Example:
// > ```
// > fields:
// > - exception\.type
// >
// > # event before processing
// > {
// > "message": "Exception occurred",
// > "exception.type": "SomeType"
// > }
// >
// > # event after processing
// > {
// > "message": "Exception occurred" # "exception.type" removed
// > }
// > ```
Fields []string `json:"fields"` // *
}
Expand Down Expand Up @@ -60,23 +96,11 @@ func (p *Plugin) Start(config pipeline.AnyConfig, _ *pipeline.ActionPluginParams

for i, f1 := range fields {
if f1 == "" {
logger.Fatalf("empty field; pos = %d", i)
}

fieldPath := cfg.ParseFieldSelector(f1)
if len(fieldPath) == 0 {
logger.Fatalf("empty field selector parsed; field pos = %d", i)
logger.Fatal("empty field found")
}

ok := true
for j := 0; j < i; j++ {
f2 := fields[j]

if f1 == f2 {
logger.Warnf("duplicate path '%s' found; remove extra occurrences", f1)
continue
}

for _, f2 := range fields[:i] {
if strings.HasPrefix(f1, f2) {
logger.Warnf("path '%s' included in path '%s'; remove nested path", f1, f2)
ok = false
Expand All @@ -85,7 +109,7 @@ func (p *Plugin) Start(config pipeline.AnyConfig, _ *pipeline.ActionPluginParams
}

if ok {
p.fieldPaths = append(p.fieldPaths, fieldPath)
p.fieldPaths = append(p.fieldPaths, cfg.ParseFieldSelector(f1))
}
}
}
Expand Down

0 comments on commit c083993

Please sign in to comment.