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 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 @@ -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 works in newer synthetics betas. {pull}25808[25808]

*Journalbeat*

Expand Down
17 changes: 17 additions & 0 deletions heartbeat/_meta/fields.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@
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
description: Attributes representing individual screenshot blocks. Only hash is indexed since it's the only one we'd query on.
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
34 changes: 34 additions & 0 deletions heartbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10500,6 +10500,40 @@ type: text
--


*`synthetics.screenshot_ref.width`*::
+
--
Width of the full screenshot in pixels.

type: integer

--

*`synthetics.screenshot_ref.height`*::
+
--
Height of the full screenshot in pixels

type: integer

--

[float]
=== blocks

Attributes representing individual screenshot blocks. Only hash is indexed since it's the only one we'd query on.


*`synthetics.screenshot_ref.blocks.hash`*::
+
--
Hash that uniquely identifies this image by content. Corresponds to block document id.

type: keyword

--



*`synthetics.browser.experience.name`*::
+
Expand Down
12 changes: 12 additions & 0 deletions heartbeat/docs/monitors/monitor-browser.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ Example configuration:
local:
path: "/path/to/synthetics/journeys"
-------------------------------------------------------------------------------

[float]
[[monitor-browser-sandbox]]
==== `sandbox`

Set this option to `true` to enable the normally disabled chromium sandbox. Defaults to false.

[float]
[[monitor-browser-synthetics-args]]
==== `synthetics_args`

Extra arguments to pass to the synthetics agent package. Takes a list of strings.
2 changes: 1 addition & 1 deletion 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.

Loading