Skip to content

Commit

Permalink
Add support for parsing json log bodies in transform processor
Browse files Browse the repository at this point in the history
Signed-off-by: Sampras Lopes <[email protected]>
  • Loading branch information
lsampras committed Oct 19, 2022
1 parent 42049e1 commit 121082c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/json_log_parse_for_transform_processor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support extracting values from json log bodies

# One or more tracking issues related to the change
issues: [14938]

# (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:
25 changes: 25 additions & 0 deletions pkg/ottl/contexts/ottllogs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"time"

jsoniter "github.com/json-iterator/go"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
Expand All @@ -35,13 +37,15 @@ type TransformContext struct {
logRecord plog.LogRecord
instrumentationScope pcommon.InstrumentationScope
resource pcommon.Resource
json jsoniter.API
}

func NewTransformContext(logRecord plog.LogRecord, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
logRecord: logRecord,
instrumentationScope: instrumentationScope,
resource: resource,
json: jsoniter.ConfigFastest,
}
}

Expand Down Expand Up @@ -120,6 +124,12 @@ func newPathGetSetter(path []ottl.Field) (ottl.GetSetter[TransformContext], erro
return accessSeverityNumber(), nil
case "severity_text":
return accessSeverityText(), nil
case "json_body":
mapkey := path[0].MapKey
if mapkey == nil {
return nil, fmt.Errorf("json_body needs a field to extract.")
}
return accessJsonBody(mapkey), nil
case "body":
return accessBody(), nil
case "attributes":
Expand Down Expand Up @@ -203,6 +213,21 @@ func accessSeverityText() ottl.StandardGetSetter[TransformContext] {
}
}

func accessJsonBody(mapKey *string) ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx TransformContext) interface{} {
var parsedValue map[string]interface{}
err := ctx.json.UnmarshalFromString(ctx.GetLogRecord().Body().AsString(), &parsedValue)
if err != nil {
return nil
}
return parsedValue[*mapKey]
},
Setter: func(ctx TransformContext, val interface{}) {
// TODO: Remove this setter and throw an error during config parsing
},
}
}
func accessBody() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx TransformContext) interface{} {
Expand Down

0 comments on commit 121082c

Please sign in to comment.