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

[jsonlogencodingextension] Add Modes for Body with Attributes #32722

Merged
merged 18 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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/pretty-json-encoding.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: json_log_encoding

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Adds a new encoding option for JSON log encoding exension to grab attributes and resources from a log and output that in JSON format."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32679]

# (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: [user]
36 changes: 35 additions & 1 deletion extension/encoding/jsonlogencodingextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,38 @@
[development]: https://github.com/open-telemetry/opentelemetry-collector#development
<!-- end autogenerated section -->

The `jsonlog` encoding extension is used to marshal/unmarshal JSON log body, ignoring other log fields.
## Configuration

| Name | Description | Default |
| ------------------------ | -------------------------------------------------- | -------------------------------------------- |
| mode | What mode of the JSON Encoding Extension you want | body |
blakeromano marked this conversation as resolved.
Show resolved Hide resolved



### Mode

#### body Mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to make the headings consistent

Suggested change
#### body Mode
#### body


The `body` mode of the json encoding extension is used to marshal/unmarshal JSON log body, ignoring other log fields.
blakeromano marked this conversation as resolved.
Show resolved Hide resolved


#### body_with_inline_attributes

The `body_with_inline_attributes` mode within the json encoding extension will grab resource and attributes and add them as key value pairs to the json body iterating through all the logs and create a json array looking along the lines of:
blakeromano marked this conversation as resolved.
Show resolved Hide resolved

```json
[
{
"body": "{\"log\": \"test\"}",
blakeromano marked this conversation as resolved.
Show resolved Hide resolved
"resource": {
"test": "logs-test"
}
},
{
"body": "log testing",
"resource": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be resourceAttributes I'm guessing?

Suggested change
"resource": {
"resourceAttributes": {

"test": "logs-test"
}
}
]
```
20 changes: 19 additions & 1 deletion extension/encoding/jsonlogencodingextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@

package jsonlogencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/jsonlogencodingextension"

type Config struct{}
import "fmt"

type JSONEncodingMode string

const (
JSONEncodingModeBodyWithInlineAttributes JSONEncodingMode = "body_with_inline_attributes"
JSONEncodingModeBody JSONEncodingMode = "body"
)

type Config struct {
// Export raw log string instead of log wrapper
Mode JSONEncodingMode `mapstructure:"mode,omitempty"`
}

func (c *Config) Validate() error {
switch c.Mode {
case JSONEncodingModeBodyWithInlineAttributes:
case JSONEncodingModeBody:
default:
return fmt.Errorf("invalid mode %q", c.Mode)
}
return nil
}
50 changes: 49 additions & 1 deletion extension/encoding/jsonlogencodingextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ var (
)

type jsonLogExtension struct {
config component.Config
}

