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

[Heartbeat] Dedupe screenshots / Extra Args #25808

Merged
merged 22 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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 @@ -106,6 +106,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Change logging in logs input to structure logging. Some log message formats have changed. {pull}25299[25299]

*Heartbeat*
- Add support for screenshot blocks and use newer synthetics flags that only worker in newer synthetics betas. {pull}25808[25808]
andrewvc marked this conversation as resolved.
Show resolved Hide resolved

*Journalbeat*

Expand Down
16 changes: 16 additions & 0 deletions heartbeat/_meta/fields.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@
type: text
- name: stack
type: text
- name: screenshot_ref
type: group
dynamic: false
fields:
- name: width
type: integer
description: Width of the full screenshot in pixels.
Copy link
Member

Choose a reason for hiding this comment

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

Shall we remove the use of full here? Full seems like full page screenshot but we are capture only the viewport width and height.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, I intended it to mean not of an individual block. I'll clarify that.

- name: height
type: integer
description: Height of the full screenshot in pixels
- name: blocks
type: group
fields:
- name: hash
Copy link
Member

Choose a reason for hiding this comment

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

Need to add top and left here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

Copy link
Member

Choose a reason for hiding this comment

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

I don't see the top & left yet in the new changes.

type: keyword
description: Hash that uniquely identifies this image by content. Corresponds to block document id.
- name: browser
type: group
fields:
Expand Down
4 changes: 4 additions & 0 deletions heartbeat/include/fields.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x-pack/heartbeat/include/fields.go

Large diffs are not rendered by default.

38 changes: 4 additions & 34 deletions x-pack/heartbeat/monitors/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
package browser

import (
"context"
"fmt"
"os"
"os/user"
"sync"

"github.com/elastic/beats/v7/heartbeat/monitors/jobs"
"github.com/elastic/beats/v7/heartbeat/monitors/plugin"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/synthexec"
)

