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

117 json encode #120

Merged
merged 3 commits into from
Jul 11, 2022
Merged
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
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [flatten](plugin/action/flatten/README.md)
- [join](plugin/action/join/README.md)
- [json_decode](plugin/action/json_decode/README.md)
- [json_encode](plugin/action/json_encode/README.md)
- [keep_fields](plugin/action/keep_fields/README.md)
- [mask](plugin/action/mask/README.md)
- [modify](plugin/action/modify/README.md)
Expand Down
1 change: 1 addition & 0 deletions cmd/file.d.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "github.com/ozontech/file.d/plugin/action/flatten"
_ "github.com/ozontech/file.d/plugin/action/join"
_ "github.com/ozontech/file.d/plugin/action/json_decode"
_ "github.com/ozontech/file.d/plugin/action/json_encode"
_ "github.com/ozontech/file.d/plugin/action/keep_fields"
_ "github.com/ozontech/file.d/plugin/action/mask"
_ "github.com/ozontech/file.d/plugin/action/modify"
Expand Down
17 changes: 17 additions & 0 deletions plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ It decodes a JSON string from the event field and merges the result with the eve
If the decoded JSON isn't an object, the event will be skipped.

[More details...](plugin/action/json_decode/README.md)
## json_encode
It replaces field with its JSON string representation.

**Example:**
```yaml
pipelines:
example_pipeline:
...
actions:
- type: json_encode
field: server
...
```
It transforms `{"server":{"os":"linux","arch":"amd64"}}` into `{"server":"{\"os\":\"linux\",\"arch\":\"amd64\"}"}`.


[More details...](plugin/action/json_encode/README.md)
## keep_fields
It keeps the list of the event fields and removes others.

Expand Down
17 changes: 17 additions & 0 deletions plugin/action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ It decodes a JSON string from the event field and merges the result with the eve
If the decoded JSON isn't an object, the event will be skipped.

[More details...](plugin/action/json_decode/README.md)
## json_encode
It replaces field with its JSON string representation.

**Example:**
```yaml
pipelines:
example_pipeline:
...
actions:
- type: json_encode
field: server
...
```
It transforms `{"server":{"os":"linux","arch":"amd64"}}` into `{"server":"{\"os\":\"linux\",\"arch\":\"amd64\"}"}`.


[More details...](plugin/action/json_encode/README.md)
## keep_fields
It keeps the list of the event fields and removes others.

Expand Down
5 changes: 5 additions & 0 deletions plugin/action/json_encode/README.idoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# JSON encode plugin
@introduction

### Config params
@config-params|description
25 changes: 25 additions & 0 deletions plugin/action/json_encode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# JSON encode plugin
It replaces field with its JSON string representation.

**Example:**
```yaml
pipelines:
example_pipeline:
...
actions:
- type: json_encode
field: server
...
```
It transforms `{"server":{"os":"linux","arch":"amd64"}}` into `{"server":"{\"os\":\"linux\",\"arch\":\"amd64\"}"}`.


### Config params
**`field`** *`cfg.FieldSelector`* *`required`*

The event field to encode. Must be a string.

<br>


<br>*Generated using [__insane-doc__](https://github.com/vitkovskii/insane-doc)*
68 changes: 68 additions & 0 deletions plugin/action/json_encode/json_encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package json_encode

import (
"github.com/ozontech/file.d/cfg"
"github.com/ozontech/file.d/fd"
"github.com/ozontech/file.d/pipeline"
)

/*{ introduction
It replaces field with its JSON string representation.

**Example:**
```yaml
pipelines:
example_pipeline:
...
actions:
- type: json_encode
field: server
...
```
It transforms `{"server":{"os":"linux","arch":"amd64"}}` into `{"server":"{\"os\":\"linux\",\"arch\":\"amd64\"}"}`.

}*/
type Plugin struct {
config *Config
}

//! config-params
//^ config-params
type Config struct {
//> @3@4@5@6
//>
//> The event field to encode. Must be a string.
Field cfg.FieldSelector `json:"field" parse:"selector" required:"true"` //*
Field_ []string
}

func init() {
fd.DefaultPluginRegistry.RegisterAction(&pipeline.PluginStaticInfo{
Type: "json_encode",
Factory: factory,
})
}

func factory() (pipeline.AnyPlugin, pipeline.AnyConfig) {
return &Plugin{}, &Config{}
}

func (p *Plugin) Start(config pipeline.AnyConfig, _ *pipeline.ActionPluginParams) {
p.config = config.(*Config)
}

func (p *Plugin) Stop() {
}

func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
node := event.Root.Dig(p.config.Field_...)
if node == nil {
return pipeline.ActionPass
}

vitkovskii marked this conversation as resolved.
Show resolved Hide resolved
s := len(event.Buf)
event.Buf = node.Encode(event.Buf)

node.MutateToString(pipeline.ByteToStringUnsafe(event.Buf[s:]))
return pipeline.ActionPass
}
44 changes: 44 additions & 0 deletions plugin/action/json_encode/json_encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package json_encode

import (
"sync"
"testing"

"github.com/ozontech/file.d/cfg"
"github.com/ozontech/file.d/logger"
"github.com/ozontech/file.d/pipeline"
"github.com/ozontech/file.d/test"
"github.com/stretchr/testify/assert"
)

func TestEncode(t *testing.T) {
config := &Config{Field: "server"}
p, input, output := test.NewPipelineMock(test.NewActionPluginStaticInfo(factory, config, pipeline.MatchModeAnd, nil, false))
wg := &sync.WaitGroup{}
wg.Add(1)

err := cfg.Parse(config, nil)
if err != nil {
logger.Panicf("wrong config")
}

inEvents := 0
input.SetInFn(func() {
inEvents++
})

outEvents := make([]*pipeline.Event, 0)
output.SetOutFn(func(e *pipeline.Event) {
outEvents = append(outEvents, e)
wg.Done()
})

input.In(0, "test.log", 0, []byte(`{"server":{"os":"linux","arch":"amd64"}}`))

wg.Wait()
p.Stop()

assert.Equal(t, 1, inEvents, "wrong in events count")
assert.Equal(t, 1, len(outEvents), "wrong out events count")
assert.Equal(t, `{"server":"{\"os\":\"linux\",\"arch\":\"amd64\"}"}`, outEvents[0].Root.EncodeToString(), "wrong out event")
}