func (e *jsonLogExtension) MarshalLogs(ld plog.Logs) ([]byte, error) {
if e.config.(*Config).Mode == JSONEncodingModeBodyWithInlineAttributes {
return e.LogProcessor(ld)
}
logRecord := ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body()
var raw map[string]any
switch logRecord.Type() {
Expand All @@ -38,7 +42,6 @@ func (e *jsonLogExtension) MarshalLogs(ld plog.Logs) ([]byte, error) {
return nil, err
}
return buf, nil

}

func (e *jsonLogExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) {
Expand Down Expand Up @@ -68,3 +71,48 @@ func (e *jsonLogExtension) Start(_ context.Context, _ component.Host) error {
func (e *jsonLogExtension) Shutdown(_ context.Context) error {
return nil
}

func (e *jsonLogExtension) LogProcessor(ld plog.Logs) ([]byte, error) {
blakeromano marked this conversation as resolved.
Show resolved Hide resolved
prettyLogs := []prettyLogBody{}
blakeromano marked this conversation as resolved.
Show resolved Hide resolved

rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
rl := rls.At(i)
resourceAttrs := attrsValue(rl.Resource().Attributes())

sls := rl.ScopeLogs()
for j := 0; j < sls.Len(); j++ {
sl := sls.At(j)
logs := sl.LogRecords()
for k := 0; k < logs.Len(); k++ {
log := logs.At(k)
logEvent := prettyLogBody{
Body: log.Body().AsRaw(),
Resource: resourceAttrs,
Attributes: attrsValue(log.Attributes()),
}
prettyLogs = append(prettyLogs, logEvent)
}
}
}

return jsoniter.Marshal(prettyLogs)
}

type prettyLogBody struct {
blakeromano marked this conversation as resolved.
Show resolved Hide resolved
Body any `json:"body,omitempty"`
Attributes map[string]any `json:"attributes,omitempty"`
Resource map[string]any `json:"resource,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log_attributes and resource_attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of trying to stay with camelCase as most json is but I wouldn't be opposed to logAttributes and resourceAttributes

}

func attrsValue(attrs pcommon.Map) map[string]any {
blakeromano marked this conversation as resolved.
Show resolved Hide resolved
if attrs.Len() == 0 {
return nil
}
out := make(map[string]any, attrs.Len())
attrs.Range(func(k string, v pcommon.Value) bool {
out[k] = v.AsRaw()
return true
})
return out
}
10 changes: 7 additions & 3 deletions extension/encoding/jsonlogencodingextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ func NewFactory() extension.Factory {
)
}

func createExtension(_ context.Context, _ extension.CreateSettings, _ component.Config) (extension.Extension, error) {
return &jsonLogExtension{}, nil
func createExtension(_ context.Context, _ extension.CreateSettings, config component.Config) (extension.Extension, error) {
return &jsonLogExtension{
config: config,
}, nil
}

func createDefaultConfig() component.Config {
return &Config{}
return &Config{
Mode: JSONEncodingModeBody,
}
}
39 changes: 36 additions & 3 deletions extension/encoding/jsonlogencodingextension/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (

func TestMarshalUnmarshal(t *testing.T) {
t.Parallel()
e := &jsonLogExtension{}
e := &jsonLogExtension{
config: &Config{
Mode: JSONEncodingModeBody,
},
}
json := `{"example":"example valid json to test that the unmarshaler is correctly returning a plog value"}`
ld, err := e.UnmarshalLogs([]byte(json))
assert.NoError(t, err)
Expand All @@ -25,15 +29,44 @@ func TestMarshalUnmarshal(t *testing.T) {
}

func TestInvalidMarshal(t *testing.T) {
e := &jsonLogExtension{}
e := &jsonLogExtension{
config: &Config{
Mode: JSONEncodingModeBody,
},
}
p := plog.NewLogs()
p.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("NOT A MAP")
_, err := e.MarshalLogs(p)
assert.ErrorContains(t, err, "Marshal: Expected 'Map' found 'Str'")
}

func TestInvalidUnmarshal(t *testing.T) {
e := &jsonLogExtension{}
e := &jsonLogExtension{
config: &Config{
Mode: JSONEncodingModeBody,
},
}
_, err := e.UnmarshalLogs([]byte("NOT A JSON"))
assert.ErrorContains(t, err, "ReadMapCB: expect { or n, but found N")
}

func TestPrettyLogProcessor(t *testing.T) {
j := &jsonLogExtension{
config: &Config{
Mode: JSONEncodingModeBodyWithInlineAttributes,
},
}
lp, err := j.LogProcessor(sampleLog())
assert.NoError(t, err)
assert.NotNil(t, lp)
assert.Equal(t, string(lp), `[{"body":"{\"log\": \"test\"}","resource":{"test":"logs-test"}},{"body":"log testing","resource":{"test":"logs-test"}}]`)
}

func sampleLog() plog.Logs {
l := plog.NewLogs()
rl := l.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("test", "logs-test")
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr(`{"log": "test"}`)
blakeromano marked this conversation as resolved.
Show resolved Hide resolved
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("log testing")
return l
}