Skip to content

Commit

Permalink
[jsonlogencodingextension] Add Modes for Body with Attributes (open-t…
Browse files Browse the repository at this point in the history
…elemetry#32722)

**Description:** <!--Ex. Fixing a bug - Describe the bug and how this
fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

The goal of this PR is to add configuration to the JSON Encoding method
to have metadata from resources and attributes in logs and output those
into a json attribute. This also will output the body of the log in the
`body` attribute. This follows a similar pattern that the AWS Cloudwatch
Log Exporter uses but removes any of the AWS specific logic and makes
this a more generalized solution

**Link to tracking Issue:** <Issue number if applicable>
open-telemetry#32679

**Testing:** <Describe what testing was performed and which tests were
added.> Unit Tests were added to validate that the output is desired and
that the existing logic remains the same.

**Documentation:** <Describe the documentation added.> Documentation
regarding the usage of the configuration as well as the output to expect
is added.

---------

Signed-off-by: Blake R <[email protected]>
  • Loading branch information
blakeromano authored May 7, 2024
1 parent 3987074 commit f22eadd
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 9 deletions.
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: jsonlogencodingextension

# 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]
41 changes: 40 additions & 1 deletion extension/encoding/jsonlogencodingextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,43 @@
[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 |



### Mode

#### body Mode

The `body` mode of the JSON encoding extension is used to marshal or unmarshal the JSON log body, ignoring other log fields.


#### body_with_inline_attributes

The `body_with_inline_attributes` mode within the JSON encoding extension grabs the resource and attributes and adds them as key value pairs to the JSON body. It iterates through all the logs and creates a JSON array like the following example:

```json
[
{
"body": {
"log": "test"
},
"resourceAttributes": {
"test": "logs-test"
},
"logAttributes": {
"foo": "bar"
}
},
{
"body": "log testing",
"resource": {
"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
}
38 changes: 37 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,36 @@ 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) {
logs := make([]logBody, ld.ResourceLogs().Len()-1)

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

sls := rl.ScopeLogs()
for j := 0; j < sls.Len(); j++ {
sl := sls.At(j)
logSlice := sl.LogRecords()
for k := 0; k < logSlice.Len(); k++ {
log := logSlice.At(k)
logEvent := logBody{
Body: log.Body().AsRaw(),
ResourceAttributes: resourceAttrs,
LogAttributes: log.Attributes().AsRaw(),
}
logs = append(logs, logEvent)
}
}
}

return jsoniter.Marshal(logs)
}

type logBody struct {
Body any `json:"body,omitempty"`
LogAttributes map[string]any `json:"logAttributes,omitempty"`
ResourceAttributes map[string]any `json:"resourceAttributes,omitempty"`
}
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,
}
}
40 changes: 37 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,45 @@ 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"},"logAttributes":{"foo":"bar"},"resourceAttributes":{"test":"logs-test"}},{"body":"log testing","resourceAttributes":{"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().SetEmptyMap().PutStr("log", "test")
rl.ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("foo", "bar")
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("log testing")
return l
}

0 comments on commit f22eadd

Please sign in to comment.