func init() {
Expand All @@ -25,14 +21,14 @@ func init() {

var showExperimentalOnce = sync.Once{}

var NotSyntheticsCapableError = fmt.Errorf("synthetic monitors cannot be created outside the official elastic docker image")
var ErrNotSyntheticsCapableError = fmt.Errorf("synthetic monitors cannot be created outside the official elastic docker image")

func create(name string, cfg *common.Config) (p plugin.Plugin, err error) {
// We don't want users running synthetics in environments that don't have the required GUI libraries etc, so we check
// this flag. When we're ready to support the many possible configurations of systems outside the docker environment
// we can remove this check.
if os.Getenv("ELASTIC_SYNTHETICS_CAPABLE") != "true" {
return plugin.Plugin{}, NotSyntheticsCapableError
return plugin.Plugin{}, ErrNotSyntheticsCapableError
}

showExperimentalOnce.Do(func() {
Expand All @@ -47,36 +43,10 @@ func create(name string, cfg *common.Config) (p plugin.Plugin, err error) {
return plugin.Plugin{}, fmt.Errorf("script monitors cannot be run as root! Current UID is %s", curUser.Uid)
}

ss, err := NewSuite(cfg)
s, err := NewSuite(cfg)
if err != nil {
return plugin.Plugin{}, err
}

extraArgs := []string{}
if ss.suiteCfg.Sandbox {
extraArgs = append(extraArgs, "--sandbox")
}

var j jobs.Job
if src, ok := ss.InlineSource(); ok {
j = synthexec.InlineJourneyJob(context.TODO(), src, ss.Params(), extraArgs...)
} else {
j = func(event *beat.Event) ([]jobs.Job, error) {
err := ss.Fetch()
if err != nil {
return nil, fmt.Errorf("could not fetch for suite job: %w", err)
}
sj, err := synthexec.SuiteJob(context.TODO(), ss.Workdir(), ss.Params(), extraArgs...)
if err != nil {
return nil, err
}
return sj(event)
}
}

return plugin.Plugin{
Jobs: []jobs.Job{j},
Close: ss.Close,
Endpoints: 1,
}, nil
return s.plugin(), nil
}
5 changes: 3 additions & 2 deletions x-pack/heartbeat/monitors/browser/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type Config struct {
// Name is optional for lightweight checks but required for browsers
Name string `config:"name"`
// Id is optional for lightweight checks but required for browsers
Id string `config:"id"`
Sandbox bool `config:"sandbox"`
Id string `config:"id"`
Sandbox bool `config:"sandbox"`
SyntheticsArgs []string `config:"synthetics_args"`
}

var ErrNameRequired = fmt.Errorf("config 'name' must be specified for this monitor")
Expand Down
108 changes: 108 additions & 0 deletions x-pack/heartbeat/monitors/browser/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package browser
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 file isn't new, it's just moved. Enough of it changed due to renaming the ss var to s that git thinks it's all new.


import (
"context"
"fmt"

"github.com/elastic/beats/v7/heartbeat/monitors/jobs"
"github.com/elastic/beats/v7/heartbeat/monitors/plugin"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/synthexec"
)

type JourneyLister func(ctx context.Context, suitePath string, params common.MapStr) (journeyNames []string, err error)

type Suite struct {
rawCfg *common.Config
suiteCfg *Config
}

func NewSuite(rawCfg *common.Config) (*Suite, error) {
s := &Suite{
rawCfg: rawCfg,
suiteCfg: DefaultConfig(),
}
err := rawCfg.Unpack(s.suiteCfg)
if err != nil {
return nil, ErrBadConfig(err)
}

return s, nil
}

func ErrBadConfig(err error) error {
return fmt.Errorf("could not parse suite config: %w", err)
}

func (s *Suite) String() string {
panic("implement me")
}

func (s *Suite) Fetch() error {
return s.suiteCfg.Source.Active().Fetch()
}

func (s *Suite) Workdir() string {
return s.suiteCfg.Source.Active().Workdir()
}

func (s *Suite) InlineSource() (string, bool) {
if s.suiteCfg.Source.Inline != nil {
return s.suiteCfg.Source.Inline.Script, true
}
return "", false
}

func (s *Suite) Params() map[string]interface{} {
return s.suiteCfg.Params
}

func (s *Suite) Close() error {
if s.suiteCfg.Source.ActiveMemo != nil {
s.suiteCfg.Source.ActiveMemo.Close()
}

return nil
}

func (s *Suite) extraArgs() []string {
extraArgs := s.suiteCfg.SyntheticsArgs
if s.suiteCfg.Sandbox {
extraArgs = append(extraArgs, "--sandbox")
}

return extraArgs
}

func (s *Suite) jobs() []jobs.Job {
var j jobs.Job
if src, ok := s.InlineSource(); ok {
j = synthexec.InlineJourneyJob(context.TODO(), src, s.Params(), s.extraArgs()...)
} else {
j = func(event *beat.Event) ([]jobs.Job, error) {
err := s.Fetch()
if err != nil {
return nil, fmt.Errorf("could not fetch for suite job: %w", err)
}
sj, err := synthexec.SuiteJob(context.TODO(), s.Workdir(), s.Params(), s.extraArgs()...)
if err != nil {
return nil, err
}
return sj(event)
}
}
return []jobs.Job{j}
}

func (s *Suite) plugin() plugin.Plugin {
return plugin.Plugin{
Jobs: s.jobs(),
Close: s.Close,
Endpoints: 1,
}
}
69 changes: 0 additions & 69 deletions x-pack/heartbeat/monitors/browser/suite_runner.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package browser
import (
"path"
"path/filepath"
"reflect"
"runtime"
"testing"

Expand Down Expand Up @@ -114,3 +115,37 @@ func TestEmptySource(t *testing.T) {
require.Regexp(t, ErrBadConfig(source.ErrInvalidSource), e)
require.Nil(t, s)
}

func TestExtraArgs(t *testing.T) {
tests := []struct {
name string
cfg *Config
want []string
}{
{
"no args",
&Config{},
nil,
},
{
"sandbox",
&Config{Sandbox: true},
[]string{"--sandbox"},
},
{
"kitchen sink",
&Config{SyntheticsArgs: []string{"--capability", "trace"}, Sandbox: true},
Copy link
Member

Choose a reason for hiding this comment

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

I am curios to see a test with capability > 2 and other flags as part of synthetic args. Reason being variadic args are prone to errors.

[]string{"--capability", "trace", "--sandbox"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Suite{
suiteCfg: tt.cfg,
}
if got := s.extraArgs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Suite.extraArgs() = %v, want %v", got, tt.want)
}
})
}
}
11 changes: 11 additions & 0 deletions x-pack/heartbeat/monitors/browser/synthexec/enrich.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"time"

"github.com/elastic/beats/v7/libbeat/beat/events"
"github.com/elastic/beats/v7/libbeat/processors/add_data_stream_index"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -108,11 +109,21 @@ func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) e
case "step/end":
je.stepCount++
case "step/screenshot":
fallthrough
case "step/screenshot_ref":
fallthrough
case "screenshot/block":
add_data_stream_index.SetEventDataset(event, "browser_screenshot")
case "journey/network_info":
add_data_stream_index.SetEventDataset(event, "browser_network")
}

if se.Id != "" {
event.SetID(se.Id)
// This is only relevant for screenshots, which have a specific ID
// In that case we always want to issue an update op
event.Meta.Put(events.FieldMetaOpType, events.OpTypeCreate)
}
eventext.MergeEventFields(event, se.ToMap())

if je.urlFields == nil {
Expand Down
Loading