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

x-pack/filebeat/input/cel: use structpb.Struct as intermediate type #35915

Merged
merged 1 commit into from
Jun 28, 2023
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 CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ automatic splitting at root level, if root level element is an array. {pull}3415
- Add Okta input package for entity analytics. {pull}35611[35611]
- Expose harvester metrics from filestream input {pull}35835[35835] {issue}33771[33771]
- Add device support for Azure AD entity analytics. {pull}35807[35807]
- Improve CEL input performance. {pull}35915[35915]

*Auditbeat*
- Migration of system/package module storage from gob encoding to flatbuffer encoding in bolt db. {pull}34817[34817]
Expand Down
24 changes: 9 additions & 15 deletions x-pack/filebeat/input/cel/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package cel
import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -33,8 +32,6 @@ import (

"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"

v2 "github.com/elastic/beats/v7/filebeat/input/v2"
Expand Down Expand Up @@ -909,23 +906,20 @@ func evalWith(ctx context.Context, prg cel.Program, state map[string]interface{}
return state, fmt.Errorf("failed eval: %w", err)
}

v, err := out.ConvertToNative(reflect.TypeOf(&structpb.Value{}))
v, err := out.ConvertToNative(reflect.TypeOf((*structpb.Struct)(nil)))
if err != nil {
state["events"] = errorMessage(fmt.Sprintf("failed proto conversion: %v", err))
return state, fmt.Errorf("failed proto conversion: %w", err)
}
b, err := protojson.MarshalOptions{Indent: ""}.Marshal(v.(proto.Message))
if err != nil {
state["events"] = errorMessage(fmt.Sprintf("failed native conversion: %v", err))
return state, fmt.Errorf("failed native conversion: %w", err)
}
var res map[string]interface{}
err = json.Unmarshal(b, &res)
if err != nil {
state["events"] = errorMessage(fmt.Sprintf("failed json conversion: %v", err))
return state, fmt.Errorf("failed json conversion: %w", err)
switch v := v.(type) {
case *structpb.Struct:
return v.AsMap(), nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given the results of investigation in elastic/mito#37, I've looked at whether out.Value() is ever a map[string]any (out is a ref.Val as is the case in the mito change). It is not, always being a map[ref.Val]ref.Val, so we need to go through the ConvertToNative call to make a structpb.Struct type that can then be converted.

default:
// This should never happen.
errMsg := fmt.Sprintf("unexpected native conversion type: %T", v)
state["events"] = errorMessage(errMsg)
return state, errors.New(errMsg)
}
return res, nil
}

func errorMessage(msg string) map[string]interface{} {
Expand Down