Skip to content

Commit

Permalink
Incorporate hound feedback + tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvc committed Nov 27, 2018
1 parent fb70739 commit 1ebbb81
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 54 deletions.
8 changes: 4 additions & 4 deletions heartbeat/monitors/active/dialchain/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func makeEndpointJobs(

// Create job that first resolves one or multiple IP (depending on
// config.Mode) in order to create one continuation Task per IP.
jobId := jobId(typ, scheme, endpoint.Host, endpoint.Ports)
settings := monitors.MakeHostJobSettings(jobId, endpoint.Host, mode)
jobID := jobID(typ, scheme, endpoint.Host, endpoint.Ports)
settings := monitors.MakeHostJobSettings(jobID, endpoint.Host, mode)

job, err := monitors.MakeByHostJob(settings,
monitors.MakePingAllIPPortFactory(endpoint.Ports,
Expand All @@ -201,10 +201,10 @@ func makeEndpointJobs(
if err != nil {
return nil, err
}
return []monitors.Job{monitors.WithJobId(jobId, monitors.WithFields(fields, job))}, nil
return []monitors.Job{monitors.WithJobId(jobID, monitors.WithFields(fields, job))}, nil
}

func jobId(typ, jobType, host string, ports []uint16) string {
func jobID(typ, jobType, host string, ports []uint16) string {
var h string
if len(ports) == 1 {
h = fmt.Sprintf("%v:%v", host, ports[0])
Expand Down
7 changes: 4 additions & 3 deletions heartbeat/monitors/active/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import (
)

func init() {
monitors.RegisterActive("http", Create)
monitors.RegisterActive("http", create)
}

var debugf = logp.MakeDebug("http")

func Create(
// Create makes a new HTTP monitor
func create(
name string,
cfg *common.Config,
) (jobs []monitors.Job, endpoints int, err error) {
Expand Down Expand Up @@ -91,7 +92,7 @@ func Create(
}
} else {
for i, url := range config.URLs {
jobs[i], err = NewHTTPMonitorIPsJob(&config, url, tls, enc, body, validator)
jobs[i], err = newHTTPMonitorIPsJob(&config, url, tls, enc, body, validator)
if err != nil {
return nil, 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions heartbeat/monitors/active/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testTLSRequest(t *testing.T, testURL string, extraConfig map[string]interfa
config, err := common.NewConfigFrom(configSrc)
require.NoError(t, err)

jobs, endpoints, err := Create("tls", config)
jobs, endpoints, err := create("tls", config)
require.NoError(t, err)

job := jobs[0]
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestLargeResponse(t *testing.T) {
config, err := common.NewConfigFrom(configSrc)
require.NoError(t, err)

jobs, _, err := Create("largeresp", config)
jobs, _, err := create("largeresp", config)
require.NoError(t, err)

job := jobs[0]
Expand Down
12 changes: 6 additions & 6 deletions heartbeat/monitors/active/http/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newHTTPMonitorHostJob(
Transport: transport,
Timeout: config.Timeout,
}
request, err := BuildRequest(addr, config, enc)
request, err := buildRequest(addr, config, enc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func newHTTPMonitorHostJob(
)), nil
}

func NewHTTPMonitorIPsJob(
func newHTTPMonitorIPsJob(
config *Config,
addr string,
tls *transport.TLSConfig,
Expand All @@ -100,7 +100,7 @@ func NewHTTPMonitorIPsJob(
typ := config.Name
id := fmt.Sprintf("%v@%v", typ, addr)

req, err := BuildRequest(addr, config, enc)
req, err := buildRequest(addr, config, enc)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +112,7 @@ func NewHTTPMonitorIPsJob(

settings := monitors.MakeHostJobSettings(id, hostname, config.Mode)

pingFactory := CreatePingFactory(config, hostname, port, tls, req, body, validator)
pingFactory := createPingFactory(config, hostname, port, tls, req, body, validator)
job, err := monitors.MakeByHostJob(settings, pingFactory)

fields := common.MapStr{
Expand All @@ -130,7 +130,7 @@ func NewHTTPMonitorIPsJob(
return monitors.WithJobId(id, monitors.WithFields(fields, job)), err
}

func CreatePingFactory(
func createPingFactory(
config *Config,
hostname string,
port uint16,
Expand Down Expand Up @@ -201,7 +201,7 @@ func CreatePingFactory(
})
}

func BuildRequest(addr string, config *Config, enc contentEncoder) (*http.Request, error) {
func buildRequest(addr string, config *Config, enc contentEncoder) (*http.Request, error) {
method := strings.ToUpper(config.Check.Request.Method)
request, err := http.NewRequest(method, addr, nil)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions heartbeat/monitors/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,48 @@ import (
"github.com/elastic/beats/libbeat/beat"
)

// A Job represents a unit of execution, and may return multiple continuation jobs.
type Job interface {
Name() string
Run() (*beat.Event, []Job, error)
}

// NamedJob represents a job with an explicitly specified name.
type NamedJob struct {
name string
run func() (*beat.Event, []Job, error)
}

// CreateNamedJob makes a new NamedJob.
func CreateNamedJob(name string, run func() (*beat.Event, []Job, error)) *NamedJob {
return &NamedJob{name, run}
}

// Name returns the name of this job.
func (f *NamedJob) Name() string {
return f.name
}

// Run executes the job.
func (f *NamedJob) Run() (*beat.Event, []Job, error) {
return f.run()
}

// AnonJob represents a job with no assigned name, backed by just a function.
type AnonJob func() (*beat.Event, []Job, error)

// Name returns "" for AnonJob values.
func (aj AnonJob) Name() string {
return ""
}

// Run executes the function.
func (aj AnonJob) Run() (*beat.Event, []Job, error) {
return aj()
}

// AfterJob creates a wrapped version of the given Job that runs additional
// code after the original Job, possibly altering return values.
func AfterJob(j Job, after func(*beat.Event, []Job, error) (*beat.Event, []Job, error)) Job {

return CreateNamedJob(
Expand All @@ -65,6 +75,9 @@ func AfterJob(j Job, after func(*beat.Event, []Job, error) (*beat.Event, []Job,
)
}

// AfterJobSuccess creates a wrapped version of the given Job that runs additional
// code after the original job if the original Job succeeds, possibly altering
// return values.
func AfterJobSuccess(j Job, after func(*beat.Event, []Job, error) (*beat.Event, []Job, error)) Job {
return AfterJob(j, func(event *beat.Event, cont []Job, err error) (*beat.Event, []Job, error) {
if err != nil {
Expand All @@ -85,6 +98,8 @@ func MakeSimpleJob(f func() (*beat.Event, error)) Job {
})
}

// WrapAll takes a list of jobs and wraps them all with the provided Job wrapping
// function.
func WrapAll(jobs []Job, fn func(Job) Job) []Job {
var wrapped []Job
for _, j := range jobs {
Expand Down
14 changes: 12 additions & 2 deletions heartbeat/monitors/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ var DefaultIPSettings = IPSettings{
Mode: PingAny,
}

// emptyTask is a helper value for a Noop.
var emptyTask = MakeSimpleCont(func() (*beat.Event, error) { return nil, nil })

// Network determines the Network type used for IP name resolution, based on the
// provided settings.
func (s IPSettings) Network() string {
Expand All @@ -77,6 +80,10 @@ func (s IPSettings) Network() string {
return ""
}

// WithErrAsField wraps the given Job's execution such that any error returned
// by the original Job will be set as a field. The original error will not be
// passed through as a return value. Errors may still be present but only if there
// is an actual error wrapping the error.
func WithErrAsField(job Job) Job {
return AfterJob(job, func(event *beat.Event, jobs []Job, err error) (*beat.Event, []Job, error) {

Expand All @@ -96,6 +103,9 @@ func WithErrAsField(job Job) Job {
})
}

// TimeAndCheckJob executes the given Job, checking the duration of its run and setting
// its status.
// It adds the monitor.duration and monitor.status fields.
func TimeAndCheckJob(job Job) Job {
// This should probably execute before job.Run
return CreateNamedJob(
Expand All @@ -122,6 +132,7 @@ func TimeAndCheckJob(job Job) Job {
)
}

// WithJobId wraps the given Job setting the monitor.id field.
func WithJobId(id string, job Job) Job {
return CreateNamedJob(
id,
Expand Down Expand Up @@ -152,8 +163,6 @@ func MakePingIPFactory(
}
}

var emptyTask = MakeSimpleCont(func() (*beat.Event, error) { return nil, nil })

// MakePingAllIPFactory wraps a function for building a recursive Task Runner from function callbacks.
func MakePingAllIPFactory(
f func(*net.IPAddr) []func() (*beat.Event, error),
Expand Down Expand Up @@ -446,6 +455,7 @@ func addFields(to *common.MapStr, m common.MapStr) {
fields.DeepUpdate(m)
}

// MergeEventFields merges the given common.MapStr into the given Event's Fields.
func MergeEventFields(e *beat.Event, merge common.MapStr) {
if e.Fields != nil {
e.Fields.DeepUpdate(merge)
Expand Down
37 changes: 0 additions & 37 deletions libbeat/docs/shared-autodiscover.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -188,43 +188,6 @@ Pods share an identical host. If only the `{data.host}` variable is interpolated
then one config will be generated per host. The configs will be identical.
After they are de-duplicated, only one will be used.



ifdef::autodiscoverAWSELB[]
[float]
===== AWS ELB

Use the `aws_elb` autodiscover provider to automatically configure beats against ELB endpoints. It should be noted that classic ELBs are currently not supported.

The aws_elb autodiscover provider actually renders once per load balancer listener, not once per load balancer. Since ELBs
may expose multiple ports one generally wants to have a unique config per port.

The `aws_elb` autodiscover provider requires AWS credentials to speak with the AWS API.
If you are running heartbeat on an AWS instance, and IAM credentials are already associated with
that instance these credentials will be automatically picked up. To set credentials via environment
variables or credentials file read the https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html[AWS configuration docs].

The available fields are:

* `arn`, A string uniquely identifying this load balancer listener.
* `host`, the DNS name of the given ELB
* `port`, the port of the ELB listener
* `protocol`, One of `HTTP`, `HTTPS`, or `TCP`.
* `type`, the type of load balancer, either `application` or `network`.
* `scheme`, the scheme used by this ELB. Either `internet-facing` or `internal`
* `availability_zones`, a list of strings names of AZs this ELB is present in.
* `created`, the time this load balancer was created.
* `state.code`, One of `active`, `provisioning`, `active_impaired`, or `failed`
* `state.reason`, A string giving more detail about the current state.
* `load_balancer_arn`, A string uniquely identifying the load balancer. Note that this may be shared by multiple configs. If you need a unique value use the `listener_arn`.
* `ip_address_type`, One of `ipv4` or `dualstack`.
* `security_groups`, A string list of security group names.
* `vpc_id`, The name of the VPC this ELB is configured within.
* `ssl_policy`, A string representing the security policy for this listener.

include::../../{beatname_lc}/docs/autodiscover-aws-elb-config.asciidoc[]
endif::autodiscoverAWSELB[]

ifdef::autodiscoverJolokia[]
[float]
===== Jolokia (experimental)
Expand Down

0 comments on commit 1ebbb81

Please sign in to comment.