Skip to content

Commit

Permalink
[Heartbeat] Incorporate factory metadata for autodiscover (elastic#10258
Browse files Browse the repository at this point in the history
)

[Heartbeat] Incorporate factory metadata for autodiscover

Heartbeat factories get metadata from autodiscover and other sources.

This change automatically adds that data to events keeping heartbeat behavior in-line with other beats.
  • Loading branch information
andrewvc authored Jan 24, 2019
1 parent 818e1de commit 8f4e186
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Heartbeat*

- Made monitors.d configuration part of the default config. {pull}9004[9004]
- Fixed rare issue where TLS connections to endpoints with x509 certificates missing either notBefore or notAfter would cause the check to fail with a stacktrace. {pull}9566[9566]

*Journalbeat*

Expand Down Expand Up @@ -182,7 +183,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Heartbeat*

- Fixed rare issue where TLS connections to endpoints with x509 certificates missing either notBefore or notAfter would cause the check to fail with a stacktrace. {pull}9566[9566]
- Autodiscover metadata is now included in events by default. So, if you are using the docker provider for instance, you'll see the correct fields under the `docker` key. {pull}10258[10258]

*Journalbeat*

Expand Down
2 changes: 1 addition & 1 deletion heartbeat/monitors/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewFactory(sched *scheduler.Scheduler, allowWatches bool) *RunnerFactory {

// Create makes a new Runner for a new monitor with the given Config.
func (f *RunnerFactory) Create(p beat.Pipeline, c *common.Config, meta *common.MapStrPointer) (cfgfile.Runner, error) {
monitor, err := newMonitor(c, globalPluginsReg, p, f.sched, f.allowWatches)
monitor, err := newMonitor(c, globalPluginsReg, p, f.sched, f.allowWatches, meta)
return monitor, err
}

Expand Down
7 changes: 5 additions & 2 deletions heartbeat/monitors/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ type Monitor struct {

// stats is the countersRecorder used to record lifecycle events
// for global metrics + telemetry
stats registryRecorder
stats registryRecorder
factoryMetadata *common.MapStrPointer
}

// String prints a description of the monitor in a threadsafe way. It is important that this use threadsafe
Expand All @@ -72,7 +73,7 @@ func (m *Monitor) String() string {
}

func checkMonitorConfig(config *common.Config, registrar *pluginsReg, allowWatches bool) error {
m, err := newMonitor(config, registrar, nil, nil, allowWatches)
m, err := newMonitor(config, registrar, nil, nil, allowWatches, nil)
m.Stop() // Stop the monitor to free up the ID from uniqueness checks
return err
}
Expand All @@ -97,6 +98,7 @@ func newMonitor(
pipelineConnector beat.PipelineConnector,
scheduler *scheduler.Scheduler,
allowWatches bool,
factoryMetadata *common.MapStrPointer,
) (*Monitor, error) {
// Extract just the Id, Type, and Enabled fields from the config
// We'll parse things more precisely later once we know what exact type of
Expand All @@ -123,6 +125,7 @@ func newMonitor(
internalsMtx: sync.Mutex{},
config: config,
stats: monitorPlugin.stats,
factoryMetadata: factoryMetadata,
}

if m.id != "" {
Expand Down
4 changes: 2 additions & 2 deletions heartbeat/monitors/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMonitor(t *testing.T) {
require.NoError(t, err)
defer sched.Stop()

mon, err := newMonitor(serverMonConf, reg, pipelineConnector, sched, false)
mon, err := newMonitor(serverMonConf, reg, pipelineConnector, sched, false, nil)
require.NoError(t, err)

mon.Start()
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestDuplicateMonitorIDs(t *testing.T) {
defer sched.Stop()

makeTestMon := func() (*Monitor, error) {
return newMonitor(serverMonConf, reg, pipelineConnector, sched, false)
return newMonitor(serverMonConf, reg, pipelineConnector, sched, false, nil)
}

m1, m1Err := makeTestMon()
Expand Down
7 changes: 6 additions & 1 deletion heartbeat/monitors/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ func (t *configuredJob) makeSchedulerTaskFunc() scheduler.TaskFunc {
func (t *configuredJob) Start() {
var err error

fields := common.MapStr{"event": common.MapStr{"dataset": "uptime"}}
if t.monitor.factoryMetadata != nil {
fields.DeepUpdate(t.monitor.factoryMetadata.Get())
}

t.client, err = t.monitor.pipelineConnector.ConnectWith(beat.ClientConfig{
EventMetadata: t.config.EventMetadata,
Processor: t.processors,
Fields: common.MapStr{"event": common.MapStr{"dataset": "uptime"}},
Fields: fields,
})
if err != nil {
logp.Err("could not start monitor: %v", err)
Expand Down
7 changes: 5 additions & 2 deletions heartbeat/tests/system/test_autodiscovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ def test_docker(self):
host = network_settings['Networks'].values()[
0]['IPAddress']
port = network_settings['Ports'].keys()[0].split("/")[0]
# Check metadata is added
if output[0]['monitor']['id'] == 'myid':
# Check metadata and docker fields are added
# We don't check all the docker fields because this is really the responsibility
# of libbeat's autodiscovery code.
event = output[0]
if event['monitor']['id'] == 'myid' and event['docker']['container']['id'] is not None:
matched = True

assert matched

0 comments on commit 8f4e186

Please sign in to comment.