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

Osquerybeat: Return the query result count with the action response #28576

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Changes from 1 commit
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
Next Next commit
Osquerybeat: Return the query result count with the action response
  • Loading branch information
aleksmaus committed Oct 20, 2021
commit 4cad636aef909bb85b94f395a51ec17784a367c8
19 changes: 11 additions & 8 deletions x-pack/osquerybeat/beater/action_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *actionHandler) Name() string {
func (a *actionHandler) Execute(ctx context.Context, req map[string]interface{}) (map[string]interface{}, error) {

start := time.Now().UTC()
err := a.execute(ctx, req)
count, err := a.execute(ctx, req)
end := time.Now().UTC()

res := map[string]interface{}{
Expand All @@ -59,14 +59,16 @@ func (a *actionHandler) Execute(ctx context.Context, req map[string]interface{})

if err != nil {
res["error"] = err.Error()
} else {
res["count"] = count
}
return res, nil
}

func (a *actionHandler) execute(ctx context.Context, req map[string]interface{}) error {
func (a *actionHandler) execute(ctx context.Context, req map[string]interface{}) (int, error) {
ac, err := action.FromMap(req)
if err != nil {
return fmt.Errorf("%v: %w", err, ErrQueryExecution)
return 0, fmt.Errorf("%v: %w", err, ErrQueryExecution)
}

var namespace string
Expand All @@ -80,13 +82,13 @@ func (a *actionHandler) execute(ctx context.Context, req map[string]interface{})
return a.executeQuery(ctx, config.Datastream(namespace), ac, "", req)
}

func (a *actionHandler) executeQuery(ctx context.Context, index string, ac action.Action, responseID string, req map[string]interface{}) error {
func (a *actionHandler) executeQuery(ctx context.Context, index string, ac action.Action, responseID string, req map[string]interface{}) (int, error) {

if a.queryExec == nil {
return ErrNoQueryExecutor
return 0, ErrNoQueryExecutor
}
if a.publisher == nil {
return ErrNoPublisher
return 0, ErrNoPublisher
}

a.log.Debugf("Execute query: %s", ac.Query)
Expand All @@ -97,11 +99,12 @@ func (a *actionHandler) executeQuery(ctx context.Context, index string, ac actio

if err != nil {
a.log.Errorf("Failed to execute query, err: %v", err)
return err
return 0, err
}

a.log.Debugf("Completed query in: %v", time.Since(start))

a.publisher.Publish(index, ac.ID, responseID, hits, ac.ECSMapping, req["data"])
Copy link
Contributor

Choose a reason for hiding this comment

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

can this have a partial failure on Publish? if so, how are failures to publish surfaced in the publisher?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this API doesn't return errors. publishing is handled by libbeat, including retries and batching. the errors are surfaces through the logs

return nil

return len(hits), nil
}