Skip to content

Commit 991ddb3

Browse files
andrewvcmergify-bot
authored andcommitted
[Heartbeat] Make run_once syntax a boolean (#28548)
* [Heartbeat] Make run_once syntax a boolean Fixes #28437. This is an improvement over #25972 which had a more complicated config interface. * Fix python tests * Fix python (cherry picked from commit d31cae9)
1 parent 55496da commit 991ddb3

8 files changed

Lines changed: 22 additions & 113 deletions

File tree

heartbeat/_meta/config/beat.yml.tmpl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,8 @@ heartbeat.monitors:
3737
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
3838
#service.name: my-apm-service-name
3939
40-
# Experimental: Configure monitors that run exactly once.
41-
# If enabled, heartbeat.monitors will be ignored
42-
# Heartbeat will run these monitors once then exit.
43-
#heartbeat.run_once:
44-
#- type: http
45-
#id: my-monitor
46-
#name: My Monitor
47-
#urls: ["http://localhost:9200"]
48-
# NOTE: you must still provide the schedule field! Heartbeat
49-
# Uses this to determine the contents of the monitor.timespan field
50-
#schedule: '@every 10s'
40+
# Experimental: Set this to true to run heartbeat monitors exactly once at startup
41+
#heartbeat.run_once: true
5142
5243
{{header "Elasticsearch template setting"}}
5344

heartbeat/beater/heartbeat.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (bt *Heartbeat) Run(b *beat.Beat) error {
8989
groups, _ := syscall.Getgroups()
9090
logp.Info("Effective user/group ids: %d/%d, with groups: %v", syscall.Geteuid(), syscall.Getegid(), groups)
9191

92-
if bt.config.RunOnce != nil {
92+
if bt.config.RunOnce {
9393
err := bt.runRunOnce(b)
9494
if err != nil {
9595
return err
@@ -141,7 +141,6 @@ func (bt *Heartbeat) Run(b *beat.Beat) error {
141141
// runRunOnce runs the given config then exits immediately after any queued events have been sent to ES
142142
func (bt *Heartbeat) runRunOnce(b *beat.Beat) error {
143143
logp.Info("Starting run_once run. This is an experimental feature and may be changed or removed in the future!")
144-
cfgs := bt.config.RunOnce
145144

146145
publishClient, err := core.NewSyncClient(logp.NewLogger("run_once mode"), b.Publisher, beat.ClientConfig{})
147146
if err != nil {
@@ -150,7 +149,7 @@ func (bt *Heartbeat) runRunOnce(b *beat.Beat) error {
150149
defer publishClient.Close()
151150

152151
wg := &sync.WaitGroup{}
153-
for _, cfg := range cfgs {
152+
for _, cfg := range bt.config.Monitors {
154153
err := runRunOnceSingleConfig(cfg, publishClient, wg)
155154
if err != nil {
156155
logp.Warn("error running run_once config: %s", err)

heartbeat/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// Config defines the structure of heartbeat.yml.
2929
type Config struct {
30-
RunOnce []*common.Config `config:"run_once"`
30+
RunOnce bool `config:"run_once"`
3131
Monitors []*common.Config `config:"monitors"`
3232
ConfigMonitors *common.Config `config:"config.monitors"`
3333
Scheduler Scheduler `config:"scheduler"`

heartbeat/docs/heartbeat-options.asciidoc

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,17 @@ include::monitors/monitor-browser.asciidoc[]
114114
[[run-once-mode]]
115115
=== Run Once Mode (Experimental)
116116

117-
You can configure {beatname_uc} run monitors exactly once then exit, bypassing the scheduler. This is referred to as running {beatname_uc} in "run once" mode. This is an experimental feature
118-
and is subject to change.
117+
You can configure {beatname_uc} run monitors exactly once then exit, bypassing the scheduler. This is referred to as running {beatname_uc} in
118+
"run once" mode by setting `heartbeat.run_once: true`. All {beatname_uc} monitors will ignore their schedules and run exactly once at startup.
119+
This is an experimental feature and is subject to change.
120+
121+
Note, the `schedule` field is still required and is used by {beatname_uc} to set the expectation around when
122+
the next run will occur. That duration is encoded in the `monitor.timespan` field in the {beatname_uc} output.
119123

120124
[source,yaml]
121125
----------------------------------------------------------------------
122126
# heartbeat.yml
123-
heartbeat.run_once:
124-
- type: icmp
125-
id: ping-myhost
126-
name: My Host Ping
127-
hosts: ["myhost"]
128-
# Note that schedule is still needed to inform heartbeat when the next
129-
# expected check is to be run. This is needed to populate the monitor.timespan field used by the Uptime app.
130-
schedule: '@every 5s'
131-
- type: tcp
132-
id: myhost-tcp-echo
133-
name: My Host TCP Echo
134-
hosts: ["myhost:777"] # default TCP Echo Protocol
135-
check.send: "Check"
136-
check.receive: "Check"
137-
schedule: '@every 5s'
138-
- type: http
139-
id: service-status
140-
name: Service Status
141-
service.name: my-apm-service-name
142-
hosts: ["http://localhost:80/service/status"]
143-
check.response.status: [200]
144-
schedule: '@every 5s'
127+
heartbeat.run_once: true
128+
heartbeat.monitors:
129+
# your monitor config here...
145130
----------------------------------------------------------------------

heartbeat/heartbeat.yml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,8 @@ heartbeat.monitors:
3737
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
3838
#service.name: my-apm-service-name
3939

40-
# Experimental: Configure monitors that run exactly once.
41-
# If enabled, heartbeat.monitors will be ignored
42-
# Heartbeat will run these monitors once then exit.
43-
#heartbeat.run_once:
44-
#- type: http
45-
#id: my-monitor
46-
#name: My Monitor
47-
#urls: ["http://localhost:9200"]
48-
# NOTE: you must still provide the schedule field! Heartbeat
49-
# Uses this to determine the contents of the monitor.timespan field
50-
#schedule: '@every 10s'
40+
# Experimental: Set this to true to run heartbeat monitors exactly once at startup
41+
#heartbeat.run_once: true
5142

5243
# ======================= Elasticsearch template setting =======================
5344

heartbeat/tests/system/config/heartbeat.yml.j2

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,8 @@ heartbeat.monitors:
5151
{% endfor -%}
5252

5353
{%- if run_once is defined %}
54-
heartbeat.run_once:
55-
{% for monitor in run_once -%}
56-
- type: {{ monitor.type }}
57-
schedule: '{{ monitor.schedule|default("@every 1s") }}'
58-
{%- if monitor.timeout is defined %}
59-
timeout: {{monitor.timeout}}
60-
{% endif -%}
61-
62-
{%- if monitor.enabled is defined %}
63-
enabled: {{monitor.enabled}}
64-
{% endif -%}
65-
66-
{%- if monitor.tags is defined %}
67-
tags:
68-
{% for tag in monitor.tags -%}
69-
- '{{ tag }}'
70-
{% endfor %}
71-
{% endif -%}
72-
73-
{%- if monitor.hosts is defined %}
74-
hosts:
75-
{%- for host in monitor.hosts %}
76-
- '{{ host }}'
77-
{% endfor -%}
78-
{% endif -%}
79-
80-
{%- if monitor.urls is defined %}
81-
urls:
82-
{%- for url in monitor.urls %}
83-
- '{{ url }}'
84-
{% endfor %}
85-
{% endif -%}
86-
87-
88-
{%- if monitor.check_response_json is defined %}
89-
check.response.json:
90-
{%- for check in monitor.check_response_json %}
91-
- {{check}}
92-
{% endfor %}
93-
{% endif -%}
94-
95-
{%- if monitor.fields is defined %}
96-
{% if monitor.fields_under_root %}fields_under_root: true{% endif %}
97-
fields:
98-
{% for k, v in monitor.fields.items() -%}
99-
{{ k }}: {{ v }}
100-
{% endfor %}
101-
{% endif %}
102-
{% endfor -%}
103-
{% endif %}
104-
54+
heartbeat.run_once: {{run_once}}
55+
{% endif -%}
10556

10657
{% if reload or reload_path -%}
10758
heartbeat.config.monitors:

heartbeat/tests/system/test_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def test_run_once(self):
3939
"""
4040

4141
config = {
42-
"run_once": [
42+
"run_once": True,
43+
"monitors": [
4344
{
4445
"type": "http",
4546
"id": "http-check",

x-pack/heartbeat/heartbeat.yml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,8 @@ heartbeat.monitors:
3737
# Name of corresponding APM service, if Elastic APM is in use for the monitored service.
3838
#service.name: my-apm-service-name
3939

40-
# Experimental: Configure monitors that run exactly once.
41-
# If enabled, heartbeat.monitors will be ignored
42-
# Heartbeat will run these monitors once then exit.
43-
#heartbeat.run_once:
44-
#- type: http
45-
#id: my-monitor
46-
#name: My Monitor
47-
#urls: ["http://localhost:9200"]
48-
# NOTE: you must still provide the schedule field! Heartbeat
49-
# Uses this to determine the contents of the monitor.timespan field
50-
#schedule: '@every 10s'
40+
# Experimental: Set this to true to run heartbeat monitors exactly once at startup
41+
#heartbeat.run_once: true
5142

5243
# ======================= Elasticsearch template setting =======================
5344

0 commit comments

Comments
 (0)